source: src/main/java/agents/qoagent2/QAgentsCore.java

Last change on this file 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: 85.0 KB
RevLine 
[127]1package agents.qoagent2;
2
3import java.io.FileWriter;
4import java.io.IOException;
5import java.io.PrintWriter;
6import java.util.ArrayList;
7import java.util.Random;
8
9import genius.core.issue.ValueDiscrete;
10import genius.core.utility.AdditiveUtilitySpace;
11import genius.core.utility.EvaluatorDiscrete;
12
13
14/*
15 * Created on 11/09/2004
16 *
17 */
18
19/**
20 * @author raz
21 * @version 1.0
22 *
23 * QAgentsCore class:
24 * In charge of handling the different agent's types.
25 * In charge for returning the agreements for the desired type.
26 *
27 * @see QOAgent
28 * @see QAgentType
29 */
30
31public class QAgentsCore {
32 //++public static final double NORMALIZE_INCREMENTOR = 20;//TODO: Currently not using normalize incrementor
33
34 public static final String COMMENT_CHAR_STR = "#";
35 public static final String ISSUE_HEADER_STR = "!";
36 public static final String ISSUE_SEPARATOR_STR = "*";
37 public static final String VALUES_UTILITY_SEPARATOR_STR = " ";
38 public static final String VALUES_NAMES_SEPARATOR_STR = "~";
39 public static final String GENERAL_DATA_SEPARATOR_STR = "@";
40 public static final String TIME_EFFECT_STR = "Time-Effect";
41 public static final String OPT_OUT_STR = "Opt-Out";
42 public static final String STATUS_QUO_STR = "Status-Quo";
43 public static final int TIME_EFFECT_IND = 0;
44 public static final int OPT_OUT_IND = 1;
45 public static final int STATUS_QUO_IND = 2;
46 public static final int GENERAL_VALUES_NUM = 3;
47
48
49 public static final int LONG_TERM_TYPE_IDX = 0;
50 public static final int SHORT_TERM_TYPE_IDX = 1;
51 public static final int COMPROMISE_TYPE_IDX = 2;
52 public static final int AGENT_TYPES_NUM = 3;
53
54 // list of all possible england types - each value is FullUtility
55 private ArrayList<QAgentType> m_EnglandAgentTypesList;
56 // list of all possible zimbabwe types - each value is FullUtility
57 private ArrayList<QAgentType> m_ZimbabweAgentTypesList;
58
59 // list of all possible england types - each value is FullUtility
60 // values for the next turn
61 private ArrayList<QAgentType> m_EnglandAgentTypesNextTurnList;
62 // list of all possible zimbabwe types - each value is FullUtility
63 // values for the next turn
64 private ArrayList<QAgentType> m_ZimbabweAgentTypesNextTurnList;
65
66
67 private QAgentType m_CurrentAgentType;
68 private QAgentType m_CurrentAgentNextTurnType;
69
70 private int m_nNextTurnOppType;
71
72 private String m_sLogFileName;
73 private String m_sProbFileName;
74
75 // inner class for calculating QO agreement
76 private QGenerateAgreement m_GenerateAgreement;
77 private boolean m_bEquilibriumAgent = false;
78
79
80 private agents.QOAgent m_Agent;
81
82 public class QGenerateAgreement
83 {
84 class QCombinedAgreement
85 {
86 public double m_dAgentAgreementValue;
87 public double m_dOpponentAgreementValue;
88 public String m_sAgreement;
89 }
90
91 // selected agreement values
92 private double m_dQOValue, m_dNextTurnQOValue, m_dAgentSelectedValue, m_dAgentSelectedNextTurnValue, m_dOppSelectedValue, m_dOppSelectedNextTurnValue;
93 private double m_dFirstEquilibriumValue[], m_dFirstEquilibriumValueNextTurn[];
94 private double m_dSecondEquilibriumValue[], m_dSecondEquilibriumValueNextTurn[];
95 private double m_dEquilibriumValue, m_dNextTurnEquilibriumValue, m_dNextTurnCurrentAgentValueForEqOffer;
96 private String m_sAgreement, m_sNextTurnAgreement;
97 private String m_sFirstEquilibriumAgreement[];
98 private String m_sFirstEquilibriumAgreementNextTurn[];
99 private String m_sSecondEquilibriumAgreement[];
100 private String m_sSecondEquilibriumAgreementNextTurn[];
101 private String m_sEquilibriumAgreement, m_sNextTurnEquilibriumAgreement;
102
103 private boolean m_bCalcNashAgreement;
104
105 private double m_dBelievedThreshold;
106
107 public static final double BELIEVED_THRESHOLD_VAR = 0.1; //TODO: Change threshold
108 public static final boolean CALC_NASH_AGREEMENT_VAR = false;
109
110 public static final int FIRST_EQUILIBRIUM = 1;
111 public static final int SECOND_EQUILIBRIUM = 2;
112 private static final int OFFER_SET_SIZE = 4;
113
114 public QGenerateAgreement()
115 {
116 m_dQOValue = QAgentType.VERY_SMALL_NUMBER;
117 m_dNextTurnQOValue = QAgentType.VERY_SMALL_NUMBER;
118
119 m_nNextTurnOppType = QAgentType.NO_TYPE;
120
121 m_sAgreement = "";
122 m_sNextTurnAgreement = "";
123
124 m_dBelievedThreshold = BELIEVED_THRESHOLD_VAR;
125 m_bCalcNashAgreement = CALC_NASH_AGREEMENT_VAR;
126
127 m_sEquilibriumAgreement = "";
128 m_sNextTurnEquilibriumAgreement = "";
129 m_dEquilibriumValue = QAgentType.VERY_SMALL_NUMBER;
130 m_dNextTurnEquilibriumValue = QAgentType.VERY_SMALL_NUMBER;
131 m_dNextTurnCurrentAgentValueForEqOffer = QAgentType.VERY_SMALL_NUMBER;
132
133 m_sFirstEquilibriumAgreement = new String[AGENT_TYPES_NUM + 1];
134 m_sFirstEquilibriumAgreementNextTurn = new String[AGENT_TYPES_NUM + 1];
135 m_sSecondEquilibriumAgreement = new String[AGENT_TYPES_NUM + 1];
136 m_sSecondEquilibriumAgreementNextTurn = new String[AGENT_TYPES_NUM + 1];
137
138 m_dFirstEquilibriumValue = new double[AGENT_TYPES_NUM];
139 m_dFirstEquilibriumValueNextTurn = new double[AGENT_TYPES_NUM];
140 m_dSecondEquilibriumValue = new double[AGENT_TYPES_NUM];
141 m_dSecondEquilibriumValueNextTurn = new double[AGENT_TYPES_NUM];
142
143 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
144 {
145 m_sFirstEquilibriumAgreement[i] = "";
146 m_sFirstEquilibriumAgreementNextTurn[i] = "";
147 m_sSecondEquilibriumAgreement[i] = "";
148 m_sSecondEquilibriumAgreementNextTurn[i] = "";
149 m_dFirstEquilibriumValue[i] = QAgentType.VERY_SMALL_NUMBER;
150 m_dFirstEquilibriumValueNextTurn[i] = QAgentType.VERY_SMALL_NUMBER;
151 m_dSecondEquilibriumValue[i] = QAgentType.VERY_SMALL_NUMBER;
152 m_dSecondEquilibriumValueNextTurn[i] = QAgentType.VERY_SMALL_NUMBER;
153 }
154 }
155
156 // PRE-CONDITION: m_CurrentAgentType should be updated for the current turn
157 public void calculateAgreement(QAgentType agentType, int nCurrentTurn, boolean bCalcForNextTurn)
158 {
159 if (bCalcForNextTurn)
160 m_CurrentAgentNextTurnType = agentType;
161 else
162 m_CurrentAgentType = agentType;
163
164 if (m_CurrentAgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
165 calculateOfferAgainstOpponent("England", nCurrentTurn, bCalcForNextTurn);
166 else if (m_CurrentAgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
167 calculateOfferAgainstOpponent("Zimbabwe", nCurrentTurn, bCalcForNextTurn);
168 else
169 {
170 System.out.println("[QO]Agent type is unknown [QAgentsCore::calculateAgreement(204)]");
171 System.err.println("[QO]Agent type is unknown [QAgentsCore::calculateAgreement(204)]");
172 }
173 }
174
175 public void calculateOfferAgainstOpponent(String sOpponentType, int nCurrentTurn, boolean bCalcForNextTurn)
176 {
177 try {
178 PrintWriter bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
179
180
181 QAgentType agentOpponentCompromise = null;
182 QAgentType agentOpponentLongTerm = null;
183 QAgentType agentOpponentShortTerm = null;
184
185 m_dQOValue = QAgentType.VERY_SMALL_NUMBER;
186 m_dNextTurnQOValue = QAgentType.VERY_SMALL_NUMBER;
187
188 m_nNextTurnOppType = QAgentType.NO_TYPE;
189
190 int nIssuesNum = m_CurrentAgentType.getIssuesNum();
191
192 //06-05-06
193 int OpponentShortTermIdx[][] = new int[OFFER_SET_SIZE][nIssuesNum];
194 int OpponentLongTermIdx[][] = new int[OFFER_SET_SIZE][nIssuesNum];
195 int OpponentCompromiseIdx[][] = new int[OFFER_SET_SIZE][nIssuesNum];
196
197
198 double /*06-05-06 dOpponentShortTermQOValue = QAgentType.VERY_SMALL_NUMBER, */dOpponentShortTermAgreementValue = QAgentType.VERY_SMALL_NUMBER, dOpponentShortTermLuceAgreementValue = 0;
199 double /*06-05-06 dOpponentLongTermQOValue = QAgentType.VERY_SMALL_NUMBER, */dOpponentLongTermAgreementValue = QAgentType.VERY_SMALL_NUMBER, dOpponentLongTermLuceAgreementValue = 0;
200 double /*06-05-06 dOpponentCompromiseQOValue = QAgentType.VERY_SMALL_NUMBER, */dOpponentCompromiseAgreementValue = QAgentType.VERY_SMALL_NUMBER, dOpponentCompromiseLuceAgreementValue = 0;
201 double dQOValue = QAgentType.VERY_SMALL_NUMBER;
202
203 //06-05-06
204 double dOpponentShortTermQOValue[] = new double[OFFER_SET_SIZE];
205 double dOpponentLongTermQOValue[] = new double[OFFER_SET_SIZE];
206 double dOpponentCompromiseQOValue[] = new double[OFFER_SET_SIZE];
207
208 for (int i = 0; i < dOpponentShortTermQOValue.length; ++i) {
209 dOpponentShortTermQOValue[i] = QAgentType.VERY_SMALL_NUMBER;
210 dOpponentLongTermQOValue[i] = QAgentType.VERY_SMALL_NUMBER;
211 dOpponentCompromiseQOValue[i] = QAgentType.VERY_SMALL_NUMBER;
212 }
213
214 // nash variables
215 double dCurrentNashValue = QAgentType.VERY_SMALL_NUMBER;
216 double dOpponentCompromiseNashValue = QAgentType.VERY_SMALL_NUMBER;
217 double dOpponentLongTermNashValue = QAgentType.VERY_SMALL_NUMBER;
218 double dOpponentShortTermNashValue = QAgentType.VERY_SMALL_NUMBER;
219
220 double dBelievedTypeOpponentShortTerm = 0;
221 double dBelievedTypeOpponentLongTerm = 0;
222 double dBelievedTypeOpponentCompromise = 0;
223
224 boolean bCalcOpponentShortTerm = false;
225 boolean bCalcOpponentLongTerm = false;
226 boolean bCalcOpponentCompromise = false;
227
228 double dCurrentAgentAgreementValue = QAgentType.VERY_SMALL_NUMBER, dCurrentAgentLuceAgreementValue = QAgentType.VERY_SMALL_NUMBER;
229
230 int index = -1;//raz 06-05-06
231
232 if (sOpponentType.equals("England"))
233 {
234 if (bCalcForNextTurn)
235 {
236 agentOpponentCompromise = getEnglandCompromiseNextTurnType();
237 agentOpponentLongTerm = getEnglandLongTermNextTurnType();
238 agentOpponentShortTerm = getEnglandShortTermNextTurnType();
239 }
240 else
241 {
242 agentOpponentCompromise = getEnglandCompromiseType();
243 agentOpponentLongTerm = getEnglandLongTermType();
244 agentOpponentShortTerm = getEnglandShortTermType();
245 }
246 }
247 else if (sOpponentType.equals("Zimbabwe"))
248 {
249 if (bCalcForNextTurn)
250 {
251 agentOpponentCompromise = getZimbabweCompromiseNextTurnType();
252 agentOpponentLongTerm = getZimbabweLongTermNextTurnType();
253 agentOpponentShortTerm = getZimbabweShortTermNextTurnType();
254 }
255 else
256 {
257 agentOpponentCompromise = getZimbabweCompromiseType();
258 agentOpponentLongTerm = getZimbabweLongTermType();
259 agentOpponentShortTerm = getZimbabweShortTermType();
260 }
261 }
262 else
263 {
264 System.out.println("[QO]Agent type is unknown [QAgentsCore::calculateOfferAgainstOpponent(291)]");
265 System.err.println("[QO]Agent type is unknown [QAgentsCore::calculateOfferAgainstOpponent(291)]");
266 return;
267 }
268
269 dBelievedTypeOpponentCompromise = agentOpponentCompromise.getTypeProbability();
270 dBelievedTypeOpponentLongTerm = agentOpponentLongTerm.getTypeProbability();
271 dBelievedTypeOpponentShortTerm = agentOpponentShortTerm.getTypeProbability();
272
273 if (dBelievedTypeOpponentCompromise > dBelievedTypeOpponentLongTerm)
274 {
275 if (dBelievedTypeOpponentCompromise > dBelievedTypeOpponentShortTerm)
276 {
277 bCalcOpponentCompromise = true;
278
279 if (dBelievedTypeOpponentCompromise - dBelievedTypeOpponentShortTerm < m_dBelievedThreshold)
280 bCalcOpponentShortTerm = true;
281 if (dBelievedTypeOpponentCompromise - dBelievedTypeOpponentLongTerm < m_dBelievedThreshold)
282 bCalcOpponentLongTerm = true;
283 }
284 else
285 {
286 bCalcOpponentShortTerm = true;
287
288 if (dBelievedTypeOpponentShortTerm - dBelievedTypeOpponentCompromise < m_dBelievedThreshold)
289 bCalcOpponentCompromise = true;
290 if (dBelievedTypeOpponentShortTerm - dBelievedTypeOpponentLongTerm < m_dBelievedThreshold)
291 bCalcOpponentLongTerm = true;
292 }
293 }
294 else
295 {
296 if (dBelievedTypeOpponentLongTerm > dBelievedTypeOpponentShortTerm)
297 {
298 bCalcOpponentLongTerm = true;
299
300 if (dBelievedTypeOpponentLongTerm - dBelievedTypeOpponentShortTerm < m_dBelievedThreshold)
301 bCalcOpponentShortTerm = true;
302 if (dBelievedTypeOpponentLongTerm - dBelievedTypeOpponentCompromise < m_dBelievedThreshold)
303 bCalcOpponentCompromise = true;
304 }
305 else
306 {
307 bCalcOpponentShortTerm = true;
308
309 if (dBelievedTypeOpponentShortTerm - dBelievedTypeOpponentCompromise < m_dBelievedThreshold)
310 bCalcOpponentCompromise = true;
311 if (dBelievedTypeOpponentShortTerm - dBelievedTypeOpponentLongTerm < m_dBelievedThreshold)
312 bCalcOpponentLongTerm = true;
313 }
314 }
315
316 int CurrentAgreementIdx[] = new int[nIssuesNum];
317 int MaxIssueValues[] = new int[nIssuesNum];
318
319 for (int i = 0; i < nIssuesNum; ++i)
320 {
321 CurrentAgreementIdx[i] = 0;
322 MaxIssueValues[i] = m_CurrentAgentType.getMaxIssueValue(i);
323 }
324
325 int nTotalAgreements = m_CurrentAgentType.getTotalAgreements();
326
327 for (int i = 0; i < nTotalAgreements; ++i)
328 {
329 // calculate agreements
330 if (bCalcForNextTurn)
331 {
332 //22-09-05
333 // dCurrentAgentAgreementValue = m_CurrentAgentNextTurnType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
334 dCurrentAgentAgreementValue = m_CurrentAgentNextTurnType.getAgreementRankingProbability(CurrentAgreementIdx);
335 double agreementUtilityValue = m_CurrentAgentNextTurnType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
336 dCurrentAgentLuceAgreementValue = m_CurrentAgentNextTurnType.getAgreementLuceValue(agreementUtilityValue);
337
338 //25-09-05
339 // take into account only agreements that worth more than the status quo
340 if (agreementUtilityValue <= m_CurrentAgentNextTurnType.getSQValue()) {
341// receiveMessage issue values indices
342 boolean bFinishUpdate = false;
343 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
344 {
345 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
346 {
347 CurrentAgreementIdx[k] = 0;
348 }
349 else
350 {
351 CurrentAgreementIdx[k]++;
352 bFinishUpdate = true;
353 }
354 }
355
356 continue;
357
358 }
359
360 //06-05-06
361 // take into account only agreements that are less than the X best agreements
362 // (as time passes, the chances of them being accepted are lower)
363 /*if (agreementUtilityValue > m_CurrentAgentNextTurnType.getMaxValue()) {
364// receiveMessage issue values indices
365 boolean bFinishUpdate = false;
366 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
367 {
368 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
369 {
370 CurrentAgreementIdx[k] = 0;
371 }
372 else
373 {
374 CurrentAgreementIdx[k]++;
375 bFinishUpdate = true;
376 }
377 }
378
379 continue;
380
381 }
382 */
383 }
384 else
385 {
386 //22-09-05
387 //dCurrentAgentAgreementValue = m_CurrentAgentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
388 dCurrentAgentAgreementValue = m_CurrentAgentType.getAgreementRankingProbability(CurrentAgreementIdx);
389 double agreementUtilityValue = m_CurrentAgentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
390 dCurrentAgentLuceAgreementValue = m_CurrentAgentType.getAgreementLuceValue(agreementUtilityValue);
391
392 //25-09-05
393 if (agreementUtilityValue <= m_CurrentAgentType.getSQValue()) {
394
395// receiveMessage issue values indices
396 boolean bFinishUpdate = false;
397 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
398 {
399 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
400 {
401 CurrentAgreementIdx[k] = 0;
402 }
403 else
404 {
405 CurrentAgreementIdx[k]++;
406 bFinishUpdate = true;
407 }
408 }
409 continue;
410 }
411 //06-05-06
412 // take into account only agreements that are less than the X best agreements
413 // (as time passes, the chances of them being accepted are lower)
414 /*if (agreementUtilityValue > m_CurrentAgentType.getMaxValue()) {
415// receiveMessage issue values indices
416 boolean bFinishUpdate = false;
417 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
418 {
419 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
420 {
421 CurrentAgreementIdx[k] = 0;
422 }
423 else
424 {
425 CurrentAgreementIdx[k]++;
426 bFinishUpdate = true;
427 }
428 }
429
430 continue;
431
432 }
433 */
434 }
435
436 bw.println("----------------------------------------");
437 bw.println("Agreement: " + m_CurrentAgentType.getAgreementStr(CurrentAgreementIdx) + "(turn: " + nCurrentTurn + ")");
438 bw.println("Agreement Value: " + dCurrentAgentAgreementValue);
439 bw.println("Agreement Luce: " + dCurrentAgentLuceAgreementValue);
440
441 if (bCalcOpponentCompromise)
442 {
443 //22-09-05
444 //dOpponentCompromiseAgreementValue = agentOpponentCompromise.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
445 dOpponentCompromiseAgreementValue = agentOpponentCompromise.getAgreementRankingProbability(CurrentAgreementIdx);
446 double agreementUtilityValue = agentOpponentCompromise.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
447 dOpponentCompromiseLuceAgreementValue = agentOpponentCompromise.getAgreementLuceValue(agreementUtilityValue);
448
449 //25-09-05
450 if (agreementUtilityValue <= agentOpponentCompromise.getSQValue()) {
451// receiveMessage issue values indices
452 boolean bFinishUpdate = false;
453 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
454 {
455 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
456 {
457 CurrentAgreementIdx[k] = 0;
458 }
459 else
460 {
461 CurrentAgreementIdx[k]++;
462 bFinishUpdate = true;
463 }
464 }
465
466 continue; // don't compute this agreement
467
468 }
469
470 bw.println("CompOpponent Value: " + dOpponentCompromiseAgreementValue);
471 bw.println("CompOpponent Luce: " + dOpponentCompromiseLuceAgreementValue);
472
473 dQOValue = Math.min(
474 dCurrentAgentAgreementValue * dCurrentAgentLuceAgreementValue, //@@,
475 dOpponentCompromiseAgreementValue * (dOpponentCompromiseLuceAgreementValue + dCurrentAgentLuceAgreementValue)
476 );
477
478 bw.println("CompQO Value: " + dQOValue);
479
480 index = 0;
481 if (dQOValue > dOpponentCompromiseQOValue[0])
482 {
483 if (dQOValue > dOpponentCompromiseQOValue[1]) {
484 index = 1;
485 if (dQOValue > dOpponentCompromiseQOValue[2]) {
486 index = 2;
487 if (dQOValue > dOpponentCompromiseQOValue[3]) {
488 index = 3;
489 }
490 }
491 }
492
493 // 06-05-06
494 // save value
495 if (index == 3) {
496 dOpponentCompromiseQOValue[0] = dOpponentCompromiseQOValue[1];
497 dOpponentCompromiseQOValue[1] = dOpponentCompromiseQOValue[2];
498 dOpponentCompromiseQOValue[2] = dOpponentCompromiseQOValue[3];
499 dOpponentCompromiseQOValue[3] = dQOValue;
500
501 for (int j = 0; j < nIssuesNum; ++j) {
502 OpponentCompromiseIdx[0][j] = OpponentCompromiseIdx[1][j];
503 OpponentCompromiseIdx[1][j] = OpponentCompromiseIdx[2][j];
504 OpponentCompromiseIdx[2][j] = OpponentCompromiseIdx[3][j];
505 OpponentCompromiseIdx[3][j] = CurrentAgreementIdx[j];
506 }
507 }
508 else if (index == 2) {
509 dOpponentCompromiseQOValue[0] = dOpponentCompromiseQOValue[1];
510 dOpponentCompromiseQOValue[1] = dOpponentCompromiseQOValue[2];
511 dOpponentCompromiseQOValue[2] = dQOValue;
512
513 for (int j = 0; j < nIssuesNum; ++j) {
514 OpponentCompromiseIdx[0][j] = OpponentCompromiseIdx[1][j];
515 OpponentCompromiseIdx[1][j] = OpponentCompromiseIdx[2][j];
516 OpponentCompromiseIdx[2][j] = CurrentAgreementIdx[j];
517 }
518 }
519 else if (index == 1) {
520 dOpponentCompromiseQOValue[0] = dOpponentCompromiseQOValue[1];
521 dOpponentCompromiseQOValue[1] = dQOValue;
522
523 for (int j = 0; j < nIssuesNum; ++j) {
524 OpponentCompromiseIdx[0][j] = OpponentCompromiseIdx[1][j];
525 OpponentCompromiseIdx[1][j] = CurrentAgreementIdx[j];
526 }
527 }
528 else { // index == 0
529 dOpponentCompromiseQOValue[0] = dQOValue;
530
531 for (int j = 0; j < nIssuesNum; ++j) {
532 OpponentCompromiseIdx[0][j] = CurrentAgreementIdx[j];
533 }
534 }
535
536 bw.println("------------SAVED VALUE--------------");
537 // save value
538 //06-05-06 dOpponentCompromiseQOValue = dQOValue;
539
540 //06-05-06
541 // save agreement
542 //for (int j = 0; j < nIssuesNum; ++j)
543 //OpponentCompromiseIdx[j] = CurrentAgreementIdx[j];
544 }
545 if (m_bCalcNashAgreement)
546 {
547 dCurrentNashValue = dOpponentCompromiseAgreementValue * dCurrentAgentAgreementValue;
548
549 if (dCurrentNashValue > dOpponentCompromiseNashValue)
550 {
551 dOpponentCompromiseNashValue = dCurrentNashValue;
552 }
553 }
554 }
555 if (bCalcOpponentLongTerm)
556 {
557 //22-09-05
558 //dOpponentLongTermAgreementValue = agentOpponentLongTerm.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
559 dOpponentLongTermAgreementValue = agentOpponentLongTerm.getAgreementRankingProbability(CurrentAgreementIdx);
560 double agreementUtilityValue = agentOpponentLongTerm.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
561 dOpponentLongTermLuceAgreementValue = agentOpponentLongTerm.getAgreementLuceValue(agreementUtilityValue);
562
563 //25-09-05
564 if (agreementUtilityValue <= agentOpponentLongTerm.getSQValue()) {
565// receiveMessage issue values indices
566 boolean bFinishUpdate = false;
567 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
568 {
569 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
570 {
571 CurrentAgreementIdx[k] = 0;
572 }
573 else
574 {
575 CurrentAgreementIdx[k]++;
576 bFinishUpdate = true;
577 }
578 }
579
580 continue; // don't compute this agreement
581 }
582
583 bw.println("LongOpponent Value: " + dOpponentLongTermAgreementValue);
584 bw.println("LongOpponent Luce: " + dOpponentLongTermLuceAgreementValue);
585
586 dQOValue = Math.min(
587 dCurrentAgentAgreementValue * dCurrentAgentLuceAgreementValue, //@@,
588 dOpponentLongTermAgreementValue * (dOpponentLongTermLuceAgreementValue + dCurrentAgentLuceAgreementValue)
589 );
590
591 bw.println("LongQO Value: " + dQOValue);
592
593 index = 0;
594 if (dQOValue > dOpponentLongTermQOValue[0])
595 {
596 if (dQOValue > dOpponentLongTermQOValue[1]) {
597 index = 1;
598 if (dQOValue > dOpponentLongTermQOValue[2]) {
599 index = 2;
600 if (dQOValue > dOpponentLongTermQOValue[3]) {
601 index = 3;
602 }
603 }
604 }
605
606 // 06-05-06
607 // save value
608 if (index == 3) {
609 dOpponentLongTermQOValue[0] = dOpponentLongTermQOValue[1];
610 dOpponentLongTermQOValue[1] = dOpponentLongTermQOValue[2];
611 dOpponentLongTermQOValue[2] = dOpponentLongTermQOValue[3];
612 dOpponentLongTermQOValue[3] = dQOValue;
613
614 for (int j = 0; j < nIssuesNum; ++j) {
615 OpponentLongTermIdx[0][j] = OpponentLongTermIdx[1][j];
616 OpponentLongTermIdx[1][j] = OpponentLongTermIdx[2][j];
617 OpponentLongTermIdx[2][j] = OpponentLongTermIdx[3][j];
618 OpponentLongTermIdx[3][j] = CurrentAgreementIdx[j];
619 }
620 }
621 else if (index == 2) {
622 dOpponentLongTermQOValue[0] = dOpponentLongTermQOValue[1];
623 dOpponentLongTermQOValue[1] = dOpponentLongTermQOValue[2];
624 dOpponentLongTermQOValue[2] = dQOValue;
625
626 for (int j = 0; j < nIssuesNum; ++j) {
627 OpponentLongTermIdx[0][j] = OpponentLongTermIdx[1][j];
628 OpponentLongTermIdx[1][j] = OpponentLongTermIdx[2][j];
629 OpponentLongTermIdx[2][j] = CurrentAgreementIdx[j];
630 }
631 }
632 else if (index == 1) {
633 dOpponentLongTermQOValue[0] = dOpponentLongTermQOValue[1];
634 dOpponentLongTermQOValue[1] = dQOValue;
635
636 for (int j = 0; j < nIssuesNum; ++j) {
637 OpponentLongTermIdx[0][j] = OpponentLongTermIdx[1][j];
638 OpponentLongTermIdx[1][j] = CurrentAgreementIdx[j];
639 }
640 }
641 else { // index == 0
642 dOpponentLongTermQOValue[0] = dQOValue;
643
644 for (int j = 0; j < nIssuesNum; ++j) {
645 OpponentLongTermIdx[0][j] = CurrentAgreementIdx[j];
646 }
647 }
648
649 bw.println("------------SAVED VALUE--------------");
650 // save value
651 //06-05-06 dOpponentLongTermQOValue = dQOValue;
652
653 //06-05-06
654 // save agreement
655 //for (int j = 0; j < nIssuesNum; ++j)
656 //OpponentLongTermIdx[j] = CurrentAgreementIdx[j];
657 }
658 if (m_bCalcNashAgreement)
659 {
660 dCurrentNashValue = dOpponentLongTermAgreementValue * dCurrentAgentAgreementValue;
661
662 if (dCurrentNashValue > dOpponentLongTermNashValue)
663 {
664 dOpponentLongTermNashValue = dCurrentNashValue;
665 }
666 }
667 }
668 if (bCalcOpponentShortTerm)
669 {
670 //22-09-05
671 //dOpponentShortTermAgreementValue = agentOpponentShortTerm.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
672 dOpponentShortTermAgreementValue = agentOpponentShortTerm.getAgreementRankingProbability(CurrentAgreementIdx);
673 double agreementUtilityValue = agentOpponentShortTerm.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
674 dOpponentShortTermLuceAgreementValue = agentOpponentShortTerm.getAgreementLuceValue(agreementUtilityValue);
675
676 //25-09-05
677 if (agreementUtilityValue <= agentOpponentShortTerm.getSQValue()) {
678// receiveMessage issue values indices
679 boolean bFinishUpdate = false;
680 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
681 {
682 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
683 {
684 CurrentAgreementIdx[k] = 0;
685 }
686 else
687 {
688 CurrentAgreementIdx[k]++;
689 bFinishUpdate = true;
690 }
691 }
692
693 continue; // don't compute this agreement
694 }
695
696 bw.println("ShortOpponent Value: " + dOpponentShortTermAgreementValue);
697 bw.println("ShortOpponent Luce: " + dOpponentShortTermLuceAgreementValue);
698
699 dQOValue = Math.min(
700 dCurrentAgentAgreementValue * dCurrentAgentLuceAgreementValue, //@@,
701 dOpponentShortTermAgreementValue * (dOpponentShortTermLuceAgreementValue + dCurrentAgentLuceAgreementValue)
702 );
703
704 bw.println("ShortQO Value: " + dQOValue);
705
706 index = 0;
707 if (dQOValue > dOpponentShortTermQOValue[0])
708 {
709 if (dQOValue > dOpponentShortTermQOValue[1]) {
710 index = 1;
711 if (dQOValue > dOpponentShortTermQOValue[2]) {
712 index = 2;
713 if (dQOValue > dOpponentShortTermQOValue[3]) {
714 index = 3;
715 }
716 }
717 }
718
719 // 06-05-06
720 // save value
721 if (index == 3) {
722 dOpponentShortTermQOValue[0] = dOpponentShortTermQOValue[1];
723 dOpponentShortTermQOValue[1] = dOpponentShortTermQOValue[2];
724 dOpponentShortTermQOValue[2] = dOpponentShortTermQOValue[3];
725 dOpponentShortTermQOValue[3] = dQOValue;
726
727 for (int j = 0; j < nIssuesNum; ++j) {
728 OpponentShortTermIdx[0][j] = OpponentShortTermIdx[1][j];
729 OpponentShortTermIdx[1][j] = OpponentShortTermIdx[2][j];
730 OpponentShortTermIdx[2][j] = OpponentShortTermIdx[3][j];
731 OpponentShortTermIdx[3][j] = CurrentAgreementIdx[j];
732 }
733 }
734 else if (index == 2) {
735 dOpponentShortTermQOValue[0] = dOpponentShortTermQOValue[1];
736 dOpponentShortTermQOValue[1] = dOpponentShortTermQOValue[2];
737 dOpponentShortTermQOValue[2] = dQOValue;
738
739 for (int j = 0; j < nIssuesNum; ++j) {
740 OpponentShortTermIdx[0][j] = OpponentShortTermIdx[1][j];
741 OpponentShortTermIdx[1][j] = OpponentShortTermIdx[2][j];
742 OpponentShortTermIdx[2][j] = CurrentAgreementIdx[j];
743 }
744 }
745 else if (index == 1) {
746 dOpponentShortTermQOValue[0] = dOpponentShortTermQOValue[1];
747 dOpponentShortTermQOValue[1] = dQOValue;
748
749 for (int j = 0; j < nIssuesNum; ++j) {
750 OpponentShortTermIdx[0][j] = OpponentShortTermIdx[1][j];
751 OpponentShortTermIdx[1][j] = CurrentAgreementIdx[j];
752 }
753 }
754 else { // index == 0
755 dOpponentShortTermQOValue[0] = dQOValue;
756
757 for (int j = 0; j < nIssuesNum; ++j) {
758 OpponentShortTermIdx[0][j] = CurrentAgreementIdx[j];
759 }
760 }
761
762 bw.println("------------SAVED VALUE--------------");
763 // save value
764 //06-05-06 dOpponentShortTermQOValue = dQOValue;
765
766 //06-05-06
767 // save agreement
768 //for (int j = 0; j < nIssuesNum; ++j)
769 //OpponentShortTermIdx[j] = CurrentAgreementIdx[j];
770 }
771 if (m_bCalcNashAgreement)
772 {
773 dCurrentNashValue = dOpponentShortTermAgreementValue * dCurrentAgentAgreementValue;
774
775 if (dCurrentNashValue > dOpponentShortTermNashValue)
776 {
777 dOpponentShortTermNashValue = dCurrentNashValue;
778 }
779 }
780 }
781
782 // receiveMessage issue values indices
783 boolean bFinishUpdate = false;
784 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
785 {
786 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
787 {
788 CurrentAgreementIdx[k] = 0;
789 }
790 else
791 {
792 CurrentAgreementIdx[k]++;
793 bFinishUpdate = true;
794 }
795 }
796 } // end for - going over all possible agreements
797
798 Random rand = new Random();
799
800 // select which offer to propose
801 // generate a random number between 0 and OFFER_SET_SIZE
802 int randNum = rand.nextInt(OFFER_SET_SIZE);
803
804 if (bCalcForNextTurn)
805 {
806 if (bCalcOpponentCompromise)
807 {
808 if (dOpponentCompromiseQOValue[randNum] * dBelievedTypeOpponentCompromise > m_dNextTurnQOValue)
809 {
810 m_dNextTurnQOValue = dOpponentCompromiseQOValue[randNum] * dBelievedTypeOpponentCompromise;
811 m_dAgentSelectedNextTurnValue = m_CurrentAgentNextTurnType.getAgreementValue(OpponentCompromiseIdx[randNum], nCurrentTurn);
812 m_dOppSelectedNextTurnValue = agentOpponentCompromise.getAgreementValue(OpponentCompromiseIdx[randNum], nCurrentTurn);
813 m_sNextTurnAgreement = m_CurrentAgentType.getAgreementStr(OpponentCompromiseIdx[randNum]);
814 m_nNextTurnOppType = COMPROMISE_TYPE_IDX;
815 }
816 }
817 if (bCalcOpponentLongTerm)
818 {
819 if (dOpponentLongTermQOValue[randNum] * dBelievedTypeOpponentLongTerm > m_dNextTurnQOValue)
820 {
821 m_dNextTurnQOValue = dOpponentLongTermQOValue[randNum] * dBelievedTypeOpponentLongTerm;
822 m_dAgentSelectedNextTurnValue = m_CurrentAgentNextTurnType.getAgreementValue(OpponentLongTermIdx[randNum], nCurrentTurn);
823 m_dOppSelectedNextTurnValue = agentOpponentLongTerm.getAgreementValue(OpponentLongTermIdx[randNum], nCurrentTurn);
824 m_sNextTurnAgreement = m_CurrentAgentType.getAgreementStr(OpponentLongTermIdx[randNum]);
825 m_nNextTurnOppType = LONG_TERM_TYPE_IDX;
826 }
827 }
828 if (bCalcOpponentShortTerm)
829 {
830 if (dOpponentShortTermQOValue[randNum] * dBelievedTypeOpponentShortTerm > m_dNextTurnQOValue)
831 {
832 m_dNextTurnQOValue = dOpponentShortTermQOValue[randNum] * dBelievedTypeOpponentShortTerm;
833 m_dAgentSelectedNextTurnValue = m_CurrentAgentNextTurnType.getAgreementValue(OpponentShortTermIdx[randNum], nCurrentTurn);
834 m_dOppSelectedNextTurnValue = agentOpponentShortTerm.getAgreementValue(OpponentShortTermIdx[randNum], nCurrentTurn);
835 m_sNextTurnAgreement = m_CurrentAgentType.getAgreementStr(OpponentShortTermIdx[randNum]);
836 m_nNextTurnOppType = SHORT_TERM_TYPE_IDX;
837 }
838 }
839 } // end if - calculate for next turn
840 else // calculate for current turn
841 {
842 String sSelectedOffer = "";
843 String sSelectedOpponent = "";
844 if (bCalcOpponentCompromise)
845 {
846 if (dOpponentCompromiseQOValue[randNum] * dBelievedTypeOpponentCompromise > m_dQOValue)
847 {
848 bw.println("~~~~~~~~~~SELECTED COMP~~~~~~~~~~");
849 m_dQOValue = dOpponentCompromiseQOValue[randNum] * dBelievedTypeOpponentCompromise;
850 m_dAgentSelectedValue = m_CurrentAgentType.getAgreementValue(OpponentCompromiseIdx[randNum], nCurrentTurn);
851 m_dOppSelectedValue = agentOpponentCompromise.getAgreementValue(OpponentCompromiseIdx[randNum], nCurrentTurn);
852 m_sAgreement = m_CurrentAgentType.getAgreementStr(OpponentCompromiseIdx[randNum]);
853 bw.println("Agreement: " + m_sAgreement);
854 bw.println("QO Value: " + m_dQOValue);
855 bw.println("Agent Value: " + m_dAgentSelectedValue);
856 bw.println("Opponent Value: " + m_dOppSelectedValue);
857
858 sSelectedOpponent = "Compromise";
859 }
860 }
861 if (bCalcOpponentLongTerm)
862 {
863 if (dOpponentLongTermQOValue[randNum] * dBelievedTypeOpponentLongTerm > m_dQOValue)
864 {
865 bw.println("~~~~~~~~~~SELECTED LONG~~~~~~~~~~");
866 m_dQOValue = dOpponentLongTermQOValue[randNum] * dBelievedTypeOpponentLongTerm;
867 m_dAgentSelectedValue = m_CurrentAgentType.getAgreementValue(OpponentLongTermIdx[randNum], nCurrentTurn);
868 m_dOppSelectedValue = agentOpponentLongTerm.getAgreementValue(OpponentLongTermIdx[randNum], nCurrentTurn);
869 m_sAgreement = m_CurrentAgentType.getAgreementStr(OpponentLongTermIdx[randNum]);
870 bw.println("Agreement: " + m_sAgreement);
871 bw.println("QO Value: " + m_dQOValue);
872 bw.println("Agent Value: " + m_dAgentSelectedValue);
873 bw.println("Opponent Value: " + m_dOppSelectedValue);
874
875 sSelectedOpponent = "Long";
876 }
877 }
878 if (bCalcOpponentShortTerm)
879 {
880 if (dOpponentShortTermQOValue[randNum] * dBelievedTypeOpponentShortTerm > m_dQOValue)
881 {
882 bw.println("~~~~~~~~~~SELECTED SHORT~~~~~~~~~~");
883 m_dQOValue = dOpponentShortTermQOValue[randNum] * dBelievedTypeOpponentShortTerm;
884 m_dAgentSelectedValue = m_CurrentAgentType.getAgreementValue(OpponentShortTermIdx[randNum], nCurrentTurn);
885 m_dOppSelectedValue = agentOpponentShortTerm.getAgreementValue(OpponentShortTermIdx[randNum], nCurrentTurn);
886 m_sAgreement = m_CurrentAgentType.getAgreementStr(OpponentShortTermIdx[randNum]);
887 bw.println("Agreement: " + m_sAgreement);
888 bw.println("QO Value: " + m_dQOValue);
889 bw.println("Agent Value: " + m_dAgentSelectedValue);
890 bw.println("Opponent Value: " + m_dOppSelectedValue);
891
892 sSelectedOpponent = "Short";
893 }
894 }
895
896 sSelectedOffer = "Agreement: " + m_sAgreement + "\n" +
897 "QO Value: " + m_dQOValue + "\n" +
898 "Agent Value: " + m_dAgentSelectedValue + "\n" +
899 "Opponent Value: " + m_dOppSelectedValue;
900
901 System.err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
902 System.err.println("Will Send Message for Opponent: " + sSelectedOpponent);
903 System.err.println(sSelectedOffer);
904 System.err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
905
906 } // end if-else - calculate for current/next turn
907
908 // write probabilities to file
909 bw.println("-----------------Final Probabilities-------------");
910 bw.println("Short: " + dBelievedTypeOpponentShortTerm);
911 bw.println("Long: " + dBelievedTypeOpponentLongTerm);
912 bw.println("Comp: " + dBelievedTypeOpponentCompromise);
913
914 bw.close();
915
916 } catch (Exception e) {
917 System.out.println("Error opening QO log file [QAgentsCore::OfferAgainstOpponent(592)]");
918 System.err.println("Error opening QO log file [QAgentsCore::OfferAgainstOpponent(592)]");
919 e.printStackTrace();
920 }
921 }
922
923 public double getSelectedQOAgreementValue()
924 {
925 return m_dQOValue;
926 }
927
928 public double getSelectedEquilibriumAgreementValue()
929 {
930 return m_dEquilibriumValue;
931 }
932
933 public String getSelectedQOAgreementStr()
934 {
935 return m_sAgreement;
936 }
937
938 public String getSelectedEquilibriumAgreementStr()
939 {
940 return m_sEquilibriumAgreement;
941 }
942
943 public double getNextTurnAgentQOUtilityValue()
944 {
945 return m_dAgentSelectedNextTurnValue;
946 }
947
948 public double getNextTurnAgentEquilibriumUtilityValue()
949 {
950 //return m_dNextTurnEquilibriumValue;
951 //22-09-05
952 return m_dNextTurnCurrentAgentValueForEqOffer;
953 }
954
955 public String getNextTurnQOAgreement()
956 {
957 return m_sNextTurnAgreement;
958 }
959
960 public String getNextTurnEquilibriumAgreement()
961 {
962 return m_sNextTurnEquilibriumAgreement;
963 }
964
965 public double getNextTurnOpponentQOUtilityValue()
966 {
967 return m_dOppSelectedNextTurnValue;
968 }
969
970 public int getNextTurnOpponentType()
971 {
972 return m_nNextTurnOppType;
973 }
974
975 // PRE-CONDITION: m_CurrentAgentType should be updated for the current turn
976 public void calculateEquilibrium(QAgentType agentType, int nMaxTurns, boolean bCalculateForAllAgents, boolean bCalcForNextTurn, int nCurrentTurn)
977 {
978 m_dEquilibriumValue = QAgentType.VERY_SMALL_NUMBER;
979 m_dNextTurnEquilibriumValue = QAgentType.VERY_SMALL_NUMBER;
980
981 if (bCalcForNextTurn)
982 m_CurrentAgentNextTurnType = agentType;
983 else
984 m_CurrentAgentType = agentType;
985
986 if (m_CurrentAgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
987 {
988 QAgentType engCompromise = null;
989 if (bCalcForNextTurn)
990 engCompromise = getEnglandCompromiseNextTurnType();
991 else
992 engCompromise = getEnglandCompromiseType();
993
994// if (getDoesOpponentEnd())
995 calculateEquilibrium(engCompromise, true, nMaxTurns, COMPROMISE_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
996// else
997// {
998// calculateEquilibrium(engCompromise, true, nMaxTurns, COMPROMISE_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
999// calculateEquilibrium(engCompromise, false, nMaxTurns, COMPROMISE_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1000// }
1001
1002 QAgentType engLongTerm = null;
1003 if (bCalcForNextTurn)
1004 engLongTerm = getEnglandLongTermNextTurnType();
1005 else
1006 engLongTerm = getEnglandLongTermType();
1007
1008// if (getDoesOpponentEnd())
1009 calculateEquilibrium(engLongTerm, true, nMaxTurns, LONG_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1010// else {
1011// calculateEquilibrium(engLongTerm, true, nMaxTurns, LONG_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1012// calculateEquilibrium(engLongTerm, false, nMaxTurns, LONG_TERM_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1013// }
1014
1015 // this is the real opponent
1016 QAgentType engShortTerm = null;
1017 if (bCalcForNextTurn)
1018 engShortTerm = getEnglandShortTermNextTurnType();
1019 else
1020 engShortTerm = getEnglandShortTermType();
1021
1022// if (getDoesOpponentEnd())
1023 calculateEquilibrium(engShortTerm, true, nMaxTurns, SHORT_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1024// else {
1025// calculateEquilibrium(engShortTerm, true, nMaxTurns, SHORT_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1026// calculateEquilibrium(engShortTerm, false, nMaxTurns, SHORT_TERM_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1027// }
1028 }
1029 else if (m_CurrentAgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
1030 {
1031 QAgentType zimCompromise = null;
1032 if (bCalcForNextTurn)
1033 zimCompromise = getZimbabweCompromiseNextTurnType();
1034 else
1035 zimCompromise = getZimbabweCompromiseType();
1036
1037// if (getDoesOpponentEnd())
1038 calculateEquilibrium(zimCompromise, true, nMaxTurns, COMPROMISE_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1039// else {
1040// calculateEquilibrium(zimCompromise, true, nMaxTurns, COMPROMISE_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1041// calculateEquilibrium(zimCompromise, false, nMaxTurns, COMPROMISE_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1042// }
1043
1044 QAgentType zimLongTerm = null;
1045 if (bCalcForNextTurn)
1046 zimLongTerm = getZimbabweLongTermNextTurnType();
1047 else
1048 zimLongTerm = getZimbabweLongTermType();
1049
1050// if (getDoesOpponentEnd())
1051 calculateEquilibrium(zimLongTerm, true, nMaxTurns, LONG_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1052// else {
1053// calculateEquilibrium(zimLongTerm, true, nMaxTurns, LONG_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1054// calculateEquilibrium(zimLongTerm, false, nMaxTurns, LONG_TERM_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1055// }
1056
1057 // this is the real opponent
1058 QAgentType zimShortTerm = null;
1059 if (bCalcForNextTurn)
1060 zimShortTerm = getZimbabweShortTermNextTurnType();
1061 else
1062 zimShortTerm = getZimbabweShortTermType();
1063
1064// if (getDoesOpponentEnd())
1065 calculateEquilibrium(zimShortTerm, true, nMaxTurns, SHORT_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1066// else {
1067// calculateEquilibrium(zimShortTerm, true, nMaxTurns, SHORT_TERM_TYPE_IDX, FIRST_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1068// calculateEquilibrium(zimShortTerm, false, nMaxTurns, SHORT_TERM_TYPE_IDX, SECOND_EQUILIBRIUM, bCalculateForAllAgents, bCalcForNextTurn, nCurrentTurn);
1069// }
1070 }
1071 else
1072 {
1073 System.out.println("[QO]Agent type is unknown [QAgentsCore::calculateEquilibrium(658");
1074 System.err.println("[QO]Agent type is unknown [QAgentsCore::calculateEquilibrium(658)]");
1075 return;
1076 }
1077
1078 // calculate the selected equilbrium agreement and value
1079 if (bCalculateForAllAgents)
1080 {
1081 // choose the best value from all possible values
1082 ArrayList<Double> dSortedValuesList = new ArrayList<Double>();
1083 ArrayList<String> sSortedValuesList = new ArrayList<String>();
1084
1085 double dCurrentValue = QAgentType.VERY_SMALL_NUMBER;
1086 boolean bFoundInd = false;
1087 int nInsertionPoint = 0;
1088 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
1089 {
1090 // insert values in a sorted way
1091
1092 // first values
1093 bFoundInd = false;
1094 for (int ind = 0; ind < dSortedValuesList.size() && !bFoundInd; ++ind)
1095 {
1096 dCurrentValue = ((Double)dSortedValuesList.get(ind)).doubleValue();
1097
1098 if (bCalcForNextTurn)
1099 {
1100 if (m_dFirstEquilibriumValueNextTurn[i] < dCurrentValue)
1101 {
1102 bFoundInd = true;
1103 nInsertionPoint = ind;
1104 }
1105 }
1106 else
1107 {
1108 if (m_dFirstEquilibriumValue[i] < dCurrentValue)
1109 {
1110 bFoundInd = true;
1111 nInsertionPoint = ind;
1112 }
1113 }
1114 }
1115 if (bFoundInd)
1116 {
1117 if (bCalcForNextTurn)
1118 {
1119 dSortedValuesList.add(nInsertionPoint, new Double(m_dFirstEquilibriumValueNextTurn[i]));
1120 sSortedValuesList.add(nInsertionPoint, m_sFirstEquilibriumAgreementNextTurn[i]);
1121 }
1122 else
1123 {
1124 dSortedValuesList.add(nInsertionPoint, new Double(m_dFirstEquilibriumValue[i]));
1125 sSortedValuesList.add(nInsertionPoint, m_sFirstEquilibriumAgreement[i]);
1126 }
1127 }
1128 else
1129 {
1130 if (bCalcForNextTurn)
1131 {
1132 dSortedValuesList.add(new Double(m_dFirstEquilibriumValueNextTurn[i]));
1133 sSortedValuesList.add(m_sFirstEquilibriumAgreementNextTurn[i]);
1134 }
1135 else
1136 {
1137 dSortedValuesList.add(new Double(m_dFirstEquilibriumValue[i]));
1138 sSortedValuesList.add(m_sFirstEquilibriumAgreement[i]);
1139 }
1140 }
1141
1142 // second values
1143 bFoundInd = false;
1144 for (int ind = 0; ind < dSortedValuesList.size() && !bFoundInd; ++ind)
1145 {
1146 dCurrentValue = ((Double)dSortedValuesList.get(ind)).doubleValue();
1147
1148 if (bCalcForNextTurn)
1149 {
1150 if (m_dSecondEquilibriumValueNextTurn[i] < dCurrentValue)
1151 {
1152 bFoundInd = true;
1153 nInsertionPoint = ind;
1154 }
1155 }
1156 else
1157 {
1158 if (m_dSecondEquilibriumValue[i] < dCurrentValue)
1159 {
1160 bFoundInd = true;
1161 nInsertionPoint = ind;
1162 }
1163 }
1164 }
1165 if (bFoundInd)
1166 {
1167 if (bCalcForNextTurn)
1168 {
1169 dSortedValuesList.add(nInsertionPoint, new Double(m_dSecondEquilibriumValueNextTurn[i]));
1170 sSortedValuesList.add(nInsertionPoint, m_sSecondEquilibriumAgreementNextTurn[i]);
1171 }
1172 else
1173 {
1174 dSortedValuesList.add(nInsertionPoint, new Double(m_dSecondEquilibriumValue[i]));
1175 sSortedValuesList.add(nInsertionPoint, m_sSecondEquilibriumAgreement[i]);
1176 }
1177 }
1178 else
1179 {
1180 if (bCalcForNextTurn)
1181 {
1182 dSortedValuesList.add(new Double(m_dSecondEquilibriumValueNextTurn[i]));
1183 sSortedValuesList.add(m_sSecondEquilibriumAgreementNextTurn[i]);
1184 }
1185 else
1186 {
1187 dSortedValuesList.add(new Double(m_dSecondEquilibriumValue[i]));
1188 sSortedValuesList.add(m_sSecondEquilibriumAgreement[i]);
1189 }
1190 }
1191 }
1192
1193 if (bCalcForNextTurn)
1194 {
1195 m_dNextTurnEquilibriumValue = ((Double)dSortedValuesList.get(dSortedValuesList.size() - 1)).doubleValue();
1196 m_sNextTurnEquilibriumAgreement = (String)sSortedValuesList.get(sSortedValuesList.size() - 1);
1197 m_dNextTurnCurrentAgentValueForEqOffer = agentType.getAgreementValue(agentType.getAgreementIndices(m_sNextTurnEquilibriumAgreement), nCurrentTurn+1);
1198 }
1199 else
1200 {
1201 m_dEquilibriumValue = ((Double)dSortedValuesList.get(dSortedValuesList.size() - 1)).doubleValue();
1202 m_sEquilibriumAgreement = (String)sSortedValuesList.get(sSortedValuesList.size() - 1);
1203 }
1204 }
1205 else // calculate only for a predefined agent (short-term)
1206 {
1207 if (bCalcForNextTurn)
1208 {
1209 if (m_dFirstEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX] > m_dSecondEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX])
1210 {
1211 m_dNextTurnEquilibriumValue = m_dFirstEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX];
1212 m_sNextTurnEquilibriumAgreement = m_sFirstEquilibriumAgreementNextTurn[SHORT_TERM_TYPE_IDX];
1213 m_dNextTurnCurrentAgentValueForEqOffer = agentType.getAgreementValue(agentType.getAgreementIndices(m_sNextTurnEquilibriumAgreement), nCurrentTurn+1);
1214 }
1215 else if (m_dFirstEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX] < m_dSecondEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX])
1216 {
1217 m_dNextTurnEquilibriumValue = m_dSecondEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX];
1218 m_sNextTurnEquilibriumAgreement = m_sSecondEquilibriumAgreementNextTurn[SHORT_TERM_TYPE_IDX];
1219 m_dNextTurnCurrentAgentValueForEqOffer = agentType.getAgreementValue(agentType.getAgreementIndices(m_sNextTurnEquilibriumAgreement), nCurrentTurn+1);
1220 }
1221 else // equals
1222 {
1223 // flip a coin
1224 Random generator = new Random();
1225 double dRandNum = generator.nextDouble();
1226 if (dRandNum <= 0.5)
1227 {
1228 m_dNextTurnEquilibriumValue = m_dFirstEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX];
1229 m_sNextTurnEquilibriumAgreement = m_sFirstEquilibriumAgreementNextTurn[SHORT_TERM_TYPE_IDX];
1230 m_dNextTurnCurrentAgentValueForEqOffer = agentType.getAgreementValue(agentType.getAgreementIndices(m_sNextTurnEquilibriumAgreement), nCurrentTurn+1);
1231 }
1232 else
1233 {
1234 m_dNextTurnEquilibriumValue = m_dSecondEquilibriumValueNextTurn[SHORT_TERM_TYPE_IDX];
1235 m_sNextTurnEquilibriumAgreement = m_sSecondEquilibriumAgreementNextTurn[SHORT_TERM_TYPE_IDX];
1236 m_dNextTurnCurrentAgentValueForEqOffer = agentType.getAgreementValue(agentType.getAgreementIndices(m_sNextTurnEquilibriumAgreement), nCurrentTurn+1);
1237 }
1238 }
1239 }
1240 else
1241 {
1242 if (m_dFirstEquilibriumValue[SHORT_TERM_TYPE_IDX] > m_dSecondEquilibriumValue[SHORT_TERM_TYPE_IDX])
1243 {
1244 m_dEquilibriumValue = m_dFirstEquilibriumValue[SHORT_TERM_TYPE_IDX];
1245 m_sEquilibriumAgreement = m_sFirstEquilibriumAgreement[SHORT_TERM_TYPE_IDX];
1246 }
1247 else if (m_dFirstEquilibriumValue[SHORT_TERM_TYPE_IDX] < m_dSecondEquilibriumValue[SHORT_TERM_TYPE_IDX])
1248 {
1249 m_dEquilibriumValue = m_dSecondEquilibriumValue[SHORT_TERM_TYPE_IDX];
1250 m_sEquilibriumAgreement = m_sSecondEquilibriumAgreement[SHORT_TERM_TYPE_IDX];
1251 }
1252 else // equals
1253 {
1254 // flip a coin
1255 Random generator = new Random();
1256 double dRandNum = generator.nextDouble();
1257 if (dRandNum <= 0.5)
1258 {
1259 m_dEquilibriumValue = m_dFirstEquilibriumValue[SHORT_TERM_TYPE_IDX];
1260 m_sEquilibriumAgreement = m_sFirstEquilibriumAgreement[SHORT_TERM_TYPE_IDX];
1261 }
1262 else
1263 {
1264 m_dEquilibriumValue = m_dSecondEquilibriumValue[SHORT_TERM_TYPE_IDX];
1265 m_sEquilibriumAgreement = m_sSecondEquilibriumAgreement[SHORT_TERM_TYPE_IDX];
1266 }
1267 }
1268 }
1269 } // end if-else: calculate for all agents or only one
1270 } // end calculateEquilibrium
1271
1272 public void calculateEquilibrium(QAgentType oppAgentType, boolean bOppEnds, int nMaxTurns, int nAgentType, int nEquilibriumNum, boolean bCalculateForAllAgents, boolean bCalcForNextTurn, int nCurrentTurn)
1273 {
1274 QCombinedAgreement ca = new QCombinedAgreement();
1275// ca.m_dAgentAgreementValue = dCurrentRecvValue;
1276// ca.m_dOpponentAgreementValue = dCurrentOppValue;
1277//
1278 QAgentType currentAgentType = null;
1279
1280 if (bCalcForNextTurn)
1281 currentAgentType = m_CurrentAgentNextTurnType;
1282 else
1283 currentAgentType = m_CurrentAgentType;
1284
1285// if (bCalcForNextTurn)
1286 {
1287 if (nCurrentTurn >= nMaxTurns)
1288 nCurrentTurn = nMaxTurns - 1;
1289 }
1290
1291 // get expected utility for best agreement at time nMaxTurns - 1
1292 double dAgreements[] = new double[2];
1293 dAgreements[0] = QAgentType.VERY_SMALL_NUMBER;
1294 dAgreements[1] = QAgentType.VERY_SMALL_NUMBER;
1295 getBothBestAgreementsAtTime(currentAgentType, oppAgentType, nMaxTurns -1, dAgreements, ca);
1296
1297 for (int t = nMaxTurns - 2; t >= nCurrentTurn; --t)
1298 {
1299 getBothBestAgreementsAtTime(currentAgentType, oppAgentType, t, dAgreements, ca);
1300 }
1301
1302 // save agreement values
1303 if (nEquilibriumNum == FIRST_EQUILIBRIUM)
1304 {
1305 if (bCalcForNextTurn)
1306 {
1307 m_sFirstEquilibriumAgreementNextTurn[nAgentType] = ca.m_sAgreement;
1308
1309 int tempCurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
1310 tempCurrentAgreementIdx = currentAgentType.getAgreementIndices(ca.m_sAgreement);
1311 double dCurrentAgentValue = currentAgentType.getAgreementValue(tempCurrentAgreementIdx, nCurrentTurn);
1312 /*if (bOppEnds)
1313 m_dFirstEquilibriumValueNextTurn[nAgentType] = ca.m_dAgentAgreementValue;
1314 else
1315 m_dFirstEquilibriumValueNextTurn[nAgentType] = ca.m_dOpponentAgreementValue;
1316 */
1317
1318 m_dFirstEquilibriumValueNextTurn[nAgentType] = dCurrentAgentValue;
1319
1320 if (bCalculateForAllAgents)
1321 m_dFirstEquilibriumValueNextTurn[nAgentType] *= oppAgentType.getTypeProbability();
1322 }
1323 else
1324 {
1325 m_sFirstEquilibriumAgreement[nAgentType] = ca.m_sAgreement;
1326
1327 int tempCurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
1328 tempCurrentAgreementIdx = currentAgentType.getAgreementIndices(ca.m_sAgreement);
1329 double dCurrentAgentValue = currentAgentType.getAgreementValue(tempCurrentAgreementIdx, nCurrentTurn);
1330 /*if (bOppEnds)
1331 m_dFirstEquilibriumValue[nAgentType] = ca.m_dAgentAgreementValue;
1332 else
1333 m_dFirstEquilibriumValue[nAgentType] = ca.m_dOpponentAgreementValue;
1334 */
1335 m_dFirstEquilibriumValue[nAgentType] = dCurrentAgentValue;
1336 if (bCalculateForAllAgents)
1337 m_dFirstEquilibriumValue[nAgentType] *= oppAgentType.getTypeProbability();
1338 }
1339 }
1340 else if (nEquilibriumNum == SECOND_EQUILIBRIUM)
1341 {
1342 if (bCalcForNextTurn)
1343 {
1344 m_sSecondEquilibriumAgreementNextTurn[nAgentType] = ca.m_sAgreement;
1345
1346 int tempCurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
1347 tempCurrentAgreementIdx = currentAgentType.getAgreementIndices(ca.m_sAgreement);
1348 double dCurrentAgentValue = currentAgentType.getAgreementValue(tempCurrentAgreementIdx, nCurrentTurn);
1349 /*if (bOppEnds)
1350 m_dSecondEquilibriumValueNextTurn[nAgentType] = ca.m_dAgentAgreementValue;
1351 else
1352 m_dSecondEquilibriumValueNextTurn[nAgentType] = ca.m_dOpponentAgreementValue;
1353 */
1354 m_dSecondEquilibriumValueNextTurn[nAgentType] = dCurrentAgentValue;
1355
1356 if (bCalculateForAllAgents)
1357 m_dSecondEquilibriumValueNextTurn[nAgentType] *= oppAgentType.getTypeProbability();
1358 }
1359 else
1360 {
1361 m_sSecondEquilibriumAgreement[nAgentType] = ca.m_sAgreement;
1362
1363 int tempCurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
1364 tempCurrentAgreementIdx = currentAgentType.getAgreementIndices(ca.m_sAgreement);
1365 double dCurrentAgentValue = currentAgentType.getAgreementValue(tempCurrentAgreementIdx, nCurrentTurn);
1366 /*if (bOppEnds)
1367 m_dSecondEquilibriumValue[nAgentType] = ca.m_dAgentAgreementValue;
1368 else
1369 m_dSecondEquilibriumValue[nAgentType] = ca.m_dOpponentAgreementValue;
1370 */
1371 m_dSecondEquilibriumValue[nAgentType] = dCurrentAgentValue;
1372
1373 if (bCalculateForAllAgents)
1374 m_dSecondEquilibriumValue[nAgentType] *= oppAgentType.getTypeProbability();
1375 }
1376 }
1377/*
1378 m_out.println("Perfect Equilibrium Agreement:");
1379 if (bOppEnds)
1380 m_out.println("England Ends--- Me: " + ca.m_dAgentAgreementValue + " Opp: " + ca.m_dOpponentAgreementValue + "\n" + ca.m_sAgreement);
1381 else
1382 m_out.println("Zimbabwe Ends--- Me: " + ca.m_dOpponentAgreementValue + " Opp: " + ca.m_dAgentAgreementValue + "\n" + ca.m_sAgreement);
1383*/
1384 }
1385
1386 /**
1387 * @param i
1388 * @param agreements
1389 */
1390 private void getBothBestAgreementsAtTime(QAgentType first, QAgentType second, int nCurrentTurn, double[] agreements, QCombinedAgreement ca) {
1391 double dFirstValue = QAgentType.VERY_SMALL_NUMBER;
1392 double dSecondValue = QAgentType.VERY_SMALL_NUMBER;
1393
1394 double dFirstUtilityFromSecondBest = QAgentType.VERY_SMALL_NUMBER;
1395 double dSecondUtilityFromFirstBest = QAgentType.VERY_SMALL_NUMBER;
1396 // double dFirstUtilityFromFirstBest = agreements[0];
1397 // double dSecondUtilityFromSecondBest = agreements[1];
1398
1399 // double agreementsSum = agreements[0] + agreements[1];
1400
1401 // agreements[0] = 0.5 * (agreementsSum);
1402 // agreements[1] = 0.5 * (agreementsSum);
1403
1404 double initialAgreement0 = agreements[0];
1405 double initialAgreement1 = agreements[1];
1406
1407 int nIssuesNum = m_CurrentAgentType.getIssuesNum();
1408 int nTotalAgreements = m_CurrentAgentType.getTotalAgreements();
1409
1410 int CurrentAgreementIdx[] = new int[nIssuesNum];
1411 int MaxIssueValues[] = new int[nIssuesNum];
1412
1413 for (int i = 0; i < nIssuesNum; ++i)
1414 {
1415 CurrentAgreementIdx[i] = 0;
1416 MaxIssueValues[i] = m_CurrentAgentType.getMaxIssueValue(i);
1417 }
1418
1419 for (int i = 0; i < nTotalAgreements; ++i)
1420 {
1421 dFirstValue = first.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1422 dSecondValue = second.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1423
1424 // if this agreement is better for the opponent than the agreement
1425 // offered at time t+1
1426 if (dSecondValue > initialAgreement1)
1427 {
1428 if (dFirstValue > agreements[0])
1429 {
1430 agreements[0] = dFirstValue;
1431 dSecondUtilityFromFirstBest = dSecondValue;
1432 ca.m_sAgreement = first.getAgreementStr(CurrentAgreementIdx);
1433 }
1434 }
1435 // receiveMessage issue values indices
1436 boolean bFinishUpdate = false;
1437 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
1438 {
1439 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
1440 {
1441 CurrentAgreementIdx[k] = 0;
1442 }
1443 else
1444 {
1445 CurrentAgreementIdx[k]++;
1446 bFinishUpdate = true;
1447 }
1448 }
1449 }
1450
1451 for (int i = 0; i < nIssuesNum; ++i)
1452 {
1453 CurrentAgreementIdx[i] = 0;
1454 MaxIssueValues[i] = m_CurrentAgentType.getMaxIssueValue(i);
1455 }
1456
1457 for (int i = 0; i < nTotalAgreements; ++i)
1458 {
1459 dFirstValue = first.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1460 dSecondValue = second.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1461
1462 if (dFirstValue > initialAgreement0)
1463 {
1464 if (dSecondValue > agreements[1])
1465 {
1466 agreements[1] = dSecondValue;
1467 dFirstUtilityFromSecondBest = dFirstValue;
1468
1469 }
1470 }
1471
1472 // receiveMessage issue values indices
1473 boolean bFinishUpdate = false;
1474 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
1475 {
1476 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
1477 {
1478 CurrentAgreementIdx[k] = 0;
1479 }
1480 else
1481 {
1482 CurrentAgreementIdx[k]++;
1483 bFinishUpdate = true;
1484 }
1485 }
1486 }
1487
1488 // receiveMessage values
1489 if (dFirstUtilityFromSecondBest > QAgentType.VERY_SMALL_NUMBER)
1490 agreements[0] = 0.5 * (agreements[0] + dFirstUtilityFromSecondBest);
1491 if (dSecondUtilityFromFirstBest > dFirstUtilityFromSecondBest)
1492 agreements[1] = 0.5 * (agreements[1] + dSecondUtilityFromFirstBest);
1493 }
1494
1495
1496 // return an agreement the offerAgent will offer at time period nCurrentTime
1497 // the agreement is the best agreement for offerAgent that is highest than fGivenOfferValue
1498 // and somewhat higher than fGivenRecvValue
1499 // the new values for the agent and the opponent are saved in the CombinedAgreement parameter
1500 public void getBestAgreementAtTime(QAgentType offerAgent, QAgentType recvAgent, QCombinedAgreement ca, int nCurrentTurn)
1501 {
1502 double dGivenRecvValue = ca.m_dOpponentAgreementValue;
1503 double dGivenOfferValue = ca.m_dAgentAgreementValue;
1504
1505 double dAgreementValue = dGivenOfferValue, dOfferAgentValue = 0, dRecvAgentValue = 0;
1506
1507 int nIssuesNum = m_CurrentAgentType.getIssuesNum();
1508 int nTotalAgreements = m_CurrentAgentType.getTotalAgreements();
1509
1510 int CurrentAgreementIdx[] = new int[nIssuesNum];
1511 int MaxIssueValues[] = new int[nIssuesNum];
1512
1513 for (int i = 0; i < nIssuesNum; ++i)
1514 {
1515 CurrentAgreementIdx[i] = 0;
1516 MaxIssueValues[i] = m_CurrentAgentType.getMaxIssueValue(i);
1517 }
1518
1519 for (int i = 0; i < nTotalAgreements; ++i)
1520 {
1521 dRecvAgentValue = recvAgent.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1522 dOfferAgentValue = offerAgent.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
1523
1524 // if this agreement is better for the opponent than the agreement
1525 // offered at time t+1
1526 if (dRecvAgentValue > dGivenRecvValue)
1527 {
1528 if (dOfferAgentValue > dAgreementValue)
1529 {
1530 dAgreementValue = dOfferAgentValue;
1531
1532 ca.m_dOpponentAgreementValue = dRecvAgentValue;
1533 ca.m_dAgentAgreementValue = dOfferAgentValue;
1534
1535 ca.m_sAgreement = offerAgent.getAgreementStr(CurrentAgreementIdx);
1536 }
1537 }
1538
1539 // receiveMessage issue values indices
1540 boolean bFinishUpdate = false;
1541 for (int k = nIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
1542 {
1543 if (CurrentAgreementIdx[k]+1 >= MaxIssueValues[k])
1544 {
1545 CurrentAgreementIdx[k] = 0;
1546 }
1547 else
1548 {
1549 CurrentAgreementIdx[k]++;
1550 bFinishUpdate = true;
1551 }
1552 }
1553 }
1554 }
1555 };
1556
1557
1558
1559 /**
1560 * Initializes the agent's core.
1561 * Creates the different England types and Zimbabwe types.
1562 */
1563 public QAgentsCore(String sFileName, String sNow, agents.QOAgent agent)
1564 {
1565 m_Agent = agent;
1566 m_bEquilibriumAgent = false;
1567 m_sLogFileName = sFileName;
1568
1569 m_CurrentAgentType = null;
1570 m_CurrentAgentNextTurnType = null;
1571
1572 m_EnglandAgentTypesList = new ArrayList<QAgentType>();
1573 m_ZimbabweAgentTypesList = new ArrayList<QAgentType>();
1574
1575 m_EnglandAgentTypesNextTurnList = new ArrayList<QAgentType>();
1576 m_ZimbabweAgentTypesNextTurnList = new ArrayList<QAgentType>();
1577
1578 m_sProbFileName = "logs\\prob" + sNow + ".";
1579
1580 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
1581 {
1582 m_EnglandAgentTypesList.add(i, new QAgentType(m_bEquilibriumAgent));
1583 m_ZimbabweAgentTypesList.add(i, new QAgentType(m_bEquilibriumAgent));
1584
1585 m_EnglandAgentTypesNextTurnList.add(i, new QAgentType(m_bEquilibriumAgent));
1586 m_ZimbabweAgentTypesNextTurnList.add(i, new QAgentType(m_bEquilibriumAgent));
1587
1588 }
1589
1590 createEnglandLongTermType();
1591 createEnglandShortTermType();
1592 createEnglandCompromiseType();
1593
1594 createZimbabweLongTermType();
1595 createZimbabweShortTermType();
1596 createZimbabweCompromiseType();
1597 }
1598
1599 public QAgentsCore(String sFileName, String sNow, boolean bIsEquilibriumAgent, agents.QOAgent agent)
1600 {
1601 m_Agent = agent;
1602 m_bEquilibriumAgent = bIsEquilibriumAgent;
1603 m_sLogFileName = sFileName;
1604
1605 m_CurrentAgentType = null;
1606 m_CurrentAgentNextTurnType = null;
1607
1608 m_EnglandAgentTypesList = new ArrayList<QAgentType>();
1609 m_ZimbabweAgentTypesList = new ArrayList<QAgentType>();
1610
1611 m_EnglandAgentTypesNextTurnList = new ArrayList<QAgentType>();
1612 m_ZimbabweAgentTypesNextTurnList = new ArrayList<QAgentType>();
1613
1614 m_sProbFileName = "logs\\prob" + sNow + ".";
1615
1616 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
1617 {
1618 m_EnglandAgentTypesList.add(i, new QAgentType(m_bEquilibriumAgent));
1619 m_ZimbabweAgentTypesList.add(i, new QAgentType(m_bEquilibriumAgent));
1620
1621 m_EnglandAgentTypesNextTurnList.add(i, new QAgentType(m_bEquilibriumAgent));
1622 m_ZimbabweAgentTypesNextTurnList.add(i, new QAgentType(m_bEquilibriumAgent));
1623
1624 }
1625
1626 createEnglandLongTermType();
1627 createEnglandShortTermType();
1628 createEnglandCompromiseType();
1629
1630 createZimbabweLongTermType();
1631 createZimbabweShortTermType();
1632 createZimbabweCompromiseType();
1633 }
1634
1635 /**
1636 * @return QAgentType - england's long term type
1637 */
1638 public QAgentType getEnglandLongTermType()
1639 {
1640 return (QAgentType)m_EnglandAgentTypesList.get(LONG_TERM_TYPE_IDX);
1641 }
1642
1643 /**
1644 * @return QAgentType - england's short term type
1645 */
1646 public QAgentType getEnglandShortTermType()
1647 {
1648 return (QAgentType)m_EnglandAgentTypesList.get(SHORT_TERM_TYPE_IDX);
1649 }
1650
1651 /**
1652 * @return QAgentType - england's compromise type
1653 */
1654 public QAgentType getEnglandCompromiseType()
1655 {
1656 return (QAgentType)m_EnglandAgentTypesList.get(COMPROMISE_TYPE_IDX);
1657 }
1658
1659 /**
1660 * @return QAgentType - zimbabwe's long term type
1661 */
1662 public QAgentType getZimbabweLongTermType()
1663 {
1664 return (QAgentType)m_ZimbabweAgentTypesList.get(LONG_TERM_TYPE_IDX);
1665 }
1666
1667 /**
1668 * @return QAgentType - zimbabwe's short term type
1669 */
1670 public QAgentType getZimbabweShortTermType()
1671 {
1672 return (QAgentType)m_ZimbabweAgentTypesList.get(SHORT_TERM_TYPE_IDX);
1673 }
1674
1675 /**
1676 * @return QAgentType - zimbabwe's compromise type
1677 */
1678 public QAgentType getZimbabweCompromiseType()
1679 {
1680 return (QAgentType)m_ZimbabweAgentTypesList.get(COMPROMISE_TYPE_IDX);
1681 }
1682
1683 /**
1684 * @return QAgentType - england's long term type
1685 */
1686 public QAgentType getEnglandLongTermNextTurnType()
1687 {
1688 return (QAgentType)m_EnglandAgentTypesNextTurnList.get(LONG_TERM_TYPE_IDX);
1689 }
1690
1691 /**
1692 * @return QAgentType - england's short term type
1693 */
1694 public QAgentType getEnglandShortTermNextTurnType()
1695 {
1696 return (QAgentType)m_EnglandAgentTypesNextTurnList.get(SHORT_TERM_TYPE_IDX);
1697 }
1698
1699 /**
1700 * @return QAgentType - england's compromise type
1701 */
1702 public QAgentType getEnglandCompromiseNextTurnType()
1703 {
1704 return (QAgentType)m_EnglandAgentTypesNextTurnList.get(COMPROMISE_TYPE_IDX);
1705 }
1706
1707 /**
1708 * @return QAgentType - zimbabwe's long term type
1709 */
1710 public QAgentType getZimbabweLongTermNextTurnType()
1711 {
1712 return (QAgentType)m_ZimbabweAgentTypesNextTurnList.get(LONG_TERM_TYPE_IDX);
1713 }
1714
1715 /**
1716 * @return QAgentType - zimbabwe's short term type
1717 */
1718 public QAgentType getZimbabweShortTermNextTurnType()
1719 {
1720 return (QAgentType)m_ZimbabweAgentTypesNextTurnList.get(SHORT_TERM_TYPE_IDX);
1721 }
1722
1723 /**
1724 * @return QAgentType - zimbabwe's compromise type
1725 */
1726 public QAgentType getZimbabweCompromiseNextTurnType()
1727 {
1728 return (QAgentType)m_ZimbabweAgentTypesNextTurnList.get(COMPROMISE_TYPE_IDX);
1729 }
1730
1731
1732 /**
1733 * Creates zimbabwe's compromise type from the utility file.
1734 * Saves the type in m_ZimbabweAgentTypesList
1735 */
1736 private void createZimbabweCompromiseType()
1737 {
1738 QAgentType compromiseType = new QAgentType(m_bEquilibriumAgent);
1739 compromiseType.setAgentType(QAgentType.ZIMBABWE_TYPE);
1740
1741 createAgentTypeFromFile(m_Agent.opponentModels[COMPROMISE_TYPE_IDX], compromiseType);
1742
1743 m_ZimbabweAgentTypesList.set(COMPROMISE_TYPE_IDX, compromiseType);
1744
1745 QAgentType agentTypeNextTurn = compromiseType;
1746 agentTypeNextTurn.calculateValues(2);
1747 m_ZimbabweAgentTypesNextTurnList.set(COMPROMISE_TYPE_IDX, agentTypeNextTurn);
1748 }
1749
1750 /**
1751 * Creates zimbabwe's short term type from the utility file.
1752 * Saves the type in m_ZimbabweAgentTypesList
1753 */
1754 private void createZimbabweShortTermType()
1755 {
1756 QAgentType shortTermType = new QAgentType(m_bEquilibriumAgent);
1757 shortTermType.setAgentType(QAgentType.ZIMBABWE_TYPE);
1758
1759 createAgentTypeFromFile(m_Agent.opponentModels[SHORT_TERM_TYPE_IDX], shortTermType);
1760
1761 m_ZimbabweAgentTypesList.set(SHORT_TERM_TYPE_IDX, shortTermType);
1762
1763 QAgentType agentTypeNextTurn = shortTermType;
1764 agentTypeNextTurn.calculateValues(2);
1765 m_ZimbabweAgentTypesNextTurnList.set(SHORT_TERM_TYPE_IDX, agentTypeNextTurn);
1766 }
1767
1768 /**
1769 * Creates zimbabwe's long term type from the utility file.
1770 * Saves the type in m_ZimbabweAgentTypesList
1771 */
1772 private void createZimbabweLongTermType()
1773 {
1774 QAgentType longTermType = new QAgentType(m_bEquilibriumAgent);
1775 longTermType.setAgentType(QAgentType.ZIMBABWE_TYPE);
1776
1777 createAgentTypeFromFile(m_Agent.opponentModels[LONG_TERM_TYPE_IDX], longTermType);
1778
1779 m_ZimbabweAgentTypesList.set(LONG_TERM_TYPE_IDX, longTermType);
1780
1781 QAgentType agentTypeNextTurn = longTermType;
1782 agentTypeNextTurn.calculateValues(2);
1783 m_ZimbabweAgentTypesNextTurnList.set(LONG_TERM_TYPE_IDX, agentTypeNextTurn);
1784 }
1785
1786 /**
1787 * Creates england's comrpomise type from the utility file.
1788 * Saves the type in m_EnglandAgentTypesList
1789 */
1790 private void createEnglandCompromiseType()
1791 {
1792 QAgentType compromiseType = new QAgentType(m_bEquilibriumAgent);
1793 compromiseType.setAgentType(QAgentType.ENGLAND_TYPE);
1794
1795 createAgentTypeFromFile(m_Agent.opponentModels[COMPROMISE_TYPE_IDX], compromiseType);
1796
1797
1798 m_EnglandAgentTypesList.set(COMPROMISE_TYPE_IDX, compromiseType);
1799
1800 QAgentType agentTypeNextTurn = compromiseType;
1801 agentTypeNextTurn.calculateValues(2);
1802 m_EnglandAgentTypesNextTurnList.set(COMPROMISE_TYPE_IDX, agentTypeNextTurn);
1803 }
1804
1805 /**
1806 * Creates england's short term type from the utility file.
1807 * Saves the type in m_EnglandAgentTypesList
1808 */
1809 private void createEnglandShortTermType()
1810 {
1811 QAgentType shortTermType = new QAgentType(m_bEquilibriumAgent);
1812 shortTermType.setAgentType(QAgentType.ENGLAND_TYPE);
1813
1814 createAgentTypeFromFile(m_Agent.opponentModels[SHORT_TERM_TYPE_IDX], shortTermType);
1815
1816 m_EnglandAgentTypesList.set(SHORT_TERM_TYPE_IDX, shortTermType);
1817
1818 QAgentType agentTypeNextTurn = shortTermType;
1819 agentTypeNextTurn.calculateValues(2);
1820 m_EnglandAgentTypesNextTurnList.set(SHORT_TERM_TYPE_IDX, agentTypeNextTurn);
1821 }
1822
1823 /**
1824 * Creates england's long term type from the utility file.
1825 * Saves the type in m_EnglandAgentTypesList
1826 */
1827 private void createEnglandLongTermType()
1828 {
1829 QAgentType longTermType = new QAgentType(m_bEquilibriumAgent);
1830 longTermType.setAgentType(QAgentType.ENGLAND_TYPE);
1831
1832 createAgentTypeFromFile(m_Agent.opponentModels[LONG_TERM_TYPE_IDX], longTermType);
1833
1834 m_EnglandAgentTypesList.set(LONG_TERM_TYPE_IDX, longTermType);
1835
1836 QAgentType agentTypeNextTurn = longTermType;
1837 agentTypeNextTurn.calculateValues(2);
1838 m_EnglandAgentTypesNextTurnList.set(LONG_TERM_TYPE_IDX, agentTypeNextTurn);
1839 }
1840
1841 /**
1842 * Creates the specific agent type from the file name
1843 * Returns the new type in agentType.
1844 * @param sFileName - the file name of the agent's type
1845 * @param agentType - the returned agent
1846 * Note: this function is identical to readUtilityFile in the Client
1847 */
1848 private void createAgentTypeFromFile(AdditiveUtilitySpace utilitySpace, QAgentType agentType)
1849 {
1850 //DT: BufferedReader br = null;
1851 String line;
1852
1853 double dGeneralValues[] = new double[GENERAL_VALUES_NUM];
1854
1855 // init values to default
1856 dGeneralValues[TIME_EFFECT_IND] = 0;
1857 dGeneralValues[STATUS_QUO_IND] = QAgentType.VERY_SMALL_NUMBER;
1858 dGeneralValues[OPT_OUT_IND] = QAgentType.VERY_SMALL_NUMBER;
1859
1860 line = readUtilityDetails(utilitySpace, agentType.m_fullUtility.lstUtilityDetails, dGeneralValues);
1861 agentType.m_fullUtility.dTimeEffect = dGeneralValues[TIME_EFFECT_IND];
1862 agentType.m_fullUtility.dStatusQuoValue = dGeneralValues[STATUS_QUO_IND];
1863 agentType.m_fullUtility.dOptOutValue = dGeneralValues[OPT_OUT_IND];
1864
1865 // calculate luce numbers, best agreement and worst agreement at time 0
1866 agentType.calculateValues(1);
1867
1868 //agentType.printValuesToFile(sFileName);
1869
1870 }
1871
1872 /**
1873 * Read the utility details from the agent's file
1874 * @param br - the reader of the file
1875 * @param line - the read line
1876 * @param lstUtilityDetails - list of the utility details
1877 * @param dGeneralValues - array of the general values
1878 * @return line - the new line
1879 */
1880 public String readUtilityDetails(AdditiveUtilitySpace utilitySpace, ArrayList<UtilityDetails> lstUtilityDetails, double dGeneralValues[])
1881 {
1882 UtilityDetails utilityDetails = null;
1883
1884
1885 dGeneralValues[TIME_EFFECT_IND] = -6.;
1886
1887 dGeneralValues[STATUS_QUO_IND] = 306;
1888
1889 dGeneralValues[OPT_OUT_IND] = 215;
1890
1891 utilityDetails = new UtilityDetails();
1892
1893 // need to add new element to the utilityDetails list
1894
1895 // get the title
1896
1897 for(genius.core.issue.Issue lTmp : utilitySpace.getDomain().getIssues()) {
1898 genius.core.issue.IssueDiscrete lIssue = (genius.core.issue.IssueDiscrete )lTmp;
1899 utilityDetails.sTitle = lIssue.getName();
1900 // get the attribute name and side
1901 UtilityIssue utilityIssue = new UtilityIssue();
1902 utilityIssue.sAttributeName = lIssue.getName();
1903 utilityIssue.sSide = "Both";
1904 utilityIssue.dAttributeWeight = utilitySpace.getWeight(lIssue.getNumber())*100;
1905 for(ValueDiscrete lValue : lIssue.getValues()) {
1906
1907
1908 // go over all values
1909 UtilityValue utilityValue = new UtilityValue();
1910
1911 utilityValue.sValue = lValue.getValue();
1912
1913 // get corresponding utility value
1914 try {
1915 utilityValue.dUtility = ((EvaluatorDiscrete)(utilitySpace.getEvaluator(lIssue.getNumber()))).getEvaluationNotNormalized(lValue);
1916 // ++utilityValue.dUtility += NORMALIZE_INCREMENTOR;//TODO: Currently not using normalize incrementor
1917 }catch (Exception e) {
1918 e.printStackTrace();
1919 }
1920
1921 utilityValue.dTimeEffect = new Double(0);
1922
1923 utilityIssue.lstUtilityValues.add(utilityValue);
1924 utilityIssue.sExplanation = lIssue.getDescription();
1925 } // end for
1926
1927 utilityDetails.lstUtilityIssues.add(utilityIssue);
1928 } // end while - reading attributes
1929
1930 lstUtilityDetails.add(utilityDetails);
1931 return "";
1932}
1933
1934 public void updateAgreementsValues(int nTimePeriod)
1935 {
1936 QAgentType agentType = null;
1937 QAgentType agentTypeNextTurn = null;
1938 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
1939 {
1940 agentType = (QAgentType)m_EnglandAgentTypesList.get(i);
1941 agentType.calculateValues(nTimePeriod);
1942 m_EnglandAgentTypesList.set(i, agentType);
1943
1944 agentTypeNextTurn = agentType;
1945 agentTypeNextTurn.calculateValues(nTimePeriod + 1);
1946 m_EnglandAgentTypesNextTurnList.set(i, agentTypeNextTurn);
1947
1948 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(i);
1949 agentType.calculateValues(nTimePeriod);
1950 m_ZimbabweAgentTypesList.set(i, agentType);
1951
1952 agentTypeNextTurn = agentType;
1953 agentTypeNextTurn.calculateValues(nTimePeriod + 1);
1954 m_ZimbabweAgentTypesNextTurnList.set(i, agentTypeNextTurn);
1955 }
1956 }
1957
1958 public void initGenerateAgreement(QAgentType agentType)
1959 {
1960 m_CurrentAgentType = agentType;
1961
1962 m_GenerateAgreement = new QGenerateAgreement();
1963 }
1964
1965 public void calculateAgreement(QAgentType agentType, int nCurrentTurn)
1966 {
1967 m_GenerateAgreement.calculateAgreement(agentType, nCurrentTurn, false);
1968 }
1969
1970 public void calculateEquilibriumAgreement(QAgentType agentType, int nMaxTurns, boolean bCalculateForAllAgents, int nCurrentTurn)
1971 {
1972 m_GenerateAgreement.calculateEquilibrium(agentType, nMaxTurns, bCalculateForAllAgents, false, nCurrentTurn);
1973 }
1974
1975 public String getQOAgreement()
1976 {
1977 return m_GenerateAgreement.getSelectedQOAgreementStr();
1978 }
1979
1980 public String getEquilibriumAgreement()
1981 {
1982 return m_GenerateAgreement.getSelectedEquilibriumAgreementStr();
1983 }
1984
1985
1986 public void calculateNextTurnAgreement(QAgentType agentType, int nNextTurn)
1987 {
1988 m_GenerateAgreement.calculateAgreement(agentType, nNextTurn, true);
1989 }
1990
1991 public void calculateNextTurnEquilibriumAgreement(QAgentType agentType, int nMaxTurns, boolean bCalculateForAllAgents, int nNextTurn)
1992 {
1993 m_GenerateAgreement.calculateEquilibrium(agentType, nMaxTurns, bCalculateForAllAgents, true, nNextTurn);
1994 }
1995
1996 public double getNextTurnAgentQOUtilityValue()
1997 {
1998 return m_GenerateAgreement.getNextTurnAgentQOUtilityValue();
1999 }
2000
2001 public double getNextTurnAgentEquilibriumUtilityValue()
2002 {
2003 return m_GenerateAgreement.getNextTurnAgentEquilibriumUtilityValue();
2004 }
2005
2006 public String getNextTurnAgentQOAgreement()
2007 {
2008 return m_GenerateAgreement.getNextTurnQOAgreement();
2009 }
2010
2011 public String getNextTurnAgentEquilibriumAgreement()
2012 {
2013 return m_GenerateAgreement.getNextTurnEquilibriumAgreement();
2014 }
2015
2016 public double getNextTurnOpponentQOUtilityValue()
2017 {
2018 return m_GenerateAgreement.getNextTurnOpponentQOUtilityValue();
2019 }
2020
2021 public QAgentType getNextTurnOpponentType()
2022 {
2023 QAgentType opponentNextTurnType = null;
2024 int nOppType = m_GenerateAgreement.getNextTurnOpponentType();
2025
2026 if (m_CurrentAgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
2027 {
2028 switch (nOppType)
2029 {
2030 case COMPROMISE_TYPE_IDX:
2031 opponentNextTurnType = getEnglandCompromiseNextTurnType();
2032 break;
2033 case LONG_TERM_TYPE_IDX:
2034 opponentNextTurnType = getEnglandLongTermNextTurnType();
2035 break;
2036 case SHORT_TERM_TYPE_IDX:
2037 opponentNextTurnType = getEnglandShortTermNextTurnType();
2038 break;
2039 default:
2040 System.out.println("[QO]Agent type is unknown [QAgentsCore::getNextTurnOpponentType(1310)]");
2041 System.err.println("[QO]Agent type is unknown [QAgentsCore::getNextTurnOpponentType(1310)]");
2042 break;
2043 }
2044 }
2045 else if (m_CurrentAgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
2046 {
2047 switch (nOppType)
2048 {
2049 case COMPROMISE_TYPE_IDX:
2050 opponentNextTurnType = getZimbabweCompromiseNextTurnType();
2051 break;
2052 case LONG_TERM_TYPE_IDX:
2053 opponentNextTurnType = getZimbabweLongTermNextTurnType();
2054 break;
2055 case SHORT_TERM_TYPE_IDX:
2056 opponentNextTurnType = getZimbabweShortTermNextTurnType();
2057 break;
2058 default:
2059 System.out.println("[QO]Agent type is unknown [QAgentsCore::getNextTurnOpponentType(1329)]");
2060 System.err.println("[QO]Agent type is unknown [QAgentsCore::getNextTurnOpponentType(1329)]");
2061 break;
2062 }
2063 }
2064
2065 return opponentNextTurnType;
2066 }
2067
2068 public void updateOpponentProbability(int CurrentAgreementIdx[], int nCurrentTurn, int nMessageType, int nResponseType)
2069 {
2070 if (nResponseType == QMessages.MESSAGE_RECEIVED)
2071 updateOpponentProbabilityUponMessageReceived(CurrentAgreementIdx, nCurrentTurn, nMessageType);
2072 else if (nResponseType == QMessages.MESSAGE_REJECTED)
2073 updateOpponentProbabilityUponMessageRejected(CurrentAgreementIdx, nCurrentTurn, nMessageType);
2074 }
2075
2076 private void updateOpponentProbabilityUponMessageReceived(int CurrentAgreementIdx[], int nCurrentTurn, int nMessageType)
2077 {
2078 QAgentType agentType = null;
2079 double dPrevTypeProbability = 0;
2080 double dPrevOfferValue = 0;
2081 double dPrevOfferProbability = 0;
2082 double dOfferSum = 0;
2083 double dUpdatedTypeProbability = 0;
2084
2085 if (m_CurrentAgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
2086 {
2087 // calculate posteriori proability using Bayes formula:
2088 // P(type | Ht) = [P(Ht|Type)*P(type)] / P(Ht)
2089 // where P(Ht) = sigam(i=1 to #types)[P(Ht|Type_i) * P(type_i)]
2090 // and P(Ht|Type_i) = luce number of Ht (Ht - last agreement)
2091 // [this is done incrementally after each agreement
2092
2093 // calculate P(Ht)
2094 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2095 {
2096 agentType = (QAgentType)m_EnglandAgentTypesList.get(i);
2097
2098 dPrevTypeProbability = agentType.getTypeProbability();
2099 dPrevOfferValue = agentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
2100 dPrevOfferProbability = agentType.getAgreementLuceValue(dPrevOfferValue, true);
2101
2102 dOfferSum += (dPrevOfferProbability * dPrevTypeProbability);
2103 }
2104
2105 // calculate P(type | Ht) and receiveMessage P(type)
2106 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2107 {
2108 agentType = (QAgentType)m_EnglandAgentTypesList.get(i);
2109
2110 dPrevTypeProbability = agentType.getTypeProbability();
2111 dPrevOfferValue = agentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
2112 dPrevOfferProbability = agentType.getAgreementLuceValue(dPrevOfferValue, true);
2113
2114 dUpdatedTypeProbability = (dPrevOfferProbability * dPrevTypeProbability) / dOfferSum;
2115
2116 System.err.println("%%%%%%%%%%%%%%%%%%%%%%%");
2117 System.err.println("PREV = " + dPrevTypeProbability + ", UP = " + dUpdatedTypeProbability);
2118
2119 agentType.setTypeProbability(dUpdatedTypeProbability);
2120
2121 m_EnglandAgentTypesList.set(i, agentType);
2122 }
2123
2124 PrintWriter pw;
2125 try {
2126 pw = new PrintWriter(new FileWriter(m_sProbFileName + "Eng.txt", true));
2127 pw.println(getEnglandProbabilitiesStr());
2128 pw.close();
2129 } catch (IOException e) {
2130 e.printStackTrace();
2131 }
2132 }
2133 else if (m_CurrentAgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
2134 {
2135 // calculate posteriori proability using Bayes formula:
2136 // P(type | Ht) = [P(Ht|Type)*P(type)] / P(Ht)
2137 // where P(Ht) = sigma(i=1 to #types)[P(Ht|Type_i) * P(type_i)]
2138 // and P(Ht|Type_i) = luce number of Ht (Ht - last agreement)
2139 // [this is done incrementally after each agreement
2140
2141 // calculate P(Ht)
2142 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2143 {
2144 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(i);
2145
2146 dPrevTypeProbability = agentType.getTypeProbability();
2147 dPrevOfferValue = agentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
2148 dPrevOfferProbability = agentType.getAgreementLuceValue(dPrevOfferValue, true);
2149
2150 dOfferSum += (dPrevOfferProbability * dPrevTypeProbability);
2151 }
2152
2153 // calculate P(type | Ht) and receiveMessage P(type)
2154 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2155 {
2156 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(i);
2157
2158 dPrevTypeProbability = agentType.getTypeProbability();
2159 dPrevOfferValue = agentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
2160 dPrevOfferProbability = agentType.getAgreementLuceValue(dPrevOfferValue, true);
2161
2162 dUpdatedTypeProbability = (dPrevOfferProbability * dPrevTypeProbability) / dOfferSum;
2163
2164 System.err.println("%%%%%%%%%%%%%%%%%%%%%%%");
2165 System.err.println("PREV = " + dPrevTypeProbability + ", UP = " + dUpdatedTypeProbability);
2166
2167 agentType.setTypeProbability(dUpdatedTypeProbability);
2168
2169 m_ZimbabweAgentTypesList.set(i, agentType);
2170 }
2171
2172 PrintWriter pw;
2173 try {
2174 pw = new PrintWriter(new FileWriter(m_sProbFileName + "Zim.txt", true));
2175 pw.println(getZimbabweProbabilitiesStr());
2176 pw.close();
2177 } catch (IOException e) {
2178 System.out.println("Error opening QO prob file [QAgentsCore::UpdateOpponentProbabilityUponMessageReceived(1423)]");
2179 System.err.println("Error opening QO prob file [QAgentsCore::UpdateOpponentProbabilityUponMessageReceived(1423)]");
2180 e.printStackTrace();
2181 }
2182 } // end if-else checking the agent's type
2183 }
2184
2185 private void updateOpponentProbabilityUponMessageRejected(int CurrentAgreementIdx[], int nCurrentTurn, int nMessageType)
2186 {
2187 QAgentType agentType = null;
2188 double dPrevTypeProbability = 0;
2189 double dPrevOfferProbability = 0;
2190 double dOfferSum = 0;
2191 double dUpdatedTypeProbability = 0;
2192 double dAgentOfferSum = 0;
2193
2194 String sRejectedMsg = m_CurrentAgentType.getAgreementStr(CurrentAgreementIdx);
2195
2196 if (m_CurrentAgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
2197 {
2198 // calculate posteriori proability using Bayes formula:
2199 // P(type | Ht) = [P(Ht|Type)*P(type)] / P(Ht)
2200 // where P(Ht) = sigma(i=1 to #types)[P(Ht|Type_i) * P(type_i)]
2201 // and P(Ht|Type_i) = luce number of Ht (Ht - last agreement)
2202 // [this is done incrementally after each agreement
2203
2204 // calculate P(Ht)
2205 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2206 {
2207 agentType = (QAgentType)m_EnglandAgentTypesList.get(i);
2208
2209 dOfferSum += agentType.calcRejectionProbabilities(sRejectedMsg, nCurrentTurn);
2210 }
2211
2212 // calculate P(type | Ht) and receiveMessage P(type)
2213 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2214 {
2215 agentType = (QAgentType)m_EnglandAgentTypesList.get(i);
2216
2217 dPrevTypeProbability = agentType.getTypeProbability();
2218
2219 dAgentOfferSum = agentType.calcRejectionProbabilities(sRejectedMsg, nCurrentTurn);
2220
2221 dUpdatedTypeProbability = (dAgentOfferSum * dPrevTypeProbability) / dOfferSum;
2222
2223 System.err.println("%%%%%%%%%%%%%%%%%%%%%%%");
2224 System.err.println("PREV = " + dPrevTypeProbability + ", UP = " + dUpdatedTypeProbability);
2225
2226 agentType.setTypeProbability(dUpdatedTypeProbability);
2227
2228 m_EnglandAgentTypesList.set(i, agentType);
2229 }
2230
2231 PrintWriter pw;
2232 try {
2233 pw = new PrintWriter(new FileWriter(m_sProbFileName + "Eng.txt", true));
2234 pw.println(getEnglandProbabilitiesStr());
2235 pw.close();
2236 } catch (IOException e) {
2237 e.printStackTrace();
2238 }
2239 }
2240 else if (m_CurrentAgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
2241 {
2242 // calculate posteriori proability using Bayes formula:
2243 // P(type | Ht) = [P(Ht|Type)*P(type)] / P(Ht)
2244 // where P(Ht) = sigam(i=1 to #types)[P(Ht|Type_i) * P(type_i)]
2245 // and P(Ht|Type_i) = luce number of Ht (Ht - last agreement)
2246 // [this is done incrementally after each agreement
2247
2248 // calculate P(Ht)
2249 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2250 {
2251 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(i);
2252
2253 dOfferSum += agentType.calcRejectionProbabilities(sRejectedMsg, nCurrentTurn);
2254
2255 dOfferSum += (dPrevOfferProbability * dPrevTypeProbability);
2256 }
2257
2258 // calculate P(type | Ht) and receiveMessage P(type)
2259 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
2260 {
2261 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(i);
2262
2263 dPrevTypeProbability = agentType.getTypeProbability();
2264
2265 dAgentOfferSum = agentType.calcRejectionProbabilities(sRejectedMsg, nCurrentTurn);
2266
2267 dUpdatedTypeProbability = (dAgentOfferSum * dPrevTypeProbability) / dOfferSum;
2268
2269 System.err.println("%%%%%%%%%%%%%%%%%%%%%%%");
2270 System.err.println("PREV = " + dPrevTypeProbability + ", UP = " + dUpdatedTypeProbability);
2271
2272 agentType.setTypeProbability(dUpdatedTypeProbability);
2273
2274 m_ZimbabweAgentTypesList.set(i, agentType);
2275 }
2276
2277 PrintWriter pw;
2278 try {
2279 pw = new PrintWriter(new FileWriter(m_sProbFileName + "Zim.txt", true));
2280 pw.println(getZimbabweProbabilitiesStr());
2281 pw.close();
2282 } catch (IOException e) {
2283 System.out.println("Error opening QO prob file [QAgentsCore::UpdateOpponentProbabilityUponMessageRejected(1525)]");
2284 System.err.println("Error opening QO prob file [QAgentsCore::UpdateOpponentProbabilityUponMessageRejected(1525)]");
2285 e.printStackTrace();
2286 }
2287 } // end if-else checking the agent's type
2288 }
2289
2290 public String getEnglandProbabilitiesStr()
2291 {
2292 String sProbabilitiesStr = "";
2293
2294 QAgentType agentType = null;
2295 double dAgentProbability = 0;
2296
2297 agentType = (QAgentType)m_EnglandAgentTypesList.get(LONG_TERM_TYPE_IDX);
2298 dAgentProbability = agentType.getTypeProbability();
2299
2300 sProbabilitiesStr = "EngLong: " + dAgentProbability;
2301
2302 agentType = (QAgentType)m_EnglandAgentTypesList.get(SHORT_TERM_TYPE_IDX);
2303 dAgentProbability = agentType.getTypeProbability();
2304
2305 sProbabilitiesStr += "; EngShort: " + dAgentProbability;
2306
2307 agentType = (QAgentType)m_EnglandAgentTypesList.get(COMPROMISE_TYPE_IDX);
2308 dAgentProbability = agentType.getTypeProbability();
2309
2310 sProbabilitiesStr += "; EngComp: " + dAgentProbability;
2311
2312 return sProbabilitiesStr;
2313 }
2314
2315 public String getZimbabweProbabilitiesStr()
2316 {
2317 String sProbabilitiesStr = "";
2318
2319 QAgentType agentType = null;
2320 double dAgentProbability = 0;
2321
2322 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(LONG_TERM_TYPE_IDX);
2323 dAgentProbability = agentType.getTypeProbability();
2324
2325 sProbabilitiesStr = "ZimLong: " + dAgentProbability;
2326
2327 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(SHORT_TERM_TYPE_IDX);
2328 dAgentProbability = agentType.getTypeProbability();
2329
2330 sProbabilitiesStr += "; ZimShort: " + dAgentProbability;
2331
2332 agentType = (QAgentType)m_ZimbabweAgentTypesList.get(COMPROMISE_TYPE_IDX);
2333 dAgentProbability = agentType.getTypeProbability();
2334
2335 sProbabilitiesStr += "; ZimComp: " + dAgentProbability;
2336
2337 return sProbabilitiesStr;
2338 }
2339}
Note: See TracBrowser for help on using the repository browser.