source: src/main/java/agents/anac/y2013/MetaAgent/MetaAgent2013.java@ 346

Last change on this file since 346 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: 14.8 KB
Line 
1package agents.anac.y2013.MetaAgent;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Map.Entry;
9import java.util.Set;
10
11import genius.core.Agent;
12import genius.core.Bid;
13import genius.core.Domain;
14import genius.core.NegotiationResult;
15import genius.core.SupportedNegotiationSetting;
16import genius.core.actions.Action;
17import genius.core.actions.EndNegotiation;
18import genius.core.actions.Offer;
19import genius.core.issue.ISSUETYPE;
20import genius.core.issue.Issue;
21import genius.core.issue.IssueDiscrete;
22import genius.core.issue.IssueInteger;
23import genius.core.issue.IssueReal;
24import genius.core.issue.Objective;
25import genius.core.issue.Value;
26import genius.core.issue.ValueDiscrete;
27import genius.core.issue.ValueInteger;
28import genius.core.issue.ValueReal;
29import genius.core.utility.AdditiveUtilitySpace;
30import genius.core.utility.EVALUATORTYPE;
31import genius.core.utility.Evaluator;
32import genius.core.utility.EvaluatorDiscrete;
33import genius.core.utility.EvaluatorInteger;
34import genius.core.utility.EvaluatorReal;
35
36public class MetaAgent2013 extends Agent {
37 private Agent myAgent;
38 private boolean agentInit;
39 private Action actionOfPartner;
40 HashMap<String, Double> features;
41 double opponentFirstBid, slopeBBUA, absSlopeBBUA, avgUtilBBUA;
42 boolean isUpdated;
43 AgentManager manager = new AgentManager();
44
45 @Override
46 @SuppressWarnings("deprecation")
47 public void init() {
48 agentInit = false;
49 opponentFirstBid = 0;
50 slopeBBUA = 0;
51 absSlopeBBUA = 0;
52 avgUtilBBUA = 0;
53 Serializable s = loadSessionData();
54 if (s != null) {
55 manager = (AgentManager) s;
56 myAgent = manager.SelectBestAgent();
57 myAgent.internalInit(this.sessionNr, this.sessionsTotal,
58 this.startTime, this.totalTime, this.timeline,
59 this.utilitySpace, this.parametervalues, this.getAgentID());
60 myAgent.setName(this.getName());
61 myAgent.init();
62 agentInit = true;
63 }
64 isUpdated = false;
65 }
66
67 @Override
68 public void endSession(NegotiationResult res) {
69 if (!isUpdated) {
70 manager.UpdateUtility("", res.getMyDiscountedUtility());
71 isUpdated = true;
72 }
73 saveSessionData(manager);
74 }
75
76 private void getDomainParams() {
77 Domain d = this.utilitySpace.getDomain();
78 List<Issue> a = d.getIssues();
79
80 // params = new double[14];
81 // //(Intercept),firstBBUA,slopeBBUA,absSlopeBBUA,avgUtilBBUA,DiscountFactor,DomainSize,numOfIssues,MinSize,MaxSize,AvgUtil,AvgUtilStdev,WeightStdev,Begins
82 features = new HashMap<String, Double>();
83
84 int min = Integer.MAX_VALUE, max = -1, size = 1, sumSize = 0;// ,
85 // relCount
86 // = 0;
87
88 // params 5+6+7 - Expected Utility of role, Standard deviation of
89 // Utility of role, Standard deviation of weights of role
90 double EU = 0, stdevU = 0, stdevW = 0, sW = 0, ssW = 0, countW = 0,
91 relevantEU = 0;
92 Iterator<Entry<Objective, Evaluator>> issue = ((AdditiveUtilitySpace) utilitySpace)
93 .getEvaluators().iterator();
94 List<Double> ssWList = new ArrayList<Double>();
95 while (issue.hasNext()) { // every issue
96 Entry<Objective, Evaluator> entry = issue.next();
97 Evaluator e = entry.getValue();
98 double weight = e.getWeight();
99 countW++;
100 sW += weight;
101 ssWList.add(weight);
102 double tempEU = 0, tempStdevU = 0;
103 if (e.getType() == EVALUATORTYPE.DISCRETE) {
104 Iterator<ValueDiscrete> v = ((EvaluatorDiscrete) e).getValues()
105 .iterator();
106 List<Double> s = new ArrayList<Double>();
107 double sumU = 0;
108 while (v.hasNext()) {
109 ValueDiscrete vd = v.next();
110 try {
111 double val = ((EvaluatorDiscrete) e).getEvaluation(vd);
112 s.add(val);
113 sumU += val;
114 if (opponentFirstBid > 0 && opponentFirstBid <= val) {
115 // relCount ++;
116 relevantEU += val;
117 }
118 } catch (Exception e1) {
119 // System.out.println("META-Agent IO exception: " +
120 // e1.toString());
121 }
122 }
123 int currSize = s.size();
124 min = (min > currSize) ? currSize : min;
125 max = (max < currSize) ? currSize : max;
126 sumSize += currSize;
127 size = size * currSize;
128 tempEU = sumU / currSize;
129 Iterator<Double> valIt = s.iterator();
130 while (valIt.hasNext()) {
131 tempStdevU += Math.pow(valIt.next() - tempEU, 2);
132 }
133 tempStdevU = Math.sqrt(tempStdevU / ((double) currSize - 1));
134 } else if (e.getType() == EVALUATORTYPE.INTEGER) {
135 tempEU = ((double) (((EvaluatorInteger) e).getUpperBound()
136 + ((EvaluatorInteger) e).getLowerBound())) / 2;
137 tempStdevU = Math.sqrt((Math
138 .pow(((EvaluatorInteger) e).getUpperBound() - tempEU, 2)
139 + Math.pow(
140 ((EvaluatorInteger) e).getLowerBound() - tempEU,
141 2))
142 / 2);
143 } else if (e.getType() == EVALUATORTYPE.REAL) {
144 tempEU = (((EvaluatorReal) e).getUpperBound()
145 + ((EvaluatorReal) e).getLowerBound()) / 2;
146 tempStdevU = Math.sqrt((Math
147 .pow(((EvaluatorReal) e).getUpperBound() - tempEU, 2)
148 + Math.pow(((EvaluatorReal) e).getLowerBound() - tempEU,
149 2))
150 / 2);
151 } else {
152 tempEU = 0.5;
153 tempStdevU = 0;
154 }
155
156 EU += tempEU * weight;
157 stdevU += tempStdevU * weight;
158 }
159 Iterator<Double> wIt = ssWList.iterator();
160 double avgW = sW / countW;
161 double avgSize = ((double) sumSize)
162 / ((double) ((AdditiveUtilitySpace) utilitySpace)
163 .getEvaluators().size());
164
165 while (wIt.hasNext()) {
166 ssW += Math.pow(wIt.next() - avgW, 2);
167 }
168 stdevW = countW <= 1 ? 0 : Math.sqrt(ssW / (countW - 1));
169
170 double relsumUtility = 0, relcountUtility = 0, relstdevUtility = 0,
171 stdevUtil = 0, countbids = 0, sumbids = 0, relevantStdevU = 0;
172 ArrayList<Double> bidsUtil = new ArrayList<Double>();
173 ArrayList<Bid> bids = GetDiscreteBids();
174 for (Bid bid : bids) {
175 try {
176 bidsUtil.add(utilitySpace.getUtility(bid));
177 } catch (Exception e) {
178 e.printStackTrace();
179 }
180 }
181 for (double bidUtil : bidsUtil) {
182 stdevUtil += Math.pow(bidUtil, 2);
183 sumbids += bidUtil;
184 countbids++;
185 if (bidUtil >= opponentFirstBid) {
186 relsumUtility += bidUtil;
187 relcountUtility++;
188 relstdevUtility += Math.pow(bidUtil, 2);
189 }
190 }
191 if (relcountUtility > 0) {
192 relevantEU = relsumUtility / relcountUtility;
193 relevantStdevU = Math.sqrt((relstdevUtility / relcountUtility)
194 - Math.pow(relevantEU, 2));
195 }
196 if (countbids > 0) {
197 EU = sumbids / countbids;
198 stdevU = Math.sqrt((stdevUtil / countbids) - Math.pow(EU, 2));
199 }
200
201 features.put("(Intercept)", 1.0);
202 features.put("UtilityOfFirstOpponentBid", opponentFirstBid); // firstBBUA
203 features.put("ReservationValue",
204 this.utilitySpace.getReservationValue()); // RV
205 features.put("DiscountFactor",
206 this.utilitySpace.getDiscountFactor() == 0 ? 1
207 : this.utilitySpace.getDiscountFactor()); // DiscountFactor
208 features.put("numOfIssues", a.size() + 0.0);
209 features.put("DomainSize", size + 0.0);
210 features.put("AvgUtil", EU);
211 features.put("AvgUtilStdev", stdevU);
212 features.put("AvgSize", avgSize);
213 features.put("WeightStdev", stdevW);
214 features.put("RelevantEU", relevantEU);
215 features.put("RelevantStdevU", relevantStdevU);
216
217 // params[13] = first ? 1 : 0;//Begins
218
219 }
220
221 @Override
222 @SuppressWarnings("deprecation")
223 public Action chooseAction() {
224 try {
225 if (!agentInit) {
226 if (actionOfPartner == null) // first bid, and Meta-Agent is the
227 // first bidder (still not
228 // initialized any agent)
229 {
230 // System.out.println("actionOfPartner == null.");
231 return new Offer(this.getAgentID(),
232 utilitySpace.getMaxUtilityBid());
233 } else // second bid, or first bid as responder (and not
234 // initiator)
235 {
236 // System.out.println("actionOfPartner not null.");
237 agentInit = true;
238 if (actionOfPartner instanceof Offer) {
239 // System.out.println("actionOfPartner is Offer.");
240 opponentFirstBid = utilitySpace
241 .getUtility(((Offer) actionOfPartner).getBid());
242 }
243
244 // initialize agent.
245 getDomainParams();
246 UpdateAllAgents();
247 manager.SetAvgUtil(
248 features.get("RelevantEU") * Math
249 .pow(utilitySpace.getDiscountFactor(), 0.5),
250 features.get("RelevantStdevU"));
251 myAgent = manager.SelectBestAgent();
252 myAgent.internalInit(this.sessionNr, this.sessionsTotal,
253 this.startTime, this.totalTime, this.timeline,
254 this.utilitySpace, this.parametervalues,
255 this.getAgentID());
256 myAgent.setName(this.getName());
257 myAgent.init();
258 // send the message that received to the new agent..
259 myAgent.ReceiveMessage(actionOfPartner);
260 }
261 }
262 Action a = myAgent.chooseAction();
263 if (a == null) {
264 return new Offer(this.getAgentID(),
265 utilitySpace.getMaxUtilityBid());
266 } else { // handle reservation value, because all agent are from
267 // Genius version 3.1 (and don't know what is
268 // reservation value)
269 // System.out.println("Action a is not null.");
270 double time = timeline.getTime();
271 if (a instanceof Offer
272 && this.utilitySpace.getReservationValueWithDiscount(
273 time) >= utilitySpace.getUtilityWithDiscount(
274 ((Offer) a).getBid(), time)) {
275 a = new EndNegotiation(getAgentID());
276 }
277
278 }
279 return a;
280
281 } catch (Exception e) {
282 // System.out.println("META-AGENT error in ChooseAction: " +
283 // e.toString());
284 return null;
285 }
286 }
287
288 private void UpdateAllAgents() {
289 Set<String> agents = manager.GetAgents();
290 for (String agentName : agents) {
291 double predicted = Parser.getMean(manager.GetAgentData(agentName),
292 features);
293 manager.UpdateUtility(agentName, predicted);
294 }
295
296 }
297
298 @Override
299 public void ReceiveMessage(Action opponentAction) {
300 actionOfPartner = opponentAction;
301 if (agentInit)
302 myAgent.ReceiveMessage(opponentAction);
303 }
304
305 @Override
306 public String getName() {
307 return "Meta-Agent 2013";
308 }
309
310 @Override
311 public String getVersion() {
312 return "2.0";
313 }
314
315 private ArrayList<Bid> GetDiscreteBids() {
316 ArrayList<Bid> bids = new ArrayList<Bid>();
317 HashMap<Integer, Value> issusesFirstValue = new HashMap<Integer, Value>();
318
319 // initial bids list - contains only one bid.
320 for (Issue issue : utilitySpace.getDomain().getIssues()) {
321 Value v = null;
322 if (issue.getType() == ISSUETYPE.INTEGER)
323 v = new ValueInteger(((IssueInteger) issue).getLowerBound());
324 else if (issue.getType() == ISSUETYPE.REAL)
325 v = new ValueReal(((IssueReal) issue).getLowerBound());
326 else if (issue.getType() == ISSUETYPE.DISCRETE)
327 v = ((IssueDiscrete) issue).getValue(0);
328 issusesFirstValue.put(issue.getNumber(), v);
329 }
330 try {
331 bids.add(new Bid(utilitySpace.getDomain(), issusesFirstValue));
332 } catch (Exception e) {
333 return null;
334 }
335
336 for (Issue issue : utilitySpace.getDomain().getIssues()) { // for every
337 // issue
338 ArrayList<Bid> tempBids = new ArrayList<Bid>(); // createFrom a list
339 // of
340 // bids
341 ArrayList<Value> issueValues = new ArrayList<Value>();
342 if (issue.getType() == ISSUETYPE.DISCRETE) {
343 ArrayList<ValueDiscrete> valuesD = (ArrayList<ValueDiscrete>) ((IssueDiscrete) issue)
344 .getValues(); // get
345 // list
346 // of
347 // options/values
348 // for
349 // this
350 // issue
351 for (Value v : valuesD) {
352 issueValues.add(v);
353 }
354 } else if (issue.getType() == ISSUETYPE.INTEGER) {
355 int k = Math.min(10, ((IssueInteger) issue).getUpperBound()
356 - ((IssueInteger) issue).getLowerBound());
357 for (int i = 0; i <= k; i++) {
358 ValueInteger vi = (ValueInteger) GetRepresentorOfBucket(i,
359 issue, k, true);
360 issueValues.add(vi);
361 }
362 } else if (issue.getType() == ISSUETYPE.REAL) {
363 int k = 10;
364 for (int i = 0; i <= k; i++) {
365 ValueReal vr = (ValueReal) GetRepresentorOfBucket(i, issue,
366 k, false);
367 issueValues.add(vr);
368 }
369 }
370
371 for (Bid bid : bids) { // for each bid seen so far (init bids list)
372 for (Value value : issueValues) { // for every value
373 HashMap<Integer, Value> bidValues = new HashMap<Integer, Value>(); // make
374 // new
375 // ("empty")
376 // bid
377 // -
378 // only
379 // values.
380 for (Issue issue1 : utilitySpace.getDomain().getIssues())
381 // go over all issues
382 try {
383 bidValues.put(issue1.getNumber(),
384 bid.getValue(issue1.getNumber())); // each
385 // issue
386 // is
387 // entered
388 } catch (Exception e) {
389 e.printStackTrace();
390 }
391 bidValues.put(issue.getNumber(), value);
392 try {
393 Bid newBid = new Bid(utilitySpace.getDomain(),
394 bidValues);
395 tempBids.add(newBid);
396 } catch (Exception e) {
397 e.printStackTrace();
398 }
399 }
400 }
401 bids = tempBids;
402 }
403 return bids;
404 }
405
406 private Value GetRepresentorOfBucket(int bucket, Issue issue, int k,
407 boolean isInteger) {
408 double ans = 0;
409
410 if (isInteger) {
411 EvaluatorInteger ei = new EvaluatorInteger();
412 boolean upperIsTheBest = ei.getEvaluation(
413 ((IssueInteger) issue).getUpperBound()) > ei.getEvaluation(
414 ((IssueInteger) issue).getLowerBound());
415 if (upperIsTheBest) {
416 if (bucket < k) {
417 ans = ((double) (bucket + 1)) / k;
418 ans = ans
419 * (((IssueInteger) issue).getUpperBound()
420 - ((IssueInteger) issue).getLowerBound())
421 + ((IssueInteger) issue).getLowerBound() - 1;
422 } else
423 ans = ((IssueInteger) issue).getUpperBound();
424 } else {
425 ans = ((double) (bucket)) / k;
426 ans = ans
427 * (((IssueInteger) issue).getUpperBound()
428 - ((IssueInteger) issue).getLowerBound())
429 + ((IssueInteger) issue).getLowerBound();
430 }
431 return new ValueInteger((int) Math.round(ans));
432 }
433
434 EvaluatorReal ei = new EvaluatorReal();
435 boolean upperIsTheBest = ei
436 .getEvaluation(((IssueReal) issue).getUpperBound()) > ei
437 .getEvaluation(((IssueReal) issue).getLowerBound());
438 if (upperIsTheBest) {
439 if (bucket < k) {
440 ans = ((double) (bucket + 1)) / k;
441 ans = ans
442 * (((IssueReal) issue).getUpperBound()
443 - ((IssueReal) issue).getLowerBound())
444 + ((IssueReal) issue).getLowerBound();
445 } else
446 ans = ((IssueReal) issue).getUpperBound();
447 } else {
448 ans = ((double) (bucket)) / k;
449 ans = ans
450 * (((IssueReal) issue).getUpperBound()
451 - ((IssueReal) issue).getLowerBound())
452 + ((IssueReal) issue).getLowerBound();
453 }
454 return new ValueReal(ans);
455
456 }
457
458 @Override
459 public SupportedNegotiationSetting getSupportedNegotiationSetting() {
460 return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
461 }
462
463 @Override
464 public String getDescription() {
465 return "ANAC2013";
466 }
467}
Note: See TracBrowser for help on using the repository browser.