source: src/main/java/agents/qoagent/AutomatedAgent.java@ 346

Last change on this file since 346 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: 22.0 KB
Line 
1package agents.qoagent;
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 AutomatedAgent class represents the automated agent.
12 * @author Raz Lin
13 * @version 1.0
14 * @see AutomatedAgentThread
15 * @see AutomatedAgentMessages
16 * @see AutomatedAgentCommunication
17 * @see AutomatedAgentsCore
18 * @see AutomatedAgentType
19 */
20
21public class AutomatedAgent {
22 final public static String SIDE_A_NAME = "SIDE_A"; //England/Employer
23 final public static String SIDE_B_NAME = "SIDE_B"; //Zimbabwe/Job Candidate
24
25 final static String AGENT_NAME = "Automated_Agent";
26 final static String AGENT_ID = "000000";
27
28 private String m_sAgentSide;
29 private String m_sAgentName;
30 private String m_sAgentId;
31 private String m_sLogFileName;
32 private String m_sOppAgentId;
33
34 private int m_nMaxTurns;
35 private int m_nCurrentTurn;
36 private int m_nMsgId;
37
38 private int m_nPortNum;
39 private boolean m_bHasOpponent;
40 private long m_lSecondsForTurn;
41
42 private AgentTools m_agentTools = null;
43 private AbstractAutomatedAgent m_abstractAgent = null;
44
45 private AutomatedAgentMessages m_messages;
46 //DT: private AutomatedAgentCommunication m_communication;
47
48 public AutomatedAgentGameTime m_gtStopTurn, m_gtStopNeg; // timers till end of turn and end of negotiation
49
50 private AutomatedAgentsCore m_AgentCore;
51 private AutomatedAgentType m_AgentType; // to obtain agreement values of current turn
52 private AutomatedAgentType m_AgentTypeNextTurn; // to obtain agreement values for next turn
53
54 private int m_PreviosAcceptedOffer[]; // indices of attributes that were agreed upon previously
55 private boolean m_bSendOffer = true;
56
57 public AutomatedAgent() {}
58
59 /**
60 * Initialize the Automated Agent
61 * @param sSide - the side of the Automated Agent
62 * @param nPortNum - the port number it connects to
63 * @param sName - the name of the Automated Agent
64 * @param sId - the id of the Automated Agent
65 */
66 public AutomatedAgent(String sSide, int nPortNum, String sName, String sId)
67 {
68 m_bSendOffer = true; // flag used to decide whetehr to send offers or not
69 m_agentTools = new AgentTools(this);
70 m_abstractAgent = new AbstractAutomatedAgent(m_agentTools);
71
72 m_sLogFileName = "";
73
74 m_PreviosAcceptedOffer = new int[AutomatedAgentType.MAX_ISSUES];
75
76 for (int i = 0; i < AutomatedAgentType.MAX_ISSUES; ++i)
77 m_PreviosAcceptedOffer[i] = AutomatedAgentType.NO_VALUE;
78
79 if (sName.equals(""))
80 sName = AGENT_NAME;
81 if (sId.equals(""))
82 sId = AGENT_ID;
83
84 m_messages = new AutomatedAgentMessages(this, m_agentTools, m_abstractAgent);
85
86 m_sAgentSide = sSide;
87 m_nPortNum = nPortNum;
88 m_sAgentName = sName;
89 m_sAgentId = sId;
90
91 setHasOpponent(false, null);
92 setMaxTurns(0);
93 setCurrentTurn(1);
94
95 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy.H.mm.ss");
96 Date now = new Date();
97 String sNow = formatter.format(now);
98
99 m_sLogFileName = "logs\\AutomatedAgentData" + sNow + ".log";
100
101 m_AgentCore = new AutomatedAgentsCore(m_sLogFileName, sNow, m_agentTools, m_abstractAgent);
102// m_AgentCore.setAgentTools(m_agentTools);
103 // m_AgentCore.setAbstractAgent(m_abstractAgent);
104
105 // Choose the agent type
106 m_agentTools.setAutomatedAgentType(m_sAgentSide);
107
108 // init agent's values
109 // both for the first turn (1) and for the following turn (2)
110 m_AgentType.calculateValues(m_abstractAgent, m_nCurrentTurn);
111 m_AgentTypeNextTurn = m_AgentType;
112 m_AgentTypeNextTurn.calculateValues(m_abstractAgent, m_nCurrentTurn + 1);
113
114 m_AgentCore.initGenerateAgreement(m_AgentType);
115
116 // initialize connection to server
117 //DT: m_communication = new AutomatedAgentCommunication(this, m_nPortNum);
118
119 //DT: Thread CommunicationThread = new Thread(m_communication);
120 //DT: CommunicationThread.start();
121 }
122
123 public AutomatedAgentType getAgentType() {
124 return m_AgentType;
125 }
126
127 public void setAgentType(String side, int type) {
128 if (side.equals(AutomatedAgent.SIDE_B_NAME)) {
129 switch (type) {
130 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
131 m_AgentType = m_AgentCore.getSideBShortTermType();
132 break;
133 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
134 m_AgentType = m_AgentCore.getSideBLongTermType();
135 break;
136 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
137 m_AgentType = m_AgentCore.getSideBCompromiseType();
138 break;
139 default:
140 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::setAgentType(129)]");
141 break;
142 }
143 }
144 else if (side.equals(AutomatedAgent.SIDE_A_NAME)) {
145 switch (type) {
146 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
147 m_AgentType = m_AgentCore.getSideAShortTermType();
148 break;
149 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
150 m_AgentType = m_AgentCore.getSideALongTermType();
151 break;
152 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
153 m_AgentType = m_AgentCore.getSideACompromiseType();
154 break;
155 default:
156 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::setAgentType(129)]");
157 break;
158 }
159 }
160 }
161
162 /**
163 * Main function of the agent.
164 * Used if the agent is run outside the Server
165 * Run using the following options:
166 * % java AutomatedAgent <side_name> <port_num> <equilibrium_agent> <calc_for_all_agents> [<agent_name> <agent_id>]
167 *
168 */
169/* DT: public static void main(String[] args)
170 {
171 if (args.length < 2)
172 {
173 System.err.println("Error: Usage - java AutomatedAgent <agent name> <port_num> [<name> <id>]");
174 System.exit(1);
175 }
176
177 // createFrom instance of class
178 // includes first connection to server
179 String sSideName = args[0];
180 String sPortNum = args[1];
181 int nPortNum = new Integer(sPortNum).intValue();
182
183 String sName = "";
184 String sId = "";
185 if (args.length > 2)
186 {
187 sName = args[2];
188
189 if (args.length > 3)
190 sId = args[3];
191 }
192
193 AutomatedAgent agent = new AutomatedAgent(sSideName, nPortNum, sName, sId);
194
195 // register with the server
196 agent.register();
197 }
198*/
199 /**
200 * @return agent's name
201 */
202 public String getAgentName()
203 {
204 return m_sAgentName;
205 }
206
207 /**
208 * @return agent's side
209 */
210 public String getAgentSide()
211 {
212 return m_sAgentSide;
213 }
214
215 /**
216 * @return agent's id
217 */
218 public String getAgentId()
219 {
220 return m_sAgentId;
221 }
222
223 /**
224 * Called by AutomatedAgentCommunication when a message is received from the server.
225 * Parses the message using AutomatedAgentMessages
226 * @param sMessage - the received message
227 * @see AutomatedAgentMessages
228 * @see AutomatedAgentCommunication
229 */
230 public void receivedMessage(String sMessage)
231 {
232 //logic is done in parseMessage()
233 String sParsedMsg = m_messages.parseMessage(sMessage);
234
235 //if the message is nak, it means that there is a registration error
236 if (sParsedMsg.equals("nak")) // registration error
237 {
238 setMsgId(1); // first msg sent
239 generateId();
240 String sRegister = m_messages.formatMessage(AutomatedAgentMessages.REGISTER, m_sAgentId);
241
242 // need to send message to server
243 //DT: m_communication.printMsg(sRegister);
244 }
245 }
246
247 /**
248 * Used to send a message back to the server/opponent
249 * @param sMessage - the sent message
250 * @see AutomatedAgentMessages
251 * @see AutomatedAgentCommunication
252 */
253 public void printMessageToServer(String sMessage)
254 {
255 //DT: m_communication.printMsg(sMessage);
256 System.out.println("Agent tries to send a message to the server:" + sMessage);
257 }
258
259 /**
260 * Ends the negotiation. Closes all communication
261 * @see AutomatedAgentCommunication
262 */
263 public void endNegotiation()
264 {
265 //DT: m_communication.endNegotiation();
266 }
267
268 /**
269 * @return the port number
270 */
271 public int getPort()
272 {
273 return m_nPortNum;
274 }
275
276 /**
277 * Sets whether the agent's opponent has registered yet
278 * @param bHasOpponent - whether there is an opponent
279 * @param sOppId - the id of the opponent
280 */
281 public void setHasOpponent(boolean bHasOpponent, String sOppId)
282 {
283 m_bHasOpponent = bHasOpponent;
284
285 if (m_bHasOpponent)
286 {
287 setOpponentAgentId(sOppId);
288
289 PrintWriter bw;
290 try {
291 bw = new PrintWriter(new FileWriter(m_sLogFileName, true));
292 bw.println("AutomatedAgent Side: " + m_sAgentSide);
293 bw.println("Opponent ID: " + sOppId);
294 bw.close();
295 } catch (IOException e) {
296 System.out.println("[AA]ERROR----" + "Error opening logfile: " + e.getMessage() + " [AutomatedAgent::setHasOpponent(266)]");
297 System.err.println("[AA]ERROR----" + "Error opening logfile: " + e.getMessage() + " [AutomatedAgent::setHasOpponent(266)]");
298 }
299 }
300 else
301 setOpponentAgentId("");
302 }
303
304 /**
305 * Sets the opponent's id
306 * @param sOppId - the opponent's id
307 */
308 public void setOpponentAgentId(String sOppId)
309 {
310 m_sOppAgentId = sOppId;
311 }
312
313 /**
314 * @return - the opponent's id
315 */
316 public String getOpponentAgentId()
317 {
318 return m_sOppAgentId;
319 }
320
321 /**
322 * Sets the number of seconds for each turn
323 * @param lSeconds - the number of seconds
324 */
325 public void setSecondsForTurn(long lSeconds)
326 {
327 m_lSecondsForTurn = lSeconds;
328 }
329
330 /**
331 * Get the number of seconds for each turn
332 * return m_lSecondsForTurn - the number of seconds per turn
333 */
334 public long getSecondsForTurn()
335 {
336 return m_lSecondsForTurn;
337 }
338
339 /**
340 * @return "no" - the Automated Agent does not support mediator
341 */
342 public String getSupportMediator()
343 {
344 return "no";
345 }
346
347 /**
348 * @return max number of turns for the negotiation
349 */
350 public int getMaxTurns()
351 {
352 return m_nMaxTurns;
353 }
354
355 /**
356 * @param nMaxTurns - max number of turns for the negotiation
357 */
358 public void setMaxTurns(int nMaxTurns)
359 {
360 m_nMaxTurns = nMaxTurns;
361 }
362
363 /**
364 * Returns the current negotiation's turn
365 * @return m_nCurrentTurn
366 */
367 public int getCurrentTurn()
368 {
369 return m_nCurrentTurn;
370 }
371
372 /**
373 * Sets the current negotiation's turn
374 * @param nCurrentTurn - the current turn
375 */
376 public void setCurrentTurn(int nCurrentTurn)
377 {
378 m_nCurrentTurn = nCurrentTurn;
379 }
380
381 /**
382 * Increments the current turn
383 */
384 public void incrementCurrentTurn()
385 {
386 m_nCurrentTurn++;
387 updateAgreementsValues(); // receiveMessage values for the new turn
388
389 calculateAgreement();
390 }
391
392 public void calculateAgreement() {
393 // AutomatedAgentsCore.calculateAgreement method is responsible for the actual logic
394 m_AgentCore.calculateAgreement(m_AgentType, m_nCurrentTurn);
395 }
396
397 /**
398 * Update the agreement values when new turn begins
399 *
400 */
401 public void updateAgreementsValues()
402 {
403 m_AgentType.calculateValues(m_abstractAgent, m_nCurrentTurn);
404 m_AgentTypeNextTurn.calculateValues(m_abstractAgent, m_nCurrentTurn + 1);
405
406 m_AgentCore.updateAgreementsValues(m_nCurrentTurn);
407 }
408
409 /**
410 * @return m_nMsgId - the current message id
411 */
412 public int getMsgId()
413 {
414 return m_nMsgId;
415 }
416
417 /**
418 * Sets a new message id
419 * @param nMsgId - the new message id
420 */
421 public void setMsgId(int nMsgId)
422 {
423 m_nMsgId = nMsgId;
424 }
425
426 /**
427 * Increments the next message id.
428 * Called only after a message is sent
429 * @see AutomatedAgentCommunication#printMsg
430 */
431 public void incrementMsgId()
432 {
433 m_nMsgId++;
434 }
435
436 /**
437 * Generates a random id for the Automated Agent and saves it to m_sAgentId
438 */
439 public void generateId()
440 {
441 // generate random id
442 Random rn = new Random(); // new random, seed to current time
443
444 int nRandomAgentId = rn.nextInt();
445 nRandomAgentId = Math.abs(nRandomAgentId);
446
447 // call Message class with registration tag
448 String sAgentId = new Integer(nRandomAgentId).toString();
449 m_sAgentId = sAgentId;
450 }
451
452 /**
453 * Resgisters the agent with the server
454 * @see AutomatedAgentMessages
455 * @see AutomatedAgentCommunication
456 */
457/* public void register()
458 {
459 setMsgId(1); // first msg sent
460 String sRegister = m_messages.formatMessage(AutomatedAgentMessages.REGISTER, m_sAgentId);
461
462 // need to send message to server
463 m_communication.printMsg(sRegister);
464 }*/
465
466 /**
467 *
468 * @return indices of given agreement for the current agent
469 */
470 public int[] getAgreementIndices(String sAgreementStr)
471 {
472 return m_AgentType.getAgreementIndices(sAgreementStr);
473 }
474
475 /**
476 * If an offer is accepted, need to save it for future
477 * references and comparisons
478 * @param sMessage - the accepted offer
479 */
480 public void saveAcceptedMsg(String sMessage)
481 {
482 m_PreviosAcceptedOffer = getAgreementIndices(sMessage);
483
484 if (m_PreviosAcceptedOffer == null) // error occured
485 m_PreviosAcceptedOffer = new int[AutomatedAgentType.MAX_ISSUES];
486 }
487
488 /**
489 * Decide regarding a received message
490 * For example, decide whether to accept or reject it, or receiveMessage other agent's data
491 *
492 * @param nMessageType - message type
493 * @param CurrentAgreementIdx - array of agreement indices
494 */
495 public void calculateResponse(int nMessageType, int CurrentAgreementIdx[], String sOriginalMessage)
496 {
497 // if a partial agreement was agreed in the past,
498 // the current agreement may include only partial
499 // value - merge it with previous accepted agreement
500 for (int i = 0; i < AutomatedAgentType.MAX_ISSUES; ++i)
501 {
502 // if value of current issue is "no agreement" or "no value"
503 if (CurrentAgreementIdx[i] == AutomatedAgentType.NO_VALUE)
504 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
505 else if (m_AgentType.isIssueValueNoAgreement(i, CurrentAgreementIdx[i]))
506 {
507 // if previous accepted agreement has values
508 // for it, copy the value
509 if (m_PreviosAcceptedOffer[i] != AutomatedAgentType.NO_VALUE)
510 CurrentAgreementIdx[i] = m_PreviosAcceptedOffer[i];
511 }
512 }
513
514 m_abstractAgent.calculateResponse(nMessageType, CurrentAgreementIdx, sOriginalMessage);
515 }
516
517 public String getAutomatedAgentAgreement() {
518 String sAgreement = m_AgentCore.getAutomatedAgentAgreement();
519 return sAgreement;
520 }
521
522 public String formatMessage(int message, String sMessage) {
523 String formattedMessage = m_messages.formatMessage(message, sMessage);
524 return formattedMessage;
525 }
526
527 public double getAgreementValue(int[] agreementIndices) {
528 double agreementValue = m_AgentType.getAgreementValue(agreementIndices, m_nCurrentTurn);
529 return agreementValue;
530 }
531
532 public int[] getPreviousAcceptedAgreementsIndices() {
533 return m_PreviosAcceptedOffer;
534 }
535
536 public void calculateNextTurnOffer() {
537 m_AgentCore.calculateNextTurnAgreement(m_AgentTypeNextTurn, m_nCurrentTurn + 1);
538 }
539
540 public double getNextTurnAutomatedAgentOfferValue() {
541 double dAutomatedAgentNextOfferValueForAgent = m_AgentCore.getNextTurnAutomatedAgentUtilityValue();
542 return dAutomatedAgentNextOfferValueForAgent;
543 }
544
545 public AutomatedAgentType getNextTurnSideAgentType(String sideName, int type) {
546 AutomatedAgentType agentType = null;
547 if (sideName.equals(AutomatedAgent.SIDE_A_NAME)) {
548 switch (type) {
549 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
550 agentType = m_AgentCore.getSideACompromiseNextTurnType();
551 break;
552 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
553 agentType = m_AgentCore.getSideALongTermNextTurnType();
554 break;
555 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
556 agentType = m_AgentCore.getSideAShortTermNextTurnType();
557 break;
558 default:
559 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::getNextTurnSideAgentType(585)]");
560 break;
561 }
562 } else if (sideName.equals(AutomatedAgent.SIDE_B_NAME)) {
563 switch (type) {
564 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
565 agentType = m_AgentCore.getSideBCompromiseNextTurnType();
566 break;
567 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
568 agentType = m_AgentCore.getSideBLongTermNextTurnType();
569 break;
570 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
571 agentType = m_AgentCore.getSideBShortTermNextTurnType();
572 break;
573 default:
574 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::getNextTurnSideAgentType(600)]");
575 break;
576 }
577 } else {
578 System.out.println("[AA]Agent type is unknown [AutomatedAgent::getNextTurnSideAgentType(604)]");
579 System.err.println("[AA]Agent type is unknown [AutomatedAgent::getNextTurnSideAgentType(604)]");
580 return null;
581 }
582
583 return agentType;
584 }
585
586 public AutomatedAgentType getCurrentTurnSideAgentType(String sideName, int type) {
587 AutomatedAgentType agentType = null;
588 if (sideName.equals(AutomatedAgent.SIDE_A_NAME)) {
589 switch (type) {
590 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
591 agentType = m_AgentCore.getSideACompromiseType();
592 break;
593 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
594 agentType = m_AgentCore.getSideALongTermType();
595 break;
596 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
597 agentType = m_AgentCore.getSideAShortTermType();
598 break;
599 default:
600 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::getCurrentTurnSideAgentType(622)]");
601 break;
602 }
603 } else if (sideName.equals(AutomatedAgent.SIDE_B_NAME)) {
604 switch (type) {
605 case AutomatedAgentsCore.COMPROMISE_TYPE_IDX:
606 agentType = m_AgentCore.getSideBCompromiseType();
607 break;
608 case AutomatedAgentsCore.LONG_TERM_TYPE_IDX:
609 agentType = m_AgentCore.getSideBLongTermType();
610 break;
611 case AutomatedAgentsCore.SHORT_TERM_TYPE_IDX:
612 agentType = m_AgentCore.getSideBShortTermType();
613 break;
614 default:
615 System.err.println("[AA]ERROR----" + "Wrong type for agent: " + type + " [AutomatedAgent::getCurrentTurnSideAgentType(637)]");
616 break;
617 }
618 } else {
619 System.out.println("[AA]Agent type is unknown [AutomatedAgent::getCurrentTurnSideAgentType(642)]");
620 System.err.println("[AA]Agent type is unknown [AutomatedAgent::getCurrentTurnSideAgentType(642)]");
621 return null;
622 }
623
624 return agentType;
625 }
626
627 public double getNextTurnAutomatedAgentValue() {
628 return m_AgentCore.getNextTurnAutomatedAgentValue();
629 }
630
631 public double getCurrentTurnAutomatedAgentValue() {
632 return m_AgentCore.getCurrentTurnAutomatedAgentValue();
633 }
634
635 public void setNextTurnAutomatedAgentValue(double value) {
636 m_AgentCore.setNextTurnAutomatedAgentValue(value);
637 }
638
639 public void setCurrentTurnAutomatedAgentValue(double value) {
640 m_AgentCore.setCurrentTurnAutomatedAgentValue(value);
641 }
642
643 public void setNextTurnAutomatedAgentSelectedValue(double agreementValue) {
644 m_AgentCore.setNextTurnAutomatedAgentSelectedValue(agreementValue);
645 }
646
647 public void setNextTurnOpponentSelectedValue(double agreementValue) {
648 m_AgentCore.setNextTurnOpponentSelectedValue(agreementValue);
649 }
650
651 public void setCurrentTurnOpponentSelectedValue(double agreementValue) {
652 m_AgentCore.setCurrentTurnOpponentSelectedValue(agreementValue);
653 }
654
655 public void setNextTurnAgreementString(String agreementStr) {
656 m_AgentCore.setNextTurnAgreementString(agreementStr);
657 }
658
659 public void setCurrentTurnAgreementString(String agreementStr) {
660 m_AgentCore.setCurrentTurnAgreementString(agreementStr);
661 }
662
663 public void setNextTurnOpponentType(int type) {
664 m_AgentCore.setNextTurnOpponentType(type);
665 }
666
667 public String getAgreementStr(int[] currentAgreementIdx) {
668 return m_AgentType.getAgreementStr(currentAgreementIdx);
669 }
670
671 public String getBestAgreementStr() {
672 return m_AgentType.getBestAgreementStr();
673 }
674
675 public double getBestAgreementValue() {
676 return m_AgentType.getBestAgreementValue(m_nCurrentTurn);
677 }
678
679 public String getWorstAgreementStr() {
680 return m_AgentType.getWorstAgreementStr();
681 }
682
683 public double getWorstAgreementValue() {
684 return m_AgentType.getWorstAgreementValue(m_nCurrentTurn);
685 }
686
687 public double getStatusQuoValue() {
688 return m_AgentType.getSQValue();
689 }
690
691 public double getOptingOutValue() {
692 return m_AgentType.getOptOutValue();
693 }
694
695 public int getTotalAgreementsNum() {
696 return m_AgentType.getTotalAgreements();
697 }
698
699 public int getTotalIssues() {
700 return m_AgentType.getIssuesNum();
701 }
702
703 public boolean getSendOfferFlag() {
704 return m_bSendOffer ;
705 }
706
707 public void setSendOfferFlag(boolean flag) {
708 m_bSendOffer = flag;
709 }
710}
Note: See TracBrowser for help on using the repository browser.