source: src/main/java/agents/anac/y2011/TheNegotiator/TimeManager.java@ 84

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

Initial import : Genius 9.0.0

File size: 6.8 KB
Line 
1package agents.anac.y2011.TheNegotiator;
2
3import java.util.ArrayList;
4
5import genius.core.bidding.BidDetails;
6import genius.core.timeline.TimeLineInfo;
7
8/**
9 * The TimeManager class is used for time-related functions.
10 *
11 * @author Alex Dirkzwager, Mark Hendrikx, Julian de Ruiter
12 */
13public class TimeManager {
14
15 // used to store the timeline of the negotiation
16 private TimeLineInfo timeline;
17 // total time steps considered
18 private final double totalTime = 180;
19 // times when phases end for a non discount domain
20 private final double[] endPhases1 = { 140.0 / totalTime, 35.0 / totalTime, 5.0 / totalTime };
21 private final double maxThresArray1[] = { 0, 0, 0, 0 };
22 private final double[] endPhases2 = { 0, 0, 0 };
23 private final double maxThresArray2[] = { 0, 0, 0, 0 };
24 // collection of bids
25 private BidsCollection bidsCollection;
26
27 // array for storing how many bids are in a certain threshold interval
28 private final int propArray[] = { 0, 0, 0 };
29 // queue holding last 30 lapsed time
30 private Queue queue = new Queue();
31 // queue size
32 private int queueSize = 15;
33 // last time
34 double lastTime = 0;
35
36 double discount;
37
38 /**
39 * Creates a TimeManager-object which stores the timeline of the
40 * negotiation.
41 *
42 * @param timeline
43 * of the negotiation
44 */
45 public TimeManager(TimeLineInfo timeline, double discount, BidsCollection bidsCollection) {
46 this.timeline = timeline;
47 this.bidsCollection = bidsCollection;
48 this.discount = discount;
49
50 if (this.discount >= 1.0) {
51 this.discount = 0; // compatibility with old discount method
52 }
53
54 // no discounts
55 calculateEndPhaseThresholds();
56
57 // discounts
58 if (this.discount != 0) {
59 calculatePropArray();
60 calculateEndPhases();
61 }
62 }
63
64 private void calculateEndPhaseThresholds() {
65 maxThresArray1[0] = bidsCollection.getPossibleBids().get(0).getMyUndiscountedUtil();
66 maxThresArray1[3] = bidsCollection.getPossibleBids().get(bidsCollection.getPossibleBids().size() - 1)
67 .getMyUndiscountedUtil();
68 double range = maxThresArray1[0] - maxThresArray1[3];
69 maxThresArray1[1] = maxThresArray1[0] - ((1.0 / 8) * range);
70 maxThresArray1[2] = maxThresArray1[0] - ((3.0 / 8) * range);
71 }
72
73 /**
74 * Returns the current phase of the negotiation.
75 *
76 * @return phase of the negotiation
77 */
78 public int getPhase(double time) {
79
80 int phase = 1;
81 double[] endPhases = endPhases1;
82
83 if (discount != 0 && time > discount) {
84 endPhases = endPhases2;
85 }
86
87 if (time > (endPhases[1] + endPhases[0])) {
88 double lapsedTime = time - lastTime;
89 queue.enqueue(lapsedTime);
90 if (queue.size() > queueSize) {
91 queue.dequeue();
92 }
93 phase = 3;
94 } else if (time > endPhases[0]) {
95 phase = 2;
96 }
97 lastTime = time;
98 return phase;
99 }
100
101 /**
102 * Returns the time dependent threshold which specifies how good a bid of an
103 * opponent should be to be accepted. This threshold is also used as a
104 * minimum for the utility of the bid of our agent.
105 *
106 * @return threshold
107 */
108 public double getThreshold(double time) {
109 int phase = getPhase(time);
110 double threshold = 0.98; // safe value
111 double[] maxThresArray = maxThresArray1;
112 double[] endPhases = endPhases1;
113
114 if (discount != 0 && time > discount) {
115 maxThresArray = maxThresArray2;
116 endPhases = endPhases2;
117 }
118 double discountActive = discount;
119 if (time <= discount) {
120 discountActive = 0;
121 }
122
123 switch (phase) {
124 case 1:
125 threshold = maxThresArray[0] - ((time - discountActive) / (endPhases[0] - discountActive))
126 * (maxThresArray[0] - maxThresArray[1]);
127 break;
128 case 2:
129 threshold = maxThresArray[1]
130 - (((time - endPhases[0]) / (endPhases[1])) * (maxThresArray[1] - maxThresArray[2]));
131 break;
132 case 3:
133 threshold = maxThresArray[2]
134 - (((time - endPhases[0] - endPhases[1]) / (endPhases[2])) * (maxThresArray[2] - maxThresArray[3]));
135 break;
136 default:
137 ErrorLogger.log("Unknown phase: " + phase);
138 break;
139 }
140 return threshold;
141 }
142
143 public int getMovesLeft() {
144 int movesLeft = -1;
145
146 if (queue.isEmpty()) {
147 movesLeft = 500; // to avoid an error
148 } else {
149 Double[] lapsedTimes = queue.toArray();
150 double total = 0;
151 for (int i = 0; i < queueSize; i++) {
152 if (lapsedTimes[i] != null) {
153 total += lapsedTimes[i];
154 }
155 }
156 movesLeft = (int) Math.floor((1.0 - timeline.getTime()) / (total / (double) queueSize));
157 }
158 return movesLeft;
159 }
160
161 /**
162 * Calculate how many possible bids are within a certain threshold interval.
163 * This is done for all the bins (phases).
164 */
165 public void calculatePropArray() {
166 ArrayList<BidDetails> posBids = bidsCollection.getPossibleBids();
167
168 double max = getThreshold(discount); // 0.0001 is just to be sure :)
169 double min = bidsCollection.getPossibleBids().get(bidsCollection.getPossibleBids().size() - 1)
170 .getMyUndiscountedUtil();
171 double range = max - min;
172 double rangeStep = range / 3;
173
174 for (int i = 0; i < posBids.size(); i++) {
175 double util = posBids.get(i).getMyUndiscountedUtil();
176
177 // calculate if a utility of a bid is within a certain interval.
178 // Intervals should be calculated!!!!
179 if (util >= max - rangeStep && util <= max) {
180 propArray[0]++;
181 } else if (util >= max - 2 * rangeStep && util < max - rangeStep) {
182 propArray[1]++;
183 } else if (util >= max - 3 * rangeStep && util < max - 2 * rangeStep) {
184 propArray[2]++;
185 }
186 }
187 // find the maximum possible utility within a bin (plus the lowest bid
188 // of the last bin)
189 ArrayList<BidDetails> bidsCol = bidsCollection.getPossibleBids();
190 maxThresArray2[0] = max;
191 if (propArray[0] == 0) {
192 maxThresArray2[1] = bidsCol.get(0).getMyUndiscountedUtil();
193 } else {
194 maxThresArray2[1] = bidsCol.get(propArray[0] - 1).getMyUndiscountedUtil(); // -1
195 // to
196 // correct
197 // for
198 // array
199 // offset
200 // of
201 // zero
202 }
203 if (propArray[0] + propArray[1] - 1 >= 0) {
204 maxThresArray2[2] = bidsCol.get(propArray[0] + propArray[1] - 1).getMyUndiscountedUtil();
205 } else {
206 maxThresArray2[2] = bidsCol.get(0).getMyUndiscountedUtil();
207 }
208 maxThresArray2[3] = min;
209 }
210
211 /**
212 * Calculates the time which should be spend on each phase based on the
213 * distribution of the utilities of the bids.
214 */
215 public void calculateEndPhases() {
216 int sum = 0;
217 for (int i = 0; i < propArray.length; i++) {
218 sum += propArray[i];
219 }
220
221 endPhases2[0] = discount + (((double) propArray[0] / (double) sum) * (1 - discount));
222 endPhases2[1] = (((double) propArray[1] / (double) sum) * (1 - discount));
223 endPhases2[2] = (((double) propArray[2] / (double) sum) * (1 - discount));
224 }
225
226 public double getTime() {
227 return timeline.getTime();
228 }
229}
Note: See TracBrowser for help on using the repository browser.