source: src/main/java/agents/anac/y2011/TheNegotiator/BidsCollection.java@ 326

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 5.1 KB
Line 
1package agents.anac.y2011.TheNegotiator;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Random;
6
7import genius.core.Bid;
8import genius.core.bidding.BidDetails;
9import genius.core.bidding.BidDetailsStrictSorterUtility;
10
11/**
12 * The BidsCollection class stores the bids of the partner and all possible bids.
13 *
14 * @author Alex Dirkzwager, Mark Hendrikx, Julian de Ruiter
15 */
16public class BidsCollection {
17
18 // bids done by partner
19 private ArrayList<BidDetails> partnerBids;
20 // all possible bids (for us) which do not violate the constraints
21 private ArrayList<BidDetails> possibleBids;
22 private Random random200;
23 private final boolean TEST_EQUIVALENCE = false;
24
25 /**
26 * Creates a BidsCollection-object which stores the partner bids and all possible
27 * bids.
28 */
29 public BidsCollection() {
30 partnerBids = new ArrayList<BidDetails>();
31 possibleBids = new ArrayList<BidDetails>();
32 if (TEST_EQUIVALENCE) {
33 random200 = new Random(200);
34 } else {
35 random200 = new Random(200);
36 }
37 }
38
39 /**
40 * @return the partnerBids
41 */
42 public ArrayList<BidDetails> getPartnerBids() {
43 return partnerBids;
44 }
45
46 /**
47 * @return the possibleBids
48 */
49 public ArrayList<BidDetails> getPossibleBids() {
50 return possibleBids;
51 }
52
53 /**
54 * Add a partner bid to the history. Bids are stored at the front
55 * to preserve the timeline.
56 *
57 * @param bid made by partner in the current turn
58 * @param utility of the bid
59 */
60 public void addPartnerBid(Bid bid, double utility, double time) {
61 BidDetails utbid = new BidDetails(bid, utility, time);
62 partnerBids.add(0, utbid);
63 }
64
65 /**
66 * Add a possible bid to the list of possible bids. The given bid
67 * should not violate the constraints of the negotiation.
68 *
69 * @param bid which is possible
70 * @param utility of the bid
71 */
72 public void addPossibleBid(Bid bid, double utility) {
73 BidDetails utbid = new BidDetails(bid, utility, -1.0);
74 possibleBids.add(utbid);
75 }
76
77 /**
78 * Sorts all possible bids in reverse natural order.
79 */
80 public void sortPossibleBids() {
81 if (TEST_EQUIVALENCE) {
82 Collections.sort(possibleBids, new BidDetailsStrictSorterUtility());
83 } else {
84 Collections.sort(possibleBids);
85 }
86 }
87
88 /**
89 * Get a partner bid.
90 *
91 * @param i
92 * @return the i'th bid in the timeline
93 */
94 public Bid getPartnerBid(int i) {
95 Bid bid = null;
96
97 if (i < partnerBids.size()) {
98 bid = partnerBids.get(i).getBid();
99 } else {
100 ErrorLogger.log("BIDSCOLLECTION: Out of bounds");
101 }
102 return bid;
103 }
104
105 /**
106 * Get a partner bid which has a utility of at least a certain
107 * value. Null is returned if no such bid exists.
108 *
109 * @param threshold
110 * @return bid with utility > threshold if exists
111 */
112 public Bid getBestPartnerBids(double threshold) {
113 ArrayList<BidDetails> temp = partnerBids;
114 if (TEST_EQUIVALENCE) {
115 Collections.sort(temp, new BidDetailsStrictSorterUtility());
116 } else {
117 Collections.sort(temp);
118 }
119 Bid bid = null;
120
121 int count = 0;
122 while (count < temp.size() && temp.get(count).getMyUndiscountedUtil() >= threshold) {
123 count++;
124 }
125
126 if (count > 0) {
127 bid = temp.get(random200.nextInt(count)).getBid();
128 }
129 return bid;
130 }
131
132 public Bid getOwnBidBetween(double lowerThres, double upperThres) {
133 return getOwnBidBetween(lowerThres, upperThres, 0);
134 }
135
136 /**
137 * Get a random bid between two given thresholds.
138 *
139 * @param lowerThres lowerbound threshold
140 * @param upperThres upperbound threshold
141 * @return random bid between thresholds
142 */
143 public Bid getOwnBidBetween(double lowerThres, double upperThres, int counter) {
144 int lB = 0;
145 int uB = 0;
146 Bid bid = null;
147
148 // determine upperbound and lowerbound by visiting all points
149 for (int i = 0; i < possibleBids.size(); i++) {
150 double util = possibleBids.get(i).getMyUndiscountedUtil();
151 if (util > upperThres) {
152 uB++;
153 }
154 if (util >= lowerThres) {
155 lB++;
156 }
157 }
158 // if there are no points between the bounds
159 if (lB == uB) {
160 if (counter == 1) {
161 return possibleBids.get(0).getBid(); // safe fallback value
162 }
163 // ignore upper threshold
164 bid = getOwnBidBetween(lowerThres, 1.1, 1);
165 } else {
166 // decrement upper- and lowerbound to get the correct index
167 // (count counts from 1, while arrays are indexed from 0)
168 if (lB > 0) {
169 lB--;
170 }
171 if ((uB + 1) <= lB) {
172 uB++;
173 }
174 // calculate a random bid index
175 int result = uB + (int) ( random200.nextDouble() * (lB - uB) + 0.5);
176 bid = possibleBids.get(result).getBid();
177 }
178 return bid;
179 }
180
181 /**
182 * Calculate the upperthreshold based on the lowerthreshold and a given percentage.
183 * @param threshold
184 * @param percentage
185 * @return
186 */
187 public double getUpperThreshold(double threshold, double percentage) {
188 int boundary = 0;
189 while (boundary < possibleBids.size() && possibleBids.get(boundary).getMyUndiscountedUtil() >= threshold) {
190 boundary++;
191 }
192 if (boundary > 0)
193 boundary--;
194 int index = boundary - (int) Math.ceil(percentage * boundary);
195
196 double utility = possibleBids.get(index).getMyUndiscountedUtil();
197 return utility;
198 }
199}
Note: See TracBrowser for help on using the repository browser.