source: src/main/java/agents/qoagent2/QOAgent.java@ 84

Last change on this file since 84 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 32.2 KB
Line 
1package agents.qoagent2;
2
3import java.io.FileWriter;
4import java.io.IOException;
5import java.io.PrintWriter;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8import java.util.Random;
9
10/**
11 * The QOAgent class represents the automated agent.
12 * @author Raz Lin
13 * @version 1.0
14 * @see QOAgentThread
15 * @see QMessages
16 * @see QCommunication
17 * @see QAgentsCore
18 * @see QAgentType
19 */
20
21/*
22class QAttributeValue
23{
24 public String sAttribute;
25 public String sValue;
26}
27
28class QPromiseType
29{
30 public ArrayList agentIssueSet;
31 public ArrayList opponentIssueSet;
32}
33*/
34
35public class QOAgent {
36 final static double OFFERS_VALUE_THRESHOLD = 0.05; // TODO: Change threshold
37 public final static String NOT_APPLICABLE_STR1 = "No agreement";
38 final static String AGENT_NAME = "Automated_Agent";
39 final static String AGENT_ID = "000000";
40 private agents.QOAgent m_Agent;
41 private String m_sAgentSide;
42 private String m_sAgentName;
43 private String m_sAgentId;
44 private String m_sSupportMediator; // "yes", "no"
45 private String m_sLogFileName;
46 private String m_sProbFileName;
47
48 private String m_sOppAgentId;
49
50 private int m_nMaxTurns;
51 private int m_nCurrentTurn;
52 private int m_nMsgId;
53
54//DT: private int m_nPortNum;
55 private boolean m_bHasOpponent;
56 private long m_lSecondsForTurn;
57
58 //private ArrayList m_offerList;
59 //private QPromiseType m_promiseCombinedList;
60
61 private QMessages m_messages;
62//DT: private QCommunication m_communication;
63
64 public QGameTime m_gtStopTurn, m_gtStopNeg;
65
66 private QAgentsCore m_AgentCore;
67 private QAgentType m_AgentType;
68 private QAgentType m_AgentTypeNextTurn;
69
70 private int m_PreviosAcceptedOffer[];
71
72 private boolean m_bSendOffer;
73
74 private boolean m_bEquilibriumAgent = false;
75 private boolean m_bCalculateForAllAgents;
76
77 /**
78 * Initialize the QOAgent
79 * @param sSide - the side of the QOAgent
80 * @param nPortNum - the port number it connects to
81 * @param sSupportMediator - whether there is a mediator
82 * @param sName - the name of the QOAgent
83 * @param sId - the id of the QOAgent
84 */
85 public QOAgent(agents.QOAgent pAgent, String sSide,/*DT: int nPortNum, */ String sSupportMediator, String sName, String sId)
86 {
87 m_Agent = pAgent;
88 m_sLogFileName = "";
89 m_bSendOffer = true;
90 m_bEquilibriumAgent = false;
91 m_bCalculateForAllAgents = false;
92 m_PreviosAcceptedOffer = new int[QAgentType.MAX_ISSUES];
93
94 for (int i = 0; i < QAgentType.MAX_ISSUES; ++i)
95 m_PreviosAcceptedOffer[i] = QAgentType.NO_VALUE;
96
97 if (sName.equals(""))
98 sName = AGENT_NAME;
99 if (sId.equals(""))
100 sId = AGENT_ID;
101
102 m_messages = new QMessages(this);
103
104 m_sAgentSide = sSide;
105//DT: m_nPortNum = nPortNum;
106 m_sSupportMediator = sSupportMediator;
107 m_sAgentName = sName;
108 m_sAgentId = sId;
109
110 setHasOpponent(false, null);
111 setMaxTurns(0);
112 setCurrentTurn(1);
113
114 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy.H.mm.ss");
115 Date now = new Date();
116 String sNow = formatter.format(now);
117
118 m_sLogFileName = "logs\\QOData" + sNow + ".log";
119 m_sProbFileName = "logs\\prob" + sNow + ".";
120 m_AgentCore = new QAgentsCore( m_sLogFileName, sNow, m_Agent);
121
122 // using long term type for england
123 // using short term type for zimbabwe
124 if (m_sAgentSide.equals("Zimbabwe"))
125 m_AgentType = m_AgentCore.getZimbabweShortTermType();
126 else if (m_sAgentSide.equals("England"))
127 //@@m_AgentType = m_AgentCore.getEnglandLongTermType();
128 m_AgentType = m_AgentCore.getEnglandShortTermType();
129
130 // init agent's values
131 m_AgentType.calculateValues(m_nCurrentTurn);
132 m_AgentTypeNextTurn = m_AgentType;
133 m_AgentTypeNextTurn.calculateValues(m_nCurrentTurn + 1);
134
135 m_AgentCore.initGenerateAgreement(m_AgentType);
136
137 // initialize connection to server
138/*DT: m_communication = new QCommunication(this, m_nPortNum);
139
140 Thread CommunicationThread = new Thread(m_communication);
141 CommunicationThread.start();
142*/
143 /*
144 m_utility = new QUtility(this);
145
146 m_threat = new QThreat(this);
147 m_comment = new QComment(this);
148*/ }
149
150 public QOAgent(agents.QOAgent pAgent, boolean bIsEquilibriumAgent, String sSide, /*DT: int nPortNum,*/ String sSupportMediator, String sName, String sId)
151 {
152 m_Agent = pAgent;
153 m_sLogFileName = "";
154 m_bSendOffer = true;
155 m_bEquilibriumAgent = bIsEquilibriumAgent;
156 m_bCalculateForAllAgents = false;
157 m_PreviosAcceptedOffer = new int[QAgentType.MAX_ISSUES];
158
159 for (int i = 0; i < QAgentType.MAX_ISSUES; ++i)
160 m_PreviosAcceptedOffer[i] = QAgentType.NO_VALUE;
161
162 if (sName.equals(""))
163 sName = AGENT_NAME;
164 if (sId.equals(""))
165 sId = AGENT_ID;
166
167 m_messages = new QMessages(this);
168
169 m_sAgentSide = sSide;
170 //DT: m_nPortNum = nPortNum;
171 m_sSupportMediator = sSupportMediator;
172 m_sAgentName = sName;
173 m_sAgentId = sId;
174
175 setHasOpponent(false, null);
176 setMaxTurns(0);
177 setCurrentTurn(1);
178
179 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy.H.mm.ss");
180 Date now = new Date();
181 String sNow = formatter.format(now);
182
183 m_sLogFileName = "logs\\QOData" + sNow + ".log";
184 m_sProbFileName = "logs\\prob" + sNow + ".";
185 m_AgentCore = new QAgentsCore(m_sLogFileName, sNow, m_bEquilibriumAgent, m_Agent);
186
187 // using long term type for england
188 // using short term type for zimbabwe
189 if (m_sAgentSide.equals("Zimbabwe"))
190 m_AgentType = m_AgentCore.getZimbabweShortTermType();
191 else if (m_sAgentSide.equals("England"))
192 //@@m_AgentType = m_AgentCore.getEnglandLongTermType();
193 m_AgentType = m_AgentCore.getEnglandShortTermType();
194
195 // init agent's values
196 m_AgentType.calculateValues(m_nCurrentTurn);
197 m_AgentTypeNextTurn = m_AgentType;
198 m_AgentTypeNextTurn.calculateValues(m_nCurrentTurn + 1);
199
200 m_AgentCore.initGenerateAgreement(m_AgentType);
201
202 // initialize connection to server
203/* DT: m_communication = new QCommunication(this, m_nPortNum);
204
205 Thread CommunicationThread = new Thread(m_communication);
206 CommunicationThread.start();
207*/
208 }
209
210
211 /**
212 * Main function of the agent.
213 * Used if the agent is run outside the Server
214 * Run using the following options:
215 * % java QOAgent <side_name> <port_num> <support_mediator> [<agent_name> <agent_id>]
216 *
217 */
218/* DT: public static void main(String[] args)
219 {
220 if (args.length < 5)
221 {
222 System.err.println("Error: Usage - java QOAgent <agent name> <port_num> <support_mediator> <equilibrium_agent> <calc_for_all_agents> [<name> <id>]");
223 System.exit(1);
224 }
225
226 // createFrom instance of class
227 // includes first connection to server
228 String sSideName = args[0];
229 String sPortNum = args[1];
230 String sEquilibriumAgent = args[3];
231 String sCalculateForAllAgents = args[4];
232 int nPortNum = new Integer(sPortNum).intValue();
233
234 String sSupportMediator = args[2];
235
236 String sName = "";
237 String sId = "";
238 if (args.length > 5)
239 {
240 sName = args[5];
241
242 if (args.length > 6)
243 sId = args[6];
244 }
245
246 boolean bEquilibriumAgent;
247
248 if (sEquilibriumAgent.equals("Yes"))
249 bEquilibriumAgent = true;
250 else
251 bEquilibriumAgent = false;
252
253 QOAgent agent = new QOAgent(bEquilibriumAgent, sSideName, nPortNum, sSupportMediator, sName, sId);
254
255 if (sEquilibriumAgent.equals("Yes"))
256 agent.setEquilibriumAgent(true);
257 else
258 agent.setEquilibriumAgent(false);
259
260 if (sCalculateForAllAgents.equals("Yes"))
261 agent.setCalculateEquilibriumForAllAgents(true);
262 else
263 agent.setCalculateEquilibriumForAllAgents(false);
264 // register with the server
265 agent.register();
266 }
267*/
268 public void setEquilibriumAgent(boolean bIsEquilibriumAgent)
269 {
270 m_bEquilibriumAgent = bIsEquilibriumAgent;
271 }
272
273 public void setCalculateEquilibriumForAllAgents(boolean bCalculateForAllAgents)
274 {
275 m_bCalculateForAllAgents = bCalculateForAllAgents;
276 }
277
278 /**
279 * @return agent's name
280 */
281 public String getAgentName()
282 {
283 return m_sAgentName;
284 }
285
286 /**
287 * @return agent's side
288 */
289 public String getAgentSide()
290 {
291 return m_sAgentSide;
292 }
293
294 /**
295 * @return agent's id
296 */
297 public String getAgentId()
298 {
299 return m_sAgentId;
300 }
301
302 /**
303 * Called by QCommunication when a message is received from the server.
304 * Parses the message using QMessages
305 * @param sMessage - the received message
306 * @see QMessages
307 * @see QCommunication
308 */
309 public void receivedMessage(String sMessage)
310 {
311 String sParsedMsg = m_messages.parseMessage(sMessage);
312
313 if (sParsedMsg.equals("nak")) // registration error
314 {
315 setMsgId(1); // first msg sent
316 generateId();
317 String sRegister = m_messages.formatMessage(QMessages.REGISTER, m_sAgentId);
318
319 // need to send message to server
320// DT: m_communication.printMsg(sRegister);
321 }
322 }
323
324 public void printMessageToServer(String sMessage)
325 {
326// DT: m_communication.printMsg(sMessage);
327 }
328
329 /**
330 * Ends the negotiation. Closes all communication
331 * @see QCommunication
332 */
333 public void endNegotiation()
334 {
335 m_bSendOffer = false;
336// DT: m_communication.endNegotiation();
337
338 // write final probabilities to log file
339 PrintWriter bw = null;
340 try {
341 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
342 bw.println("Final Probabilities:");
343 bw.println(getOpponentsProbabilitStr());
344 bw.close();
345
346 if (m_AgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
347 {
348 bw = new PrintWriter(new FileWriter(m_sProbFileName + "Zim.txt", true));
349 bw.println("Final Probabilities:");
350 bw.println(getOpponentsProbabilitStr());
351 bw.close();
352 }
353 else if (m_AgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
354 {
355 bw = new PrintWriter(new FileWriter(m_sProbFileName + "Eng.txt", true));
356 bw.println("Final Probabilities: " + getOpponentsProbabilitStr());
357 bw.close();
358 }
359 } catch (IOException e) {
360 System.out.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::endNegotiation(245)]");
361 System.err.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::endNegotiation(245)]");
362 }
363 }
364
365 /**
366 * @return the port number
367 */
368/* public int getPort()
369 {
370 return m_nPortNum;
371 }*/
372
373 /**
374 * Sets whether the agent has an opponent already or not
375 * @param bHasOpponent - whether there is an opponent
376 * @param sOppId - the id of the opponent
377 */
378 public void setHasOpponent(boolean bHasOpponent, String sOppId)
379 {
380 m_bHasOpponent = bHasOpponent;
381
382 if (m_bHasOpponent)
383 {
384 setOpponentAgentId(sOppId);
385
386 PrintWriter bw;
387 try {
388 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
389 bw.println("QO Side: " + m_sAgentSide);
390 bw.println("Opponent ID: " + sOppId);
391 bw.close();
392 } catch (IOException e) {
393 System.out.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::setHasOpponent(266)]");
394 System.err.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::setHasOpponent(266)]");
395 }
396 }
397 else
398 setOpponentAgentId("");
399 }
400
401 /**
402 * Sets the opponent's id
403 * @param sOppId - the opponent's id
404 */
405 public void setOpponentAgentId(String sOppId)
406 {
407 m_sOppAgentId = sOppId;
408 }
409
410 /**
411 * @return - the opponent's id
412 */
413 public String getOpponentAgentId()
414 {
415 return m_sOppAgentId;
416 }
417
418 /**
419 * Sets the number of seconds for each turn
420 * @param lSeconds - the number of seconds
421 */
422 public void setSecondsForTurn(long lSeconds)
423 {
424 m_lSecondsForTurn = lSeconds;
425 }
426
427 public long getSecondsForTurn()
428 {
429 return m_lSecondsForTurn;
430 }
431
432 /**
433 * @return "no" - the QOAgent does not support mediator
434 */
435 public String getSupportMediator()
436 {
437 return "no";
438 }
439
440 /**
441 * @return max number of turns for the negotiation
442 */
443 public int getMaxTurns()
444 {
445 return m_nMaxTurns;
446 }
447
448 /**
449 * @param nMaxTurns - max number of turns for the negotiation
450 */
451 public void setMaxTurns(int nMaxTurns)
452 {
453 m_nMaxTurns = nMaxTurns;
454 }
455
456 /**
457 * Returns the current negotiation's turn
458 * @return m_nCurrentTurn
459 */
460 public int getCurrentTurn()
461 {
462 return m_nCurrentTurn;
463 }
464
465 /**
466 * Sets the current negotiation's turn
467 * @param nCurrentTurn - the current turn
468 */
469 public void setCurrentTurn(int nCurrentTurn)
470 {
471 m_nCurrentTurn = nCurrentTurn;
472 }
473
474 /**
475 * Increments the current turn
476 */
477 public void incrementCurrentTurn()
478 {
479 m_nCurrentTurn++;
480 m_bSendOffer = true;
481 updateAgreementsValues();
482
483 double dNextAgreementValue = 0;
484 double dAcceptedAgreementValue = 0;
485 String sQOAgreement = "";
486 String sEquilibriumAgreement = "";
487 String sOffer = "";
488
489 if (m_bEquilibriumAgent)
490 {
491 if (m_nCurrentTurn == m_nMaxTurns)
492 {
493 m_bSendOffer = false;
494 return;
495 }
496 //TODO: Calculate the equilibrium offer
497 m_AgentCore.calculateEquilibriumAgreement(m_AgentType, m_nMaxTurns, m_bCalculateForAllAgents, m_nCurrentTurn);
498
499 sEquilibriumAgreement = m_AgentCore.getEquilibriumAgreement();
500 sOffer = m_messages.formatMessage(QMessages.OFFER, sEquilibriumAgreement);
501
502 // check value of next offer:
503 // if it's less than previously agreed agreement - don't send it
504 int nextAgreementIndices[] = new int[QAgentType.MAX_ISSUES];
505 nextAgreementIndices = getAgreementIndices(sEquilibriumAgreement);
506
507 dNextAgreementValue = m_AgentType.getAgreementValue(nextAgreementIndices, m_nCurrentTurn);
508 dAcceptedAgreementValue = m_AgentType.getAgreementValue(m_PreviosAcceptedOffer, m_nCurrentTurn);
509
510 System.err.println("~~~~~~~~~~~~~~~~~~~~~");
511 System.err.println("Accepted Agreement Value for Agent: " + dAcceptedAgreementValue);//@@
512 System.err.println("~~~~~~~~~~~~~~~~~~~~~");
513 }
514 else
515 {
516 // calculate QO offer
517 m_AgentCore.calculateAgreement(m_AgentType, m_nCurrentTurn);
518
519 // send the selected agreement
520 sQOAgreement = m_AgentCore.getQOAgreement();
521 sOffer = m_messages.formatMessage(QMessages.OFFER, sQOAgreement);
522
523 // check value of next offer:
524 // if it's less than previously agreed agreement - don't send it
525 int nextAgreementIndices[] = new int[QAgentType.MAX_ISSUES];
526 nextAgreementIndices = getAgreementIndices(sQOAgreement);
527
528 dNextAgreementValue = m_AgentType.getAgreementValue(nextAgreementIndices, m_nCurrentTurn);
529 dAcceptedAgreementValue = m_AgentType.getAgreementValue(m_PreviosAcceptedOffer, m_nCurrentTurn);
530
531 System.err.println("~~~~~~~~~~~~~~~~~~~~~");
532 System.err.println("Accepted Agreement Value for Agent: " + dAcceptedAgreementValue);//@@
533 System.err.println("~~~~~~~~~~~~~~~~~~~~~");
534 }
535
536 if (dAcceptedAgreementValue >= dNextAgreementValue)
537 {
538 // don't send message - previously accepted offer
539 // has better score
540 m_bSendOffer = false;
541 }
542
543 if (m_bSendOffer)
544 {
545 // createFrom thread to send delayed message
546/* DT: QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sOffer, m_nCurrentTurn);
547 delayedMessageThread.start();
548*/
549 m_Agent.prepareAction(QMessages.OFFER, sOffer);
550 }
551 }
552
553 public void calculateFirstOffer()
554 {
555 m_bSendOffer = true;
556
557 String sQOAgreement = "";
558 String sEquilibriumAgreement = "";
559 String sOffer = "";
560
561 if (m_bEquilibriumAgent)
562 {
563 //TODO: Calculate the equilibrium offer
564 m_AgentCore.calculateEquilibriumAgreement(m_AgentType, m_nMaxTurns, m_bCalculateForAllAgents, m_nCurrentTurn);
565
566 sEquilibriumAgreement = m_AgentCore.getEquilibriumAgreement();
567 sOffer = m_messages.formatMessage(QMessages.OFFER, sEquilibriumAgreement);
568 }
569 else
570 {
571 // calculate QO offer
572 m_AgentCore.calculateAgreement(m_AgentType, m_nCurrentTurn);
573
574 // send the selected agreement
575 sQOAgreement = m_AgentCore.getQOAgreement();
576 sOffer = m_messages.formatMessage(QMessages.OFFER, sQOAgreement);
577 }
578
579 if (m_bSendOffer)
580 {
581 // createFrom thread to send delayed message
582/* DT: QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sOffer, m_nCurrentTurn);
583 delayedMessageThread.start();
584*/
585 m_Agent.prepareAction(QMessages.OFFER, sOffer);
586 }
587 }
588
589 public boolean getIsOfferToSend()
590 {
591 return m_bSendOffer;
592 }
593
594 public void updateAgreementsValues()
595 {
596 m_AgentType.calculateValues(m_nCurrentTurn);
597 m_AgentTypeNextTurn.calculateValues(m_nCurrentTurn + 1);
598
599 m_AgentCore.updateAgreementsValues(m_nCurrentTurn);
600 }
601
602 /**
603 * @return m_nMsgId - the current message id
604 */
605 public int getMsgId()
606 {
607 return m_nMsgId;
608 }
609
610 /**
611 * Sets a new message id
612 * @param nMsgId - the new message id
613 */
614 public void setMsgId(int nMsgId)
615 {
616 m_nMsgId = nMsgId;
617 }
618
619 /**
620 * Increments the next message id.
621 * Called only after a message is sent
622 * @see QCommunication#printMsg
623 */
624 public void incrementMsgId()
625 {
626 m_nMsgId++;
627 }
628
629 /**
630 * Generates a random id for the QOAgent and saves it to m_sAgentId
631 */
632 public void generateId()
633 {
634 // generate random id
635 Random rn = new Random(); // new random, seed to current time
636
637 int nRandomAgentId = rn.nextInt();
638 nRandomAgentId = Math.abs(nRandomAgentId);
639
640 // call Message class with registration tag
641 String sAgentId = new Integer(nRandomAgentId).toString();
642 m_sAgentId = sAgentId;
643 }
644
645 /**
646 * Resgisters the agent with the server
647 * @see QMessages
648 * @see QCommunication
649 */
650 public void register()
651 {
652 setMsgId(1); // first msg sent
653 String sRegister = m_messages.formatMessage(QMessages.REGISTER, m_sAgentId);
654
655 // need to send message to server
656//DT: m_communication.printMsg(sRegister);
657 }
658
659 /**
660 * Sends the best agreeement for the agent at the current turn
661 */
662 public void sendBestAgreement()
663 {
664 String sBestAgreement = m_AgentType.getBestAgreementStr();
665
666/* DT: String sAgreementMsg = m_messages.formatMessage(QMessages.OFFER, sBestAgreement);
667
668 m_communication.printMsg(sAgreementMsg);
669*/
670 m_Agent.prepareAction(QMessages.OFFER, sBestAgreement);
671 }
672
673 /**
674 *
675 * @return indices of given agreement for the current agent
676 */
677 public int[] getAgreementIndices(String sAgreementStr)
678 {
679 return m_AgentType.getAgreementIndices(sAgreementStr);
680 }
681
682 /**
683 * Update opponent's probability given the message received
684 * and devicde whether to accept the message or reject it
685 *
686 * @param nMessageType - message type
687 * @param CurrentAgreementIdx - array of agreement indices
688 */
689 public void calculateResponse(int nMessageType, int CurrentAgreementIdx[], String sOriginalMessage)
690 {
691 System.err.println("Is Equ. Agent? " + m_bEquilibriumAgent);
692 // if it's the equilibrium agent - call the equilibrium response method
693 if (m_bEquilibriumAgent)
694 {
695 calculateEquilibriumResponse(nMessageType, CurrentAgreementIdx, sOriginalMessage);
696
697 return;
698 }
699
700 //String sOffer = m_AgentType.getAgreementStr(CurrentAgreementIdx);
701
702 // if a partial agreement was proposed in the past,
703 // the current agreement may include only partial
704 // value - merge it with previous accepted agreement
705 for (int i = 0; i < QAgentType.MAX_ISSUES; ++i)
706 {
707 // if value of current issue is "no agreement" or "no value"
708 if (CurrentAgreementIdx[i] == QAgentType.NO_VALUE)
709 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
710 else if (m_AgentType.isIssueValueNoAgreement(i, CurrentAgreementIdx[i]))
711 {
712 // if previous accepted agreement has values
713 // for it, copy the value
714 if (m_PreviosAcceptedOffer[i] != QAgentType.NO_VALUE)
715 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
716 }
717 }
718
719 // Update probability of opponent based on message
720 m_AgentCore.updateOpponentProbability(CurrentAgreementIdx, m_nCurrentTurn, nMessageType, QMessages.MESSAGE_RECEIVED);
721
722 // decide whether to accept the message or reject it:
723 double dOppOfferValueForAgent = QAgentType.VERY_SMALL_NUMBER;
724 double dQONextOfferValueForAgent = QAgentType.VERY_SMALL_NUMBER;
725
726 double dOppOfferValueForOpponent = QAgentType.VERY_SMALL_NUMBER;
727 double dQONextOfferValueForOpponent = QAgentType.VERY_SMALL_NUMBER;
728
729 // 1. Check the utility value of the opponent's offer
730 // oppVal = u1(opp_offer)
731 dOppOfferValueForAgent = m_AgentType.getAgreementValue(CurrentAgreementIdx, m_nCurrentTurn);
732
733 // check whether previous accepted agreement is better - if so, reject
734 double dAcceptedAgreementValue = m_AgentType.getAgreementValue(m_PreviosAcceptedOffer, m_nCurrentTurn);
735
736 System.err.println("~~~~~~~~~~~~~~~~~~~~");
737 System.err.println("Opponent Offer Value for Agent: " + dOppOfferValueForAgent);//@@
738 System.err.println("Accepted Agreement Value for Agent: " + dAcceptedAgreementValue);//@@
739
740 if (dAcceptedAgreementValue >= dOppOfferValueForAgent)
741 {
742 // reject offer
743/* DT: String sRejectMsg = m_messages.formatMessage(QMessages.REJECT, sOriginalMessage);
744
745 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sRejectMsg);
746 delayedMessageThread.start();
747 //m_communication.printMsg(sRejectMsg);
748*/
749 m_Agent.prepareAction(QMessages.REJECT, sOriginalMessage);
750 return;
751 }
752
753 // 2. nextVal = Check the value of my offer in the next turn
754 // nextVal = u1(next_QO_offer at time t+1)
755
756 // calculate QO offer for next turn
757 m_AgentCore.calculateNextTurnAgreement(m_AgentTypeNextTurn, m_nCurrentTurn + 1);
758 dQONextOfferValueForAgent = m_AgentCore.getNextTurnAgentQOUtilityValue();
759
760 System.err.println("Next Turn Offer Value for Agent: " + dQONextOfferValueForAgent);//@@
761 System.err.println("Next Turn Offer: " + m_AgentCore.getNextTurnAgentQOAgreement());//@@
762 System.err.println("~~~~~~~~~~~~~~~~~~~~");
763
764 // 3. if oppVal >= nextVal --> Accept
765 if (dOppOfferValueForAgent >= dQONextOfferValueForAgent)
766 {
767 PrintWriter bw;
768 try {
769 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
770 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
771 bw.println("Accepted offer: val (" + dOppOfferValueForAgent + ") >= QO_Next (" + dQONextOfferValueForAgent + ")");
772 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
773 bw.close();
774 } catch (IOException e) {
775 System.out.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateResponse(605)]");
776 System.err.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateResponse(605)]");
777 }
778
779 // accept offer
780/* DT: String sAcceptMsg = m_messages.formatMessage(QMessages.ACCEPT, sOriginalMessage);
781 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sAcceptMsg);
782 delayedMessageThread.start();
783
784 //m_communication.printMsg(sAcceptMsg);
785*/
786 m_Agent.prepareAction(QMessages.ACCEPT, sOriginalMessage);
787 // set flag not to propose offer if didn't propose yet this turn
788 m_bSendOffer = false;
789 }
790 else
791 {
792 // 4. Otherwise,
793 // if |u2(next_QO_offer at time t+1) - u2(opp_offer)| <= T --> Reject
794 // else, Accept with prob. lu(opp_offer).
795 QAgentType opponentNextTurnAgentType = m_AgentCore.getNextTurnOpponentType();
796
797 dOppOfferValueForOpponent = opponentNextTurnAgentType.getAgreementValue(CurrentAgreementIdx, m_nCurrentTurn + 1);
798 dQONextOfferValueForOpponent = m_AgentCore.getNextTurnOpponentQOUtilityValue();
799
800 double dValueDifference = Math.abs(dOppOfferValueForOpponent - dQONextOfferValueForOpponent);
801
802 if (dValueDifference <= OFFERS_VALUE_THRESHOLD)
803 {
804 // reject offer
805/* DT: String sRejectMsg = m_messages.formatMessage(QMessages.REJECT, sOriginalMessage);
806 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sRejectMsg);
807 delayedMessageThread.start();
808*/
809 m_Agent.prepareAction(QMessages.REJECT, sOriginalMessage);
810 //m_communication.printMsg(sRejectMsg);
811 }
812 else
813 {
814 Random generator = new Random();
815 double dRandNum = generator.nextDouble();
816
817 System.err.println("Rand num = " + dRandNum);//REMOVE
818
819 //@@
820 // accept offer using probability of agreements ranking
821 double dOfferProbabilityValue = m_AgentType.getAgreementRankingProbability(CurrentAgreementIdx);
822 System.err.println("Agreement ranking prob. = " + dOfferProbabilityValue);//REMOVE
823
824
825 //raz 08-05-06
826 boolean accept = true;
827 double dSQValue = m_AgentType.getSQValue();
828 double dAgreementValue = m_AgentType.getAgreementValue(CurrentAgreementIdx, m_nCurrentTurn);
829
830 double originalAgreementValue = Math.log(dAgreementValue) / QAgentType.PRECISION_VALUE;
831 if (originalAgreementValue < dSQValue)
832 accept = false;
833
834 if (!accept) {
835 System.out.println("====Agreement will not be accepted, lower than SQ");
836 System.err.println("====Agreement will not be accepted, lower than SQ");
837 }
838
839 PrintWriter bw;
840 try {
841 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
842 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
843 bw.println("Using probability to decide whether to accept:");
844 bw.println("Agreement ranking prob. = " + dOfferProbabilityValue);
845 bw.println("Rand num = " + dRandNum);
846 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
847 bw.close();
848 } catch (IOException e) {
849 System.out.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateResponse(605)]");
850 System.err.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateResponse(605)]");
851 }
852
853 /*
854 accept offer with a probability given by the luce number
855 lu(offer_opp)
856 double dOfferProbabilityValue = m_AgentType.getAgreementLuceValue(dOppOfferValueForAgent);
857 */
858
859 if ( (dRandNum <= dOfferProbabilityValue) && (accept))//08-05-06
860 {
861 // accept offer
862/* DT: String sAcceptMsg = m_messages.formatMessage(QMessages.ACCEPT, sOriginalMessage);
863 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sAcceptMsg);
864 delayedMessageThread.start();
865*/
866 m_Agent.prepareAction(QMessages.ACCEPT, sOriginalMessage);
867 //m_communication.printMsg(sAcceptMsg);
868
869 // set flag not to propose offer if didn't propose yet this turn
870 m_bSendOffer = false;
871 }
872 else
873 {
874 // reject offer
875/*DT: String sRejectMsg = m_messages.formatMessage(QMessages.REJECT, sOriginalMessage);
876 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sRejectMsg);
877 delayedMessageThread.start();
878*/
879 m_Agent.prepareAction(QMessages.REJECT, sOriginalMessage);
880 //m_communication.printMsg(sRejectMsg);
881 }
882 } // end if-else - difference of offer and QO value for opponent
883 } // end if-else - value of offer compared to next QO value
884 }
885
886 public void calculateEquilibriumResponse(int nMessageType, int CurrentAgreementIdx[], String sOriginalMessage)
887 {
888 //String sOffer = m_AgentType.getAgreementStr(CurrentAgreementIdx);
889
890 // if a partial agreement was proposed in the past,
891 // the current agreement may include only partial
892 // value - merge it with previous accepted agreement
893 for (int i = 0; i < QAgentType.MAX_ISSUES; ++i)
894 {
895 // if value of current issue is "no agreement" or "no value"
896 if (CurrentAgreementIdx[i] == QAgentType.NO_VALUE)
897 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
898 else if (m_AgentType.isIssueValueNoAgreement(i, CurrentAgreementIdx[i]))
899 {
900 // if previous accepted agreement has values
901 // for it, copy the value
902 if (m_PreviosAcceptedOffer[i] != QAgentType.NO_VALUE)
903 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
904 }
905 }
906
907 // Update probability of opponent based on message
908 m_AgentCore.updateOpponentProbability(CurrentAgreementIdx, m_nCurrentTurn, nMessageType, QMessages.MESSAGE_RECEIVED);
909
910 // decide whether to accept the message or reject it:
911 double dOppOfferValueForAgent = QAgentType.VERY_SMALL_NUMBER;
912 double dEquilibriumNextOfferValueForAgent = QAgentType.VERY_SMALL_NUMBER;
913
914 double dOppOfferValueForOpponent = QAgentType.VERY_SMALL_NUMBER;
915 double dEquilibriumNextOfferValueForOpponent = QAgentType.VERY_SMALL_NUMBER;
916
917 // 1. Check the utility value of the opponent's offer
918 // oppVal = u1(opp_offer)
919 dOppOfferValueForAgent = m_AgentType.getAgreementValue(CurrentAgreementIdx, m_nCurrentTurn);
920
921 // check whether previous accepted agreement is better - if so, reject
922 double dAcceptedAgreementValue = m_AgentType.getAgreementValue(m_PreviosAcceptedOffer, m_nCurrentTurn);
923
924 System.err.println("~~~~~~~~~~~~~~~~~~~~");
925 System.err.println("Opponent Offer Value for Agent: " + dOppOfferValueForAgent);//@@
926 System.err.println("Accepted Agreement Value for Agent: " + dAcceptedAgreementValue);//@@
927
928 if (dAcceptedAgreementValue >= dOppOfferValueForAgent)
929 {
930 // reject offer
931/* DT: String sRejectMsg = m_messages.formatMessage(QMessages.REJECT, sOriginalMessage);
932
933 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sRejectMsg);
934 delayedMessageThread.start();
935*/
936 m_Agent.prepareAction(QMessages.REJECT, sOriginalMessage);
937 //m_communication.printMsg(sRejectMsg);
938
939 return;
940 }
941
942 // 2. nextVal = Check the value of my offer in the next turn
943 // nextVal = u1(next_QO_offer at time t+1)
944
945 // calculate QO offer for next turn
946 m_AgentCore.calculateNextTurnEquilibriumAgreement(m_AgentTypeNextTurn, m_nMaxTurns, m_bCalculateForAllAgents, m_nCurrentTurn + 1);
947 dEquilibriumNextOfferValueForAgent = m_AgentCore.getNextTurnAgentEquilibriumUtilityValue();
948
949 System.err.println("Next Turn Offer Value for Agent: " + dEquilibriumNextOfferValueForAgent);//@@
950 System.err.println("Next Turn Offer: " + m_AgentCore.getNextTurnAgentEquilibriumAgreement());//@@
951 System.err.println("~~~~~~~~~~~~~~~~~~~~");
952
953 // 3. if oppVal >= nextVal --> Accept
954 if (dOppOfferValueForAgent >= dEquilibriumNextOfferValueForAgent)
955 {
956 PrintWriter bw;
957 try {
958 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
959 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
960 bw.println("Accepted offer: val (" + dOppOfferValueForAgent + ") >= QO_Next (" + dEquilibriumNextOfferValueForAgent + ")");
961 bw.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
962 bw.close();
963 } catch (IOException e) {
964 System.out.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateEquilibriumResponse(605)]");
965 System.err.println("[QO]ERROR----" + "Error opening logfile: " + e.getMessage() + " [QOAgent::calculateEquilibriumResponse(605)]");
966 }
967
968 // accept offer
969/* DT: String sAcceptMsg = m_messages.formatMessage(QMessages.ACCEPT, sOriginalMessage);
970 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sAcceptMsg);
971 delayedMessageThread.start();
972*/
973 m_Agent.prepareAction(QMessages.ACCEPT, sOriginalMessage);
974 //m_communication.printMsg(sAcceptMsg);
975
976 // set flag not to propose offer if didn't propose yet this turn
977 m_bSendOffer = false;
978 }
979 else
980 {
981 // reject offer
982/* DT: String sRejectMsg = m_messages.formatMessage(QMessages.REJECT, sOriginalMessage);
983 QDelayedMessageThread delayedMessageThread = new QDelayedMessageThread(this, sRejectMsg);
984 delayedMessageThread.start();
985*/
986 m_Agent.prepareAction(QMessages.REJECT, sOriginalMessage);
987 //m_communication.printMsg(sRejectMsg);
988 }
989 }
990
991
992 public void saveAcceptedMsg(String sMessage)
993 {
994 m_PreviosAcceptedOffer = getAgreementIndices(sMessage);
995
996 if (m_PreviosAcceptedOffer == null) // error occured
997 m_PreviosAcceptedOffer = new int[QAgentType.MAX_ISSUES];
998 }
999
1000 public String getOpponentsProbabilitStr()
1001 {
1002 if (m_AgentType.isTypeOf(QAgentType.ENGLAND_TYPE))
1003 {
1004 return m_AgentCore.getZimbabweProbabilitiesStr();
1005 } // end if agent's type is zimbabwe
1006 else if (m_AgentType.isTypeOf(QAgentType.ZIMBABWE_TYPE))
1007 {
1008 return m_AgentCore.getEnglandProbabilitiesStr();
1009 }
1010
1011 return "";
1012 }
1013
1014 public void updateOpponentProbability(int CurrentAgreementIdx[], int nMessageType, int nResponseType)
1015 {
1016 // Update probability of opponent based on message
1017 m_AgentCore.updateOpponentProbability(CurrentAgreementIdx, m_nCurrentTurn, nMessageType, nResponseType);
1018 }
1019
1020 /*public ArrayList getOfferList()
1021 {
1022 // pre-condition
1023 // m_offerList contains all attributes
1024 // attributes that should not be considered should
1025 // have the value "N/A" (NOT_APPLICABLE_STR).
1026 return m_offerList;
1027 }
1028 */
1029
1030 /*public QPromiseType getPromiseList()
1031 {
1032 return m_promiseCombinedList;
1033 }
1034 */
1035}
Note: See TracBrowser for help on using the repository browser.