source: anac2021/dicehaggler/src/main/java/geniusweb/exampleparties/learningagent/PersistentState.java

Last change on this file was 14, checked in by wouter, 3 years ago

#2 ANAC2021 parties received from Bram

File size: 3.0 KB
Line 
1package geniusweb.exampleparties.learningagent; // TODO: change name
2
3import java.util.HashMap;
4import java.util.Map;
5
6import com.fasterxml.jackson.annotation.JsonAutoDetect;
7import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
8
9/**
10 * This class can hold the persistent state of your agent. You can off course
11 * also write something else to the file path that is provided to your agent,
12 * but this provides an easy usable method. This object is serialized using
13 * Jackson. NOTE that Jackson can serialize many default java classes, but not
14 * custom classes out-of-the-box.
15 */
16@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
17public class PersistentState {
18
19 private Double averageUtility = 0.0;
20 private Integer negotiations = 0;
21 private Map<String, Double> avgMaxUtilityOpponent = new HashMap<String, Double>();
22 private Map<String, Integer> opponentEncounters = new HashMap<String, Integer>();
23
24 /**
25 * Update the persistent state with a negotiation data of a previous negotiation
26 * session
27 *
28 * @param negotiationData NegotiationData class holding the negotiation data
29 * that is obtain during a negotiation session.
30 */
31 public void update(NegotiationData negotiationData) {
32 // Keep track of the average utility that we obtained
33 this.averageUtility = (this.averageUtility * negotiations + negotiationData.getAgreementUtil())
34 / (negotiations + 1);
35
36 // Keep track of the number of negotiations that we performed
37 negotiations++;
38
39 // Get the name of the opponent that we negotiated against
40 String opponent = negotiationData.getOpponentName();
41
42 // Check for safety
43 if (opponent != null) {
44 // Update the number of encounters with an opponent
45 Integer encounters = opponentEncounters.containsKey(opponent) ? opponentEncounters.get(opponent) : 0;
46 opponentEncounters.put(opponent, encounters + 1);
47 // Track the average value of the maximum that an opponent has offered us across
48 // multiple negotiation sessions
49 Double avgUtil = avgMaxUtilityOpponent.containsKey(opponent) ? avgMaxUtilityOpponent.get(opponent) : 0.0;
50 avgMaxUtilityOpponent.put(opponent,
51 (avgUtil * encounters + negotiationData.getMaxReceivedUtil()) / (encounters + 1));
52 }
53 }
54
55 public Double getAvgMaxUtility(String opponent) {
56 if (avgMaxUtilityOpponent.containsKey(opponent)) {
57 return avgMaxUtilityOpponent.get(opponent);
58 }
59 return null;
60 }
61
62 public Integer getOpponentEncounters(String opponent) {
63 if (opponentEncounters.containsKey(opponent)) {
64 return opponentEncounters.get(opponent);
65 }
66 return null;
67 }
68
69 public Boolean knownOpponent(String opponent) {
70 return opponentEncounters.containsKey(opponent);
71 }
72}
Note: See TracBrowser for help on using the repository browser.