source: src/main/java/agents/anac/y2019/minf/etc/HardHeadedFrequencyModel.java

Last change on this file was 200, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents

  • Property svn:executable set to *
File size: 6.5 KB
Line 
1package agents.anac.y2019.minf.etc;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Map.Entry;
7import java.util.Set;
8import java.util.Iterator;
9
10import genius.core.Bid;
11import genius.core.bidding.BidDetails;
12import genius.core.boaframework.BOAparameter;
13import genius.core.boaframework.NegotiationSession;
14import genius.core.boaframework.OpponentModel;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.Objective;
18import genius.core.issue.Value;
19import genius.core.issue.ValueDiscrete;
20import genius.core.utility.AdditiveUtilitySpace;
21import genius.core.utility.Evaluator;
22import genius.core.utility.EvaluatorDiscrete;
23
24/**
25 * BOA framework implementation of the HardHeaded Frequecy Model.
26 *
27 * Default: learning coef l = 0.2; learnValueAddition v = 1.0
28 *
29 * paper: https://ii.tudelft.nl/sites/default/files/boa.pdf
30 */
31public class HardHeadedFrequencyModel extends OpponentModel {
32
33 /*
34 * the learning coefficient is the weight that is added each turn to the
35 * issue weights which changed. It's a trade-off between concession speed
36 * and accuracy.
37 */
38 private double learnCoef;
39 /*
40 * value which is added to a value if it is found. Determines how fast the
41 * value weights converge.
42 */
43 private int learnValueAddition;
44 private int amountOfIssues;
45 private double goldenValue;
46 private double gamma;
47
48 private AdditiveUtilitySpace opponentUtilCount;
49
50 @Override
51 public void init(NegotiationSession negotiationSession,
52 Map<String, Double> parameters) {
53 this.negotiationSession = negotiationSession;
54 if (parameters != null && parameters.get("l") != null) {
55 learnCoef = parameters.get("l");
56 } else {
57 learnCoef = 0.2;
58 }
59 learnValueAddition = 1;
60 gamma = 0.25;
61 opponentUtilitySpace = (AdditiveUtilitySpace) negotiationSession
62 .getUtilitySpace().copy();
63 amountOfIssues = opponentUtilitySpace.getDomain().getIssues().size();
64 /*
65 * This is the value to be added to weights of unchanged issues before
66 * normalization. Also the value that is taken as the minimum possible
67 * weight, (therefore defining the maximum possible also).
68 */
69 goldenValue = learnCoef / amountOfIssues;
70
71 initializeModel();
72
73 }
74
75 @Override
76 public void updateModel(Bid opponentBid, double time) {
77 if (negotiationSession.getOpponentBidHistory().size() < 2) {
78 return;
79 }
80 int numberOfUnchanged = 0;
81 BidDetails oppBid = negotiationSession.getOpponentBidHistory()
82 .getHistory()
83 .get(negotiationSession.getOpponentBidHistory().size() - 1);
84 BidDetails prevOppBid = negotiationSession.getOpponentBidHistory()
85 .getHistory()
86 .get(negotiationSession.getOpponentBidHistory().size() - 2);
87 HashMap<Integer, Integer> lastDiffSet = determineDifference(prevOppBid,
88 oppBid);
89
90 // System.out.println(lastDiffSet);
91
92 // count the number of changes in value
93 for (Integer i : lastDiffSet.keySet()) {
94 if (lastDiffSet.get(i) == 0)
95 numberOfUnchanged++;
96 }
97
98 // The total sum of weights before normalization.
99 double totalSum = 1D + goldenValue * numberOfUnchanged;
100 // The maximum possible weight
101 double maximumWeight = 1D - (amountOfIssues) * goldenValue / totalSum;
102
103 // re-weighing issues while making sure that the sum remains 1
104 for (Integer i : lastDiffSet.keySet()) {
105 Objective issue = opponentUtilitySpace.getDomain()
106 .getObjectivesRoot().getObjective(i);
107 double weight = opponentUtilitySpace.getWeight(i);
108 double newWeight;
109
110 if (lastDiffSet.get(i) == 0 && weight < maximumWeight) {
111 newWeight = (weight + goldenValue) / totalSum;
112 } else {
113 newWeight = weight / totalSum;
114 }
115 opponentUtilitySpace.setWeight(issue, newWeight);
116 }
117
118 // Then for each issue value that has been offered last time, a constant
119 // value is added to its corresponding ValueDiscrete.
120 try {
121 for (Iterator<Entry<Objective, Evaluator>> itr1 = opponentUtilitySpace.getEvaluators().iterator(),
122 itr2 = opponentUtilCount.getEvaluators().iterator(); itr1.hasNext() && itr2.hasNext();){
123 Entry<Objective, Evaluator> e1 = itr1.next();
124 Entry<Objective, Evaluator> e2 = itr2.next();
125
126 EvaluatorDiscrete value_util = (EvaluatorDiscrete) e1.getValue();
127 EvaluatorDiscrete value_cnt = (EvaluatorDiscrete) e2.getValue();
128 IssueDiscrete issue = ((IssueDiscrete) e1.getKey());
129
130 ValueDiscrete issuevalue = (ValueDiscrete) oppBid.getBid()
131 .getValue(issue.getNumber());
132
133 Integer eval = value_cnt.getEvaluationNotNormalized(issuevalue);
134 value_cnt.setEvaluation(issuevalue, (learnValueAddition + eval));
135
136 value_util.setEvaluationDouble(issuevalue, Math.pow(1+learnValueAddition+eval, gamma));
137 }
138 } catch (Exception ex) {
139 ex.printStackTrace();
140 }
141 }
142
143 @Override
144 public double getBidEvaluation(Bid bid) {
145 double result = 0;
146 try {
147 result = opponentUtilitySpace.getUtility(bid);
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 return result;
152 }
153
154 @Override
155 public String getName() {
156 return "HardHeaded Frequency Model";
157 }
158
159 @Override
160 public Set<BOAparameter> getParameterSpec() {
161 Set<BOAparameter> set = new HashSet<BOAparameter>();
162 set.add(new BOAparameter("l", 0.2,
163 "The learning coefficient determines how quickly the issue weights are learned"));
164 return set;
165 }
166
167 /**
168 * Init to flat weight and flat evaluation distribution
169 */
170 private void initializeModel() {
171 double commonWeight = 1D / amountOfIssues;
172
173 for (Entry<Objective, Evaluator> e : opponentUtilitySpace
174 .getEvaluators()) {
175
176 opponentUtilitySpace.unlock(e.getKey());
177
178 e.getValue().setWeight(commonWeight);
179 try {
180 // set all value weights to one (they are normalized when
181 // calculating the utility)
182 for (ValueDiscrete vd : ((IssueDiscrete) e.getKey())
183 .getValues())
184 ((EvaluatorDiscrete) e.getValue()).setEvaluation(vd, 1);
185 } catch (Exception ex) {
186 ex.printStackTrace();
187 }
188 }
189 opponentUtilCount = (AdditiveUtilitySpace) opponentUtilitySpace.copy();
190 }
191
192 private HashMap<Integer, Integer> determineDifference(BidDetails first,
193 BidDetails second) {
194
195 HashMap<Integer, Integer> diff = new HashMap<Integer, Integer>();
196 try {
197 for (Issue i : opponentUtilitySpace.getDomain().getIssues()) {
198 Value value1 = first.getBid().getValue(i.getNumber());
199 Value value2 = second.getBid().getValue(i.getNumber());
200 diff.put(i.getNumber(), (value1.equals(value2)) ? 0 : 1);
201 }
202 } catch (Exception ex) {
203 ex.printStackTrace();
204 }
205
206 return diff;
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.