source: src/main/java/agents/anac/y2015/agenth/BidHistory.java

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

Initial import : Genius 9.0.0

File size: 4.1 KB
Line 
1package agents.anac.y2015.agenth;
2
3import java.util.Collections;
4import java.util.Comparator;
5import java.util.HashMap;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
10
11import genius.core.Bid;
12import genius.core.Domain;
13import genius.core.issue.Issue;
14import genius.core.issue.Value;
15import genius.core.utility.AdditiveUtilitySpace;
16
17public class BidHistory extends LinkedList<BidHistory.Entry> {
18 /** UtilitySpace */
19 private AdditiveUtilitySpace mUtilitySpace;
20 /** bid と sender の対応マップ */
21 private HashMap<Bid, Object> mBids;
22 /** エージェントごとの値 */
23 private HashMap<Object, ValueCounter> mValueCounterMap;
24 /** 最後に挿入された bid */
25 private Bid mLastBid;
26
27 /** 全体の平均 bid */
28 private Bid mMeans;
29 private ValueCounter mValueCounter;
30
31 public BidHistory(AdditiveUtilitySpace utilitySpace) {
32 mUtilitySpace = utilitySpace;
33 mBids = new HashMap<Bid, Object>();
34 mValueCounterMap = new HashMap<Object, ValueCounter>();
35 mValueCounter = new ValueCounter();
36 }
37
38 public void offer(Object sender, Bid bid, double utility) {
39 add(new Entry(sender, bid, utility));
40 mBids.put(bid, sender);
41 mLastBid = bid;
42
43 // ValueCounter に入れる
44 mValueCounter.add(bid);
45
46 ValueCounter counter = mValueCounterMap.get(sender);
47 if (counter == null) {
48 counter = new ValueCounter();
49 mValueCounterMap.put(sender, counter);
50 }
51 counter.add(bid); // TODO 重み加えるとかする
52 }
53
54 public void accept(Object sender, Bid bid) {
55 ValueCounter counter = mValueCounterMap.get(sender);
56 if (counter == null) {
57 counter = new ValueCounter();
58 mValueCounterMap.put(sender, counter);
59 }
60 counter.add(bid); // TODO 重み加えるとかする
61 }
62
63 public boolean containsBid(Bid bid) {
64 return mBids.containsKey(bid);
65 }
66
67 /**
68 * 自分の効用値が高い順にソートされたリストを返す
69 *
70 * @return
71 */
72 public List<Entry> getSortedList() {
73 final LinkedList<Entry> list = new LinkedList<Entry>(this);
74 Collections.sort(list, mUtilityComparator);
75 return list;
76 }
77
78 public double getProbOfValue(Object agent, Issue issue, Value value) {
79 ValueCounter counter = mValueCounterMap.get(agent);
80 if (counter != null) {
81 return (counter.get(issue).get(value) / counter.get(issue).size());
82 }
83 return 0; // FIXME
84 }
85
86 private Comparator<Entry> mUtilityComparator = new Comparator<Entry>() {
87 @Override
88 public int compare(Entry o1, Entry o2) {
89 return (int) Math.round((o1.utility - o2.utility) * 10e5);
90 }
91 };
92
93 /**
94 * BidHistory が持つエントリ
95 */
96 public class Entry {
97 public Object sender;
98 public Bid bid;
99 public double utility;
100
101 public Entry(Object sender, Bid bid, double utility) {
102 this.bid = bid;
103 this.utility = utility;
104 }
105 }
106
107 /**
108 * Value の頻度を数えるクラス HashMap<IssueNr, HashMap<Value, Count>>
109 */
110 private class ValueCounter extends HashMap<Issue, HashMap<Value, Integer>> {
111
112 public ValueCounter() {
113 final Domain domain = mUtilitySpace.getDomain();
114 final List<Issue> issues = domain.getIssues();
115 for (Issue issue : issues) {
116 put(issue, new HashMap<Value, Integer>());
117 }
118 }
119
120 public void add(Bid bid) {
121 try {
122 final List<Issue> issues = bid.getIssues();
123 for (Issue issue : issues) {
124 final Value value = bid.getValue(issue.getNumber());
125 increase(issue, value);
126 }
127 System.out.println("BidHistory.ValueCounter#add(): " + this);
128 } catch (Exception e) {
129 e.printStackTrace();
130 }
131 }
132
133 public void increase(Issue issue, Value value) {
134 final HashMap<Value, Integer> map = get(issue);
135 if (map != null) {
136 Integer count = map.get(value);
137 if (count == null) {
138 count = new Integer(0);
139 }
140 map.put(value, count + 1);
141 }
142 }
143
144 public Value getMostLikelyValue(Issue issue) {
145 final HashMap<Value, Integer> map = get(issue);
146 if (map != null) {
147 Value v = null;
148 int n = 0;
149 final Set<Map.Entry<Value, Integer>> es = map.entrySet();
150 for (Map.Entry<Value, Integer> e : es) {
151 if (n < e.getValue()) {
152 v = e.getKey();
153 n = e.getValue();
154 }
155 }
156 return v;
157 }
158 return null;
159 }
160 }
161}
Note: See TracBrowser for help on using the repository browser.