source: src/main/java/agents/anac/y2014/KGAgent/KGAgent.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: 9.4 KB
RevLine 
[127]1package agents.anac.y2014.KGAgent;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Random;
7
8import agents.anac.y2014.kGA_gent.library_genetic.GA_Main;
9import agents.anac.y2014.kGA_gent.library_genetic.Gene;
10import genius.core.Agent;
11import genius.core.Bid;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.ActionWithBid;
15import genius.core.actions.Offer;
16import genius.core.issue.Issue;
17import genius.core.issue.IssueDiscrete;
18import genius.core.issue.IssueInteger;
19import genius.core.issue.Value;
20import genius.core.issue.ValueInteger;
21import genius.core.utility.AdditiveUtilitySpace;
22import genius.core.utility.NonlinearUtilitySpace;
23
24public class KGAgent extends Agent {
25
26 @Override
27 public String getVersion() {
28 return "5.1.1";
29 }
30
31 private Action actionOfPartner = null;
32 private static double MINIMUM_UTILITY = 0.0;
33 private boolean initflag = true;
34
35 private double acceptableline = 1.0;
36 private double acceptderta = 0.001;
37
38 private double time = 0.0;
39
40 private double enemymax = 0.1;
41
42 private double gunma_min = 0.25;
43 private double gunma_d = 3;
44
45 List<MyBidGene> bidlist = new ArrayList<MyBidGene>(40);
46
47 History history;
48
49 // TreeSet<MyBidUtil> bidlist = new TreeSet<MyBidUtil>(new CompMyBidUtil());
50
51 int type = 0;
52
53 double timepressur;
54 double reservation;
55
56 Bid enemymaxbid = null;
57
58 @Override
59 public void init() {
60 MINIMUM_UTILITY = utilitySpace.getReservationValueUndiscounted();
61 System.out.println("Minimum bid utility: " + MINIMUM_UTILITY);
62
63 reservation = utilitySpace.getReservationValueUndiscounted();
64 timepressur = utilitySpace.getDiscountFactor();
65 System.out.println(
66 "reservate = " + reservation + " timepress = " + timepressur);
67
68 if (utilitySpace instanceof AdditiveUtilitySpace) {
69 type = 0;
70 System.out.println("UtilitySpaceType = LINEAR");
71 } else if (utilitySpace instanceof NonlinearUtilitySpace) {
72 type = 0;
73 System.out.println("UtilitySpaceType = NONLINEAR");
74 } else {
75 System.out.println("NULL ?? " + utilitySpace.getClass());
76 type = 1;
77 }
78 System.out.println("Type is" + type);
79
80 }
81
82 @Override
83 public String getName() {
84 return "kGA_gent";
85 }
86
87 @Override
88 public void ReceiveMessage(Action opponentAction) {
89 actionOfPartner = opponentAction;
90 }
91
92 int count = 99999;
93
94 @Override
95 public Action chooseAction() {
96 // TODO
97 // 自動ç�?Ÿæˆ�ã�•ã‚Œã�Ÿãƒ¡ã‚½ãƒƒãƒ‰ãƒ»ã‚¹ã‚¿ãƒ–
98 Action action = null;
99 time = timeline.getTime();
100
101 if (initflag) {
102 MyInit();
103 initflag = false;
104 }
105
106 if (actionOfPartner instanceof Offer) {
107
108 Bid enemy = ((Offer) actionOfPartner).getBid();
109
110 if (enemymaxbid == null) {
111 enemymaxbid = enemy;
112 } else {
113
114 if (getUtility(enemymaxbid) < getUtility(enemy)) {
115 enemymaxbid = enemy;
116 }
117
118 }
119
120 history.Input(((Offer) actionOfPartner).getBid());
121
122 // System.out.println("EnemyBid is "
123 // +getUtility(((Offer)actionOfPartner).getBid()) +
124 // " AcceptableLine is " + acceptableline );
125
126 if (Accept(((Offer) actionOfPartner).getBid())) {
127 System.out.println("Accept!!!");
128 action = new Accept(getAgentID(), enemy);
129 return action;
130 }
131
132 }
133
134 if (count > 10) {
135
136 // bidlist = SearchBid();
137
138 // System.out.println("GAStart1");
139 bidlist = SearchBid2();
140
141 count = 0;
142
143 }
144
145 try {
146
147 // ai.PrintBestFieldData();
148
149 // Bid bid = new
150 // Bid(utilitySpace.getDomain(),ai.GetRandomMap(count/100));
151
152 Bid bid = bidlist.get(count).bid;
153
154 if (enemymaxbid != null) {
155 if (getUtility(bid) < getUtility(enemymaxbid)) {
156 bid = new Bid(enemymaxbid);
157 }
158 }
159
160 // System.out.println("Bid Utility is" + getUtility(bid));
161 acceptableline = Math.min(getUtility(bid), acceptableline);
162 action = new Offer(getAgentID(), bid);
163 } catch (Exception e) {
164 System.out.println("Errer in CooseAction ");
165 System.out.println("Exception in ChooseAction:" + e.getMessage());
166 // best guess if things go wrong.
167 action = new Accept(getAgentID(),
168 ((ActionWithBid) actionOfPartner).getBid());
169 }
170
171 if (action == null) {
172 System.out.println("BidListErrer");
173 action = new Accept(getAgentID(),
174 ((ActionWithBid) actionOfPartner).getBid());
175 }
176 count++;
177 return action;
178 }
179
180 MyBidGene st;
181
182 void MyInit() {
183 // MyHyperRectangle input = new
184 // MyHyperRectangle(utilitySpace.getDomain().getIssues());
185
186 // NonlinearUtilitySpace nonlinear = new
187 // NonlinearUtilitySpace(utilitySpace);
188 /*
189 * ai = new
190 * MyRectangleField(utilitySpace.getDomain().getIssues(),nonlinear
191 * .getAllInclusiveConstraints()); ai.Search();
192 */
193
194 history = new History(utilitySpace.getDomain().getIssues(), this, type);
195
196 // bidlist = SearchBid();
197
198 st = new MyBidGene(this);
199
200 return;
201 }
202
203 /*
204 * é�ºä¼�å­�ã�«utilityをセットã�
205 * ™ã‚‹
206 */
207 public double GetGeneUtility(MyBidGene gene) {
208 return getUtility(gene.bid);
209 }
210
211 public double GetGeneEnemyUtility(MyBidGene gene) {
212 return history.GetEnemyUtility(gene.bid);
213 }
214
215 public double EnemyPressAll(double util) {
216 return EnemyTimePress(EnemyMaxPress(util));
217 }
218
219 ArrayList<MyBidGene> SearchBid2() {
220 ArrayList<MyBidGene> ret = new ArrayList<MyBidGene>(40);
221
222 st = new MyBidGene(this);
223
224 enemymax = Math.max(history.SearchMaxPoint(), enemymax);
225
226 // System.out.println("EnemyMax is " + enemymax);
227
228 GA_Main ga;
229
230 if (bidlist.size() > 0) {
231
232 List<Gene> in = new ArrayList<Gene>(300);
233 for (int i = 0; i < 60; i++) {
234 in.add(new MyBidGene(bidlist.get(i).bid));
235 }
236 for (int i = 0; i < 300; i++) {
237 in.add(new MyBidGene(GetRandomBid()));
238 }
239 ga = new GA_Main(in, new BidGenerationChange(300, 10),
240 new CompMyBidGene(0));
241
242 } else {
243 ga = new GA_Main(new BidGenerationChange(300),
244 new CompMyBidGene(0));
245
246 }
247
248 // System.out.println("GaStart2");
249
250 ga.Start();
251
252 List<Gene> buf;
253 buf = ga.GetList();
254 for (int i = 0; i < 60; i++) {
255
256 ret.add((MyBidGene) buf.get(i));
257 // System.out.println("Searchdata MyBid " +
258 // ((MyBidGene)buf.get(i)).util + " enemybid " +
259 // ((MyBidGene)buf.get(i)).enemyutil);
260 // System.out.println("in press MyBid " +
261 // getUtility(((MyBidGene)buf.get(i)).bid) + " enemybid " +
262 // EnemyPressAll(((MyBidGene)buf.get(i)).enemyutil) + " AddUtil " +
263 // ((MyBidGene)buf.get(i)).GetValue(0));
264
265 }
266
267 return ret;
268
269 }
270
271 /*
272 * 完全ã�«ãƒ©ãƒ³ãƒ€ãƒ ã�ªBidã‚’ç�?Ÿæ
273 * ˆ�ã�™ã‚‹
274 */
275 Random randomnr = new Random();
276
277 public Bid GetRandomBid() {
278
279 List<Issue> issues = utilitySpace.getDomain().getIssues();
280 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
281
282 IssueInteger lIssueInteger;
283 Bid bid = null;
284 int optionIndex = 0;
285
286 if (type == 0) {
287
288 for (int i = 0; i < issues.size(); i++) {
289 lIssueInteger = (IssueInteger) issues.get(i);
290 optionIndex = lIssueInteger.getLowerBound()
291 + randomnr.nextInt(lIssueInteger.getUpperBound()
292 - lIssueInteger.getLowerBound());
293 values.put(lIssueInteger.getNumber(),
294 new ValueInteger(optionIndex));
295 }
296 } else {
297 for (int i = 0; i < issues.size(); i++) {
298 IssueDiscrete ID = (IssueDiscrete) issues.get(i);
299 int number = randomnr.nextInt(ID.getNumberOfValues());
300 values.put(ID.getNumber(), ID.getValue(number));
301 }
302 }
303
304 try {
305 bid = new Bid(utilitySpace.getDomain(), values);
306 } catch (Exception e) {
307 System.out.println("Enner in GetRandomBid in kGA_gent");
308 System.out.println("Exception in ChooseAction:" + e.getMessage());
309
310 }
311
312 return bid;
313 }
314
315 double TimePress(double utility) {
316
317 return Math.pow(utility, 2.0 - time);
318
319 }
320
321 double EnemyTimePress(double utility) {
322
323 return utility * 1.3 * Math.pow(time,
324 (Math.pow(timepressur * gunma_d, 2.0)) + gunma_min);
325
326 // return utility * (time*1.2 - 0.2);
327
328 }
329
330 double EnemyMaxPress(double util) {
331
332 if (enemymax < 0.001) {
333 return 1;
334 }
335
336 double ret = util / enemymax;
337
338 if (ret > 1) {
339 enemymax = util;
340 return 1.0;
341 }
342 return ret;
343
344 }
345
346 Boolean Accept(Bid partnerbid) {
347
348 double eutil = getUtility(partnerbid);
349 if (acceptableline - acceptderta < eutil) {
350 System.out.println("Accept!! for acceptableline");
351 return true;
352 }
353 /*
354 * if( eutil > (1.0-time)*0.3 + 0.7 ){
355 * System.out.println("Accept!! for pointhugher"); return true; }
356 */
357 return false;
358 }
359
360 void BidPrint(Bid bid) {
361 List<Issue> issues = bid.getIssues();
362 IssueInteger lIssueInteger;
363
364 for (int i = 0; i < issues.size(); i++) {
365
366 lIssueInteger = (IssueInteger) issues.get(i);
367 try {
368 System.out.print(bid.getValue(lIssueInteger.getNumber()) + " ");
369 } catch (Exception e) {
370 // TODO 自動ç�?Ÿæˆ�ã�•ã‚Œã�Ÿ
371 // catch ブロック
372 e.printStackTrace();
373 }
374 }
375 System.out.println();
376
377 }
378
379 @Override
380 public double getUtility(Bid bid) {
381
382 try {
383 return utilitySpace.getUtility(bid);
384 } catch (Exception e) {
385 // TODO 自動ç�?Ÿæˆ�ã�•ã‚Œã�Ÿ catch
386 // ブロック
387 e.printStackTrace();
388
389 }
390 return 0;
391
392 }
393
394 @Override
395 public String getDescription() {
396 return "ANAC2014 compatible with non-linear utility spaces";
397 }
398
399}
Note: See TracBrowser for help on using the repository browser.