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

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

Initial import : Genius 9.0.0

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