source: src/main/java/agents/anac/y2018/beta_one/AntiAnalysis.java@ 346

Last change on this file since 346 was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 5.0 KB
Line 
1package agents.anac.y2018.beta_one;
2
3import java.util.List;
4
5import java.util.Comparator;
6import java.util.PriorityQueue;
7import java.util.Queue;
8
9import genius.core.analysis.BidPoint;
10import genius.core.analysis.BidSpace;
11import genius.core.issue.Value;
12import genius.core.issue.ValueDiscrete;
13import genius.core.misc.Range;
14import genius.core.utility.AdditiveUtilitySpace;
15import genius.core.utility.EvaluatorDiscrete;
16
17public class AntiAnalysis
18{
19 private AdditiveUtilitySpace utilitySpace;
20 private AdditiveUtilitySpace anti;
21
22 private BidSpace antiSpace;
23 private List<BidPoint> antiPareto;
24 private BidPoint antiKalai;
25 private double antiKalaiUtil;
26
27 private double observeDuration;
28 private double selfishPoint;
29 private double boxSize;
30
31 public AntiAnalysis(AdditiveUtilitySpace utilitySpace, double observeDuration, double selfishRatio, double boxSize)
32 {
33 this.utilitySpace = utilitySpace;
34 this.observeDuration = observeDuration;
35 this.boxSize = boxSize;
36
37 createAnti();
38 createAntiSpace();
39
40 setSelfishPoint(selfishRatio);
41 }
42
43 public void setSelfishPoint(double selfishRatio)
44 {
45 selfishPoint = lerp(antiKalaiUtil, 1, selfishRatio);
46 }
47
48 public Range getBox(double time, boolean betrayed)
49 {
50 if (time > 1)
51 {
52 return new Range(selfishPoint, 1);
53 }
54 else if (time < observeDuration)
55 {
56 double t = time / observeDuration;
57
58 double lower = lerp(1, selfishPoint, t);
59 double upper = clamp(lower + boxSize, lower, 1);
60
61 return new Range(lower, upper);
62 }
63 else if (!betrayed)
64 {
65 double t = (time - observeDuration) / (1 - observeDuration);
66
67 double lower = lerp(selfishPoint, antiKalaiUtil - boxSize / 2, t);
68 double upper = lerp(selfishPoint + boxSize, antiKalaiUtil + boxSize / 2, t);
69
70 return new Range(lower, upper);
71 }
72 else
73 {
74 return getBox(99, true);
75 }
76 }
77
78 public static double lerp(double a, double b, double t)
79 {
80 if (t < 0)
81 {
82 t = 0;
83 }
84 else if (t > 1)
85 {
86 t = 1;
87 }
88
89 return a + (b - a) * t;
90 }
91
92 public static double clamp(double d, double min, double max)
93 {
94 if (d < min)
95 {
96 return min;
97 }
98
99 if (d > max)
100 {
101 return max;
102 }
103
104 return d;
105 }
106
107 private void createAnti()
108 {
109 anti = (AdditiveUtilitySpace) utilitySpace.copy();
110
111 for (int i = 1; i <= utilitySpace.getNrOfEvaluators(); i++)
112 {
113 EvaluatorDiscrete evaluator;
114
115 if (utilitySpace.getEvaluator(i) instanceof EvaluatorDiscrete)
116 {
117 evaluator = (EvaluatorDiscrete) utilitySpace.getEvaluator(i);
118 }
119 else
120 {
121 antiKalaiUtil = 0.5;
122 return;
123 }
124
125 Queue<ValueDataPair> queue = new PriorityQueue<ValueDataPair>(valueDataComparator);
126
127 for (ValueDiscrete value : evaluator.getValues())
128 {
129 ValueDataPair pair = new ValueDataPair(value, evaluator.getValue(value));
130 queue.add(pair);
131 }
132
133 ValueDataPair[] pairArray = queueToArray(queue);
134 interchangeArray(pairArray);
135
136 EvaluatorDiscrete antiEva = (EvaluatorDiscrete) anti.getEvaluator(i);
137
138 for (ValueDataPair pair : pairArray)
139 {
140 antiEva.setEvaluation(pair.value, pair.data);
141 }
142 }
143 }
144
145 private class ValueDataPair
146 {
147 public Value value;
148 public int data;
149
150 public ValueDataPair(Value value, int data)
151 {
152 this.value = value;
153 this.data = data;
154 }
155 }
156
157 private static Comparator<ValueDataPair> valueDataComparator = new Comparator<ValueDataPair>()
158 {
159 public int compare(ValueDataPair p1, ValueDataPair p2)
160 {
161 return p1.data - p2.data;
162 }
163 };
164
165 private ValueDataPair[] queueToArray(Queue<ValueDataPair> queue)
166 {
167 ValueDataPair[] array = new ValueDataPair[queue.size()];
168
169 for (int i = 0; i < array.length; i++)
170 {
171 array[i] = queue.poll();
172 }
173
174 return array;
175 }
176
177 private void interchangeArray(ValueDataPair[] array)
178 {
179 interchangeArray(array, 0, array.length - 1);
180 }
181
182 private void interchangeArray(ValueDataPair[] array, int bot, int top)
183 {
184 if (top <= bot)
185 {
186 return;
187 }
188
189 ValueDataPair botPair = array[bot];
190 ValueDataPair topPair = array[top];
191
192 int botData = botPair.data;
193 botPair.data = topPair.data;
194 topPair.data = botData;
195
196 array[bot] = botPair;
197 array[top] = topPair;
198
199 interchangeArray(array, bot + 1, top - 1);
200 }
201
202 private void createAntiSpace()
203 {
204 try
205 {
206 antiSpace = new BidSpace(utilitySpace, anti);
207 antiPareto = antiSpace.getParetoFrontier();
208 antiKalai = antiSpace.getKalaiSmorodinsky();
209 antiKalaiUtil = antiKalai.getUtilityA();
210 }
211 catch (Exception e)
212 {
213 e.printStackTrace();
214 }
215 }
216
217 public double getSelfishSlope(double discountFactor)
218 {
219 double dUtil = (selfishPoint + boxSize / 2) - 1;
220 double dt = discountFactor;
221
222 return dUtil / dt;
223 }
224
225 public List<BidPoint> getAntiPareto()
226 {
227 return antiPareto;
228 }
229
230 public BidPoint getAntiKalai()
231 {
232 return antiKalai;
233 }
234
235 public double getAntiKalaiUtil()
236 {
237 return antiKalaiUtil;
238 }
239
240 public double getSelfishPoint()
241 {
242 return selfishPoint;
243 }
244}
Note: See TracBrowser for help on using the repository browser.