1 | package agents.anac.y2019.gravity;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Map.Entry;
|
---|
8 |
|
---|
9 | import javax.swing.event.DocumentListener;
|
---|
10 |
|
---|
11 | import genius.core.AgentID;
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.actions.Accept;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.Offer;
|
---|
16 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
17 | import genius.core.issue.Issue;
|
---|
18 | import genius.core.issue.IssueDiscrete;
|
---|
19 | import genius.core.issue.ValueDiscrete;
|
---|
20 | import genius.core.parties.AbstractNegotiationParty;
|
---|
21 | import genius.core.parties.NegotiationInfo;
|
---|
22 | import genius.core.timeline.DiscreteTimeline;
|
---|
23 | import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
|
---|
24 | import genius.core.utility.AbstractUtilitySpace;
|
---|
25 |
|
---|
26 | public class Gravity extends AbstractNegotiationParty {
|
---|
27 | private static final double HALF_OF_TIME = 0.5;
|
---|
28 |
|
---|
29 | private AbstractUtilitySpace utilitySpace = null;
|
---|
30 | private SortedOutcomeSpace outcomeSpace = null;
|
---|
31 |
|
---|
32 | Bid sampleBid;
|
---|
33 | private List<Bid> orderedBidListInAscendingOrder;
|
---|
34 | private List<Issue> listOfIssues;
|
---|
35 | private int numberOfIssues;
|
---|
36 |
|
---|
37 | Map<Integer, double[][]> eachIssueAnItsIndexingValuesDoubleArrayMap;
|
---|
38 | Map<Integer, double[]> eachIssueAndItsValuesUtilityMap;
|
---|
39 |
|
---|
40 | double[] sumOfSquaredErrorsOfIssueValues;
|
---|
41 | Map<Integer, Double> eachIssueAndItsUtilityMap;
|
---|
42 |
|
---|
43 | // opponent modeling
|
---|
44 | Bid lastReceivedBid;
|
---|
45 | Map<Integer, double[]> eachIssueAndItsValuesFrequencyArrayMap;
|
---|
46 | Map<Integer, double[]> opponentEachIssueAndItsValuesUtilityMap;
|
---|
47 | double[] opponentSumOfSquaredErrosOfEachIssueMatrix;
|
---|
48 | Map<Integer, Double> opponentEachIssueAndItsUtilityMap;
|
---|
49 |
|
---|
50 | double lastOfferingTime;
|
---|
51 | Bid lastOfferedBid;
|
---|
52 | Bid nextBid;
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void init(NegotiationInfo info) {
|
---|
56 | super.init(info);
|
---|
57 | initAgentVariables();
|
---|
58 | initOpponentVars();
|
---|
59 | }
|
---|
60 |
|
---|
61 | // uncertain preferences
|
---|
62 | private void initAgentVariables() {
|
---|
63 | initUncertainPrefVariables();
|
---|
64 | createCopelandMatrices();
|
---|
65 | fillAgentVars();
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void fillAgentVars() {
|
---|
69 | fillCopelandMatrices();
|
---|
70 | setValueUtilities();
|
---|
71 | fillMatriceOfNoChangeOfEachIssue();
|
---|
72 | setIssueUtilitiesWithNormalization();
|
---|
73 | }
|
---|
74 |
|
---|
75 | // opponent modeling
|
---|
76 | private void initOpponentVars() {
|
---|
77 | initOpponentModelingVars();
|
---|
78 | createFrequencyArrays();
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void initUncertainPrefVariables() {
|
---|
82 | this.utilitySpace = getUtilitySpace();
|
---|
83 | this.outcomeSpace = new SortedOutcomeSpace(utilitySpace);
|
---|
84 |
|
---|
85 | this.orderedBidListInAscendingOrder = getUserModel().getBidRanking().getBidOrder();
|
---|
86 | this.sampleBid = orderedBidListInAscendingOrder.get(0);
|
---|
87 | this.listOfIssues = sampleBid.getIssues();
|
---|
88 | this.numberOfIssues = listOfIssues.size();
|
---|
89 |
|
---|
90 | this.eachIssueAnItsIndexingValuesDoubleArrayMap = new HashMap<Integer, double[][]>();
|
---|
91 | this.eachIssueAndItsValuesUtilityMap = new HashMap<Integer, double[]>();
|
---|
92 |
|
---|
93 | this.sumOfSquaredErrorsOfIssueValues = new double[numberOfIssues];
|
---|
94 | this.eachIssueAndItsUtilityMap = new HashMap<Integer, Double>();
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void initOpponentModelingVars() {
|
---|
98 | this.eachIssueAndItsValuesFrequencyArrayMap = new HashMap<Integer, double[]>();
|
---|
99 | this.opponentEachIssueAndItsValuesUtilityMap = new HashMap<Integer, double[]>();
|
---|
100 | this.opponentSumOfSquaredErrosOfEachIssueMatrix = new double[numberOfIssues];
|
---|
101 | this.opponentEachIssueAndItsUtilityMap = new HashMap<Integer, Double>();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void createCopelandMatrices() {
|
---|
105 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
106 | IssueDiscrete issueDiscrete = (IssueDiscrete) sampleBid.getIssues().get(i);
|
---|
107 | int valueSize = issueDiscrete.getValues().size();
|
---|
108 | eachIssueAnItsIndexingValuesDoubleArrayMap.put(i, new double[valueSize][valueSize]);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void createFrequencyArrays() {
|
---|
113 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
114 | IssueDiscrete issueDiscrete = (IssueDiscrete) sampleBid.getIssues().get(i);
|
---|
115 | int valueSize = issueDiscrete.getValues().size();
|
---|
116 | eachIssueAndItsValuesFrequencyArrayMap.put(i, new double[valueSize]);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Gets the bigger bid from the sorted list . Gets the smaller bids from the
|
---|
122 | * sorted list. Does pairwise Copeland comparison with one bigger bid and
|
---|
123 | * smaller bids. depending on the bigger bid > all smaller bids
|
---|
124 | */
|
---|
125 | private void fillCopelandMatrices() {
|
---|
126 | for (int i = orderedBidListInAscendingOrder.size() - 1; i > 0; i--) {
|
---|
127 | Bid biggerBid = orderedBidListInAscendingOrder.get(i);
|
---|
128 | for (int j = i - 1; j >= 0; j--) {
|
---|
129 | Bid smallerBid = orderedBidListInAscendingOrder.get(j);
|
---|
130 | fillCopelandMatriceWithBiggerAndSmaller(biggerBid, smallerBid, i, j);
|
---|
131 | }
|
---|
132 | System.out.println(biggerBid);
|
---|
133 | }
|
---|
134 | printIntDoubleDoubleArrMap(eachIssueAnItsIndexingValuesDoubleArrayMap);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * pairwise Copeland comparison in each issue value, update the frequency
|
---|
139 | * matrices for different issue values depending on the similarity.
|
---|
140 | */
|
---|
141 | private void fillCopelandMatriceWithBiggerAndSmaller(Bid biggerBid, Bid smallerBid, int biggerIndex,
|
---|
142 | int smallerIndex) {
|
---|
143 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
144 | ValueDiscrete biggerBidValue = (ValueDiscrete) (biggerBid.getValue(i + 1));
|
---|
145 | int biggerBidValueIndex = getIndexOfValueInIssue(biggerBid, i, biggerBidValue.getValue());
|
---|
146 | ValueDiscrete smallerValue = (ValueDiscrete) (smallerBid.getValue(i + 1));
|
---|
147 | int smallerBidValueIndex = getIndexOfValueInIssue(smallerBid, i, smallerValue.getValue());
|
---|
148 | int numberOfSimilarities = biggerBid.countEqualValues(smallerBid);
|
---|
149 | if (numberOfSimilarities > 0) {
|
---|
150 | eachIssueAnItsIndexingValuesDoubleArrayMap.get(i)[biggerBidValueIndex][smallerBidValueIndex] += (1d
|
---|
151 | / (biggerIndex - smallerIndex)) * numberOfSimilarities;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private int getIndexOfValueInIssue(Bid bid, int issueIndex, String value) {
|
---|
157 | IssueDiscrete is = (IssueDiscrete) bid.getIssues().get(issueIndex);
|
---|
158 | return is.getValueIndex(value);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private void setValueUtilities() {
|
---|
162 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
163 | IssueDiscrete issueDiscrete = (IssueDiscrete) sampleBid.getIssues().get(i);
|
---|
164 | int valueSize = issueDiscrete.getValues().size();
|
---|
165 | double[] valuesBeingBigInfoArray = new double[valueSize];
|
---|
166 | double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i);
|
---|
167 | for (int j = 0; j < valueSize; j++) {
|
---|
168 | double sumOfRowInMatrix = getSumOfRowInMatrix(matrix, j);
|
---|
169 | double sumOfColInMatrix = getSumOfColInMatrix(matrix, j);
|
---|
170 | double total = sumOfColInMatrix + sumOfRowInMatrix;
|
---|
171 | if (total == 0) {
|
---|
172 | valuesBeingBigInfoArray[j] = 0;
|
---|
173 | } else {
|
---|
174 | double beingBigPercentage = (sumOfRowInMatrix) / total;
|
---|
175 | valuesBeingBigInfoArray[j] = beingBigPercentage;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | normalize(i, valueSize, valuesBeingBigInfoArray);
|
---|
179 | }
|
---|
180 | System.out.println("------------AGENT------------");
|
---|
181 | printIntDoubleArrMap(eachIssueAndItsValuesUtilityMap);
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void normalize(int i, int valueSize, double[] valuesBeingBigInfoArray) {
|
---|
185 | double[] utilityArr = new double[valueSize];
|
---|
186 | double totalSum = getSumOfRowInOneDimensionalMatrix(valuesBeingBigInfoArray);
|
---|
187 | for (int j = 0; j < valueSize; j++) {
|
---|
188 | if (totalSum == 0) {
|
---|
189 | utilityArr[j] = 0;
|
---|
190 | } else {
|
---|
191 | utilityArr[j] = valuesBeingBigInfoArray[j] / totalSum;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | eachIssueAndItsValuesUtilityMap.put(i, utilityArr);
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void fillMatriceOfNoChangeOfEachIssue() {
|
---|
198 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
199 | double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i);
|
---|
200 | double sumOfMatrix = 0;
|
---|
201 | for (int j = 0; j < matrix.length; j++) {
|
---|
202 | sumOfMatrix += getSumOfRowInMatrix(matrix, j);
|
---|
203 | }
|
---|
204 | System.out.println("Sum of matrix: " + sumOfMatrix);
|
---|
205 | double average = sumOfMatrix / (matrix.length * matrix.length);
|
---|
206 | System.out.println("average of matrix: " + average);
|
---|
207 | double sumOfSquaredErrors = 0;
|
---|
208 | for (int j = 0; j < matrix.length; j++) {
|
---|
209 | for (int k = 0; k < matrix.length; k++) {
|
---|
210 | sumOfSquaredErrors += Math.pow(matrix[j][k] - average, 2);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | sumOfSquaredErrorsOfIssueValues[i] = Math.sqrt(sumOfSquaredErrors / (matrix.length * matrix.length));
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | private int getSumOfRowInMatrix(double[][] matrix, int row) {
|
---|
218 | int rowSum = 0;
|
---|
219 | for (int col = 0; col < matrix[row].length; col++) {
|
---|
220 | rowSum += matrix[row][col];
|
---|
221 | }
|
---|
222 | return rowSum;
|
---|
223 | }
|
---|
224 |
|
---|
225 | private int getSumOfColInMatrix(double[][] matrix, int col) {
|
---|
226 | int colSum = 0;
|
---|
227 | for (int row = 0; row < matrix.length; row++) {
|
---|
228 | colSum += matrix[row][col];
|
---|
229 | }
|
---|
230 | return colSum;
|
---|
231 | }
|
---|
232 |
|
---|
233 | private void setIssueUtilitiesWithNormalization() {
|
---|
234 | double totalOfsumOfSquares = 0;
|
---|
235 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
236 | totalOfsumOfSquares += sumOfSquaredErrorsOfIssueValues[i];
|
---|
237 | }
|
---|
238 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
239 | eachIssueAndItsUtilityMap.put(i, sumOfSquaredErrorsOfIssueValues[i] / totalOfsumOfSquares);
|
---|
240 | }
|
---|
241 |
|
---|
242 | System.out.println("----------------------AGENT------------------");
|
---|
243 | printIntDoubleMap(eachIssueAndItsUtilityMap);
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void printIntDoubleDoubleArrMap(Map<Integer, double[][]> eachIssueAnItsValuesDoubleArrayMap) {
|
---|
247 | System.out.println("EACH ISSUE AND ITS VALUES");
|
---|
248 | for (Entry<Integer, double[][]> entry : eachIssueAnItsValuesDoubleArrayMap.entrySet()) {
|
---|
249 | System.out.println(entry.getKey() + " ");
|
---|
250 | double[][] values = entry.getValue();
|
---|
251 | for (int j = 0; j < values.length; j++) {
|
---|
252 | for (int k = 0; k < values.length; k++) {
|
---|
253 | System.out.print(values[j][k] + " ");
|
---|
254 | }
|
---|
255 | System.out.println();
|
---|
256 | }
|
---|
257 | System.out.println();
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | private void printIntDoubleArrMap(Map<Integer, double[]> map) {
|
---|
262 | System.out.println("EACH ISSUE AND ITS VALUES UTILITIES");
|
---|
263 | for (Entry<Integer, double[]> entry : map.entrySet()) {
|
---|
264 | System.out.print(entry.getKey() + " ");
|
---|
265 | double[] values = entry.getValue();
|
---|
266 | for (int j = 0; j < values.length; j++) {
|
---|
267 | System.out.print(values[j] + " ");
|
---|
268 | }
|
---|
269 | System.out.println();
|
---|
270 | }
|
---|
271 | System.out.println();
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void printIntDoubleMap(Map<Integer, Double> map) {
|
---|
275 | System.out.println("----------------------EACH ISSUE AND ITS UTILITIES------------------");
|
---|
276 | for (Entry<Integer, Double> entry : map.entrySet()) {
|
---|
277 | System.out.println(entry.getKey() + " " + entry.getValue());
|
---|
278 | }
|
---|
279 | System.out.println();
|
---|
280 | }
|
---|
281 |
|
---|
282 | @Override
|
---|
283 | public void receiveMessage(AgentID sender, Action action) {
|
---|
284 | try {
|
---|
285 | super.receiveMessage(sender, action);
|
---|
286 | if (action instanceof Offer) {
|
---|
287 | lastReceivedBid = ((Offer) action).getBid();
|
---|
288 | updateFrequencyArrays();
|
---|
289 | updateIssueValueUtilities();
|
---|
290 | fillOpponentMatriceOfSumOfSquaresOfEachIssue();
|
---|
291 | updateIsseUtilities();
|
---|
292 | }
|
---|
293 | } catch (Exception e) {
|
---|
294 | e.printStackTrace();
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | private void updateFrequencyArrays() {
|
---|
299 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
300 | ValueDiscrete valueDiscrete = (ValueDiscrete) (lastReceivedBid.getValue(i + 1));
|
---|
301 | int valueIndex = getIndexOfValueInIssue(lastReceivedBid, i, valueDiscrete.getValue());
|
---|
302 | eachIssueAndItsValuesFrequencyArrayMap.get(i)[valueIndex] += 1;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | private void updateIssueValueUtilities() {
|
---|
307 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
308 | double[] frequencyArr = eachIssueAndItsValuesFrequencyArrayMap.get(i);
|
---|
309 | double[] utilityArr = new double[frequencyArr.length];
|
---|
310 | for (int j = 0; j < frequencyArr.length; j++) {
|
---|
311 | double freqSumOfEachIssue = getSumOfRowInOneDimensionalMatrix(frequencyArr);
|
---|
312 | utilityArr[j] = ((double) frequencyArr[j] / freqSumOfEachIssue);
|
---|
313 | }
|
---|
314 | opponentEachIssueAndItsValuesUtilityMap.put(i, utilityArr);
|
---|
315 | }
|
---|
316 | System.out.println("------------OPPONENT ISSUE VALUE UTULITIES------------");
|
---|
317 | printIntDoubleArrMap(opponentEachIssueAndItsValuesUtilityMap);
|
---|
318 | }
|
---|
319 |
|
---|
320 | private void fillOpponentMatriceOfSumOfSquaresOfEachIssue() {
|
---|
321 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
322 | double[] frequencyArr = eachIssueAndItsValuesFrequencyArrayMap.get(i);
|
---|
323 | double averageOfMatrix = getAverageOfOneDimensionalMatrix(frequencyArr);
|
---|
324 | double sumOfSquaresForMatrix = getSumOfSquaredErrorsForMatrix(frequencyArr, averageOfMatrix);
|
---|
325 | opponentSumOfSquaredErrosOfEachIssueMatrix[i] = sumOfSquaresForMatrix / frequencyArr.length;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | private double getAverageOfOneDimensionalMatrix(double[] matrix) {
|
---|
330 | return getSumOfRowInOneDimensionalMatrix(matrix) / (matrix.length);
|
---|
331 | }
|
---|
332 |
|
---|
333 | private double getSumOfRowInOneDimensionalMatrix(double[] matrix) {
|
---|
334 | double rowSum = 0;
|
---|
335 | for (int i = 0; i < matrix.length; i++) {
|
---|
336 | rowSum += matrix[i];
|
---|
337 | }
|
---|
338 | return rowSum;
|
---|
339 | }
|
---|
340 |
|
---|
341 | private double getSumOfSquaredErrorsForMatrix(double[] frequencyArr, double averageOfMatrix) {
|
---|
342 | double totalOfsumOfSquares = 0;
|
---|
343 | for (int j = 0; j < frequencyArr.length; j++) {
|
---|
344 | totalOfsumOfSquares += Math.pow((frequencyArr[j] - averageOfMatrix), 2);
|
---|
345 | }
|
---|
346 | return Math.sqrt(totalOfsumOfSquares);
|
---|
347 | }
|
---|
348 |
|
---|
349 | private void updateIsseUtilities() {
|
---|
350 | double totalOfsumOfSquares = 0;
|
---|
351 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
352 | totalOfsumOfSquares += opponentSumOfSquaredErrosOfEachIssueMatrix[i];
|
---|
353 | }
|
---|
354 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
355 | opponentEachIssueAndItsUtilityMap.put(i,
|
---|
356 | opponentSumOfSquaredErrosOfEachIssueMatrix[i] / totalOfsumOfSquares);
|
---|
357 | }
|
---|
358 | System.out.println("---------OPPONENT ISSUE UTILITIES-----------");
|
---|
359 | printIntDoubleMap(opponentEachIssueAndItsUtilityMap);
|
---|
360 | }
|
---|
361 |
|
---|
362 | @Override
|
---|
363 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
364 | try {
|
---|
365 | double agentUtility = getUtilityForBid(eachIssueAndItsValuesUtilityMap, eachIssueAndItsUtilityMap,
|
---|
366 | lastReceivedBid);
|
---|
367 | double agentUtilityThatAgentOkayLastTime = getUtilityForBid(eachIssueAndItsValuesUtilityMap,
|
---|
368 | eachIssueAndItsUtilityMap, lastOfferedBid);
|
---|
369 | double oppoentUtility = getUtilityForBid(opponentEachIssueAndItsValuesUtilityMap,
|
---|
370 | opponentEachIssueAndItsUtilityMap, lastReceivedBid);
|
---|
371 | double oppoentUtilityThatItsNotOkay = getUtilityForBid(opponentEachIssueAndItsValuesUtilityMap,
|
---|
372 | opponentEachIssueAndItsUtilityMap, lastOfferedBid);
|
---|
373 |
|
---|
374 | List<Bid> nextOnes = getOfferingBidsWithMostImportantIssueIsFixed();
|
---|
375 | nextBid = getBestBidInsidePossibleBids(nextOnes);
|
---|
376 | double agentNextUtility = getUtilityForBid(eachIssueAndItsValuesUtilityMap, eachIssueAndItsUtilityMap,
|
---|
377 | nextBid);
|
---|
378 |
|
---|
379 | double offset = timeline instanceof DiscreteTimeline ? 1d / ((DiscreteTimeline) timeline).getTotalRounds()
|
---|
380 | : 0d;
|
---|
381 | double time = timeline.getTime() - offset;
|
---|
382 | if (time > HALF_OF_TIME) {
|
---|
383 | System.out.println(time);
|
---|
384 | }
|
---|
385 |
|
---|
386 | // yeni gönderdiği bi sonraki göndereceğimden daha iyiyse // agentNextUtility <=
|
---|
387 | // agentUtility
|
---|
388 | // en son gonderdiğinden benim için daha iyi bir teklifle gelmişse //
|
---|
389 | // agentUtilityThatAgentOkayLastTime <= agentUtility
|
---|
390 | // en son gönderdiğinden kendisi için daha kötü bir teklifle gelmişse //
|
---|
391 | // oppoentUtility <= oppoentUtilityThatItsNotOkay
|
---|
392 | if (agentNextUtility <= agentUtility && agentUtilityThatAgentOkayLastTime <= agentUtility
|
---|
393 | && oppoentUtility <= oppoentUtilityThatItsNotOkay
|
---|
394 | && utilitySpace.getReservationValue() < agentUtility) {
|
---|
395 | return new Accept(getPartyId(), lastReceivedBid);
|
---|
396 | }
|
---|
397 | return createOffer(validActions, time);
|
---|
398 | } catch (Exception e) {
|
---|
399 | lastOfferedBid = generateBestBid();
|
---|
400 | return new Offer(getPartyId(), lastOfferedBid);
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | private Action createOffer(List<Class<? extends Action>> validActions, double time) {
|
---|
405 | if (lastReceivedBid == null || !validActions.contains(Accept.class)) {
|
---|
406 | lastOfferedBid = generateBestBid();
|
---|
407 | return new Offer(getPartyId(), lastOfferedBid);
|
---|
408 | } else {
|
---|
409 | if (time < HALF_OF_TIME) {
|
---|
410 | lastOfferedBid = generateBestBid();
|
---|
411 | return new Offer(getPartyId(), lastOfferedBid);
|
---|
412 | } else {
|
---|
413 | String currentTimeStr = String.valueOf(time);
|
---|
414 | String lastOfferingTimeStr = String.valueOf(lastOfferingTime);
|
---|
415 | String afterDotCurrentStr = currentTimeStr.substring(2, currentTimeStr.length());
|
---|
416 | String afterDotLastOfferingStr = lastOfferingTimeStr.substring(2, lastOfferingTimeStr.length());
|
---|
417 | // in 0.6, 0.7, 0.8, 0.9, send best bit to confuse opponent
|
---|
418 | if(afterDotCurrentStr.charAt(0) != afterDotLastOfferingStr.charAt(0)) {
|
---|
419 | lastOfferedBid = generateBestBid();
|
---|
420 | lastOfferingTime = time;
|
---|
421 | return new Offer(getPartyId(), lastOfferedBid);
|
---|
422 | } else {
|
---|
423 | List<Bid> offeringBidsWithMostImportantIssueIsFixed = getOfferingBidsWithMostImportantIssueIsFixed();
|
---|
424 | lastOfferedBid = getBestBidInsidePossibleBids(offeringBidsWithMostImportantIssueIsFixed);
|
---|
425 | lastOfferingTime = time;
|
---|
426 | return new Offer(getPartyId(), lastOfferedBid);
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | private List<Bid> getOfferingBidsWithMostImportantIssueIsFixed() {
|
---|
433 | List<Bid> possibleOfferingBids = new ArrayList<Bid>();
|
---|
434 | List<Bid> allBidsWithoutUtilities = outcomeSpace.getAllBidsWithoutUtilities();
|
---|
435 | for (Bid bid : allBidsWithoutUtilities) {
|
---|
436 | int mostImportantIssueForOpponent = getMostImportantIssueIndexForOpponent();
|
---|
437 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
438 | // if most important issue is the one we are looking for
|
---|
439 | if (mostImportantIssueForOpponent == i) {
|
---|
440 | // if issue values are the same in any bid and last received
|
---|
441 | // then add this any bid to possible bid.
|
---|
442 | ValueDiscrete anyBidValue = (ValueDiscrete) (bid.getValue(i + 1));
|
---|
443 |
|
---|
444 | int anyBidValueIndex = getIndexOfValueInIssue(bid, i, anyBidValue.getValue());
|
---|
445 |
|
---|
446 | ValueDiscrete lastReceivedValue = (ValueDiscrete) (lastReceivedBid.getValue(i + 1));
|
---|
447 | int lastReceivedValueIndex = getIndexOfValueInIssue(lastReceivedBid, i,
|
---|
448 | lastReceivedValue.getValue());
|
---|
449 |
|
---|
450 | if (anyBidValueIndex == lastReceivedValueIndex) {
|
---|
451 | if (bid.countEqualValues(lastReceivedBid) == 1) {
|
---|
452 | possibleOfferingBids.add(bid);
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 | return possibleOfferingBids;
|
---|
459 | }
|
---|
460 |
|
---|
461 | // returns 0, 1, 2....
|
---|
462 | private int getMostImportantIssueIndexForOpponent() {
|
---|
463 | int biggestIssueNumberForOpponent = 0;
|
---|
464 | double biggestIssueUtility = 0;
|
---|
465 | for (Entry<Integer, Double> entry : opponentEachIssueAndItsUtilityMap.entrySet()) {
|
---|
466 | if (biggestIssueUtility < entry.getValue()) {
|
---|
467 | biggestIssueUtility = entry.getValue();
|
---|
468 | biggestIssueNumberForOpponent = entry.getKey();
|
---|
469 | }
|
---|
470 | }
|
---|
471 | List<Integer> mostImportantIssueIndexes = new ArrayList<Integer>();
|
---|
472 | for (Entry<Integer, Double> entry : opponentEachIssueAndItsUtilityMap.entrySet()) {
|
---|
473 | if (entry.getValue() == biggestIssueUtility) {
|
---|
474 | mostImportantIssueIndexes.add(entry.getKey());
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (mostImportantIssueIndexes.size() == 1) {
|
---|
479 | return biggestIssueNumberForOpponent;
|
---|
480 | } else {
|
---|
481 | return getMostImportantIssueIndexForAgent(mostImportantIssueIndexes);
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | private int getMostImportantIssueIndexForAgent(List<Integer> mostImportantIssueIndexes) {
|
---|
486 | int biggestIssueNumberForAgent = 0;
|
---|
487 | double biggestIssueUtilityForAgent = 0;
|
---|
488 | for (Entry<Integer, Double> entry : eachIssueAndItsUtilityMap.entrySet()) {
|
---|
489 | for (Integer mostImportantIssueIndexForOpponent : mostImportantIssueIndexes) {
|
---|
490 | if (mostImportantIssueIndexForOpponent == entry.getKey()) {
|
---|
491 | if (biggestIssueUtilityForAgent < entry.getValue()) {
|
---|
492 | biggestIssueUtilityForAgent = entry.getValue();
|
---|
493 | biggestIssueNumberForAgent = entry.getKey();
|
---|
494 | }
|
---|
495 | }
|
---|
496 | }
|
---|
497 | }
|
---|
498 | return biggestIssueNumberForAgent;
|
---|
499 | }
|
---|
500 |
|
---|
501 | private Bid getBestBidInsidePossibleBids(List<Bid> possibleBids) {
|
---|
502 | Bid bestBid = null;
|
---|
503 | double bestUtility = 0;
|
---|
504 | for (Bid bid : possibleBids) {
|
---|
505 | double agentUtility = getUtilityForBid(eachIssueAndItsValuesUtilityMap, eachIssueAndItsUtilityMap, bid);
|
---|
506 | if (bestUtility <= agentUtility) {
|
---|
507 | bestBid = bid;
|
---|
508 | bestUtility = agentUtility;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | return bestBid;
|
---|
512 | }
|
---|
513 |
|
---|
514 | private Bid generateBestBid() {
|
---|
515 | List<Bid> possibleBids = outcomeSpace.getAllBidsWithoutUtilities();
|
---|
516 | Bid bestBid = null;
|
---|
517 | double bestUtility = 0;
|
---|
518 | for (Bid bid : possibleBids) {
|
---|
519 | double agentUtility = getUtilityForBid(eachIssueAndItsValuesUtilityMap, eachIssueAndItsUtilityMap, bid);
|
---|
520 | if (bestUtility <= agentUtility) {
|
---|
521 | bestBid = bid;
|
---|
522 | bestUtility = agentUtility;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | return bestBid;
|
---|
526 | }
|
---|
527 |
|
---|
528 | private double getUtilityForBid(Map<Integer, double[]> valueUtilityMap, Map<Integer, Double> issueUtilityMap,
|
---|
529 | Bid bid) {
|
---|
530 | double totalUtility = 0;
|
---|
531 | if (bid != null) {
|
---|
532 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
533 | Double utilityOfIssue = issueUtilityMap.get(i);
|
---|
534 | ValueDiscrete biggerBidValue = (ValueDiscrete) (bid.getValue(i + 1));
|
---|
535 | int indexOfValue = getIndexOfValueInIssue(bid, i, biggerBidValue.getValue());
|
---|
536 | double[] valueUtilities = valueUtilityMap.get(i);
|
---|
537 | double utilityOfValue = valueUtilities[indexOfValue];
|
---|
538 |
|
---|
539 | totalUtility += utilityOfIssue * utilityOfValue;
|
---|
540 | }
|
---|
541 | return totalUtility;
|
---|
542 | }
|
---|
543 | return totalUtility;
|
---|
544 | }
|
---|
545 |
|
---|
546 | @Override
|
---|
547 | public AbstractUtilitySpace estimateUtilitySpace() {
|
---|
548 | return new AdditiveUtilitySpaceFactory(getDomain()).getUtilitySpace();
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | public String getDescription() {
|
---|
553 | return "ANAC 2019";
|
---|
554 | }
|
---|
555 |
|
---|
556 | }
|
---|