source: anac2020/TheDiceHaggler/src/main/java/geniusweb/bagga/dicehaggler/TheDiceHaggler.java@ 31

Last change on this file since 31 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 13.6 KB
Line 
1package geniusweb.bagga.dicehaggler;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.Dictionary;
9import java.util.HashSet;
10import java.util.Hashtable;
11import java.util.List;
12import java.util.logging.Level;
13
14import javax.websocket.DeploymentException;
15
16import geniusweb.actions.Accept;
17import geniusweb.actions.Action;
18import geniusweb.actions.Comparison;
19import geniusweb.actions.ElicitComparison;
20import geniusweb.actions.Offer;
21import geniusweb.actions.PartyId;
22import geniusweb.bidspace.AllBidsList;
23import geniusweb.issuevalue.Bid;
24import geniusweb.issuevalue.Domain;
25import geniusweb.party.Capabilities;
26import geniusweb.party.DefaultParty;
27import geniusweb.party.inform.ActionDone;
28import geniusweb.party.inform.Finished;
29import geniusweb.party.inform.Inform;
30import geniusweb.party.inform.Settings;
31import geniusweb.party.inform.YourTurn;
32import geniusweb.profile.PartialOrdering;
33import geniusweb.profileconnection.ProfileConnectionFactory;
34import geniusweb.profileconnection.ProfileInterface;
35import geniusweb.progress.Progress;
36import geniusweb.progress.ProgressRounds;
37import tudelft.utilities.logging.Reporter;
38
39public class TheDiceHaggler extends DefaultParty {
40
41 private long startTime;
42 private long endTime;
43 private Action lastReceivedAction;
44 private Bid receivedBid;
45 private Progress progress;
46 private PartyId me;
47 private ProfileInterface profileint;
48 private AllBidsList allbids;
49 private PartialOrdering partialprofile;
50 private List<Bid> givenPartialOrderedBids;
51 private List<Bid> opponentBidHistory = new ArrayList<Bid>();
52 private double time;
53 private DH_OpponentModel om;
54
55 private Domain domain;
56
57 private boolean elicitCounter = true;
58 private double fixedUtility = 0.98;
59 private double dynamicUtility = 1D; //would change over time
60 private double reservationThresholdUtility = 0.85;
61
62 private Dictionary<String, DH_Issue> utilSpace = new Hashtable<String, DH_Issue>();
63 private Dictionary<String, DH_Issue> opponentModel = new Hashtable<String, DH_Issue>();
64 private DH_UserModel userM;
65 private DH_BiddingStrategy bs;
66 private SimpleLinearOrdering estimatedProfile = null;
67
68 public TheDiceHaggler() {
69 super();
70 }
71
72 public TheDiceHaggler(Reporter reporter) {
73 super(reporter);
74 }
75
76 private void init(Settings info) throws IOException, DeploymentException {
77 startTime = System.currentTimeMillis();
78 this.me = info.getID();
79 this.progress = info.getProgress();
80 endTime = progress.getTerminationTime().getTime();
81 this.profileint = ProfileConnectionFactory
82 .create(info.getProfile().getURI(), getReporter());
83 this.partialprofile = (PartialOrdering) profileint
84 .getProfile(); //patial profile also contains the reservation bid
85
86 allbids = new AllBidsList(this.partialprofile.getDomain()); //total number of possible bids.. all permutations
87 // Wouter we use SimpleLinearOrdering (from shaop party) to get sorted
88 // bids from our profile.
89 this.domain = this.partialprofile.getDomain();
90 this.time = getTime(System.currentTimeMillis());
91 //System.out.println("time "+ time+ " in ms: "+System.currentTimeMillis());
92 System.out.println("############### Now Time is : "+ time + " #############");
93 givenPartialOrderedBids = new SimpleLinearOrdering(this.partialprofile).getBids();
94
95 om = new DH_OpponentModel(this.domain);
96 opponentModel = om.initializeOpponentModel();
97
98 userM = new DH_UserModel(this.domain, givenPartialOrderedBids);
99 utilSpace = userM.estimateutilityspace();
100
101 bs = new DH_BiddingStrategy();
102 }
103
104 @Override
105 public void notifyChange(Inform info) {
106 try {
107 if (info instanceof Settings) {
108 System.out.println("............. Initializing Settings ...........");
109 init((Settings) info);
110 } else if (info instanceof ActionDone) {
111 System.out.println("............. Some action has been done ...........");
112
113 lastReceivedAction = ((ActionDone) info).getAction();
114 if (lastReceivedAction instanceof Offer) {
115 System.out.println("............. Opponent has sent an Offer ...........");
116 this.receivedBid = ((Offer) lastReceivedAction).getBid();
117
118 opponentBidHistory.add(this.receivedBid);
119 opponentModel = om.updateOpponentModel(time, opponentBidHistory);
120 }
121 else if (lastReceivedAction instanceof Comparison) {
122 System.out.println("............. COB party has done the comparison ...........");
123
124 estimatedProfile = estimatedProfile.with(
125 ((Comparison) lastReceivedAction).getBid(),
126 ((Comparison) lastReceivedAction).getWorse()); //getWorse is a list of worst bids than the given bid
127
128 System.out.println(" ...... So, let's estimate the user space again .....");
129 userM = new DH_UserModel(domain, estimatedProfile.getBids());
130 utilSpace = userM.estimateutilityspace();
131
132 System.out.println(" ...... Now, we choose the action based on updated user model");
133 chooseAction();
134 }
135 } else if (info instanceof YourTurn) {
136 System.out.println(" ....... HEy! it's my turn to make an action....");
137 chooseAction();
138
139 } else if (info instanceof Finished) {
140 getReporter().log(Level.INFO, "Final outcome:" + info);
141 }
142 } catch (Exception e) {
143 throw new RuntimeException("Failed to handle info", e);
144 }
145 }
146
147 private void chooseAction() throws IOException {
148 this.time = getTime(System.currentTimeMillis());
149 Action action = null;
150
151 if (estimatedProfile == null) {
152 estimatedProfile = new SimpleLinearOrdering(
153 profileint.getProfile());
154 }
155
156 // Start competition
157
158 if (receivedBid == null) {
159 Bid offeredBid = determineOpeningBid();
160 System.out.println("At time "+time+" , I offer an opening bid with util: "+DH_UserModel.getUtility(offeredBid, utilSpace));
161 action = new Offer(me, offeredBid);
162 }
163 else {
164 //if less than 5% of the total possible bids are given in the partial order and the variance in pairwise correlations is too large, then do elicitation
165 if(elicitCounter && givenPartialOrderedBids.size() < 0.05 * totalNumberOfPossibleBids() && userM.checkVariance()) {
166 //decide which bids to compare with what..and then do the elicitation
167 System.out.println("HEY LISTEN! at time "+ time + "I am going to ask the user to compare these bids");
168 action = new ElicitComparison(me, getMyNextBid(time), givenPartialOrderedBids);
169 elicitCounter = false;
170 }
171 else if (isAcceptable(receivedBid)) {
172 System.out.println("At time "+time+" ,I accept the received bid with util: "+DH_UserModel.getUtility(receivedBid, utilSpace));
173 action = new Accept(me, receivedBid);
174 }
175 if (progress instanceof ProgressRounds) {
176 progress = ((ProgressRounds) progress).advance();
177 }
178 }
179 if (action == null) {
180 Bid offeredBid = bs.generateBid(time, domain, utilSpace, opponentModel, opponentBidHistory, allbids);
181 System.out.println("At time "+time+" , I offer a bid with util: "+ DH_UserModel.getUtility(offeredBid, utilSpace));
182 action = new Offer(me, offeredBid);
183 }
184 getConnection().send(action);
185
186 }
187
188private double getTime(long currentTimeMillis) {
189 // TODO Auto-generated method stub
190 long duration = endTime - startTime;
191 long delta = currentTimeMillis - startTime;
192 Double ratio = delta / (double) duration;
193 return ratio;
194 }
195
196//
197// private List<Bid> getListOfTwoMostVariedBids(List<Bid> givenBids) {
198//
199// List<Bid> list = new ArrayList<>();
200// double sum = 0D;
201// for( Bid bid: givenBids) {
202// sum += DH_UserModel.getUtility(bid, utilSpace);
203// }
204// double mean = sum/givenBids.size();
205//
206// double maxSum = 0D;
207// Bid topVariedBid = null;
208// Bid secondTopBid = null;
209// int index = 0;
210// int topIndex = 0;
211// for( Bid bid: givenBids) {
212// index++;
213// if(maxSum < Math.pow(DH_UserModel.getUtility(bid, utilSpace) - mean, 2)) {
214// maxSum = Math.pow(DH_UserModel.getUtility(bid, utilSpace) - mean, 2);
215// topVariedBid = bid;
216// topIndex = index;
217// }
218// }
219// givenBids.remove(topIndex);
220// for( Bid bid: givenBids) {
221// if(maxSum < Math.pow(DH_UserModel.getUtility(bid, utilSpace) - mean, 2)) {
222// maxSum = Math.pow(DH_UserModel.getUtility(bid, utilSpace) - mean, 2);
223// secondTopBid = bid;
224// }
225// }
226// list.add(topVariedBid);
227// list.add(secondTopBid);
228// return list;
229// }
230//
231// private boolean checkVariance(List<Bid> givenBids) {
232// double sum = 0D;
233// for( Bid bid: givenBids) {
234// sum += DH_UserModel.getUtility(bid, utilSpace);
235// }
236// double mean = sum/givenBids.size();
237//
238// double sum1 = 0D;
239// for( Bid bid: givenBids) {
240// sum1 += Math.pow(DH_UserModel.getUtility(bid, utilSpace) - mean, 2);
241// }
242// double variance = sum1/(givenBids.size()-1);
243// return (variance > 0.6);
244// }
245
246 private Bid getMyNextBid(double time) {
247 return bs.generateBid(time, domain, utilSpace, opponentModel, opponentBidHistory, allbids);
248 }
249
250 private boolean isAcceptable(Bid receivedBid) {
251 this.time = getTime(System.currentTimeMillis());
252
253 DH_TargetUtility t_u = new DH_TargetUtility(domain, time, opponentBidHistory, opponentModel, allbids.size().intValue());
254
255 //time = progress.get(System.currentTimeMillis());
256 if(time >= 0.7) {
257 System.out.println("time is greater than 0.7, let's calculate the dynamic threshold");
258 try {
259 dynamicUtility = t_u.calculateTargetValue();
260 System.out.println("at time t = "+time + "the dynamic threshold utility is: "+dynamicUtility);
261 if(dynamicUtility < reservationThresholdUtility) dynamicUtility = reservationThresholdUtility;
262 }
263 catch (Exception e) {
264 e.printStackTrace();
265 }
266 }
267 double myFutureBidUtility = DH_UserModel.getUtility(getMyNextBid(time), utilSpace);
268 double lastReceivedBidUtility = DH_UserModel.getUtility(getMyNextBid(time), utilSpace);
269
270 boolean accept;
271 if((time < 0.4 && lastReceivedBidUtility >= fixedUtility)
272 ||
273 (time >= 0.4 && time < 0.5 & lastReceivedBidUtility >= DH_UserModel.getUtility(getOpponentBestBidDetails(), utilSpace))
274 //why dynamic threshold is not considered here.. because my future bid utility will always be higher than that my threshold value
275 ||
276 (time >= 0.5 && time < 0.6 & lastReceivedBidUtility >= myFutureBidUtility & lastReceivedBidUtility > bestOfQquantileOpponentBids(0.9))
277 //best out of the bottom 90% of the received bids in the ordered utility from high to less
278 ||
279 (time >= 0.6 && time < 0.7 && lastReceivedBidUtility >= myFutureBidUtility && lastReceivedBidUtility > bestOfQquantileOpponentBids(0.8))
280 //best out of the bottom 80% of the received bids
281 ||
282 (time >= 0.7 && time < 0.8 && lastReceivedBidUtility >= dynamicUtility && lastReceivedBidUtility > bestOfQquantileOpponentBids(0.7))
283 //best out of the bottom 70% of the bids
284 ||
285 (time >= 0.8 && time < 0.9 && lastReceivedBidUtility >= dynamicUtility && lastReceivedBidUtility > bestOfQquantileOpponentBids(0.6))
286 //best out of the bottom 60% of the bids
287 ||
288 (time >= 0.9 && lastReceivedBidUtility >= dynamicUtility))
289 {
290 accept = true;
291 }
292 else
293 {
294 System.out.println("At time "+ time+" , I am going to reject the received offer and propose a new offer");
295 accept = false;
296 }
297
298 return accept;
299 }
300
301 private double bestOfQquantileOpponentBids(double q) {
302 List<Bid> sortedOpponentHistory = getSortedHistory(opponentBidHistory); //in an increasing order
303 double util = 0D;
304 //int k = (int) (Math.ceil((1-q) * sortedOpponentHistory.size()) - 1); //if the list is in a decreasing order
305 int k = (int) (Math.ceil((q) * sortedOpponentHistory.size()) - 1); //if the list is in an increasing order
306
307 util = DH_UserModel.getUtility(sortedOpponentHistory.get(k), utilSpace);
308 System.out.println("Q = "+q+" size = " + sortedOpponentHistory.size() + " index = "+ k + " util = " + util);
309 return util;
310 }
311
312 private List<Bid> getSortedHistory(List<Bid> opponentBidHistory2) {
313 System.out.println("I am going to get the sorted opponent history");
314 List<Bid> sortedOpponentHistory = new ArrayList<Bid>(); //in an ascending order
315 Collections.sort(sortedOpponentHistory, new Comparator<Bid>() {
316 @Override
317 public int compare(Bid b1, Bid b2)
318 {
319 if (b1 == null || b2 == null)
320 throw new NullPointerException();
321 if (b1.equals(b2))
322 return 0;
323 if (DH_UserModel.getUtility(b1, utilSpace) > DH_UserModel.getUtility(b2, utilSpace))
324 return -1;
325 else if (DH_UserModel.getUtility(b1, utilSpace) < DH_UserModel.getUtility(b2, utilSpace))
326 return 1;
327 else
328 return ((Integer) b1.hashCode()).compareTo(b2.hashCode());
329 }
330 });
331 for(Bid l:sortedOpponentHistory) {
332 System.out.println(DH_UserModel.getUtility(l, utilSpace));
333 }
334 return sortedOpponentHistory;
335 }
336
337 private Bid getOpponentBestBidDetails() {
338 double max = Double.NEGATIVE_INFINITY;
339 Bid bestBid = null;
340 for (Bid b : opponentBidHistory) {
341 double utility = DH_UserModel.getUtility(b, utilSpace);
342 if (utility >= max) {
343 max = utility;
344 bestBid = b;
345 }
346 }
347 return bestBid;
348 }
349
350 private Bid determineOpeningBid() {
351 //maximum bid in the given user domain
352 return givenPartialOrderedBids.get(givenPartialOrderedBids.size()-1);
353 }
354
355
356 @Override
357 public Capabilities getCapabilities() {
358 return new Capabilities(new HashSet<>(Arrays.asList("SHAOP")));
359 }
360
361 @Override
362 public String getDescription() {
363 return "The Dice Haggler 2020";
364 }
365
366 private int totalNumberOfPossibleBids() {
367 int num = 1;
368 for(String i : domain.getIssues()) {
369 num *= domain.getValues(i).size().intValue();
370 }
371 //System.out.println("I count total bids = " + num + " and real value is: "+ allbids.size());
372 return num;
373
374 }
375}
376
377
Note: See TracBrowser for help on using the repository browser.