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