source: src/main/java/agents/anac/y2017/ponpokoagent/PonPokoAgent.java

Last change on this file was 46, checked in by Tim Baarslag, 6 years ago

agents get their getUtilitySpace() from the API, not the info object anymore

File size: 7.3 KB
Line 
1package agents.anac.y2017.ponpokoagent;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.Iterator;
12import java.util.Random;
13import java.util.Set;
14
15import genius.core.AgentID;
16import genius.core.Bid;
17import genius.core.Domain;
18import genius.core.actions.Accept;
19import genius.core.actions.Action;
20import genius.core.actions.Offer;
21import genius.core.issue.Issue;
22import genius.core.issue.IssueDiscrete;
23import genius.core.issue.IssueInteger;
24import genius.core.issue.IssueReal;
25import genius.core.issue.ValueDiscrete;
26import genius.core.parties.AbstractNegotiationParty;
27import genius.core.parties.NegotiationInfo;
28import genius.core.utility.UtilitySpace;
29
30public class PonPokoAgent extends AbstractNegotiationParty {
31
32 private Bid lastReceivedBid = null;
33 private Domain domain = null;
34
35 private List<BidInfo> lBids;
36
37 private double threshold_low = 0.99;
38 private double threshold_high = 1.0;
39
40 private final int PATTERN_SIZE = 5;
41 private int pattern = 0;
42
43 @Override
44 public void init(NegotiationInfo info) {
45
46 super.init(info);
47 this.domain = getUtilitySpace().getDomain();
48
49 lBids = new ArrayList<>(AgentTool.generateRandomBids(this.domain, 30000,
50 this.rand, this.utilitySpace));
51 Collections.sort(lBids, new BidInfoComp().reversed());
52
53 pattern = rand.nextInt(PATTERN_SIZE);
54 }
55
56 @Override
57 public Action chooseAction(List<Class<? extends Action>> validActions) {
58
59 // 譲歩度合いの設定
60 if (pattern == 0) {
61 threshold_high = 1 - 0.1 * timeline.getTime();
62 threshold_low = 1 - 0.1 * timeline.getTime()
63 - 0.1 * Math.abs(Math.sin(this.timeline.getTime() * 40));
64 } else if (pattern == 1) {
65 threshold_high = 1;
66 threshold_low = 1 - 0.22 * timeline.getTime();
67 } else if (pattern == 2) {
68 threshold_high = 1 - 0.1 * timeline.getTime();
69 threshold_low = 1 - 0.1 * timeline.getTime()
70 - 0.15 * Math.abs(Math.sin(this.timeline.getTime() * 20));
71 } else if (pattern == 3) {
72 threshold_high = 1 - 0.05 * timeline.getTime();
73 threshold_low = 1 - 0.1 * timeline.getTime();
74 if (timeline.getTime() > 0.99) {
75 threshold_low = 1 - 0.3 * timeline.getTime();
76 }
77 } else if (pattern == 4) {
78 threshold_high = 1 - 0.15 * this.timeline.getTime()
79 * Math.abs(Math.sin(this.timeline.getTime() * 20));
80 threshold_low = 1 - 0.21 * this.timeline.getTime()
81 * Math.abs(Math.sin(this.timeline.getTime() * 20));
82 } else {
83 threshold_high = 1 - 0.1 * timeline.getTime();
84 threshold_low = 1
85 - 0.2 * Math.abs(Math.sin(this.timeline.getTime() * 40));
86 }
87
88 // Accept判定
89 if (lastReceivedBid != null) {
90 if (getUtility(lastReceivedBid) > threshold_low) {
91 return new Accept(getPartyId(), lastReceivedBid);
92 }
93 }
94
95 // Offerするbidの選択
96 Bid bid = null;
97 while (bid == null) {
98 bid = AgentTool.selectBidfromList(this.lBids, this.threshold_high,
99 this.threshold_low);
100 if (bid == null) {
101 threshold_low -= 0.0001;
102 }
103 }
104 return new Offer(getPartyId(), bid);
105 }
106
107 @Override
108 public void receiveMessage(AgentID sender, Action action) {
109 super.receiveMessage(sender, action);
110 if (action instanceof Offer) {
111 lastReceivedBid = ((Offer) action).getBid();
112 }
113 }
114
115 @Override
116 public String getDescription() {
117 return "ANAC2017";
118 }
119
120}
121
122class AgentTool {
123
124 private static Random random = new Random();
125
126 public static Bid selectBidfromList(List<BidInfo> bidInfoList,
127 double higerutil, double lowwerutil) {
128 List<BidInfo> bidInfos = new ArrayList<>();
129 // Wouter #1536 java8 not allowed, changed the code.
130 for (BidInfo bidInfo : bidInfoList) {
131 if (bidInfo.getutil() <= higerutil
132 && bidInfo.getutil() >= lowwerutil) {
133 bidInfos.add(bidInfo);
134 }
135 }
136 if (bidInfos.size() == 0) {
137 return null;
138 } else {
139 return bidInfos.get(random.nextInt(bidInfos.size())).getBid();
140 }
141 }
142
143 public static Set<BidInfo> generateRandomBids(Domain d, int numberOfBids,
144 Random random, UtilitySpace utilitySpace) {
145 Set<BidInfo> randombids = new HashSet<>();
146 for (int i = 0; i < numberOfBids; i++) {
147 Bid b = d.getRandomBid(random);
148 randombids.add(new BidInfo(b, utilitySpace.getUtility(b)));
149 }
150 return randombids;
151 }
152
153 public static long getNumberOfPosibleBids(Domain d) {
154 List lIssues = d.getIssues();
155 if (lIssues.isEmpty()) {
156 return 0;
157 }
158 long lNumberOfPossibleBids = 1;
159 for (Iterator it = lIssues.iterator(); it.hasNext();) {
160 Issue lIssue = (Issue) it.next();
161 if (lIssue instanceof IssueDiscrete) {
162 lNumberOfPossibleBids *= ((IssueDiscrete) lIssue)
163 .getNumberOfValues();
164 } else if (lIssue instanceof IssueInteger) {
165 lNumberOfPossibleBids *= ((IssueInteger) lIssue)
166 .getNumberOfDiscretizationSteps();
167 } else if (lIssue instanceof IssueReal) {
168 lNumberOfPossibleBids *= ((IssueReal) lIssue)
169 .getNumberOfDiscretizationSteps();
170 } else {
171 // wtf happened
172 return 0;
173 }
174 }
175 return lNumberOfPossibleBids;
176 }
177
178 public static List<Bid> getAllPossibleBid2(Domain d) {
179 List<Bid> possiblebids = new ArrayList<>();
180 List lIssues = d.getIssues();
181 int[] count = new int[lIssues.size()];
182 for (int i = 0; i < lIssues.size(); i++) {
183 Issue issue = (Issue) (lIssues.get(i));
184 if (issue instanceof IssueDiscrete) {
185 List<ValueDiscrete> lValues = ((IssueDiscrete) issue)
186 .getValues();
187 lValues.get(0).getValue();
188 } else if (issue instanceof IssueInteger) {
189
190 } else if (issue instanceof IssueReal) {
191
192 }
193 }
194
195 return null;
196 }
197
198 // 可能なbidをすべて列挙する.
199 public static List<Bid> getAllPossibleBids(Domain dom) {
200 List allpossiblebids = new ArrayList<Bid>();
201 List lIssues = dom.getIssues();
202 int[] count = new int[lIssues.size()];
203 Arrays.fill(count, 0);
204 int cur = 0;
205 while (cur != lIssues.size()) {
206 HashMap e = new HashMap();
207 for (int i = 0; i < lIssues.size(); i++) {
208 e.put(Integer.valueOf(
209 ((IssueDiscrete) (lIssues.get(i))).getNumber()),
210 ((IssueDiscrete) (lIssues.get(i))).getValue(count[i]));
211 }
212
213 Bid newbid = new Bid(dom, e);
214 allpossiblebids.add(newbid);
215 boolean changed = false;
216 for (int i = 0; i <= cur && cur != lIssues.size(); i++) {
217 if (!changed) {
218 if (count[i] < ((IssueDiscrete) (lIssues.get(i)))
219 .getValues().size() - 1) {
220 count[i]++;
221 changed = true;
222 } else {
223 count[i] = 0;
224 if (i == cur) {
225 cur++;
226 }
227 }
228 }
229 }
230 }
231 return allpossiblebids;
232 }
233}
234
235// bidとその効用を保存するクラス
236class BidInfo {
237 Bid bid;
238 double util;
239
240 public BidInfo(Bid b) {
241 this.bid = b;
242 util = 0.0;
243 }
244
245 public BidInfo(Bid b, double u) {
246 this.bid = b;
247 util = u;
248 }
249
250 public void setutil(double u) {
251 util = u;
252 }
253
254 public Bid getBid() {
255 return bid;
256 }
257
258 public double getutil() {
259 return util;
260 }
261
262 // 適当実装
263 @Override
264 public int hashCode() {
265 return bid.hashCode();
266 }
267
268 public boolean equals(BidInfo bidInfo) {
269 return bid.equals(bidInfo.getBid());
270 }
271
272 @Override
273 public boolean equals(Object obj) {
274 if (obj == null) {
275 return false;
276 }
277 if (obj instanceof BidInfo) {
278 return ((BidInfo) obj).getBid().equals(bid);
279 } else {
280 return false;
281 }
282 }
283
284}
285
286final class BidInfoComp implements Comparator<BidInfo> {
287 BidInfoComp() {
288 super();
289 }
290
291 @Override
292 public int compare(BidInfo o1, BidInfo o2) {
293 return Double.compare(o1.getutil(), o2.getutil());
294 }
295}
Note: See TracBrowser for help on using the repository browser.