source: src/main/java/agents/ai2014/group10/Group10.java@ 126

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

Initial import : Genius 9.0.0

File size: 11.1 KB
Line 
1package agents.ai2014.group10;
2
3import java.math.BigInteger;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map.Entry;
8
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.DefaultAction;
14import genius.core.actions.Offer;
15import genius.core.bidding.BidDetails;
16import genius.core.boaframework.SortedOutcomeSpace;
17import genius.core.issue.Issue;
18import genius.core.issue.Value;
19import genius.core.misc.Range;
20import genius.core.parties.AbstractNegotiationParty;
21import genius.core.parties.NegotiationInfo;
22import genius.core.utility.AdditiveUtilitySpace;
23import genius.core.utility.Evaluator;
24
25import java.util.Random;
26
27/**
28 * This is your negotiation party.
29 */
30public class Group10 extends AbstractNegotiationParty {
31
32 /**
33 * Please keep this constructor. This is called by genius.
34 *
35 * @param utilitySpace
36 * Your utility space.
37 * @param deadlines
38 * The deadlines set for this negotiation.
39 * @param timeline
40 * Value counting from 0 (start) to 1 (end).
41 * @param randomSeed
42 * If you use any randomization, use this seed for it.
43 */
44 private ArrayList<Bid> high_utility_bids = null;
45 private OpponentBidLists opponent_bid_list;
46
47 private final double RANDOM_WALKER_TRESHOLD = 0.6;
48 private final double MINIMAL_WALKER_UTILITY = 0.85;
49
50 private final double CONCEDE_SPEED = (double) 1 / 4;
51 private final double CONCEDE_MINIMUM = 0.5;
52 private final double CONCEDE_TO = 0.5;
53 private Bid last_received_bid = null;
54 private Bid last_concede_bid = null;
55 private Bid final_bid = null;
56 private BigInteger current_step = BigInteger.valueOf(1);
57 private int first_to_offer = -1;
58 private double total_time = 0;
59 private double current_time = 0;
60
61 private Random random;
62
63 @Override
64 public void init(NegotiationInfo info) {
65 super.init(info);
66
67 random = new Random(info.getRandomSeed());
68 opponent_bid_list = new OpponentBidLists((AdditiveUtilitySpace) utilitySpace, true);
69 total_time = timeline.getTotalTime();
70 }
71
72 /**
73 * Each round this method gets called and ask you to accept or offer. The
74 * first party in the first round is a bit different, it can only propose an
75 * offer.
76 *
77 * @param validActions
78 * Either a list containing both accept and offer or only offer.
79 * @return The chosen action.
80 */
81 @SuppressWarnings("rawtypes")
82 @Override
83 public Action chooseAction(List<Class<? extends Action>> validActions) {
84 // with 50% chance, counter offer
85 // if we are the first party, also offer.
86 double time_left = (total_time - current_time - 1);
87 double time_after_walker = ((total_time - 1) * (1.0 - RANDOM_WALKER_TRESHOLD));
88 double temp = (time_left - 1.0) / time_after_walker;
89 double treshold = CONCEDE_TO + Math.pow(temp, CONCEDE_SPEED) / 2;
90 double last_utility;
91 try {
92 last_utility = utilitySpace.getUtility(last_received_bid);
93 } catch (Exception e) {
94 last_utility = 1.0;
95 }
96 if (first_to_offer == -1) {
97 if (!validActions.contains(Accept.class)) {
98 first_to_offer = 1;
99 } else {
100 first_to_offer = 0;
101 }
102 }
103 if (!validActions.contains(Accept.class)
104 || (last_utility < treshold && (time_left > 1 || first_to_offer == 1))) {
105 if ((current_time / total_time) < RANDOM_WALKER_TRESHOLD) {
106 final_bid = randomWalker();
107 } else {
108 if ((int) time_left < 3) {
109 // Opponent is not conceding. Deadline has been reached.
110 // Let them taste a bit of their own medicine. (They have to
111 // accept!)
112 try {
113 final_bid = utilitySpace.getMaxUtilityBid();
114 } catch (Exception e) {
115 // fail safe
116 final_bid = randomWalker();
117 }
118 } else {
119 ArrayList<Object> senders = opponent_bid_list.getSenders();
120 ArrayList<Entry<Pair<Integer, Value>, Integer>> pair_frequency = opponent_bid_list
121 .getMostFrequentIssueValues(senders.get(0));
122 ArrayList<Entry<Pair<Integer, Value>, Double>> weighted_list = opponent_bid_list
123 .weightIssueValues(pair_frequency);
124 final_bid = concederBid(weighted_list);
125 }
126 }
127 current_time++;
128 return new Offer(getPartyId(), final_bid);
129
130 } else {
131 current_time++;
132 return new Accept(getPartyId(), last_received_bid);
133 }
134 }
135
136 /**
137 * All offers proposed by the other parties will be received as a message.
138 * You can use this information to your advantage, for example to predict
139 * their utility.
140 *
141 * @param sender
142 * The party that did the action.
143 * @param action
144 * The action that party did.
145 */
146 @Override
147 public void receiveMessage(AgentID sender, Action action) {
148 super.receiveMessage(sender, action);
149
150 // Here you can listen to other parties' messages
151 Bid bid = DefaultAction.getBidFromAction(action);
152 if (bid != null) {
153 opponent_bid_list.insertBid(sender, bid);
154 last_received_bid = bid;
155 }
156 }
157
158 private Bid concederBid(ArrayList<Entry<Pair<Integer, Value>, Double>> ordered_pair) {
159 int actions_left = (int) (total_time - current_time - 1);
160 int actions_after_walker = (int) (total_time * (1.0 - RANDOM_WALKER_TRESHOLD));
161 Bid average_bid = randomWalker();
162 // calculate how far in the pairset the best bid occured
163 List<Issue> issues = average_bid.getIssues();
164 HashMap<Integer, Value> values = average_bid.getValues();
165 ArrayList<Pair<Integer, Value>> issue_pairs = new ArrayList<Pair<Integer, Value>>();
166 for (int i = 0; i < values.size(); i++) {
167 Integer issue_id = issues.get(i).getNumber();
168 Value value_id = values.get(i + 1);
169 issue_pairs.add(new Pair<Integer, Value>(issue_id, value_id));
170 }
171 int max_index = 0;
172 for (int i = 0; i < issue_pairs.size(); i++) {
173 int index = -1;
174 for (int j = 0; j < ordered_pair.size(); j++) {
175 Pair<Integer, Value> pair = ordered_pair.get(j).getKey();
176 if (pair.equals(issue_pairs.get(i))) {
177 index = j;
178 break;
179 }
180 }
181 if (index != -1 && index > max_index) {
182 max_index = index;
183 }
184 }
185 BigInteger total_steps = BigInteger.valueOf(2).pow(max_index);
186 BigInteger steps_per_action = total_steps.divide(BigInteger.valueOf(actions_after_walker));
187 BigInteger previous_step = current_step;
188 current_step = steps_per_action.multiply(BigInteger.valueOf(actions_after_walker - actions_left));
189
190 Bid concede_bid;
191
192 try {
193 concede_bid = utilitySpace.getMaxUtilityBid();
194 if (last_concede_bid == null) {
195 last_concede_bid = concede_bid;
196 }
197 } catch (Exception e) {
198 // fail safe
199 concede_bid = average_bid;
200 }
201
202 // binary concede bid
203 concede_bid = generateConcedeBid(current_step, concede_bid, ordered_pair, max_index);
204 for (BigInteger i = previous_step; i.compareTo(current_step) == -1; i = i.add(BigInteger.valueOf(1))) {
205 Bid new_bid = new Bid(concede_bid);
206 new_bid = generateConcedeBid(i, new_bid, ordered_pair, max_index);
207 try {
208 if (utilitySpace.getUtility(new_bid) > utilitySpace.getUtility(concede_bid)) {
209 // System.out.println("===\n new_bid:
210 // "+utilitySpace.getUtility(new_bid)+" concede_bid:
211 // "+utilitySpace.getUtility(concede_bid));
212 concede_bid = new_bid;
213 }
214 } catch (Exception e) {
215 }
216 }
217 final_bid = concede_bid;
218 try {
219
220 if (utilitySpace.getUtility(final_bid) > CONCEDE_MINIMUM) {
221 // System.out.println("Conceded,
222 // Utility:"+utilitySpace.getUtility(concede_bid)+" and
223 // "+actions_left+" steps to go.");
224 return final_bid;
225 } else {
226 // System.out.println("Average Bid,
227 // Utility:"+utilitySpace.getUtility(average_bid)+" and
228 // "+actions_left+" steps to go.");
229 return average_bid;
230 }
231 } catch (Exception e) {
232 return average_bid;
233 }
234 }
235
236 private Bid generateConcedeBid(BigInteger step, Bid bid,
237 ArrayList<Entry<Pair<Integer, Value>, Double>> ordered_pair, int issue_count) {
238 for (int i = 0; i < issue_count; i++) {
239 // binary split index to steps to include or not (max_index-bits
240 // number)
241 BigInteger bit = BigInteger.valueOf(2).pow(i);
242 if (step.and(bit).equals(bit)) {
243 Pair<Integer, Value> pair = ordered_pair.get(i).getKey();
244 int issue = pair.getInteger();
245 Value value = pair.getValue();
246 bid = bid.putValue(issue, value);
247 }
248 }
249 return bid;
250 }
251
252 private Bid randomWalker() {
253 ArrayList<Bid> bids = getHighUtilityBids(MINIMAL_WALKER_UTILITY);
254 double rnd = random.nextDouble();
255 double[] W = getWeights(bids);
256 int index = 0;
257 double sum = 0;
258 for (int i = 0; i < W.length; i++) { // Sum W until bigger then rnd
259 index = i;
260 sum += W[i];
261 if (sum > rnd) {
262 break;
263 }
264 }
265 // System.out.println("Random bid: "+bids.get(index).toString());
266 return bids.get(index);
267 }
268
269 private ArrayList<Bid> getHighUtilityBids(double minimal_utility) {
270 if (high_utility_bids == null) { // singleton
271 high_utility_bids = new ArrayList<Bid>();
272 SortedOutcomeSpace sorted_outcome = new SortedOutcomeSpace(utilitySpace);
273 Range r = new Range(minimal_utility, 1.0);
274 List<BidDetails> bid_list = sorted_outcome.getBidsinRange(r);
275 for (int i = 0; i < bid_list.size(); i++) {
276 high_utility_bids.add(bid_list.get(i).getBid());
277 }
278 }
279 return high_utility_bids;
280 }
281
282 private double[] getWeights(ArrayList<Bid> high_bids_list) {
283 int issue_length = high_bids_list.get(0).getIssues().size();
284 int datalength = high_bids_list.size();
285 // get mean values of the issues
286 double[] mean = new double[issue_length];
287 for (int i = 0; i < datalength; i++) {
288 double[] v = getIssueValues(high_bids_list.get(i));
289 for (int j = 0; j < issue_length; j++) {
290 mean[j] += v[j];
291 }
292 }
293 // get variance and distance
294 double[][] dist = new double[datalength][issue_length];
295 double[] sum = new double[issue_length];
296 double[] sum_sq = new double[issue_length];
297 for (int i = 0; i < datalength; i++) {
298 double[] v = getIssueValues(high_bids_list.get(i));
299 for (int j = 0; j < issue_length; j++) {
300 dist[i][j] = Math.abs(v[j] - mean[j]);
301 sum[j] += dist[i][j];
302 sum_sq[j] += sum[j] * sum[j];
303 }
304 }
305 double[] var = new double[issue_length];
306 double sum_var = 0.0;
307 for (int i = 0; i < issue_length; i++) {
308 var[i] = (sum_sq[i] - (sum[i] * sum[i] / datalength)) / (datalength - 1);
309 sum_var += var[i];
310 }
311 // normalize variance and distance
312 for (int i = 0; i < datalength; i++) {
313 for (int j = 0; j < issue_length; j++) {
314 dist[i][j] = dist[i][j] / sum[j];
315 }
316 }
317 for (int i = 0; i < issue_length; i++) {
318 var[i] = var[i] / sum_var;
319 }
320
321 // calculate weights for every sample
322 double[] W = new double[datalength];
323 for (int i = 0; i < datalength; i++) {
324 double sample_sum = 0.0;
325 for (int j = 0; j < issue_length; j++) {
326 sample_sum += dist[i][j] * var[j];
327 }
328 W[i] = sample_sum;
329 }
330 return W;
331 }
332
333 private double[] getIssueValues(Bid b) {
334 int size = b.getIssues().size();
335 double[] w = new double[size];
336 for (int j = 0; j < size; j++) {
337 Issue issue = b.getIssues().get(j);
338 double eval;
339 try {
340 Evaluator evaluator = ((AdditiveUtilitySpace) utilitySpace).getEvaluator(issue.getNumber());
341 eval = evaluator.getWeight()
342 * evaluator.getEvaluation((AdditiveUtilitySpace) utilitySpace, b, issue.getNumber());
343 } catch (Exception e) {
344 eval = 0.0;
345 }
346 w[j] = eval;
347 }
348 return w;
349 }
350
351 protected AgentID partyId = new AgentID("Group 10");
352
353 @Override
354 public String getDescription() {
355 return "ai2014 group10";
356 }
357
358}
Note: See TracBrowser for help on using the repository browser.