source: src/main/java/parties/in4010/q12015/group7/Group7.java@ 126

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

Initial import : Genius 9.0.0

File size: 11.4 KB
Line 
1package parties.in4010.q12015.group7;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Map;
6import java.util.Random;
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.bidding.BidDetails;
14import genius.core.parties.AbstractNegotiationParty;
15import genius.core.parties.NegotiationInfo;
16
17/**
18 * Negotiation Party of Group 7
19 */
20public class Group7 extends AbstractNegotiationParty {
21 /**
22 * Default toughness of this agent 0 = very tough, 1 = easily gives in
23 */
24 static final double DEFAULT_TOUGHNESS = 0.2;
25
26 /**
27 * The amount the toughness is increased according to the number of
28 * opponents (so that the agent gives in more easily!) when calculating the
29 * minimum utility
30 */
31 static final double[] ADD_TOUGHNESS_NR_PARTIES = { 0.00, // (this never
32 // happens, 0
33 // parties, no
34 // opponents)
35 0.00, // (this never happens, 1 party, no opponents)
36 0.00, // 2 parties, 1 opponent
37 0.10, // 3 parties, 2 opponents
38 0.15, // 4 parties, 3 opponents
39 0.20 // 5 parties, 4 opponents
40 };
41
42 /**
43 * Minimum toughness of this agent (even when copying opponent) 0 = very
44 * tough, 1 = easily gives in
45 */
46 static final double MIN_TOUGHNESS = 0.03;
47
48 /**
49 * Minimum acceptable toughness of opponent before copying toughness of
50 * opponent (should not be lower than default_toughness) 0 = very tough, 1 =
51 * easily gives in
52 */
53 static final double MIN_ACCEPTABLE_TOUGHNESS = 0.2;
54
55 /**
56 * Time after which the agent stops copying the opponents toughness
57 */
58 static final double MAX_TIME_COPY_TOUGHNESS = 0.8;
59
60 /**
61 * Time after which the agent accepts if a bid is higher than its dynamic
62 * minimum utility
63 */
64 static final double TIME_ACCEPT_ABOVE_MIN_UTILITY = 0.5;
65
66 /**
67 * Number of bids the toughness estimation of the opponent depends on
68 */
69 static final int TOUGHNESS_NUM_BIDS = 10;
70
71 /**
72 * Sensitivity parameter for estimation of issue and value weights
73 */
74 static final double ESTIMATOR_N = 0.1;
75
76 /**
77 * Maximum amount the received bid is less than the utility of our last bid
78 * in order to accept the received bid
79 */
80 static final double MAX_DIFF_UTILITY = 0.025;
81
82 /**
83 * Time after which the agent uses bid optimization for opponents
84 */
85 static final double MAX_TIME_BID_WITH_ESTIMATOR = 0.33;
86
87 /**
88 * Number of bids we want to accept with a certain probability
89 */
90 static final int NUM_BIDS_ACCEPT_PROBABILITY = 30;
91
92 /**
93 * Turn the logger on or off. Turning the logger on will result in a file
94 * for every negotation in the folder ../logs/
95 */
96 static final boolean CREATE_LOGS = false;
97
98 /**
99 * BidHistory of all opponents
100 */
101 private MultipleBidHistory opBidHistory;
102
103 /**
104 * Value and issue weight estimator for all opponents
105 */
106 private MultipleIssueEstimator opIssueEstimator;
107
108 /**
109 * BidHistory for own bids
110 */
111 private EnhancedBidHistory myBidHistory;
112
113 /**
114 * Bidspace for own bids
115 */
116 private BidSpace bidSpace;
117
118 /**
119 * The maximum possible utility for our agent (for some reason this is not
120 * always 1!)
121 */
122 private double maxUtility;
123
124 /**
125 * Toughness of the agent from 0 to 1 0 = very tough, 1 = easily gives in
126 */
127 private double toughness;
128
129 /**
130 * Logger
131 */
132 private Logger logger;
133
134 /**
135 * Constructor for this agent
136 */
137 public Group7() {
138 opIssueEstimator = new MultipleIssueEstimator(ESTIMATOR_N);
139 opBidHistory = new MultipleBidHistory();
140 myBidHistory = new EnhancedBidHistory();
141 bidSpace = new BidSpace();
142 logger = new Logger(CREATE_LOGS);
143 }
144
145 /**
146 * Initiates the agent
147 */
148 @Override
149 public void init(NegotiationInfo info) {
150 super.init(info);
151
152 toughness = DEFAULT_TOUGHNESS;
153
154 Bid bid = null;
155 try {
156 bid = this.utilitySpace.getMaxUtilityBid();
157 } catch (Exception e) {
158 e.printStackTrace();
159 }
160 this.maxUtility = getUtility(bid);
161 this.populateBidSpace();
162 }
163
164 /**
165 * Populates the bidSpace with 1000 bids above the minimum minimum utility
166 */
167 public void populateBidSpace() {
168 Bid bid = null;
169 try {
170 bid = this.utilitySpace.getMaxUtilityBid();
171 } catch (Exception e) {
172 e.printStackTrace();
173 }
174
175 bidSpace.add(bid, getUtility(bid));
176 for (int i = 0; i < 1000; i++) {
177 bid = generateRandomBid();
178 while (getUtility(bid) < 1 - DEFAULT_TOUGHNESS) {
179 bid = generateRandomBid();
180 }
181 bidSpace.add(bid, getUtility(bid));
182 }
183 }
184
185 /**
186 * Function which gets called if it is our turn to bid / accept / decline
187 *
188 * @return Offfer
189 */
190 @Override
191 public Action chooseAction(List<Class<? extends Action>> validActions) {
192 adjustToughness();
193
194 logger.logln("");
195 logger.logln("Time: " + this.getTimeLine().getTime());
196 logger.logln("Toughness: " + this.toughness);
197 logger.logln("Max utility: " + this.maxUtility);
198 logger.logln("Min utility: " + this.minUtility());
199 logger.logln("Probability of acceptance: " + this.probAcceptance());
200 if (this.acceptWithProbability()) {
201 logger.logln("In state of accepting with certain probability...");
202 }
203 logger.logln("");
204 if (!validActions.contains(Accept.class)) {
205 Bid bid = firstBid();
206 return generateOffer(bid);
207 } else if ((myBidHistory.getLastBid() != null && getUtility(this.opBidHistory.getCombinedHistory()
208 .getLastBid()) > (getUtility(this.myBidHistory.getLastBid()) - MAX_DIFF_UTILITY))) {
209
210 logger.logln("");
211 logger.logln("-- ACCEPTED");
212 logger.logln("My previous bid: " + this.myBidHistory.getLastBid());
213 logger.logln("Accepted bid with u = " + getUtility(this.opBidHistory.getCombinedHistory().getLastBid())
214 + ", because little difference with last bid with u = " + getUtility(myBidHistory.getLastBid()));
215 logger.logln("");
216 return new Accept(getPartyId(), this.opBidHistory.getCombinedHistory().getLastBid());
217
218 } else if (this.getTimeLine().getTime() > TIME_ACCEPT_ABOVE_MIN_UTILITY
219 && getUtility(this.opBidHistory.getCombinedHistory().getLastBid()) > this.minUtility()) {
220
221 logger.logln("");
222 logger.logln("-- ACCEPTED");
223 logger.logln("Accepted bid with u = " + getUtility(this.opBidHistory.getCombinedHistory().getLastBid())
224 + ", because its utility was higher than the minimal required utility u = " + this.minUtility()
225 + ".");
226 logger.logln("");
227 return new Accept(getPartyId(), this.opBidHistory.getCombinedHistory().getLastBid());
228
229 } else if (new Random().nextDouble() < Math.min(1, this.probAcceptance()) && this.acceptWithProbability()) {
230
231 logger.logln("");
232 logger.logln("-- ACCEPTED");
233 logger.logln("Accepted because of the acceptance probability of " + this.probAcceptance() + ".");
234 logger.logln("");
235 return new Accept(getPartyId(), this.opBidHistory.getCombinedHistory().getLastBid());
236 } else {
237 Bid bid;
238 if (this.getTimeLine().getTime() > MAX_TIME_BID_WITH_ESTIMATOR) {
239 bid = regularBidMaxOpponentUtilityEstimation();
240 } else {
241 bid = normalBid();
242 }
243 return generateOffer(bid);
244 }
245 }
246
247 public double probAcceptance() {
248 return Math.exp(-(Math.abs(this.maxUtility - getUtility(this.opBidHistory.getCombinedHistory().getLastBid())))
249 / (this.getTimeLine().getTime() / 10));
250 }
251
252 public boolean acceptWithProbability() {
253 // If there is only one party and we have not performed any bid, return
254 // false
255 if (this.myBidHistory.size() == 0)
256 return false;
257
258 // If we have time left for numberOfBids bids left
259 if (1 - this.getTimeLine().getTime() < NUM_BIDS_ACCEPT_PROBABILITY
260 * (this.getTimeLine().getTime() / this.myBidHistory.size()))
261 return true;
262
263 // Else return false
264 return false;
265 }
266
267 /**
268 * Returns the bid to be performed as the first bid
269 *
270 * @return first maximum utility bid
271 */
272 public Bid firstBid() {
273 Bid bid = null;
274 try {
275 bid = utilitySpace.getMaxUtilityBid();
276 } catch (Exception e) {
277 e.printStackTrace();
278 }
279
280 return bid;
281 }
282
283 /**
284 * Returns a bid corresponding to the agents dynamic minimum utility and the
285 * highest average estimated utility for the opponents
286 *
287 * @return a bid
288 */
289 public Bid regularBidMaxOpponentUtilityEstimation() {
290 Bid maxBid = null;
291 double maxUtil = 0;
292 double util;
293
294 ArrayList<Bid> list = bidSpace.getBids(this.minUtility(), this.maxUtility);
295 for (Bid myBid : list) {
296 util = this.opIssueEstimator.getAverageUtility(myBid);
297 if (util > maxUtil) {
298 maxBid = myBid;
299 maxUtil = util;
300 }
301 }
302
303 return maxBid;
304 }
305
306 /**
307 * Returns a bid corresponding to the minimum utility
308 *
309 * @return a regular bid
310 */
311 public Bid normalBid() {
312 Bid myBid = bidSpace.generatePseudoRandomBid(this.minUtility(), this.maxUtility);
313
314 return myBid;
315 }
316
317 /**
318 * Logs the bid, adds the bid to the BidHistory, generates an offer with the
319 * bid.
320 *
321 * @param bid
322 * @return offer according to bid
323 */
324 public Offer generateOffer(Bid bid) {
325 logger.logln("");
326 logger.logln("-- BID OFFERED: ");
327 logger.logln("Bid: " + bid);
328 logger.logln("Utility: " + getUtility(bid));
329 for (Map.Entry<AgentID, IssueEstimator> entry : this.opIssueEstimator.getIssueEstimators().entrySet()) {
330 logger.logln("Opponent Toughness: "
331 + this.opBidHistory.getHistory(entry.getKey()).getToughness(TOUGHNESS_NUM_BIDS));
332 logger.logln("Estimated Opponent Utility (" + entry.getKey() + "): "
333 + this.opIssueEstimator.getIssueEstimator(entry.getKey()).getUtility(bid));
334 }
335 logger.logln(" ");
336
337 myBidHistory.add(new BidDetails(new Bid(bid), getUtility(bid)));
338
339 logger.logln(myBidHistory.getLastBid());
340
341 return new Offer(getPartyId(), bid);
342 }
343
344 /**
345 * Returns the minimum acceptable utility of this agent according to the
346 * time and toughness
347 *
348 * @return minimum utility
349 */
350 public double minUtility() {
351 return this.maxUtility - (this.getTimeLine().getTime() * this.toughness);
352 }
353
354 /**
355 * Adjust the toughness of this agent according to the toughness of
356 * opponents
357 */
358 public void adjustToughness() {
359 if (this.opBidHistory.getMinToughness(TOUGHNESS_NUM_BIDS) < DEFAULT_TOUGHNESS
360 && this.getTimeLine().getTime() < MAX_TIME_COPY_TOUGHNESS) {
361 this.toughness = Math.max(MIN_TOUGHNESS, this.opBidHistory.getMinToughness(TOUGHNESS_NUM_BIDS));
362 } else {
363 this.toughness = DEFAULT_TOUGHNESS + this.getAdditionalToughnessNumberOfParties();
364 }
365 }
366
367 /**
368 * Get less toughness according to the number parties
369 */
370 public double getAdditionalToughnessNumberOfParties() {
371 if (this.getNumberOfParties() < ADD_TOUGHNESS_NR_PARTIES.length) {
372 return ADD_TOUGHNESS_NR_PARTIES[this.getNumberOfParties()];
373 }
374 return ADD_TOUGHNESS_NR_PARTIES[ADD_TOUGHNESS_NR_PARTIES.length - 1];
375 }
376
377 /**
378 * Function that is called when a message is received by one of the
379 * opponents
380 */
381 public void receiveMessage(AgentID sender, Action action) {
382 super.receiveMessage(sender, action);
383 AgentID agentId = new AgentID(String.valueOf(sender));
384
385 if (action.getClass() == Offer.class) {
386 Offer opponentOffer = (Offer) action;
387 this.opIssueEstimator.addBid(agentId,
388 new BidDetails(new Bid(opponentOffer.getBid()), getUtility(opponentOffer.getBid())),
389 this.opBidHistory.getHistory(agentId));
390 this.opBidHistory.addBid(agentId,
391 new BidDetails(new Bid(opponentOffer.getBid()), getUtility(opponentOffer.getBid())));
392
393 logger.logln("");
394 logger.logln("-- BID FROM PARTY " + agentId);
395 logger.logln("Bid: " + opponentOffer.getBid());
396 logger.logln("Utility: " + getUtility(opponentOffer.getBid()));
397 logger.logln("");
398 } else {
399 logger.logln("");
400 logger.logln("-- ACCEPTANCE FROM PARTY " + agentId);
401 logger.logln("");
402 }
403 }
404
405 @Override
406 public String getDescription() {
407 return "Party Group 7";
408 }
409}
Note: See TracBrowser for help on using the repository browser.