source: src/main/java/parties/in4010/q12015/group14/Group14.java@ 47

Last change on this file since 47 was 47, checked in by Tim Baarslag, 7 years ago

BOA agents get their getUtilitySpace() from the API, not the info object anymore

File size: 12.1 KB
Line 
1package parties.in4010.q12015.group14;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.Map;
9import java.util.Random;
10
11import genius.core.AgentID;
12import genius.core.Bid;
13import genius.core.BidHistory;
14import genius.core.actions.Accept;
15import genius.core.actions.Action;
16import genius.core.actions.DefaultAction;
17import genius.core.actions.Offer;
18import genius.core.bidding.BidDetails;
19import genius.core.boaframework.OutcomeSpace;
20import genius.core.parties.AbstractNegotiationParty;
21import genius.core.parties.NegotiationInfo;
22import genius.core.timeline.TimeLineInfo;
23import genius.core.utility.AdditiveUtilitySpace;
24
25/**
26 * This is your negotiation party.
27 */
28public class Group14 extends AbstractNegotiationParty {
29
30 private Random randomGen = new Random();
31 private Action lastAction = null;
32 private AdditiveUtilitySpace myUtilSpace;
33 private TimeLineInfo myTimeLine;
34 private OutcomeSpace outSpace;
35 private List<BidDetails> allbids, allbidsByNash; // eventually a sorted list
36 // of all bids
37 private List<BidDetails> firstSetBids, secondSetBids, thirdSetBids, fourthSetBids, fifthSetBids, lastSetBids;
38 // a hash map from the opponent agents to the opponentmodelling objects
39 private HashMap<String, OpponentModelling> oppModelHashMap = new HashMap<String, OpponentModelling>();
40 private BidHistory oppBidHistory;
41
42 private int count = 0;
43 // private int count2 = 0;
44 private int roundNr = 0;
45
46 /**
47 * This method is called to initialize our agent
48 */
49 @Override
50 public void init(NegotiationInfo info) {
51 super.init(info);
52 firstSetBids = new ArrayList<BidDetails>();
53 secondSetBids = new ArrayList<BidDetails>();
54 thirdSetBids = new ArrayList<BidDetails>();
55 fourthSetBids = new ArrayList<BidDetails>();
56 fifthSetBids = new ArrayList<BidDetails>();
57 lastSetBids = new ArrayList<BidDetails>();
58 oppBidHistory = new BidHistory();
59 myUtilSpace = (AdditiveUtilitySpace) getUtilitySpace();
60 myTimeLine = info.getTimeline();
61 outSpace = new OutcomeSpace(getUtilitySpace());
62 allbids = outSpace.getAllOutcomes(); // get all possible outcomes
63 Collections.sort(allbids); // sort the in descending order
64 // allbids.remove(0); //remove the best bid in the list
65 makeLists(allbids);
66 };
67
68 /**
69 * Each round this method gets called and ask you to accept or offer. The
70 * first party in the first round is a bit different, it can only propose an
71 * offer.
72 *
73 * @param validActions
74 * Either a list containing both accept and offer or only offer.
75 * @return The chosen action.
76 */
77 public Action chooseAction(List<Class<? extends Action>> validActions) {
78 // No offer to consider so we propose the one with max utility for us
79 roundNr++;
80
81 if (!validActions.contains(Accept.class))
82 return maxUtilBid(myUtilSpace);
83 /*
84 * if (lastAction instanceof Offer){ //Bid bidOfOffer=((Offer)
85 * lastAction).getBid();
86 *
87 * //Analysis on Opponent Modeling if ((myTimeLine.getTime()>0.1 &&
88 * count2==0) || (myTimeLine.getTime()>0.2 && count==1) ||
89 * (myTimeLine.getTime()>0.3 && count==2) || (myTimeLine.getTime()>0.4
90 * && count==3) || (myTimeLine.getTime()>0.5 && count==4) ||
91 * (myTimeLine.getTime()>0.6 && count==5) || (myTimeLine.getTime()>0.7
92 * && count==6) || (myTimeLine.getTime()>0.8 && count==7) ||
93 * (myTimeLine.getTime()>0.9 && count==8)){ System.out.println("TIME: "
94 * + Double.toString(myTimeLine.getTime())); for(Map.Entry<String,
95 * OpponentModelling> entry: oppModelHashMap.entrySet()){
96 * System.out.println("The name of the agent is :"+entry.getKey());
97 * entry.getValue().printOppModel(); } count2++; } }
98 */
99
100 // At certain times, we want to evaluate our bids using the latest
101 // oppModel
102 if ((myTimeLine.getTime() > 0.4 && count == 0) || (myTimeLine.getTime() > 0.55 && count == 1)
103 || (myTimeLine.getTime() > 0.65 && count == 2) || (myTimeLine.getTime() > 0.75 && count == 3)
104 || (myTimeLine.getTime() > 0.85 && count == 4) || (myTimeLine.getTime() > 0.95 && count == 5)) {
105 int phase = count + 1;
106 System.out.println("Starting phase " + phase + " at t=" + myTimeLine.getTime() + " and round=" + roundNr);
107 allbidsByNash = orderListByNash(allbids);
108 makeLists(allbidsByNash);
109
110 count++;
111 }
112
113 return myOffer();
114 // return new EndNegotiation();
115
116 }
117
118 /**
119 * All offers proposed by the other parties will be received as a message.
120 * You can use this information to your advantage, for example to predict
121 * their utility.
122 *
123 * @param sender
124 * The party that did the action.
125 * @param action
126 * The action that party did.
127 */
128 public void receiveMessage(AgentID sender, Action action) {
129 super.receiveMessage(sender, action);
130 /*
131 * if(action instanceof Offer){ lastAction=action; }
132 */
133 if (action instanceof Offer) {
134 lastAction = action;
135 Bid curBid = DefaultAction.getBidFromAction(lastAction);
136 if (!(sender == null)) { // if don't have ourselves as a
137 // bid offerer
138 // update the opponent modelling for this agent given its offer
139 if (!oppModelHashMap.containsKey(sender.toString())) {
140 oppModelHashMap.put(sender.toString(), new OpponentModelling(sender.toString(),
141 myUtilSpace.getDomain().getIssues(), curBid, getPartyId()));
142 // here we get the very first offer of the current agent
143 } else {
144 oppModelHashMap.get(sender.toString()).updateModel(curBid);
145 }
146
147 // add the offer to the bid history
148 Bid bidOfLastOffer = ((Offer) lastAction).getBid();
149 BidDetails lastbd = new BidDetails(bidOfLastOffer, getUtility(bidOfLastOffer), myTimeLine.getTime());
150 oppBidHistory.add(lastbd);
151 }
152
153 }
154
155 // System.out.println("The sender of this action is
156 // "+sender.toString());
157 }
158
159 /**
160 * We have to respond with an Action after we consult our acceptance
161 * strategy
162 *
163 * @return An Action which is either Accept or a new Offer by us
164 */
165 public Action myOffer() {
166 Offer oppOffer = (Offer) lastAction;
167 BidDetails ourNextBid;
168 if (myTimeLine.getTime() < 0.4) { // timeslot 1
169 ourNextBid = new BidDetails(maxUtilBid(myUtilSpace).getBid(), getUtility(maxUtilBid(myUtilSpace).getBid()));
170 } else if (myTimeLine.getTime() < 0.55) { // timeslot 2.1
171 int index = randomGen.nextInt(firstSetBids.size());
172 ourNextBid = firstSetBids.get(index);
173 } else if (myTimeLine.getTime() < 0.65) { // timeslot 2.2
174 int index = randomGen.nextInt(secondSetBids.size());
175 ourNextBid = secondSetBids.get(index);
176 } else if (myTimeLine.getTime() < 0.75) { // timeslot 2.3
177 int index = randomGen.nextInt(thirdSetBids.size());
178 ourNextBid = thirdSetBids.get(index);
179 } else if (myTimeLine.getTime() < 0.85) { // timeslot 2.4
180 int index = randomGen.nextInt(fourthSetBids.size());
181 ourNextBid = fourthSetBids.get(index);
182 } else if (myTimeLine.getTime() < 0.95) { // timeslot 2.5
183 int index = randomGen.nextInt(fifthSetBids.size());
184 ourNextBid = fifthSetBids.get(index);
185 } else { // timeslot 3
186 int index = randomGen.nextInt(lastSetBids.size());
187 ourNextBid = lastSetBids.get(index);
188 }
189
190 // if our utility from this offer is bigger than that we are going to
191 // propose we have to accept
192 if (getUtility(oppOffer.getBid()) >= getUtility(ourNextBid.getBid())) {
193 return new Accept(getPartyId(), oppOffer.getBid());
194 } else {
195 // System.out.println("Time is:"+myTimeLine.getTime()+" and the
196 // offer's utility is :"+getUtility(ourNextBid.getBid()));
197 return new Offer(getPartyId(), ourNextBid.getBid());
198 }
199 }
200
201 /**
202 * Divides the bidList into separate lists filtered for certain Utility
203 * intervals
204 *
205 * @param bidList
206 */
207 public void makeLists(List<BidDetails> bidList) {
208 firstSetBids.clear();
209 secondSetBids.clear();
210 thirdSetBids.clear();
211 fourthSetBids.clear();
212 fifthSetBids.clear();
213 lastSetBids.clear();
214
215 // 1st range: [1.0,0.9], 2nd range: [0.95,0.8], 3rd range: [0.85,0.75]
216 for (BidDetails bd : bidList) {
217 double util = getUtility(bd.getBid());
218 if (util > 0.95) {
219 firstSetBids.add(bd);
220 }
221 if (util > 0.9) {
222 secondSetBids.add(bd);
223 }
224 if (util > 0.85) {
225 thirdSetBids.add(bd);
226 }
227 if (util > 0.8) {
228 fourthSetBids.add(bd);
229 }
230 if (util > 0.75) {
231 fifthSetBids.add(bd);
232 }
233 }
234
235 BidDetails maxUtilbd = new BidDetails(maxUtilBid(myUtilSpace).getBid(),
236 getUtility(maxUtilBid(myUtilSpace).getBid()));
237
238 // System.out.println("list sizes are 2.1:"+firstSetBids.size()+" -
239 // 2.2:"+secondSetBids.size()+" - 2.3:"+thirdSetBids.size()+" -
240 // 2.4:"+fourthSetBids.size()+" - 2.5:"+fifthSetBids.size());
241
242 // 1
243 if (firstSetBids.isEmpty())
244 System.err.println("firstSetBids is empty at t=" + myTimeLine.getTime());
245 else if (firstSetBids.size() > 10)
246 firstSetBids = firstSetBids.subList(0, 10);
247 firstSetBids.add(maxUtilbd);
248
249 // 2
250 if (secondSetBids.isEmpty())
251 System.err.println("secondSetBids is empty at t=" + myTimeLine.getTime());
252 else if (secondSetBids.size() > 12)
253 secondSetBids = secondSetBids.subList(0, 12);
254 secondSetBids.add(maxUtilbd);
255
256 // 3
257 if (thirdSetBids.isEmpty())
258 System.err.println("thirdSetBids is empty at t=" + myTimeLine.getTime());
259 else if (thirdSetBids.size() > 15)
260 thirdSetBids = thirdSetBids.subList(0, 15);
261 thirdSetBids.add(maxUtilbd);
262
263 // 4
264 if (fourthSetBids.isEmpty())
265 System.err.println("fourthSetBids is empty at t=" + myTimeLine.getTime());
266 else if (fourthSetBids.size() > 18)
267 fourthSetBids = fourthSetBids.subList(0, 18);
268 fourthSetBids.add(maxUtilbd);
269
270 // 5
271 if (fifthSetBids.isEmpty())
272 System.err.println("fifthSetBids is empty at t=" + myTimeLine.getTime());
273 else if (fifthSetBids.size() > 20)
274 fifthSetBids = fifthSetBids.subList(0, 20);
275 fifthSetBids.add(maxUtilbd);
276
277 // Lastly the list using BidHistory
278 // if(oppBidHistory.size()>10)
279 lastSetBids = oppBidHistory.getNBestBids(10);
280 // else
281 // lastSetBids = oppBidHistory.getHistory();
282
283 }
284
285 /**
286 * Order the list of bids by descending estimated Nash product
287 *
288 * @param bidList
289 * @return The newly ordered List of bids
290 */
291 public List<BidDetails> orderListByNash(List<BidDetails> bidList) {
292 // System.out.println("1 size = "+bidList.size());
293
294 List<BidDetails> orderedList = new ArrayList<BidDetails>();
295 orderedList.add(bidList.get(1));
296
297 List<Double> nashProdList = new ArrayList<Double>();
298 double nashProd = bidList.get(0).getMyUndiscountedUtil();
299 for (Map.Entry<String, OpponentModelling> entry : oppModelHashMap.entrySet()) {
300 nashProd = nashProd * entry.getValue().getOppUtil(bidList.get(1).getBid());
301 }
302 nashProdList.add(nashProd);
303
304 BidDetails bd;
305 for (int j = 2; j < bidList.size(); j++) {
306 bd = bidList.get(j);
307 nashProd = bd.getMyUndiscountedUtil();
308 for (Map.Entry<String, OpponentModelling> entry : oppModelHashMap.entrySet()) {
309 nashProd = nashProd * entry.getValue().getOppUtil(bd.getBid());
310 }
311 for (int i = 0; i < nashProdList.size(); i++) {
312 if (nashProd > nashProdList.get(i)) {
313 orderedList.add(i, bd);
314 nashProdList.add(i, nashProd);
315 i = nashProdList.size();
316 }
317 if (i == nashProdList.size() - 1) {
318 orderedList.add(i, bd);
319 nashProdList.add(i, nashProd);
320 i = nashProdList.size();
321 }
322 }
323 }
324 // System.out.println("1 size = "+orderedList.size());
325 return orderedList;
326 }
327
328 /**
329 * Returns the bid with the highest Utility for us out of the whole
330 * UtilitySpace
331 *
332 * @param us
333 * The UtilitySpace object
334 * @return The Offer with maximum utility, in case of exception an offer
335 * with a random bid
336 */
337 public Offer maxUtilBid(AdditiveUtilitySpace us) {
338 try {
339 return new Offer(getPartyId(), us.getMaxUtilityBid());
340 } catch (Exception e) {
341 System.err.println("Error generating maximum utility!");
342 e.printStackTrace();
343 return new Offer(getPartyId(), generateRandomBid());
344 }
345 }
346
347 /**
348 * Just a string describing Group14 Agent
349 */
350 @Override
351 public String getDescription() {
352 return "Party Group 14";
353 }
354
355}
Note: See TracBrowser for help on using the repository browser.