source: src/main/java/agents/qoagent/AutomatedAgentsCore.java@ 126

Last change on this file since 126 was 126, checked in by Aron Hammond, 6 years ago

Added function to calculate opposition to MultiLateralAnalysis.java

Moved code to add RLBOA listeners to RLBOAUtils is misc package

Added input for strategyParameters to SessionPanel (gui)

!! close SessionInfo after tournament; this caused /tmp/ to fill up with GeniusData files

Our own package:

  • Added opponents and strategies that are mentioned in the report
  • Change class hierarchy, agents can now extend from RLBOAagentBilateral to inherit RL functionality.
  • States extend from AbstractState
File size: 30.9 KB
Line 
1package agents.qoagent;
2
3import java.io.BufferedReader;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.StringTokenizer;
9
10
11/*
12 * Created on 11/09/2004
13 *
14 */
15
16/**
17 * @author raz
18 * @version 1.0
19 *
20 * AutomatedAgentsCore class:
21 * In charge of handling the different agent's types.
22 * In charge for returning the agreements for the desired type.
23 *
24 * @see AutomatedAgent
25 * @see AutomatedAgentType
26 */
27
28public class AutomatedAgentsCore {
29 //General constants for reading the utility data from the utilities fils
30 public static final String COMMENT_CHAR_STR = "#";
31 public static final String ISSUE_HEADER_STR = "!";
32 public static final String ISSUE_SEPARATOR_STR = "*";
33 public static final String VALUES_UTILITY_SEPARATOR_STR = " ";
34 public static final String VALUES_NAMES_SEPARATOR_STR = "~";
35 public static final String GENERAL_DATA_SEPARATOR_STR = "@";
36 public static final String TIME_EFFECT_STR = "Time-Effect";
37 public static final String OPT_OUT_STR = "Opt-Out";
38 public static final String STATUS_QUO_STR = "Status-Quo";
39 public static final int TIME_EFFECT_IND = 0;
40 public static final int OPT_OUT_IND = 1;
41 public static final int STATUS_QUO_IND = 2;
42 public static final int GENERAL_VALUES_NUM = 3;
43
44 /*
45 * @@ This version supports 3 types of utilities:
46 * Long term, short term and compromise orientation type
47 */
48 public static final int LONG_TERM_TYPE_IDX = 0;
49 public static final int SHORT_TERM_TYPE_IDX = 1;
50 public static final int COMPROMISE_TYPE_IDX = 2;
51 public static final int AGENT_TYPES_NUM = 3;
52 private static final String SIDE_B_COMPROMISE_UTILITY_FILE = "utilitySide_BCompromise.txt";
53 private static final String SIDE_B_SHORT_TERM_UTILITY_FILE = "utilitySide_BShortTerm.txt";
54 private static final String SIDE_B_LONG_TERM_UTILITY_FILE = "utilitySide_BLongTerm.txt";
55 private static final String SIDE_A_COMPROMISE_UTILITY_FILE = "utilitySide_ACompromise.txt";
56 private static final String SIDE_A_SHORT_TERM_UTILITY_FILE = "utilitySide_AShortTerm.txt";
57 private static final String SIDE_A_LONG_TERM_UTILITY_FILE = "utilitySide_ALongTerm.txt";
58 private static final String SIDE_B_COMPROMISE_NAME = "SIDE_B_COMPROMISE";
59 private static final String SIDE_B_LONG_TERM_NAME = "SIDE_B_LONG_TERM";
60 private static final String SIDE_B_SHORT_TERM_NAME = "SIDE_B_SHORT_TERM";
61 private static final String SIDE_A_COMPROMISE_NAME = "SIDE_A_COMPROMISE";
62 private static final String SIDE_A_LONG_TERM_NAME = "SIDE_A_LONG_TERM";
63 private static final String SIDE_A_SHORT_TERM_NAME = "SIDE_A_SHORT_TERM";
64
65
66 // list of all possible Side A agent types (Eng/Emp)
67 // each value is FullUtility
68 private ArrayList<AutomatedAgentType> m_SideA_AgentTypesList;
69 // list of all possible Side B types (Zim/Job Can)
70 // each value is FullUtility
71 private ArrayList<AutomatedAgentType> m_SideB_AgentTypesList;
72
73 // list of all possible Side A agent types (Eng/Emp)
74 // with values of the next turn
75 // each value is FullUtility
76 private ArrayList<AutomatedAgentType> m_SideA_AgentTypesNextTurnList;
77 // list of all possible Side B agent types (Zim/Job Can)
78 // with values of the next turn
79 // each value is FullUtility
80 private ArrayList<AutomatedAgentType> m_SideB_AgentTypesNextTurnList;
81
82 // automated agent utility values for current turn
83 private AutomatedAgentType m_CurrentAgentType;
84 // automated agent utility values for next turn
85 private AutomatedAgentType m_CurrentAgentNextTurnType;
86 // opponent type in the next turn
87 private int m_nNextTurnOppType;
88
89 // log file name
90 private String m_sLogFileName;
91
92 // inner class for calculating the agreement the
93 // automated agent will offer
94 private AutomatedAgentGenerateAgreement m_GenerateAgreement;
95 private AgentTools agentTools = null;
96 private AbstractAutomatedAgent abstractAgent = null;
97
98 public class AutomatedAgentGenerateAgreement
99 {
100 class AutomatedAgentCombinedAgreement
101 {
102 public double m_dAgentAgreementValue;
103 public double m_dOpponentAgreementValue;
104 public String m_sAgreement;
105 }
106
107 private double m_dAutomatedAgentValue, m_dNextTurnAutomatedAgentValue, m_dOppSelectedValue, m_dAgentSelectedNextTurnValue, m_dOppSelectedNextTurnValue;
108 private String m_sAgreement, m_sNextTurnAgreement;
109
110 public AutomatedAgentGenerateAgreement()
111 {
112 m_dAutomatedAgentValue = AutomatedAgentType.VERY_SMALL_NUMBER;
113 m_dNextTurnAutomatedAgentValue = AutomatedAgentType.VERY_SMALL_NUMBER;
114 m_nNextTurnOppType = AutomatedAgentType.NO_TYPE;
115 m_sAgreement = "";
116 m_sNextTurnAgreement = "";
117 }
118
119 /**
120 * Calculate agreement to send for the opponent for a given agent and a given turn
121 * @param agentType - the given agent
122 * @param nCurrentTurn - the current turn
123 * @param bCalcForNextTurn - whether to calculate based on values of the following turn
124 * PRE-CONDITION: m_CurrentAgentType should be updated for the current turn
125 */
126 public void calculateAgreement(AutomatedAgentType agentType, int nCurrentTurn, boolean bCalcForNextTurn)
127 {
128 if (bCalcForNextTurn)
129 m_CurrentAgentNextTurnType = agentType;
130 else
131 m_CurrentAgentType = agentType;
132
133 // if the automated agent is of Side B (Zim/Job Can) need to calculate
134 // offer against opponent of Side A and vice versa
135 if (m_CurrentAgentType.isTypeOf(AutomatedAgentType.SIDE_B_TYPE))
136 calculateOfferAgainstOpponent(AutomatedAgent.SIDE_A_NAME, nCurrentTurn, bCalcForNextTurn);
137 else if (m_CurrentAgentType.isTypeOf(AutomatedAgentType.SIDE_A_TYPE))
138 calculateOfferAgainstOpponent(AutomatedAgent.SIDE_B_NAME, nCurrentTurn, bCalcForNextTurn);
139 else
140 {
141 System.out.println("[AA]Agent type is unknown [AutomatedAgentsCore::calculateAgreement(204)]");
142 System.err.println("[AA]Agent type is unknown [AutomatedAgentsCore::calculateAgreement(204)]");
143 }
144 }
145
146 /**
147 * This is where the real logic is done to
148 * calculate agreement to send for the opponent for a given agent and a given turn
149 * @param agentType - the given agent
150 * @param nCurrentTurn - the current turn
151 * @param bCalcForNextTurn - whether to calculate based on values of the following turn
152 * PRE-CONDITION: m_CurrentAgentType should be updated for the current turn
153 */
154 public void calculateOfferAgainstOpponent(String sOpponentType, int nCurrentTurn, boolean bCalcForNextTurn)
155 {
156 m_dAutomatedAgentValue = AutomatedAgentType.VERY_SMALL_NUMBER;
157 m_dNextTurnAutomatedAgentValue = AutomatedAgentType.VERY_SMALL_NUMBER;
158 m_nNextTurnOppType = AutomatedAgentType.NO_TYPE;
159
160 if (bCalcForNextTurn)
161 abstractAgent.calculateOfferAgainstOpponent(m_CurrentAgentNextTurnType, sOpponentType, nCurrentTurn);
162 else // calc for current turn
163 abstractAgent.calculateOfferAgainstOpponent(m_CurrentAgentType, sOpponentType, nCurrentTurn);
164 }
165
166 public String getSelectedAutomatedAgentAgreementStr()
167 {
168 return m_sAgreement;
169 }
170
171 public double getNextTurnAgentAutomatedAgentUtilityValue()
172 {
173 return m_dAgentSelectedNextTurnValue;
174 }
175
176 public String getNextTurnAutomatedAgentAgreement()
177 {
178 return m_sNextTurnAgreement;
179 }
180
181 public double getNextTurnOpponentAutomatedAgentUtilityValue()
182 {
183 return m_dOppSelectedNextTurnValue;
184 }
185
186 public int getNextTurnOpponentType()
187 {
188 return m_nNextTurnOppType;
189 }
190
191 public void setNextTurnOpponentType(int type) {
192 m_nNextTurnOppType = type;
193 }
194
195 };
196
197 /**
198 * Initializes the agent's core.
199 * Creates the different Side A agent types (Eng/Emp) and Side B agent types (Zim/Job Can).
200 */
201 public AutomatedAgentsCore(String sFileName, String sNow, AgentTools agentTools, AbstractAutomatedAgent abstractAgent)
202 {
203 setAgentTools(agentTools);
204 setAbstractAgent(abstractAgent);
205 m_CurrentAgentNextTurnType = null;
206 m_SideA_AgentTypesNextTurnList = new ArrayList<AutomatedAgentType>();
207 m_SideB_AgentTypesNextTurnList = new ArrayList<AutomatedAgentType>();
208
209 m_sLogFileName = sFileName;
210
211 m_CurrentAgentType = null;
212
213 m_SideA_AgentTypesList = new ArrayList<AutomatedAgentType>();
214 m_SideB_AgentTypesList = new ArrayList<AutomatedAgentType>();
215
216
217 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
218 {
219 m_SideA_AgentTypesList.add(i, new AutomatedAgentType());
220 m_SideB_AgentTypesList.add(i, new AutomatedAgentType());
221
222 m_SideA_AgentTypesNextTurnList.add(i, new AutomatedAgentType());
223 m_SideB_AgentTypesNextTurnList.add(i, new AutomatedAgentType());
224 }
225
226 /*
227 * @@ This version supports 3 types of utilities:
228 * Long term, short term and compromise orientation type
229 */
230 createSideALongTermType();
231 createSideAShortTermType();
232 createSideACompromiseType();
233
234 createSideBLongTermType();
235 createSideBShortTermType();
236 createSideBCompromiseType();
237 }
238
239 /**
240 * @return AutomatedAgentType - Side A (Eng/Emp) long term type
241 */
242 public AutomatedAgentType getSideALongTermType()
243 {
244 return (AutomatedAgentType)m_SideA_AgentTypesList.get(LONG_TERM_TYPE_IDX);
245 }
246
247 /**
248 * @return AutomatedAgentType - Side A (Eng/Emp) short term type
249 */
250 public AutomatedAgentType getSideAShortTermType()
251 {
252 return (AutomatedAgentType)m_SideA_AgentTypesList.get(SHORT_TERM_TYPE_IDX);
253 }
254
255 /**
256 * @return AutomatedAgentType - Side A (Eng/Emp) compromise type
257 */
258 public AutomatedAgentType getSideACompromiseType()
259 {
260 return (AutomatedAgentType)m_SideA_AgentTypesList.get(COMPROMISE_TYPE_IDX);
261 }
262
263 /**
264 * @return AutomatedAgentType - Side B (Zim/Job Can) long term type
265 */
266 public AutomatedAgentType getSideBLongTermType()
267 {
268 return (AutomatedAgentType)m_SideB_AgentTypesList.get(LONG_TERM_TYPE_IDX);
269 }
270
271 /**
272 * @return AutomatedAgentType - Side B (Zim/Job Can) short term type
273 */
274 public AutomatedAgentType getSideBShortTermType()
275 {
276 return (AutomatedAgentType)m_SideB_AgentTypesList.get(SHORT_TERM_TYPE_IDX);
277 }
278
279 /**
280 * @return AutomatedAgentType - Side B (Zim/Job Can) compromise type
281 */
282 public AutomatedAgentType getSideBCompromiseType()
283 {
284 return (AutomatedAgentType)m_SideB_AgentTypesList.get(COMPROMISE_TYPE_IDX);
285 }
286
287 /**
288 * @return AutomatedAgentType - Side A (Eng/Emp) long term type
289 */
290 public AutomatedAgentType getSideALongTermNextTurnType()
291 {
292 return (AutomatedAgentType)m_SideA_AgentTypesNextTurnList.get(LONG_TERM_TYPE_IDX);
293 }
294
295 /**
296 * @return AutomatedAgentType - Side A (Eng/Emp) short term type
297 */
298 public AutomatedAgentType getSideAShortTermNextTurnType()
299 {
300 return (AutomatedAgentType)m_SideA_AgentTypesNextTurnList.get(SHORT_TERM_TYPE_IDX);
301 }
302
303 /**
304 * @return AutomatedAgentType - Side A (Eng/Emp) compromise type
305 */
306 public AutomatedAgentType getSideACompromiseNextTurnType()
307 {
308 return (AutomatedAgentType)m_SideA_AgentTypesNextTurnList.get(COMPROMISE_TYPE_IDX);
309 }
310
311 /**
312 * @return AutomatedAgentType - Side B (Zim/Job Can) long term type
313 */
314 public AutomatedAgentType getSideBLongTermNextTurnType()
315 {
316 return (AutomatedAgentType)m_SideB_AgentTypesNextTurnList.get(LONG_TERM_TYPE_IDX);
317 }
318
319 /**
320 * @return AutomatedAgentType - Side B (Zim/Job Can) short term type
321 */
322 public AutomatedAgentType getSideBShortTermNextTurnType()
323 {
324 return (AutomatedAgentType)m_SideB_AgentTypesNextTurnList.get(SHORT_TERM_TYPE_IDX);
325 }
326
327 /**
328 * @return AutomatedAgentType - Side B (Zim/Job Can) compromise type
329 */
330 public AutomatedAgentType getSideBCompromiseNextTurnType()
331 {
332 return (AutomatedAgentType)m_SideB_AgentTypesNextTurnList.get(COMPROMISE_TYPE_IDX);
333 }
334
335 /**
336 * Creates Side B (Zim/Job Can) compromise type from the utility file.
337 * Saves the type in m_SideB_AgentTypesList
338 */
339 private void createSideBCompromiseType()
340 {
341 AutomatedAgentType compromiseType = new AutomatedAgentType();
342 compromiseType.setAgentType(AutomatedAgentType.SIDE_B_TYPE);
343
344 String sFileName = SIDE_B_COMPROMISE_UTILITY_FILE;
345
346 createAgentTypeFromFile(sFileName, compromiseType);
347
348 compromiseType.setName(SIDE_B_COMPROMISE_NAME);
349 m_SideB_AgentTypesList.set(COMPROMISE_TYPE_IDX, compromiseType);
350
351 AutomatedAgentType agentTypeNextTurn = compromiseType;
352 // since this agent contains the utility values for the next turn, at the beginning
353 // it should be initialized with the second turn values
354 agentTypeNextTurn.calculateValues(abstractAgent, 2);
355 m_SideB_AgentTypesNextTurnList.set(COMPROMISE_TYPE_IDX, agentTypeNextTurn);
356 }
357
358 /**
359 * Creates Side B (Zim/Job Can) short term type from the utility file.
360 * Saves the type in m_SideB_AgentTypesList
361 */
362 private void createSideBShortTermType()
363 {
364 AutomatedAgentType shortTermType = new AutomatedAgentType();
365 shortTermType.setAgentType(AutomatedAgentType.SIDE_B_TYPE);
366
367 String sFileName = SIDE_B_SHORT_TERM_UTILITY_FILE;
368
369 createAgentTypeFromFile(sFileName, shortTermType);
370
371 shortTermType.setName(SIDE_B_SHORT_TERM_NAME);
372 m_SideB_AgentTypesList.set(SHORT_TERM_TYPE_IDX, shortTermType);
373
374 AutomatedAgentType agentTypeNextTurn = shortTermType;
375 // since this agent contains the utility values for the next turn, at the beginning
376 // it should be initialized with the second turn values
377 agentTypeNextTurn.calculateValues(abstractAgent, 2);
378 m_SideB_AgentTypesNextTurnList.set(SHORT_TERM_TYPE_IDX, agentTypeNextTurn);
379 }
380
381 /**
382 * Creates Side B (Zim/Job Can) long term type from the utility file.
383 * Saves the type in m_SideB_AgentTypesList
384 */
385 private void createSideBLongTermType()
386 {
387 AutomatedAgentType longTermType = new AutomatedAgentType();
388 longTermType.setAgentType(AutomatedAgentType.SIDE_B_TYPE);
389
390 String sFileName = SIDE_B_LONG_TERM_UTILITY_FILE;
391
392 createAgentTypeFromFile(sFileName, longTermType);
393
394 longTermType.setName(SIDE_B_LONG_TERM_NAME);
395 m_SideB_AgentTypesList.set(LONG_TERM_TYPE_IDX, longTermType);
396
397 AutomatedAgentType agentTypeNextTurn = longTermType;
398 // since this agent contains the utility values for the next turn, at the beginning
399 // it should be initialized with the second turn values
400 agentTypeNextTurn.calculateValues(abstractAgent, 2);
401 m_SideB_AgentTypesNextTurnList.set(LONG_TERM_TYPE_IDX, agentTypeNextTurn);
402 }
403
404 /**
405 * Creates Side A (Eng/Emp) comrpomise type from the utility file.
406 * Saves the type in m_SideA_AgentTypesList
407 */
408 private void createSideACompromiseType()
409 {
410 AutomatedAgentType compromiseType = new AutomatedAgentType();
411 compromiseType.setAgentType(AutomatedAgentType.SIDE_A_TYPE);
412
413 String sFileName = SIDE_A_COMPROMISE_UTILITY_FILE;
414
415 createAgentTypeFromFile(sFileName, compromiseType);
416
417 compromiseType.setName(SIDE_A_COMPROMISE_NAME);
418
419 m_SideA_AgentTypesList.set(COMPROMISE_TYPE_IDX, compromiseType);
420
421 AutomatedAgentType agentTypeNextTurn = compromiseType;
422 // since this agent contains the utility values for the next turn, at the beginning
423 // it should be initialized with the second turn values
424 agentTypeNextTurn.calculateValues(abstractAgent, 2);
425 m_SideA_AgentTypesNextTurnList.set(COMPROMISE_TYPE_IDX, agentTypeNextTurn);
426 }
427
428 /**
429 * Creates Side A (Eng/Emp) short term type from the utility file.
430 * Saves the type in m_SideA_AgentTypesList
431 */
432 private void createSideAShortTermType()
433 {
434 AutomatedAgentType shortTermType = new AutomatedAgentType();
435 shortTermType.setAgentType(AutomatedAgentType.SIDE_A_TYPE);
436
437 String sFileName = SIDE_A_SHORT_TERM_UTILITY_FILE;
438
439 createAgentTypeFromFile(sFileName, shortTermType);
440
441 shortTermType.setName(SIDE_A_SHORT_TERM_NAME);
442 m_SideA_AgentTypesList.set(SHORT_TERM_TYPE_IDX, shortTermType);
443
444 AutomatedAgentType agentTypeNextTurn = shortTermType;
445 // since this agent contains the utility values for the next turn, at the beginning
446 // it should be initialized with the second turn values
447 agentTypeNextTurn.calculateValues(abstractAgent, 2);
448 m_SideA_AgentTypesNextTurnList.set(SHORT_TERM_TYPE_IDX, agentTypeNextTurn);
449 }
450
451 /**
452 * Creates Side A (Eng/Emp) long term type from the utility file.
453 * Saves the type in m_SideA_AgentTypesList
454 */
455 private void createSideALongTermType()
456 {
457 AutomatedAgentType longTermType = new AutomatedAgentType();
458 longTermType.setAgentType(AutomatedAgentType.SIDE_A_TYPE);
459
460 String sFileName = SIDE_A_LONG_TERM_UTILITY_FILE;
461
462 createAgentTypeFromFile(sFileName, longTermType);
463
464 longTermType.setName(SIDE_A_LONG_TERM_NAME);
465 m_SideA_AgentTypesList.set(LONG_TERM_TYPE_IDX, longTermType);
466
467 AutomatedAgentType agentTypeNextTurn = longTermType;
468 // since this agent contains the utility values for the next turn, at the beginning
469 // it should be initialized with the second turn values
470 agentTypeNextTurn.calculateValues(abstractAgent, 2);
471 m_SideA_AgentTypesNextTurnList.set(LONG_TERM_TYPE_IDX, agentTypeNextTurn);
472 }
473
474 /**
475 * Creates the specific agent type from the file name
476 * Returns the new type in agentType.
477 * @param sFileName - the file name of the agent's type
478 * @param agentType - the returned agent
479 * Note: this function is identical to readUtilityFile in the Client
480 */
481 private void createAgentTypeFromFile(String sFileName, AutomatedAgentType agentType)
482 {
483 BufferedReader br = null;
484 String line;
485
486 double dGeneralValues[] = new double[GENERAL_VALUES_NUM];
487
488 // init values to default
489 dGeneralValues[TIME_EFFECT_IND] = 0;
490 dGeneralValues[STATUS_QUO_IND] = AutomatedAgentType.VERY_SMALL_NUMBER;
491 dGeneralValues[OPT_OUT_IND] = AutomatedAgentType.VERY_SMALL_NUMBER;
492
493 try {
494 br = new BufferedReader(new FileReader(sFileName));
495
496 line = br.readLine();
497 while (line != null)
498 {
499 // if comment line - continue
500 if (line.startsWith(COMMENT_CHAR_STR))
501 line = br.readLine();
502 else
503 {
504 line = readUtilityDetails(br, line, agentType.m_fullUtility.lstUtilityDetails, dGeneralValues);
505
506 agentType.m_fullUtility.dTimeEffect = dGeneralValues[TIME_EFFECT_IND];
507 agentType.m_fullUtility.dStatusQuoValue = dGeneralValues[STATUS_QUO_IND];
508 agentType.m_fullUtility.dOptOutValue = dGeneralValues[OPT_OUT_IND];
509 } // end if-else comment line?
510 } // end while - read utility details
511
512 // calculate values for the first turn
513 agentType.calculateValues(abstractAgent, 1);
514
515 br.close();
516 } catch (FileNotFoundException e) {
517 System.out.println("[AA]Error reading " + sFileName + ": " + e.getMessage() + " [AutomatedAgentsCore::createAgentTypeFromFile(1059)]");
518 System.err.println("[AA]Error reading " + sFileName + ": " + e.getMessage() + " [AutomatedAgentsCore::createAgentTypeFromFile(1059)]");
519 e.printStackTrace();
520 System.exit(1);
521 } catch (IOException e1) {
522 System.out.println("[AA]Error reading from " + sFileName + ": " + e1.getMessage() + " [AutomatedAgentsCore::createAgentTypeFromFile(1065)]");
523 System.err.println("[AA]Error reading from " + sFileName + ": " + e1.getMessage() + " [AutomatedAgentsCore::createAgentTypeFromFile(105659)]");
524 e1.printStackTrace();
525 System.exit(1);
526 }
527 }
528
529 /**
530 * Read the utility details from the agent's file
531 * @param br - the reader of the file
532 * @param line - the read line
533 * @param lstUtilityDetails - list of the utility details
534 * @param dGeneralValues - array of the general values
535 * @return line - the new line
536 */
537 public String readUtilityDetails(BufferedReader br, String line, ArrayList<UtilityDetails> lstUtilityDetails, double dGeneralValues[])
538 {
539 UtilityDetails utilityDetails = null;
540
541 StringTokenizer st = new StringTokenizer(line);
542
543 String sTitle = st.nextToken();
544
545 if (sTitle.equals(GENERAL_DATA_SEPARATOR_STR)) // general details line
546 {
547 String sType = st.nextToken();
548
549 String sValue = st.nextToken();
550 Double dTemp = new Double(sValue);
551
552 if (sType.equals(TIME_EFFECT_STR))
553 dGeneralValues[TIME_EFFECT_IND] = dTemp.doubleValue();
554 if (sType.equals(STATUS_QUO_STR))
555 dGeneralValues[STATUS_QUO_IND] = dTemp.doubleValue();
556 if (sType.equals(OPT_OUT_STR))
557 dGeneralValues[OPT_OUT_IND] = dTemp.doubleValue();
558
559 try {
560 line = br.readLine();
561 } catch (IOException e) {
562 System.out.println("[AA]IOException: Error reading file: " + e.getMessage() + " [AutomatedAgentsCore::readUtilityDetails(1105)]");
563 System.err.println("[AA]IOException: Error reading file: " + e.getMessage() + " [AutomatedAgentsCore::readUtilityDetails(1105)]");
564 }
565 }
566 else if (sTitle.equals(ISSUE_HEADER_STR))
567 {
568 utilityDetails = new UtilityDetails();
569
570 // need to add new element to the utilityDetails list
571
572 // get the title
573 sTitle = line.substring(1);
574 sTitle.trim();
575
576 utilityDetails.sTitle = sTitle;
577
578 try {
579 do {
580 line = br.readLine();
581 } while ( (line != null) && (line.startsWith(COMMENT_CHAR_STR)));
582
583 while (line != null && !line.startsWith(ISSUE_HEADER_STR))
584 {
585 // get the attribute name and side
586 UtilityIssue utilityIssue = new UtilityIssue();
587 utilityIssue.sAttributeName = line.substring(0, line.indexOf(ISSUE_SEPARATOR_STR));
588 String sTemp = line.substring(line.indexOf(ISSUE_SEPARATOR_STR) + 1);
589 utilityIssue.sSide = sTemp.substring(0, sTemp.indexOf(ISSUE_SEPARATOR_STR));
590 sTemp = sTemp.substring(sTemp.indexOf(ISSUE_SEPARATOR_STR) + 1);
591 utilityIssue.dAttributeWeight = new Double(sTemp).doubleValue();
592
593 do{ //skips comment lines
594 line=br.readLine();
595 }while((line!=null)&&(line.startsWith(COMMENT_CHAR_STR)));
596
597 // read values line
598 if (line != null && !line.startsWith(ISSUE_HEADER_STR))
599 {
600 String sUtilityLine;
601 // read utility values line
602 do{
603 sUtilityLine=br.readLine();
604 }while((sUtilityLine!=null)&&(sUtilityLine.startsWith(COMMENT_CHAR_STR)));
605 String sTimeEffectLine = "";
606
607 StringTokenizer stUtilities = null;
608 StringTokenizer stTimeEffect = null;
609
610 if (sUtilityLine != null && !sUtilityLine.startsWith(ISSUE_HEADER_STR))
611 {
612 stUtilities = new StringTokenizer(sUtilityLine, VALUES_UTILITY_SEPARATOR_STR);
613
614 // read time effect line
615 do{
616 sTimeEffectLine=br.readLine();
617 }while((sTimeEffectLine!=null)&&(sTimeEffectLine.startsWith(COMMENT_CHAR_STR)));
618
619 if (sTimeEffectLine != null && !sTimeEffectLine.startsWith(ISSUE_HEADER_STR))
620 stTimeEffect = new StringTokenizer(sTimeEffectLine);
621 }
622
623 // get values
624 StringTokenizer stValues=new StringTokenizer(line, VALUES_NAMES_SEPARATOR_STR);
625
626 // go over all values
627 while (stValues.hasMoreTokens())
628 {
629 UtilityValue utilityValue = new UtilityValue();
630
631 utilityValue.sValue = stValues.nextToken();
632
633 // get corresponding utility value
634 if (stUtilities != null && stUtilities.hasMoreTokens())
635 {
636 utilityValue.dUtility = new Double(stUtilities.nextToken()).doubleValue();
637 //++utilityValue.dUtility += NORMALIZE_INCREMENTOR;//TODO: Currently not using normalize incrementor
638 }
639
640 // get corresponding time effect value
641 if (stTimeEffect != null && stTimeEffect.hasMoreTokens())
642 {
643 utilityValue.dTimeEffect = new Double(stTimeEffect.nextToken()).doubleValue();
644 }
645
646 utilityIssue.lstUtilityValues.add(utilityValue);
647 }
648
649 // read explanation
650 do{
651 line=br.readLine();
652 }while((line!=null)&&(line.startsWith(COMMENT_CHAR_STR)));
653
654 if (line != null && !line.startsWith(ISSUE_HEADER_STR))
655 {
656 StringTokenizer stExp = new StringTokenizer(line);
657 int i = 0;
658 while (stExp.hasMoreTokens())
659 {
660 if (i < 6)
661 {
662 utilityIssue.sExplanation += stExp.nextToken() + " ";
663 i++;
664 }
665 else
666 {
667 utilityIssue.sExplanation += "\n" + stExp.nextToken() + " ";
668 i = 0;
669 }
670 }
671
672 // read next line for the next iteration
673 do{
674 line=br.readLine();
675 }while((line!=null)&&(line.startsWith(COMMENT_CHAR_STR)));
676 }
677 } // end if - line starts with ! (ISSUE_HEADER_STR)
678
679 utilityDetails.lstUtilityIssues.add(utilityIssue);
680 } // end while - reading attributes
681 } catch (IOException e) {
682 System.out.println("[AA]IOException: Error reading file: " + e.getMessage() + " [AutomatedAgentsCore::readUtilityDetails(1225)]");
683 System.err.println("[AA]IOException: Error reading file: " + e.getMessage() + " [AutomatedAgentsCore::readUtilityDetails(1225)]");
684 }
685
686 lstUtilityDetails.add(utilityDetails);
687 } // end if - line starts new issue
688
689 return line;
690 }
691
692 /**
693 * Update the agreement values based on a given turn
694 * @param ntimePeriod - the specific turn
695 */
696 public void updateAgreementsValues(int nTimePeriod)
697 {
698 AutomatedAgentType agentType = null;
699 AutomatedAgentType agentTypeNextTurn = null;
700 for (int i = 0; i < AGENT_TYPES_NUM; ++i)
701 {
702 agentType = (AutomatedAgentType)m_SideA_AgentTypesList.get(i);
703 agentType.calculateValues(abstractAgent, nTimePeriod);
704 m_SideA_AgentTypesList.set(i, agentType);
705
706 agentTypeNextTurn = agentType;
707 agentTypeNextTurn.calculateValues(abstractAgent, nTimePeriod + 1);
708 m_SideA_AgentTypesNextTurnList.set(i, agentTypeNextTurn);
709
710 agentType = (AutomatedAgentType)m_SideB_AgentTypesList.get(i);
711 agentType.calculateValues(abstractAgent, nTimePeriod);
712 m_SideB_AgentTypesList.set(i, agentType);
713
714 agentTypeNextTurn = agentType;
715 agentTypeNextTurn.calculateValues(abstractAgent, nTimePeriod + 1);
716 m_SideB_AgentTypesNextTurnList.set(i, agentTypeNextTurn);
717 }
718 }
719
720 /**
721 * Initialize the GenerateAgreement classs by a given agent
722 * @param agentType - the given agent
723 */
724 public void initGenerateAgreement(AutomatedAgentType agentType)
725 {
726 m_CurrentAgentType = agentType;
727
728 m_GenerateAgreement = new AutomatedAgentGenerateAgreement();
729 }
730
731 /**
732 * Calculate agreement to send for the opponent for a given agent and a given turn
733 * @param agentType - the given agent
734 * @param nCurrentTurn - the current turn
735 */
736 public void calculateAgreement(AutomatedAgentType agentType, int nCurrentTurn)
737 {
738 m_GenerateAgreement.calculateAgreement(agentType, nCurrentTurn, false);
739 }
740
741 /**
742 * Return the agreement the automated agent selected to offer
743 * @return String - the automated agent offer as String
744 */
745 public String getAutomatedAgentAgreement()
746 {
747 return m_GenerateAgreement.getSelectedAutomatedAgentAgreementStr();
748 }
749
750 /**
751 * Calculate agreement for a given agent for the following turn
752 * @param agentType - the given agent
753 * @param nNextTurn - the next turn
754 */
755 public void calculateNextTurnAgreement(AutomatedAgentType agentType, int nNextTurn)
756 {
757 m_GenerateAgreement.calculateAgreement(agentType, nNextTurn, true);
758 }
759
760 /**
761 * Return the agreement the automated agent selected to offer based on next turn values
762 * @return double - the automated agent offer's value
763 */
764 public double getNextTurnAutomatedAgentUtilityValue()
765 {
766 return m_GenerateAgreement.getNextTurnAgentAutomatedAgentUtilityValue();
767 }
768
769 /**
770 * Return the agreement the automated agent selected to offer based on next turn values
771 * @return String - the automated agent offer as String
772 */
773 public String getNextTurnAutomatedAgentAgreement()
774 {
775 return m_GenerateAgreement.getNextTurnAutomatedAgentAgreement();
776 }
777
778 /**
779 * Return the opponnet's value in the next turn for the agreement the automated agent selected
780 * @return double - the opponent's agent offer's value
781 */
782 public double getNextTurnOpponentAutomatedAgentUtilityValue()
783 {
784 return m_GenerateAgreement.getNextTurnOpponentAutomatedAgentUtilityValue();
785 }
786
787 /**
788 * Return the type of the opponent
789 * @return AutomatedAgentType - the opponent's type
790 */
791 public AutomatedAgentType getNextTurnOpponentType()
792 {
793 AutomatedAgentType opponentNextTurnType = null;
794 int nOppType = m_GenerateAgreement.getNextTurnOpponentType();
795
796 if (m_CurrentAgentType.isTypeOf(AutomatedAgentType.SIDE_B_TYPE))
797 {
798 switch (nOppType)
799 {
800 case COMPROMISE_TYPE_IDX:
801 opponentNextTurnType = getSideACompromiseNextTurnType();
802 break;
803 case LONG_TERM_TYPE_IDX:
804 opponentNextTurnType = getSideALongTermNextTurnType();
805 break;
806 case SHORT_TERM_TYPE_IDX:
807 opponentNextTurnType = getSideAShortTermNextTurnType();
808 break;
809 default:
810 System.out.println("[AA]Agent type is unknown [AutomatedAgentsCore::getNextTurnOpponentType(1310)]");
811 System.err.println("[AA]Agent type is unknown [AutomatedAgentsCore::getNextTurnOpponentType(1310)]");
812 break;
813 }
814 }
815 else if (m_CurrentAgentType.isTypeOf(AutomatedAgentType.SIDE_A_TYPE))
816 {
817 switch (nOppType)
818 {
819 case COMPROMISE_TYPE_IDX:
820 opponentNextTurnType = getSideBCompromiseNextTurnType();
821 break;
822 case LONG_TERM_TYPE_IDX:
823 opponentNextTurnType = getSideBLongTermNextTurnType();
824 break;
825 case SHORT_TERM_TYPE_IDX:
826 opponentNextTurnType = getSideBShortTermNextTurnType();
827 break;
828 default:
829 System.out.println("[AA]Agent type is unknown [AutomatedAgentsCore::getNextTurnOpponentType(1329)]");
830 System.err.println("[AA]Agent type is unknown [AutomatedAgentsCore::getNextTurnOpponentType(1329)]");
831 break;
832 }
833 }
834
835 return opponentNextTurnType;
836 }
837
838 public void setAgentTools(AgentTools agentTools) {
839 this.agentTools = agentTools;
840 }
841
842 public void setAbstractAgent(AbstractAutomatedAgent abstractAgent) {
843 this.abstractAgent = abstractAgent;
844 }
845
846 public double getNextTurnAutomatedAgentValue() {
847 return m_GenerateAgreement.m_dNextTurnAutomatedAgentValue;
848 }
849
850 public double getCurrentTurnAutomatedAgentValue() {
851 return m_GenerateAgreement.m_dAutomatedAgentValue;
852 }
853
854 public void setNextTurnAutomatedAgentValue(double agreementValue) {
855 m_GenerateAgreement.m_dNextTurnAutomatedAgentValue = agreementValue;
856 }
857
858 public void setCurrentTurnAutomatedAgentValue(double agreementValue) {
859 m_GenerateAgreement.m_dAutomatedAgentValue = agreementValue;
860 }
861
862 public void setNextTurnAutomatedAgentSelectedValue(double agreementValue) {
863 m_GenerateAgreement.m_dAgentSelectedNextTurnValue = agreementValue;
864 }
865
866 public void setNextTurnOpponentSelectedValue(double agreementValue) {
867 m_GenerateAgreement.m_dOppSelectedNextTurnValue = agreementValue;
868 }
869
870 public void setCurrentTurnOpponentSelectedValue(double agreementValue) {
871 m_GenerateAgreement.m_dOppSelectedValue = agreementValue;
872 }
873
874 public void setNextTurnAgreementString(String agreementStr) {
875 m_GenerateAgreement.m_sNextTurnAgreement = agreementStr;
876 }
877
878 public void setCurrentTurnAgreementString(String agreementStr) {
879 m_GenerateAgreement.m_sAgreement = agreementStr;
880 }
881
882 public void setNextTurnOpponentType(int type) {
883 m_GenerateAgreement.setNextTurnOpponentType(type);
884 }
885}
Note: See TracBrowser for help on using the repository browser.