source: src/main/java/agents/anac/y2010/AgentSmith/SmithBidStrategy.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 5.6 KB
Line 
1package agents.anac.y2010.AgentSmith;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.List;
7
8import genius.core.AgentID;
9import genius.core.Bid;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.Offer;
13import genius.core.issue.Issue;
14import genius.core.issue.Value;
15import genius.core.utility.AdditiveUtilitySpace;
16
17/**
18 * The strategy that is used in our agent. It samples the bid space, takes the
19 * bids that are near Pareto and returns them as next actions.
20 *
21 * NOTE There are two bugs in this strategy: 1. The agent incorrectly assumes
22 * that there are only 2 minutes 2. The agent checks if it gave "enough" utility
23 * to the opponent instead of itself
24 */
25public class SmithBidStrategy extends ABidStrategy {
26 private int fIndex = 0;
27
28 static private double sUtilyMargin = 0.7;
29 static private double sTimeMargin = 110; // in seconds
30
31 static private double UTILITY_THRESHOLD = 0.7;
32 private final boolean TEST_EQUIVALENCE = false;
33
34 /**
35 * Constructor
36 *
37 * @param pHist
38 * The bidhistory
39 * @param utilitySpace
40 * Utilityspace
41 * @param pPreferenceProfile
42 * Preference profile
43 * @param pId
44 * Our id
45 */
46 public SmithBidStrategy(BidHistory pHist,
47 AdditiveUtilitySpace utilitySpace,
48 PreferenceProfileManager pPreferenceProfile, AgentID pId) {
49 super(pHist, utilitySpace, pPreferenceProfile, pId);
50 if (TEST_EQUIVALENCE) {
51 sTimeMargin = 170;
52 }
53 }
54
55 /**
56 * Generate in new action
57 */
58 public Action getNextAction(double time) {
59 double normalTime = time * 180;
60 Action lAction = null;
61 // Time in seconds.
62 try {
63 // Check if the session (2 min) is almost finished
64 if (normalTime >= sTimeMargin) {
65 // If the session is almost finished check if the utility is
66 // "high enough"
67 Bid lastBid = fBidHistory.getOpponentLastBid();
68
69 boolean result = false;
70 if (TEST_EQUIVALENCE) {
71 result = fPreferenceProfile.getMyUtility(lastBid) >= sUtilyMargin;
72 } else {
73 result = fPreferenceProfile.getOpponentUtility(lastBid) >= sUtilyMargin;
74 }
75 if (result) {
76 lAction = new Accept(fAgentID, lastBid);
77 } else {
78 lAction = new Offer(fAgentID, getBestOpponentOffer());
79 }
80 } else {
81 lAction = new Offer(fAgentID, getMostOptimalBid());
82 }
83 } catch (Exception e) {
84 lAction = null;
85 }
86 return lAction;
87 }
88
89 /**
90 * Calculate the most optimal bid
91 *
92 * @return the most optimal bid
93 * @throws Exception
94 */
95 public Bid getMostOptimalBid() {
96 ArrayList<Bid> lBids = getSampledBidList();
97
98 // Log.logger.info("Size of bid space: " + lBids.size());
99 BidComparator lComparator = new BidComparator(this.fPreferenceProfile);
100
101 // sort the bids in order of highest utility
102 Collections.sort(lBids, lComparator);
103
104 Bid lBid = lBids.get(fIndex);
105 if (fIndex < lBids.size() - 1)
106 fIndex++;
107
108 return lBid;
109 }
110
111 /**
112 * Return the best offer made by your opponent. When time is running out
113 * this method is called to find the best bid available.
114 *
115 * @return
116 * @throws Exception
117 */
118 public Bid getBestOpponentOffer() {
119 double util = 0;
120 Bid bestBid = null;
121
122 try {
123 // check for the highest bid offered by the opponent
124 for (int i = 0; i < fBidHistory.getOpponentBidCount(); i++) {
125 if (fUtilitySpace.getUtility(fBidHistory.getOpponentBid(i)) > util) {
126 util = fUtilitySpace.getUtility(fBidHistory
127 .getOpponentBid(i));
128 bestBid = fBidHistory.getOpponentBid(i);
129 }
130 }
131 } catch (Exception e) {
132 bestBid = null;
133 }
134
135 return bestBid;
136 }
137
138 /**
139 * returns an ArrayList with possible bids This function constructs all
140 * samples of the entire bidspace.
141 */
142 private ArrayList<Bid> getSampledBidList() {
143 ArrayList<Bid> lBids = new ArrayList<Bid>();
144 List<Issue> lIssues = this.fPreferenceProfile.getIssues();
145 HashMap<Integer, Bounds> lBounds = Bounds.getIssueBounds(lIssues);
146
147 // first createFrom a new list
148 HashMap<Integer, Value> lBidValues = new HashMap<Integer, Value>();
149 for (Issue lIssue : lIssues) {
150 Bounds b = lBounds.get(lIssue.getNumber());
151 Value v = Bounds.getIssueValue(lIssue, b.getLower());
152 lBidValues.put(lIssue.getNumber(), v);
153 }
154 try {
155 lBids.add(new Bid(this.fPreferenceProfile.getDomain(), lBidValues));
156 } catch (Exception e) {
157 }
158
159 // for each item permutate with issue values, like binary
160 // 0 0 0
161 // 0 0 1
162 // 0 1 0
163 // 0 1 1
164 // etc.
165 for (Issue lIssue : lIssues) {
166 ArrayList<Bid> lTempBids = new ArrayList<Bid>();
167 Bounds b = lBounds.get(lIssue.getNumber());
168 for (Bid lTBid : lBids) {
169 for (double i = b.getLower(); i < b.getUpper(); i += b
170 .getStepSize()) {
171 HashMap<Integer, Value> lNewBidValues = getBidValues(lTBid);
172 lNewBidValues.put(lIssue.getNumber(),
173 Bounds.getIssueValue(lIssue, i));
174
175 try {
176 Bid iBid = new Bid(this.fPreferenceProfile.getDomain(),
177 lNewBidValues);
178 lTempBids.add(iBid);
179
180 } catch (Exception e) {
181
182 }
183 }
184 }
185 lBids = lTempBids;
186 }
187
188 ArrayList<Bid> lToDestroy = new ArrayList<Bid>();
189 for (Bid lBid : lBids) {
190
191 if (this.fPreferenceProfile.getMyUtility(lBid) < UTILITY_THRESHOLD) {
192 lToDestroy.add(lBid);
193 }
194 }
195 for (Bid lBid : lToDestroy) {
196 lBids.remove(lBid);
197 }
198
199 return lBids;
200 }
201
202 /**
203 * Get the values of a bid
204 *
205 * @param pBid
206 * @return
207 */
208 private HashMap<Integer, Value> getBidValues(Bid pBid) {
209 HashMap<Integer, Value> lNewBidValues = new HashMap<Integer, Value>();
210 for (Issue lIssue : this.fPreferenceProfile.getIssues()) {
211 try {
212 lNewBidValues.put(lIssue.getNumber(),
213 pBid.getValue(lIssue.getNumber()));
214 } catch (Exception e) {
215
216 }
217 }
218 return lNewBidValues;
219 }
220
221}
Note: See TracBrowser for help on using the repository browser.