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