1 | package agents.anac.y2018.lancelot.etc;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Random;
|
---|
11 |
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.issue.Issue;
|
---|
14 | import genius.core.issue.IssueDiscrete;
|
---|
15 | import genius.core.issue.IssueInteger;
|
---|
16 | import genius.core.issue.IssueReal;
|
---|
17 | import genius.core.issue.Value;
|
---|
18 | import genius.core.issue.ValueDiscrete;
|
---|
19 | import genius.core.issue.ValueInteger;
|
---|
20 | import genius.core.issue.ValueReal;
|
---|
21 | import genius.core.timeline.TimeLineInfo;
|
---|
22 | import genius.core.utility.AbstractUtilitySpace;
|
---|
23 |
|
---|
24 |
|
---|
25 | public class bidSearch {
|
---|
26 | private boolean DEBUG = true;
|
---|
27 | private AbstractUtilitySpace utilitySpace;
|
---|
28 | private TimeLineInfo timeLine;
|
---|
29 | private ArrayList<HashMap<Value, Integer>> bidTables;
|
---|
30 | private int cnt = 0;
|
---|
31 | private int issue_num = -1;
|
---|
32 |
|
---|
33 | public bidSearch(AbstractUtilitySpace utilitySpace, TimeLineInfo timeLine){
|
---|
34 | this.utilitySpace = utilitySpace;
|
---|
35 | this.timeLine = timeLine;
|
---|
36 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
37 | this.issue_num = issues.size();
|
---|
38 | bidTables = new ArrayList<>();
|
---|
39 | createBidTables();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public Bid maxBid(){
|
---|
43 | try {
|
---|
44 | Bid max_bid = utilitySpace.getMaxUtilityBid();
|
---|
45 | System.out.println("max_bid = " + max_bid);
|
---|
46 | return max_bid;
|
---|
47 | } catch (Exception e){
|
---|
48 | System.out.println("最大効用値のbidを探索できませんでした.");
|
---|
49 | return getRandomBid((double) 0.8);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public Bid offerOppositeBid(){
|
---|
54 | Bid offer_bid = getRandomBid(0);
|
---|
55 | Random rand = new Random();
|
---|
56 | for(int i=0; i<issue_num; i++){
|
---|
57 | HashMap<Value,Integer> bidTable = bidTables.get(i);
|
---|
58 | ArrayList<Value> sorted_list = sortTable(bidTable,0);
|
---|
59 | int list_num = sorted_list.size();
|
---|
60 | try {
|
---|
61 | int idx = i+1;
|
---|
62 | offer_bid = offer_bid.putValue(idx, sorted_list.get(rand.nextInt((int)(list_num/2))));
|
---|
63 | } catch (Exception e){
|
---|
64 | System.out.println("offer_bidを更新できませんでした.");
|
---|
65 |
|
---|
66 | }
|
---|
67 | }
|
---|
68 | // System.out.println("offer_opposite_bid = " + offer_bid);
|
---|
69 | return offer_bid;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Bid offerPositiveBid(double min_util){
|
---|
73 | Bid offer_bid = null;
|
---|
74 | try {
|
---|
75 | offer_bid = getRandomBid(min_util);
|
---|
76 | } catch(Exception e){
|
---|
77 | System.out.println("min_utilの値以上のBidがありません.");
|
---|
78 | offer_bid = getRandomBid(0);
|
---|
79 | }
|
---|
80 | Bid print_bid = offer_bid;
|
---|
81 | Random rand = new Random();
|
---|
82 | for(int i=0; i<issue_num; i++) {
|
---|
83 | if (rand.nextInt(5) % 2 == 0) {
|
---|
84 | Bid replaced_bid = offer_bid;
|
---|
85 | HashMap<Value, Integer> bitTable = bidTables.get(i);
|
---|
86 | int sort_order = 1;
|
---|
87 | ArrayList<Value> sorted_list = sortTable(bitTable, sort_order);
|
---|
88 | for (int j = 0; j < sorted_list.size(); j++) {
|
---|
89 | Value value = sorted_list.get(j);
|
---|
90 | try {
|
---|
91 | // System.out.println("Value: value = " + value);
|
---|
92 | int idx = i + 1;
|
---|
93 | // System.out.println("issueId = " + idx);
|
---|
94 | // System.out.println("replaced_bid = " + replaced_bid);
|
---|
95 | replaced_bid = replaced_bid.putValue(idx, value);
|
---|
96 | // System.out.println("replaced_bid = " + replaced_bid);
|
---|
97 | } catch (Exception e) {
|
---|
98 | System.out.println("replaced_bidを変更できませんでした.");
|
---|
99 | }
|
---|
100 | // double util = utilitySpace.getUtility(replaced_bid);
|
---|
101 | double util = utilitySpace.getUtilityWithDiscount(replaced_bid,timeLine.getTime());
|
---|
102 | // double origin_util = utilitySpace.getUtility(offer_bid);
|
---|
103 | double origin_util = utilitySpace.getUtilityWithDiscount(offer_bid,timeLine.getTime());
|
---|
104 | // System.out.println("replaced_bid = " + replaced_bid);
|
---|
105 | // System.out.println(("offer_bid = " + offer_bid));
|
---|
106 | if (util - origin_util > 0) {
|
---|
107 | offer_bid = replaced_bid;
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | return offer_bid;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private ArrayList<Value> sortTable(HashMap<Value,Integer> bidTable,int sort_order){
|
---|
117 | List<Map.Entry<Value,Integer>> list_entries = new ArrayList<Map.Entry<Value, Integer>>(bidTable.entrySet());
|
---|
118 | ArrayList<Value> value_list = new ArrayList<>();
|
---|
119 | if(sort_order == 0) {
|
---|
120 | Collections.sort(list_entries, new Comparator<Map.Entry<Value, Integer>>() {
|
---|
121 | @Override
|
---|
122 | public int compare(Map.Entry<Value, Integer> o1, Map.Entry<Value, Integer> o2) {
|
---|
123 | return o1.getValue().compareTo(o2.getValue());
|
---|
124 | }
|
---|
125 | });
|
---|
126 | Random rand = new Random();
|
---|
127 | for (Map.Entry<Value, Integer> entry : list_entries) {
|
---|
128 | value_list.add(entry.getKey());
|
---|
129 | }
|
---|
130 | } else{
|
---|
131 | Collections.sort(list_entries, new Comparator<Map.Entry<Value, Integer>>() {
|
---|
132 | @Override
|
---|
133 | public int compare(Map.Entry<Value, Integer> o1, Map.Entry<Value, Integer> o2) {
|
---|
134 | return o2.getValue().compareTo(o1.getValue());
|
---|
135 | }
|
---|
136 | });
|
---|
137 | Random rand = new Random();
|
---|
138 | for (Map.Entry<Value, Integer> entry : list_entries) {
|
---|
139 | value_list.add(entry.getKey());
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return value_list;
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void createBidTables(){
|
---|
146 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
147 | for(Issue issue : issues){
|
---|
148 | HashMap<Value,Integer> bidTable = new HashMap<>();
|
---|
149 | IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
|
---|
150 | List<ValueDiscrete> value_list = issueDiscrete.getValues();
|
---|
151 | for (Value value : value_list){
|
---|
152 | bidTable.put(value,0);
|
---|
153 | }
|
---|
154 | bidTables.add(bidTable);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void printBidTables(){
|
---|
159 | for(int i=0; i<issue_num; i++){
|
---|
160 | HashMap<Value,Integer> bidTable = bidTables.get(i);
|
---|
161 |
|
---|
162 | System.out.println("bidTable = " + bidTable);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void updateBidTable(Bid recievedBid){
|
---|
167 | for (int i = 0; i < issue_num; i++) {
|
---|
168 | HashMap<Value,Integer> bidTable = bidTables.get(i);
|
---|
169 | Value key = recievedBid.getValue(i + 1);
|
---|
170 | if (bidTable.get(key) != null) {
|
---|
171 | int value_num = (int) bidTable.get(key);
|
---|
172 | bidTable.put(key, value_num + 1);
|
---|
173 | } else {
|
---|
174 | bidTable.put(key, 1);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | cnt++;
|
---|
178 | if(cnt % 50 == 0) {
|
---|
179 | printBidTables();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public Bid getRandomBid(double minUtil){
|
---|
184 | double max_util = 0;
|
---|
185 | try{
|
---|
186 | max_util = utilitySpace.getUtilityWithDiscount(utilitySpace.getMaxUtilityBid(), timeLine.getTime());
|
---|
187 | }catch (Exception e){
|
---|
188 | System.out.println("max_utilを得られませんでした.");
|
---|
189 | }
|
---|
190 | minUtil = Math.min(minUtil,max_util);
|
---|
191 | if(minUtil == max_util){
|
---|
192 | minUtil -= 0.1;
|
---|
193 | }
|
---|
194 | HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
|
---|
195 | // <issuenumber,chosen
|
---|
196 | // value
|
---|
197 | // string>
|
---|
198 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
199 | Random randomnr = new Random();
|
---|
200 |
|
---|
201 | // create a random bid with utility>MINIMUM_BID_UTIL.
|
---|
202 | // note that this may never succeed if you set MINIMUM too high!!!
|
---|
203 | // in that case we will search for a bid till the time is up (3 minutes)
|
---|
204 | // but this is just a simple agent.
|
---|
205 | Bid bid = null;
|
---|
206 | do {
|
---|
207 | for (Issue lIssue : issues) {
|
---|
208 | switch (lIssue.getType()) {
|
---|
209 | case DISCRETE:
|
---|
210 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
211 | int optionIndex = randomnr.nextInt(lIssueDiscrete.getNumberOfValues());
|
---|
212 | values.put(lIssue.getNumber(), lIssueDiscrete.getValue(optionIndex));
|
---|
213 | break;
|
---|
214 | case REAL:
|
---|
215 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
216 | int optionInd = randomnr.nextInt(lIssueReal.getNumberOfDiscretizationSteps() - 1);
|
---|
217 | values.put(lIssueReal.getNumber(),
|
---|
218 | new ValueReal(lIssueReal.getLowerBound()
|
---|
219 | + (lIssueReal.getUpperBound() - lIssueReal.getLowerBound()) * (double) (optionInd)
|
---|
220 | / (double) (lIssueReal.getNumberOfDiscretizationSteps())));
|
---|
221 | break;
|
---|
222 | case INTEGER:
|
---|
223 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
224 | int optionIndex2 = lIssueInteger.getLowerBound()
|
---|
225 | + randomnr.nextInt(lIssueInteger.getUpperBound() - lIssueInteger.getLowerBound());
|
---|
226 | values.put(lIssueInteger.getNumber(), new ValueInteger(optionIndex2));
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | bid = new Bid(utilitySpace.getDomain(), values);
|
---|
231 | // System.out.println("aaa " + minUtil + " " + max_util);
|
---|
232 | } while (utilitySpace.getUtilityWithDiscount(bid, timeLine.getTime()) < minUtil);
|
---|
233 |
|
---|
234 | return bid;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 |
|
---|
239 | // public Bid proposition1(){
|
---|
240 | // return ;
|
---|
241 | // }
|
---|
242 | }
|
---|