source: src/main/java/agents/anac/y2015/Atlas3/etc/negotiatingInfo.java

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

Initial import : Genius 9.0.0

File size: 13.2 KB
RevLine 
[1]1package agents.anac.y2015.Atlas3.etc;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.List;
8
9import agents.anac.y2015.Atlas3.Atlas3;
10import genius.core.Bid;
11import genius.core.issue.Issue;
12import genius.core.issue.IssueDiscrete;
13import genius.core.issue.IssueInteger;
14import genius.core.issue.Value;
15import genius.core.issue.ValueDiscrete;
16import genius.core.issue.ValueInteger;
17import genius.core.utility.AdditiveUtilitySpace;
18
19public class negotiatingInfo {
20 private AdditiveUtilitySpace utilitySpace; // 効用空間
21 private List<Issue> issues; // 論点
22 private ArrayList<Object> opponents; // 自身以外の交渉参加者のsender
23 private ArrayList<Bid> MyBidHistory = null; // 提案履歴
24 private ArrayList<Bid> BOBHistory = null; // BestOfferedBidの更新履歴
25 private ArrayList<Bid> PBList = null; // BestPopularBidのリスト
26 private HashMap<Object, ArrayList<Bid>> opponentsBidHistory = null; // 提案履歴
27 private HashMap<Object, Double> opponentsAverage; // 平均
28 private HashMap<Object, Double> opponentsVariance; // 分散
29 private HashMap<Object, Double> opponentsSum; // 和
30 private HashMap<Object, Double> opponentsPowSum; // 二乗和
31 private HashMap<Object, Double> opponentsStandardDeviation; // 標準偏差
32 private HashMap<Issue, HashMap<Value, Double>> valueRelativeUtility = null; // 自身の効用空間における各論点値の相対効用値行列(線形効用空間用)
33 private HashMap<Issue, HashMap<Value, Integer>> allValueFrequency = null; // 全員分の頻度行列
34 private HashMap<Object, HashMap<Issue, HashMap<Value, Integer>>> opponentsValueFrequency = null; // 交渉者別の頻度行列
35 private double BOU = 0.0; // BestOfferedUtility
36 private double MPBU = 0.0; // MaxPopularBidUtility
37 private double time_scale = 0.0; // 自分の手番が回ってくる時間間隔
38 private int round = 0; // 自分の手番数
39 private int negotiatorNum = 3; // 交渉者数(不具合が生じた場合に備えて3人で初期化)
40 private boolean isLinerUtilitySpace = true; // 線形効用空間であるかどうか
41
42 public negotiatingInfo(AdditiveUtilitySpace utilitySpace) {
43 // 初期化
44 this.utilitySpace = utilitySpace;
45 issues = utilitySpace.getDomain().getIssues();
46 opponents = new ArrayList<Object>();
47 MyBidHistory = new ArrayList<Bid>();
48 BOBHistory = new ArrayList<Bid>();
49 PBList = new ArrayList<Bid>();
50 opponentsBidHistory = new HashMap<Object, ArrayList<Bid>>();
51 opponentsAverage = new HashMap<Object, Double>();
52 opponentsVariance = new HashMap<Object, Double>();
53 opponentsSum = new HashMap<Object, Double>();
54 opponentsPowSum = new HashMap<Object, Double>();
55 opponentsStandardDeviation = new HashMap<Object, Double>();
56 valueRelativeUtility = new HashMap<Issue, HashMap<Value, Double>>();
57 allValueFrequency = new HashMap<Issue, HashMap<Value, Integer>>();
58 opponentsValueFrequency = new HashMap<Object, HashMap<Issue, HashMap<Value, Integer>>>();
59
60 try {
61 initAllValueFrequency();
62 } catch (Exception e1) {
63 System.out.println("全員分の頻度行列の初期化に失敗しました");
64 e1.printStackTrace();
65 }
66 try {
67 initValueRelativeUtility();
68 } catch (Exception e) {
69 System.out.println("相対効用行列の初期化に失敗しました");
70 e.printStackTrace();
71 }
72 }
73
74 public void initOpponent(Object sender) {
75 initNegotiatingInfo(sender); // 交渉情報を初期化
76 try {
77 initOpponentsValueFrequency(sender);
78 } // senderの頻度行列を初期化
79 catch (Exception e) {
80 System.out.println("交渉参加者の頻度行列の初期化に失敗しました");
81 e.printStackTrace();
82 }
83 opponents.add(sender); // 交渉参加者にsenderを追加
84 }
85
86 public void updateInfo(Object sender, Bid offeredBid) {
87 try {
88 updateNegotiatingInfo(sender, offeredBid);
89 } // 交渉情報の更新
90 catch (Exception e1) {
91 System.out.println("交渉情報の更新に失敗しました");
92 e1.printStackTrace();
93 }
94 try {
95 updateFrequencyList(sender, offeredBid);
96 } // senderの頻度行列の更新
97 catch (Exception e) {
98 System.out.println("頻度行列の更新に失敗しました");
99 e.printStackTrace();
100 }
101 }
102
103 private void initNegotiatingInfo(Object sender) {
104 opponentsBidHistory.put(sender, new ArrayList<Bid>());
105 opponentsAverage.put(sender, 0.0);
106 opponentsVariance.put(sender, 0.0);
107 opponentsSum.put(sender, 0.0);
108 opponentsPowSum.put(sender, 0.0);
109 opponentsStandardDeviation.put(sender, 0.0);
110 }
111
112 private void initOpponentsValueFrequency(Object sender) throws Exception {
113 opponentsValueFrequency.put(sender,
114 new HashMap<Issue, HashMap<Value, Integer>>()); // senderの頻度行列の初期化
115 for (Issue issue : issues) {
116 opponentsValueFrequency.get(sender).put(issue,
117 new HashMap<Value, Integer>()); // 頻度行列における論点行の初期化
118 ArrayList<Value> values = getValues(issue);
119 for (Value value : values) {
120 opponentsValueFrequency.get(sender).get(issue).put(value, 0);
121 } // 論点行の要素出現数の初期化
122 }
123 }
124
125 // 全員分の頻度行列の初期化
126 private void initAllValueFrequency() throws Exception {
127 ArrayList<Value> values = null;
128 for (Issue issue : issues) {
129 allValueFrequency.put(issue, new HashMap<Value, Integer>()); // 論点行の初期化
130 values = getValues(issue);
131 for (Value value : values) {
132 allValueFrequency.get(issue).put(value, 0);
133 } // 論点行の要素の初期化
134 }
135 }
136
137 // 相対効用行列の初期化
138 private void initValueRelativeUtility() throws Exception {
139 ArrayList<Value> values = null;
140 for (Issue issue : issues) {
141 valueRelativeUtility.put(issue, new HashMap<Value, Double>()); // 論点行の初期化
142 values = getValues(issue);
143 for (Value value : values) {
144 valueRelativeUtility.get(issue).put(value, 0.0);
145 } // 論点行の要素の初期化
146 }
147 }
148
149 // 相対効用行列の導出
150 public void setValueRelativeUtility(Bid maxBid) throws Exception {
151 ArrayList<Value> values = null;
152 Bid currentBid = null;
153 for (Issue issue : issues) {
154 currentBid = new Bid(maxBid);
155 values = getValues(issue);
156 for (Value value : values) {
157 currentBid = currentBid.putValue(issue.getNumber(), value);
158 valueRelativeUtility.get(issue).put(
159 value,
160 utilitySpace.getUtility(currentBid)
161 - utilitySpace.getUtility(maxBid));
162 }
163 }
164 }
165
166 public void updateNegotiatingInfo(Object sender, Bid offeredBid)
167 throws Exception {
168 opponentsBidHistory.get(sender).add(offeredBid); // 提案履歴
169
170 double util = utilitySpace.getUtility(offeredBid);
171 opponentsSum.put(sender, opponentsSum.get(sender) + util); // 和
172 opponentsPowSum.put(sender,
173 opponentsPowSum.get(sender) + Math.pow(util, 2)); // 二乗和
174
175 int round_num = opponentsBidHistory.get(sender).size();
176 opponentsAverage.put(sender, opponentsSum.get(sender) / round_num); // 平均
177 opponentsVariance.put(sender, (opponentsPowSum.get(sender) / round_num)
178 - Math.pow(opponentsAverage.get(sender), 2)); // 分散
179
180 if (opponentsVariance.get(sender) < 0) {
181 opponentsVariance.put(sender, 0.0);
182 }
183 opponentsStandardDeviation.put(sender,
184 Math.sqrt(opponentsVariance.get(sender))); // 標準偏差
185
186 if (util > BOU) {
187 BOBHistory.add(offeredBid); // BOUの更新履歴に追加
188 BOU = util; // BOUの更新
189 }
190 }
191
192 // 頻度行列を更新
193 private void updateFrequencyList(Object sender, Bid offeredBid)
194 throws Exception {
195 for (Issue issue : issues) {
196 Value value = offeredBid.getValue(issue.getNumber());
197 opponentsValueFrequency
198 .get(sender)
199 .get(issue)
200 .put(value,
201 opponentsValueFrequency.get(sender).get(issue)
202 .get(value) + 1); // リストを更新
203 allValueFrequency.get(issue).put(value,
204 allValueFrequency.get(issue).get(value) + 1); // リストを更新
205 }
206 }
207
208 // 交渉者数を返す
209 public void updateOpponentsNum(int num) {
210 negotiatorNum = num;
211 }
212
213 // 線形効用空間でない場合
214 public void utilitySpaceTypeisNonLiner() {
215 isLinerUtilitySpace = false;
216 }
217
218 // 自身の提案情報の更新
219 public void updateMyBidHistory(Bid offerBid) {
220 MyBidHistory.add(offerBid);
221 }
222
223 // 提案時間間隔を返す
224 public void updateTimeScale(double time) {
225 round = round + 1;
226 time_scale = time / round;
227 }
228
229 // PopularBidListを返す
230 public void updatePBList(Bid popularBid) throws Exception {
231 if (!PBList.contains(popularBid)) { // 一意に記録
232 PBList.add(popularBid);
233 MPBU = Math.max(MPBU, utilitySpace.getUtility(popularBid));
234 Collections.sort(PBList, new UtilityComparator()); // ソート
235
236 if (Atlas3.isPrinting) {
237 System.out.println("ranking");
238 for (int i = 0; i < PBList.size(); i++) {
239 System.out.println(utilitySpace.getUtility(PBList.get(i)));
240 }
241 System.out
242 .println("Size:"
243 + PBList.size()
244 + ", Min:"
245 + utilitySpace.getUtility(PBList.get(0))
246 + ", Max:"
247 + utilitySpace.getUtility(PBList.get(PBList
248 .size() - 1)) + ", Opponents:"
249 + opponents);
250 }
251 }
252 }
253
254 public class UtilityComparator implements Comparator<Bid> {
255 public int compare(Bid a, Bid b) {
256 try {
257 double u1 = utilitySpace.getUtility(a);
258 double u2 = utilitySpace.getUtility(b);
259 if (u1 < u2) {
260 return 1;
261 }
262 if (u1 == u2) {
263 return 0;
264 }
265 if (u1 > u2) {
266 return -1;
267 }
268 } catch (Exception e) {
269 System.out.println("効用値に基づくソートに失敗しました");
270 e.printStackTrace();
271 }
272 return 0; // 例外処理
273 }
274 }
275
276 // 平均
277 public double getAverage(Object sender) {
278 return opponentsAverage.get(sender);
279 }
280
281 // 分散
282 public double getVariancer(Object sender) {
283 return opponentsVariance.get(sender);
284 }
285
286 // 標準偏差
287 public double getStandardDeviation(Object sender) {
288 return opponentsStandardDeviation.get(sender);
289 }
290
291 // 相手の提案履歴の要素数を返す
292 public int getPartnerBidNum(Object sender) {
293 return opponentsBidHistory.get(sender).size();
294 }
295
296 // 自身のラウンド数を返す
297 public int getRound() {
298 return round;
299 }
300
301 // 交渉者数を返す
302 public int getNegotiatorNum() {
303 return negotiatorNum;
304 }
305
306 // 相対効用行列を返す
307 public HashMap<Issue, HashMap<Value, Double>> getValueRelativeUtility() {
308 return valueRelativeUtility;
309 }
310
311 // 線形効用空間であるかどうかを返す
312 public boolean isLinerUtilitySpace() {
313 return isLinerUtilitySpace;
314 }
315
316 // 論点一覧を返す
317 public List<Issue> getIssues() {
318 return issues;
319 }
320
321 // BestOfferedUtilityを返す
322 public double getBOU() {
323 return BOU;
324 }
325
326 // MaxPopularBidUtilityを返す
327 public double getMPBU() {
328 return MPBU;
329 }
330
331 // 交渉者全体のBOBHistoryを返す
332 public ArrayList<Bid> getBOBHistory() {
333 return BOBHistory;
334 }
335
336 // PBListを返す
337 public ArrayList<Bid> getPBList() {
338 return PBList;
339 }
340
341 // 提案時間間隔を返す
342 public double getTimeScale() {
343 return time_scale;
344 }
345
346 // 論点における取り得る値の一覧を返す
347 public ArrayList<Value> getValues(Issue issue) {
348 ArrayList<Value> values = new ArrayList<Value>();
349 switch (issue.getType()) {
350 case DISCRETE:
351 List<ValueDiscrete> valuesDis = ((IssueDiscrete) issue).getValues();
352 for (Value value : valuesDis) {
353 values.add(value);
354 }
355 break;
356 case INTEGER:
357 int min_value = ((IssueInteger) issue).getLowerBound();
358 int max_value = ((IssueInteger) issue).getUpperBound();
359 for (int j = min_value; j <= max_value; j++) {
360 values.add(new ValueInteger(j));
361 }
362 break;
363 default:
364 try {
365 throw new Exception("issue type " + issue.getType()
366 + " not supported by Atlas3");
367 } catch (Exception e) {
368 System.out.println("論点の取り得る値の取得に失敗しました");
369 e.printStackTrace();
370 }
371 }
372 return values;
373 }
374
375 // 交渉相手の一覧を返す
376 public ArrayList<Object> getOpponents() {
377 return opponents;
378 }
379
380 // FrequencyListの頻度に基づき,要素を返す
381 public Value getValuebyFrequencyList(Object sender, Issue issue) {
382 int current_f = 0;
383 int max_f = 0; // 最頻出要素の出現数
384 Value max_value = null; // 最頻出要素
385 ArrayList<Value> randomOrderValues = getValues(issue);
386 Collections.shuffle(randomOrderValues); // ランダムに並び替える(毎回同じ順番で評価した場合,出現数が同値の場合に返却値が偏るため)
387
388 for (Value value : randomOrderValues) {
389 current_f = opponentsValueFrequency.get(sender).get(issue)
390 .get(value);
391 // 最頻出要素を記録
392 if (max_value == null || current_f > max_f) {
393 max_f = current_f;
394 max_value = value;
395 }
396 }
397 return max_value;
398 }
399
400 // 全員分のFrequencyListの頻度に基づき,要素を返す
401 public Value getValuebyAllFrequencyList(Issue issue) {
402 int current_f = 0;
403 int max_f = 0; // 最頻出要素の出現数
404 Value max_value = null; // 最頻出要素
405 ArrayList<Value> randomOrderValues = getValues(issue);
406 Collections.shuffle(randomOrderValues); // ランダムに並び替える(毎回同じ順番で評価した場合,出現数が同値の場合に返却値が偏るため)
407
408 for (Value value : randomOrderValues) {
409 current_f = allValueFrequency.get(issue).get(value);
410 // 最頻出要素を記録
411 if (max_value == null || current_f > max_f) {
412 max_f = current_f;
413 max_value = value;
414 }
415 }
416 return max_value;
417 }
418}
Note: See TracBrowser for help on using the repository browser.