source: src/main/java/agents/anac/y2016/clockworkagent/ClockworkAgent.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: 5.7 KB
Line 
1package agents.anac.y2016.clockworkagent;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Random;
7
8import genius.core.AgentID;
9import genius.core.Bid;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.ActionWithBid;
13import genius.core.actions.DefaultAction;
14import genius.core.actions.Offer;
15import genius.core.parties.AbstractNegotiationParty;
16import genius.core.parties.NegotiationInfo;
17
18public class ClockworkAgent extends AbstractNegotiationParty {
19
20 private double discountFactor = 0;
21 private double reservationValue = 0;
22 private double rad = 0;
23 Random rand = new Random();
24 private Bid[] candidateArray = new Bid[10];
25 private ArrayList<Bid> history;
26 private int Opcnt = 3, Ofcnt = 0;
27
28 @Override
29 public void init(NegotiationInfo info) {
30
31 super.init(info);
32 discountFactor = getUtilitySpace().getDiscountFactor(); // read discount factor
33 System.out.println("Discount Factor is " + discountFactor);
34 reservationValue = getUtilitySpace()
35 .getReservationValueUndiscounted();
36 System.out.println("Reservation Value is " + reservationValue);
37 history = new ArrayList<Bid>();
38
39 }
40
41 @Override
42 public Action chooseAction(List<Class<? extends Action>> validActions) {
43
44 if (history.size() == 0) {
45 return new Offer(getPartyId(), bidFunction());
46 } else if (history.size() > 3 && checkAcceptance() == true) {
47
48 return new Accept(getPartyId(),
49 ((ActionWithBid) getLastReceivedAction()).getBid());
50 } else if (timeline.getTime() <= 0.95) {
51 return new Offer(getPartyId(), bidFunction());
52 } else if (timeline.getTime() <= 0.97 && timeline.getTime() > 0.95) {
53
54 Bid tempBid = null;
55 int c = 0;
56 while (c < 30) {
57 c++;
58 tempBid = candidateArray[rand.nextInt(9)];
59 if (tempBid != null)
60 break;
61 }
62
63 if (tempBid != null) {
64 return new Offer(getPartyId(), tempBid);
65 } else {
66 return new Offer(getPartyId(), bidFunction());
67 }
68 } else if (timeline.getTime() > 0.97) {
69 return new Offer(getPartyId(), getMaxU());
70 } else
71 return new Offer(getPartyId(), bidFunction());
72 }
73
74 @Override
75 public void receiveMessage(AgentID sender, Action action) {
76 super.receiveMessage(sender, action);
77
78 if (action instanceof Offer) {
79 Bid opponentBid = DefaultAction.getBidFromAction(action);
80 history.add(opponentBid);
81 } else if (action instanceof Accept && getUtilityWithDiscount(
82 history.get(history.size() - 1)) > uFunctionC()) {
83 candidateArray[Opcnt] = history.get(history.size() - 1);
84 Opcnt++;
85
86 if (Opcnt == 9)
87 Opcnt = 3;
88 }
89
90 }
91
92 public Boolean checkAcceptance() {
93
94 double tn, dt, ad = 0, t, botlim, uhist, avgT;
95 t = timeline.getTime();
96 tn = t + 0.01;
97
98 if (history.size() > 3) {
99 uhist = getUtilityWithDiscount(history.get(history.size() - 1));
100
101 if (t <= 0.95) {
102 if (checkConcession()) {
103 ad = 0.05;
104 }
105
106 dt = Math.pow(discountFactor, tn) * (0.95 - ad);
107
108 if (uhist > dt)
109 return true;
110 else
111 return false;
112 } else if (t > 0.95 && t <= 0.97) {
113 if (checkConcession()) {
114 ad = 0.1;
115 }
116
117 dt = Math.pow(discountFactor, tn);
118 botlim = uBotlim(tn);
119
120 avgT = ((dt + botlim) / 2) * (0.95 - ad);
121
122 if (uhist > avgT)
123 return true;
124 else
125 return false;
126 } else if (t > 0.97) {
127 if (checkConcession()) {
128 ad = 0.12;
129 }
130
131 dt = Math.pow(discountFactor, tn);
132 botlim = uBotlim(tn);
133
134 avgT = ((dt + botlim) / 2) * (0.95 - ad);
135
136 if (uhist > dt)
137 return true;
138 else
139 return false;
140 } else
141 return false;
142 } else
143 return false;
144 }
145
146 public double uBotlim(double t) {
147 double botlim, dt;
148
149 dt = Math.pow(discountFactor, t);
150 botlim = (1 - Math.pow(t, 2)) * (dt * (1 - reservationValue))
151 + dt * reservationValue;
152
153 return botlim;
154 }
155
156 public double uFunction() {
157 double botlim, avg, f, dt;
158
159 botlim = uBotlim(timeline.getTime());
160
161 dt = Math.pow(discountFactor, timeline.getTime());
162
163 avg = (dt + botlim) / 2;
164
165 if (checkConcession() == true && Ofcnt % 100 == 0) {
166 f = Math.abs(Math.cos(rad)) * (avg - botlim) + botlim;
167 Ofcnt = 0;
168 } else
169 f = Math.abs(Math.cos(rad)) * (dt - avg) + avg;
170
171 rad += Math.PI / 4;
172 Ofcnt++;
173 if (rad % Math.PI == 0)
174 rad = 0;
175
176 return f;
177 }
178
179 public double uFunctionC() {
180 double botlim, avg, f, dt;
181
182 botlim = uBotlim(timeline.getTime());
183
184 dt = Math.pow(discountFactor, timeline.getTime());
185
186 f = 0.85 * (dt - botlim) + botlim;
187
188 return f;
189 }
190
191 public Bid bidFunction() {
192 Bid newBid = null;
193 Bid bBid = null;
194 int count = 0;
195 double t = timeline.getTime();
196
197 if (t < 0.3) {
198 newBid = getMaxU();
199 } else {
200 newBid = generateRandomBid();
201 double f = uFunction();
202
203 while (count != 10000) {
204 bBid = generateRandomBid();
205
206 if (getUtilityWithDiscount(bBid) >= f
207 && getUtility(bBid) > getUtility(newBid))
208 newBid = bBid;
209
210 count++;
211 }
212
213 }
214 return newBid;
215
216 }
217
218 public boolean checkConcession() {
219 double h1, h2, h3;
220
221 if (history.size() > 3) {
222 h1 = getUtility(history.get(history.size() - 1));
223 h2 = getUtility(history.get(history.size() - 2));
224 h3 = getUtility(history.get(history.size() - 3));
225
226 if (h1 > h2 || h1 > h3)
227 return true;
228 else
229 return false;
230 } else
231 return false;
232 }
233
234 public Bid getMaxU() {
235 Bid newBid = generateRandomBid();
236 Bid tempBid = null;
237 int count = 0;
238
239 while (count != 13000) {
240 tempBid = generateRandomBid();
241
242 if (getUtility(tempBid) > getUtility(newBid)) {
243 newBid = tempBid;
244 }
245
246 count++;
247 }
248
249 return newBid;
250 }
251
252 @Override
253 public String getDescription() {
254 return "ANAC2016 Considers time.";
255 }
256}
Note: See TracBrowser for help on using the repository browser.