source: src/main/java/agents/anac/y2011/AgentK2/Agent_K2.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 7.3 KB
Line 
1package agents.anac.y2011.AgentK2;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Random;
7
8import genius.core.Agent;
9import genius.core.Bid;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.ActionWithBid;
13import genius.core.actions.Offer;
14import genius.core.issue.Issue;
15import genius.core.issue.IssueDiscrete;
16import genius.core.issue.IssueInteger;
17import genius.core.issue.IssueReal;
18import genius.core.issue.Value;
19import genius.core.issue.ValueInteger;
20import genius.core.issue.ValueReal;
21
22public class Agent_K2 extends Agent {
23
24 private Action partner = null;
25 private HashMap<Bid, Double> offeredBidMap;
26 private double target;
27 private double bidTarget;
28 private double sum;
29 private double sum2;
30 private int rounds;
31 private double tremor;
32 private final boolean TEST_EQUIVALENCE = false;
33 private Random random100;
34 private Random random200;
35 private Random random300;
36 private Random random400;
37
38 @Override
39 public void init() {
40 offeredBidMap = new HashMap<Bid, Double>();
41 target = 1.0;
42 bidTarget = 1.0;
43 sum = 0.0;
44 sum2 = 0.0;
45 rounds = 0;
46 tremor = 2.0;
47
48 if (TEST_EQUIVALENCE) {
49 random100 = new Random(100);
50 random200 = new Random(200);
51 random300 = new Random(300);
52 random400 = new Random(400);
53 } else {
54 random100 = new Random();
55 random200 = new Random();
56 random300 = new Random();
57 random400 = new Random();
58 }
59 }
60
61 @Override
62 public String getName() {
63 return "Agent_K2";
64 }
65
66 @Override
67 public String getVersion() {
68 return "0.31415_discountFix";
69 }
70
71 @Override
72 public void ReceiveMessage(Action opponentAction) {
73 partner = opponentAction;
74 }
75
76 @Override
77 public Action chooseAction() {
78 Action action = null;
79 try {
80 if (partner == null) {
81 action = selectBid();
82 }
83 if (partner instanceof Offer) {
84 Bid offeredBid = ((Offer) partner).getBid();
85
86 double p = acceptProbability(offeredBid);
87
88 if (p > random100.nextDouble()) {
89 action = new Accept(getAgentID(), offeredBid);
90 } else {
91 action = selectBid();
92 }
93 }
94 } catch (Exception e) {
95 e.printStackTrace();
96 action = new Accept(getAgentID(),
97 ((ActionWithBid) partner).getBid());
98 }
99 return action;
100 }
101
102 private Action selectBid() {
103 Bid nextBid = null;
104
105 ArrayList<Bid> bidTemp = new ArrayList<Bid>();
106
107 for (Bid bid : offeredBidMap.keySet()) {
108 if (offeredBidMap.get(bid) > target) {
109 bidTemp.add(bid);
110 }
111 }
112
113 int size = bidTemp.size();
114 if (size > 0) {
115 int sindex = (int) Math.floor(random200.nextDouble() * size);
116 nextBid = bidTemp.get(sindex);
117 } else {
118 double searchUtil = 0.0;
119 try {
120 int loop = 0;
121 while (searchUtil < bidTarget) {
122 if (loop > 500) {
123 bidTarget -= 0.01;
124 loop = 0;
125 }
126 nextBid = searchBid();
127 searchUtil = utilitySpace.getUtility(nextBid);
128 loop++;
129 }
130 } catch (Exception e) {
131 e.printStackTrace();
132 }
133 }
134
135 if (nextBid == null) {
136 return (new Accept(getAgentID(),
137 ((ActionWithBid) partner).getBid()));
138 }
139 return (new Offer(getAgentID(), nextBid));
140 }
141
142 private Bid searchBid() throws Exception {
143 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
144 List<Issue> issues = utilitySpace.getDomain().getIssues();
145
146 Bid bid = null;
147
148 for (Issue lIssue : issues) {
149 switch (lIssue.getType()) {
150 case DISCRETE:
151 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
152 int optionIndex = random300
153 .nextInt(lIssueDiscrete.getNumberOfValues());
154 values.put(lIssue.getNumber(),
155 lIssueDiscrete.getValue(optionIndex));
156 break;
157 case REAL:
158 IssueReal lIssueReal = (IssueReal) lIssue;
159 int optionInd = random300.nextInt(
160 lIssueReal.getNumberOfDiscretizationSteps() - 1);
161 values.put(lIssueReal.getNumber(),
162 new ValueReal(lIssueReal.getLowerBound() + (lIssueReal
163 .getUpperBound() - lIssueReal.getLowerBound())
164 * (optionInd) / (lIssueReal
165 .getNumberOfDiscretizationSteps())));
166 break;
167 case INTEGER:
168 IssueInteger lIssueInteger = (IssueInteger) lIssue;
169 int optionIndex2 = lIssueInteger.getLowerBound()
170 + random300.nextInt(lIssueInteger.getUpperBound()
171 - lIssueInteger.getLowerBound());
172 values.put(lIssueInteger.getNumber(),
173 new ValueInteger(optionIndex2));
174 break;
175 default:
176 throw new Exception("issue type " + lIssue.getType()
177 + " not supported by SimpleAgent2");
178 }
179 }
180
181 bid = new Bid(utilitySpace.getDomain(), values);
182 return bid;
183 }
184
185 double acceptProbability(Bid offeredBid) throws Exception {
186
187 double offeredUtility = utilitySpace.getUtility(offeredBid);
188 offeredBidMap.put(offeredBid, offeredUtility);
189
190 sum += offeredUtility;
191 sum2 += offeredUtility * offeredUtility;
192 rounds++;
193
194 double mean = sum / rounds;
195
196 double variance = (sum2 / rounds) - (mean * mean);
197
198 double deviation = Math.sqrt(variance * 12);
199 if (Double.isNaN(deviation)) {
200 deviation = 0.0;
201 }
202
203 double time = timeline.getTime();
204
205 double t = time * time * time;
206
207 if (offeredUtility < 0 || offeredUtility > 1.05) {
208 throw new Exception("utility " + offeredUtility + " outside [0,1]");
209 }
210
211 if (t < 0 || t > 1) {
212 throw new Exception("time " + t + " outside [0,1]");
213 }
214
215 if (offeredUtility > 1.) {
216 offeredUtility = 1;
217 }
218
219 double estimateMax = mean + ((1 - mean) * deviation);
220
221 double alpha = 1 + tremor + (10 * mean) - (2 * tremor * mean);
222 double beta = alpha + (random400.nextDouble() * tremor) - (tremor / 2);
223
224 double preTarget = 1 - (Math.pow(time, alpha) * (1 - estimateMax));
225 double preTarget2 = 1 - (Math.pow(time, beta) * (1 - estimateMax));
226
227 double ratio = (deviation + 0.1) / (1 - preTarget);
228 if (Double.isNaN(ratio) || ratio > 2.0) {
229 ratio = 2.0;
230 }
231
232 double ratio2 = (deviation + 0.1) / (1 - preTarget2);
233 if (Double.isNaN(ratio2) || ratio2 > 2.0) {
234 ratio2 = 2.0;
235 }
236
237 target = ratio * preTarget + 1 - ratio;
238 bidTarget = ratio2 * preTarget2 + 1 - ratio2;
239
240 double m = t * (-300) + 400;
241 if (target > estimateMax) {
242 double r = target - estimateMax;
243 double f = 1 / (r * r);
244 if (f > m || Double.isNaN(f))
245 f = m;
246 double app = r * f / m;
247 target = target - app;
248 } else {
249 target = estimateMax;
250 }
251
252 if (bidTarget > estimateMax) {
253 double r = bidTarget - estimateMax;
254 double f = 1 / (r * r);
255 if (f > m || Double.isNaN(f))
256 f = m;
257 double app = r * f / m;
258 bidTarget = bidTarget - app;
259 } else {
260 bidTarget = estimateMax;
261 }
262
263 // test code for Discount Factor
264 double discount_utility = utilitySpace
265 .getUtilityWithDiscount(offeredBid, time);
266
267 double discount_ratio = discount_utility / offeredUtility;
268 if (!Double.isNaN(discount_utility) && !Double.isNaN(discount_ratio)) {
269 target *= discount_ratio;
270 bidTarget *= discount_ratio;
271 }
272
273 double utilityEvaluation = offeredUtility - estimateMax;
274 double satisfy = offeredUtility - target;
275
276 double p = (Math.pow(time, alpha) / 5) + utilityEvaluation + satisfy;
277 if (p < 0.1) {
278 p = 0.0;
279 }
280
281 return p;
282 }
283
284 @Override
285 public String getDescription() {
286 return "ANAC2011 compatible with non-linear utility spaces";
287 }
288}
Note: See TracBrowser for help on using the repository browser.