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