source: src/main/java/agents/anac/y2014/E2Agent/AnacSampleAgent.java@ 127

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

Initial import : Genius 9.0.0

File size: 11.8 KB
Line 
1package agents.anac.y2014.E2Agent;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7import java.util.Random;
8
9import agents.anac.y2014.E2Agent.myUtility.AgentKStorategy;
10import agents.anac.y2014.E2Agent.myUtility.BidStorage;
11import agents.anac.y2014.E2Agent.myUtility.BidStorageComparator;
12import agents.anac.y2014.E2Agent.myUtility.BidStorageList;
13import agents.anac.y2014.E2Agent.myUtility.IAgentKStorategyComponent;
14import agents.anac.y2014.E2Agent.myUtility.Parameters;
15import agents.anac.y2014.E2Agent.myUtility.SessionData;
16import agents.anac.y2014.E2Agent.myUtility.SimulatedAnealing;
17import agents.anac.y2014.E2Agent.myUtility.SummaryStatistics;
18import genius.core.Agent;
19import genius.core.Bid;
20import genius.core.NegotiationResult;
21import genius.core.actions.Accept;
22import genius.core.actions.Action;
23import genius.core.actions.ActionWithBid;
24import genius.core.actions.Offer;
25import genius.core.issue.Issue;
26import genius.core.issue.IssueInteger;
27import genius.core.issue.ValueInteger;
28import genius.core.utility.AbstractUtilitySpace;
29import genius.core.utility.Bound;
30
31public class AnacSampleAgent extends Agent {
32 private Action actionOfPartner = null;
33 private static int SAMPLE_NUMBER = 3; // �得�るサンプル数
34 private static int SA_K_MAX = 10000; // 焼����法�ループ数
35 private AbstractUtilitySpace nonlinear = null; // �線形効用空間
36 // private ArrayList<BidStorage> partnerBidHistory = null; //
37 // 相手�Bid履歴
38 private BidStorageList partnerBidHistory = null;
39 private ArrayList<BidStorage> candidateBids = null; // Bid候補
40 private List<Issue> issues = null; // 効用空間�全��論点
41 private Random randomnr = null;
42 private SimulatedAnealing simulatedAnealing = null;
43 private double discountFactor = 0;
44 private double reservationValue = 0;
45 private AgentKStorategy agentKStorategy = null;
46 private SessionData sessionData = null;
47 private double time = 0;
48 private double worstUtility = 1.0;
49 private Parameters param;
50 private BidStorage bestBid;
51
52 @Override
53 public void init() {
54 Serializable s = loadSessionData();
55 if (s != null && sessionNr != 0) {
56
57 sessionData = (SessionData) s;
58 } else {
59 // System.out.println("######################################################################################################################################################");
60 sessionData = new SessionData(sessionsTotal);
61 }
62
63 param = sessionData.getParamters(reservationValue, discountFactor);
64 // System.out.println(param);
65
66 randomnr = new Random();
67 nonlinear = (AbstractUtilitySpace) utilitySpace.copy();
68 issues = utilitySpace.getDomain().getIssues();
69 discountFactor = utilitySpace.getDiscountFactor();
70 reservationValue = utilitySpace.getReservationValue();
71 agentKStorategy = new AgentKStorategy(randomnr,
72 new AgentKStorategyComponentImpl());
73
74 partnerBidHistory = new BidStorageList();
75 bestBid = new BidStorage(null, 0, 0);
76
77 try {
78 simulatedAnealing = new SimulatedAnealing(utilitySpace);
79
80 candidateBids = searchOptimumBids(SAMPLE_NUMBER);
81 // 効用値�ソート
82 Collections.sort(candidateBids, new BidStorageComparator());
83 Collections.reverse(candidateBids); // �順�変更
84
85 // System.out.println(candidateBids);
86 } catch (Exception e) {
87 e.printStackTrace();
88 candidateBids = null;
89 }
90 }
91
92 /**
93 * 最��Bidを探索
94 */
95 private ArrayList<BidStorage> searchOptimumBids(int sampleNr)
96 throws Exception {
97 ArrayList<BidStorage> bids = new ArrayList<BidStorage>();
98 for (int index = 0; index < sampleNr; ++index) {
99 Bid startBid = utilitySpace.getDomain().getRandomBid(null);
100 // 焼����法�よる最�解探索
101 BidStorage optimumBid = simulatedAnealing.run(startBid, 1.0,
102 SA_K_MAX);
103 // Bid候補追加
104 bids.add(optimumBid);
105 }
106 return bids;
107 }
108
109 /**
110 * 自分�行動�択
111 */
112 @Override
113 public Action chooseAction() {
114 Action action = null;
115 try {
116 // 一番始��Bid
117 if (actionOfPartner == null) {
118 BidStorage myBidStorage = candidateBids.get(0);
119 action = generateOffer(myBidStorage.getBid());
120 }
121 if (actionOfPartner instanceof Offer) { // 相手�Offer����場�
122 time = timeline.getTime();
123 double tau = 1;
124
125 // 相手�Bidを記憶
126 Bid partnerBid = ((Offer) actionOfPartner).getBid();
127 BidStorage partnerBidStorage = new BidStorage(partnerBid,
128 utilitySpace.getUtility(partnerBid), time);
129 partnerBidHistory.addBidStorage(partnerBidStorage);
130 SummaryStatistics stat = partnerBidHistory
131 .getSummaryStatistics();
132 // BidStorage bestBid = partnerBidHistory.getBestBidStorage();
133 // System.out.println(stat);
134 BidStorage myBidStorage = null;
135 double targetUtility = 1.0;
136
137 // 自分����最も良�相手�Bid
138 if (bestBid.getUtility() < partnerBidStorage.getUtility()) {
139 bestBid = partnerBidStorage;
140 }
141
142 if (discountFactor > 0.9) {
143 targetUtility = agentKStorategy.fintarget(time,
144 stat.getAve(), stat.getVar(), tau);
145 } else {
146 targetUtility = fintarget2(time, stat, param);
147 }
148
149 myBidStorage = search2(partnerBid, targetUtility);
150 if (myBidStorage == null) {
151 // 指定��効用値を���Bidを検索
152 myBidStorage = searchBidWithApproximateUtlity(partnerBid,
153 targetUtility);
154 if (myBidStorage == null) {
155 myBidStorage = candidateBids.get(0);
156 }
157 }
158
159 double myUtility = myBidStorage.getUtility();
160
161 if (// worstUtility <= partnerBidStorage.getUtility() ||
162 isAcceptable(time, partnerBidStorage.getUtility(),
163 stat.getAve(), stat.getVar(), tau)) {
164 // System.out.println("Accept");
165 action = new Accept(getAgentID(), partnerBid);
166 } else {
167 if (targetUtility < bestBid.getUtility()) {
168 // 目標効用値よりも高�値を相手�既�Offer����ら�れをOffer
169 action = generateOffer(bestBid.getBid());
170 // System.out.printf("bestUtility: %f.5 ",
171 // bestBid.getUtility());
172 } else {
173 action = generateOffer(myBidStorage.getBid());
174 }
175
176 // 自身�最も悪�Bidを記憶
177 if ((targetUtility - myUtility) < 0.05
178 && myUtility < worstUtility) {
179 worstUtility = myUtility;
180 }
181 // 目標���り�も離れ�bid�最大Bid�置��る
182 if (Math.abs(targetUtility - myUtility) > 0.1) {
183 action = generateOffer(candidateBids.get(0).getBid());
184 }
185 /*
186 * if(sessionNr>0 && time > 0.999 && (reservationValue *
187 * discountFactor) * 1.5 < partnerBidStorage.getUtility()) {
188 * System.out.println("Limit"); action = new
189 * Accept(getAgentID()); }
190 */
191 }
192 }
193 } catch (Exception e) { // 例外発生時�相手���案を��る
194 e.printStackTrace();
195 // System.out.println("Exception in ChooseAction:"+e.getMessage());
196 // best guess if things go wrong.
197 action = new Accept(getAgentID(),
198 ((ActionWithBid) actionOfPartner).getBid());
199 }
200 return action;
201 }
202
203 private BidStorage search2(Bid bid, double targetUtil) throws Exception {
204 double min = 1;
205 BidStorage ret = null;
206
207 for (int i = 0; i < issues.size(); ++i) {
208 IssueInteger lIssueInteger = (IssueInteger) issues.get(i);
209 int issueIndexMin = lIssueInteger.getLowerBound();
210 int issueIndexMax = lIssueInteger.getUpperBound();
211
212 for (int j = issueIndexMin; j <= issueIndexMax; ++j) {
213 Bid neighbourBid = new Bid(bid);
214 neighbourBid = neighbourBid.putValue(i + 1,
215 new ValueInteger(j));
216 double u = utilitySpace.getUtility(neighbourBid);
217 if (/* Math.abs(u-targetUtil) < min && */u > targetUtil) {
218 min = u;
219 ret = new BidStorage(neighbourBid, u, -1);
220 System.out.println(ret);
221 }
222 }
223 }
224
225 return ret;
226 }
227
228 /**
229 * 相手���案を��る�
230 */
231 private boolean isAcceptable(double time, double u, double myu, double var,
232 double tau) throws Exception {
233 // 相手�効用値�時間�0~1�値を得る
234 double p = agentKStorategy.pAccept(time, u, myu, var, tau);
235 if (p < 0.1) {
236 return false;
237 }
238
239 // System.out.printf(" P: %f.5\n", p);
240 return p > Math.random();
241 }
242
243 private double fintarget2(double time, SummaryStatistics stat,
244 Parameters param) {
245 double ret = 1;
246 double target = agentKStorategy.fintarget(time, stat.getAve(),
247 stat.getVar(), 1);
248 if (Double.isNaN(target)) {
249 target = 1.0;
250 }
251 double co = 0;
252 if (target > param.utility) {
253 co = 0.8;
254 if (param.time - time > 0) {
255 co *= (1 - Math.pow((param.time - time) / param.time,
256 param.alpha));
257 }
258 }
259 ret = co * param.utility + (1 - co) * target;
260 // System.out.printf("%.4f ",ret);
261 return ret;
262 }
263
264 private double fintarget(double time, SummaryStatistics stat,
265 Parameters param) {
266 double ret = 1;
267 double param_t = param.time * 0.85;
268 double e = agentKStorategy.emax(stat.getAve(), stat.getVar());
269
270 if (time < param_t) {
271 ret = 1 - ((1 - param.utility)
272 * Math.pow(time / param_t, 1 / param.alpha));
273 } else {
274 /*
275 * double t = (time - param_t) / (1 - param_t); double ave =
276 * stat.getAve() / param.utility; double var = stat.getVar() /
277 * Math.pow(param.utility,2); double target =
278 * agentKStorategy.fintarget(t, ave, var, 1); ret = target /
279 * param.utility;
280 */
281
282 if (e > param.utility || Double.isNaN(e)) {
283 e = param.utility;
284 }
285 ret = param.utility - (param.utility - e)
286 * Math.pow((time - param_t) / (1 - param_t), param.beta);
287 if (Double.isNaN(ret)) {
288 ret = 1.0;
289 }
290 }
291
292 // System.out.printf("%.4f ",ret);
293 return ret;
294 }
295
296 private BidStorage searchBidWithApproximateUtlity(Bid startBid, double u)
297 throws Exception {
298 // Bid startBid = utilitySpace.getDomain().getRandomBid();
299 // 焼����法�よる最�解探索
300 BidStorage optimumBid = null;
301 try {
302 optimumBid = simulatedAnealing.run(startBid, u, SA_K_MAX);
303 } catch (Exception e) {
304 return null;
305 }
306 return optimumBid;
307 }
308
309 private ArrayList<Bound> searchConstraint(Bid bid) {
310 ArrayList<Bound> constraint = new ArrayList<Bound>();
311 double u = getUtility(bid);
312 return constraint;
313 }
314
315 @Override
316 public void endSession(NegotiationResult result) {
317 // System.out.printf("\n");
318 try {
319 sessionData.save(sessionNr, partnerBidHistory, result,
320 utilitySpace.getUtility(result.getLastBid()),
321 timeline.getTime());
322 } catch (Exception e) {
323 // TODO Auto-generated catch block
324 e.printStackTrace();
325 }
326 saveSessionData(sessionData);
327 }
328
329 @Override
330 public void ReceiveMessage(Action opponentAction) {
331 actionOfPartner = opponentAction;
332 }
333
334 private Offer generateOffer(Bid bid) {
335 return new Offer(getAgentID(), bid);
336 }
337
338 @Override
339 public String getVersion() {
340 return "5.0";
341 }
342
343 @Override
344 public String getName() {
345 return "Anac Sample Agent";
346 }
347
348 class AgentKStorategyComponentImpl implements IAgentKStorategyComponent {
349 @Override
350 public double g(double t) {
351 return param.g;
352 }
353 }
354
355 @Override
356 public String getDescription() {
357 return "ANAC2014 compatible with non-linear utility spaces";
358 }
359}
Note: See TracBrowser for help on using the repository browser.