source: src/main/java/agents/qoagent/AgentTools.java@ 127

Last change on this file since 127 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: 20.4 KB
Line 
1package agents.qoagent;
2
3/**
4 * @author raz
5 * This class should serve as an auxiliary class
6 * The methods should not be changed/revised
7 *
8 */
9public class AgentTools {
10 AutomatedAgent agent = null;
11
12 /**
13 * Constructor
14 * Save a pointer to the AutomatedAgent class
15 * @param agent - pointer to the AutomatedAgent class
16 */
17 public AgentTools(AutomatedAgent agent) {
18 this.agent = agent;
19 }
20
21 /***********************************************
22 * @@ Logic for sending messages
23 * Below are messages the automated agent sends to the opponent
24 * Call them from the AbstractAutomatedAgent class, and also
25 * add any logic you need in that class, just before calling them
26 * Do not add the logic in this class!
27 ***********************************************/
28
29 /**
30 * Called when you want to accept a message
31 * @param sOriginalMessage - the message to be accepted
32 */
33 public void acceptMessage(String sOriginalMessage) {
34 String sAcceptMsg = agent.formatMessage(AutomatedAgentMessages.ACCEPT, sOriginalMessage);
35
36 //createFrom a thread to send delayed message
37 // You can choose how much time the agent should wait before accepting the opponent's offer.
38 // The delay time is defined the AutomatedAgentDelayedMessageThread class.
39 AutomatedAgentDelayedMessageThread delayedMessageThread = new AutomatedAgentDelayedMessageThread(agent, sAcceptMsg);
40 delayedMessageThread.start();
41 }
42
43 /**
44 * Called when you want to reject a message
45 * @param sOriginalMessage - the message to be rejected
46 */
47 public void rejectMessage(String sOriginalMessage) {
48 String sRejectMsg = agent.formatMessage(AutomatedAgentMessages.REJECT, sOriginalMessage);
49
50 //createFrom a thread to send delayed message
51 // You can choose how much time the agent should wait before sending the rejection to the opponent.
52 // The delay time is defined the AutomatedAgentDelayedMessageThread class.
53 AutomatedAgentDelayedMessageThread delayedMessageThread = new AutomatedAgentDelayedMessageThread(agent, sRejectMsg);
54 delayedMessageThread.start();
55 }
56
57 /**
58 * Called when you want to send a message of type offer, counter offer, promise or query
59 * @param currentAgreementIdx - the indices of the message
60 */
61 public void sendMessage(int nMessageType, int currentAgreementIdx[]) {
62 String sMessage = getMessageByIndices(currentAgreementIdx);
63 sMessage = agent.formatMessage(nMessageType, sMessage);
64
65 //createFrom a thread to send delayed message
66 // You can choose how much time the agent should wait before sending the message to the opponent.
67 // The delay time is defined the AutomatedAgentDelayedMessageThread class.
68 AutomatedAgentDelayedMessageThread delayedMessageThread = new AutomatedAgentDelayedMessageThread(agent, sMessage, agent.getCurrentTurn());
69 delayedMessageThread.start();
70 }
71
72 public void optOut() {
73 String sMessage = agent.formatMessage(AutomatedAgentMessages.OPT_OUT, "");
74 agent.printMessageToServer(sMessage);
75 }
76
77 /**
78 * Called when you want to send a message of type offer, counter offer, promise or query
79 * @param sMessage - the message as a String
80 */
81 public void sendMessage(int nMessageType, String sMessage) {
82 sMessage = agent.formatMessage(nMessageType, sMessage);
83
84 //createFrom a thread to send delayed message
85 // You can choose how much time the agent should wait before sending the message to the opponent.
86 // The delay time is defined the AutomatedAgentDelayedMessageThread class.
87 AutomatedAgentDelayedMessageThread delayedMessageThread = new AutomatedAgentDelayedMessageThread(agent, sMessage, agent.getCurrentTurn());
88 delayedMessageThread.start();
89 }
90
91
92 /**
93 * Called when you want to send an offer
94 * @param sOffer - the offer to be sent
95 */
96 public void sendOffer(String sOffer) {
97 setSendOfferFlag(true);
98 sendMessage(AutomatedAgentMessages.OFFER, sOffer);
99 }
100
101 /**
102 * Called when you want to send a query
103 * @param currentAgreementIdx - the indices of the query
104 */
105 public void sendQuery(int currentAgreementIdx[]) {
106 setSendOfferFlag(true);
107 sendMessage(AutomatedAgentMessages.QUERY, currentAgreementIdx);
108 }
109
110 /**
111 * Called when you want to send a promise
112 * @param currentAgreementIdx - the indices of the promise
113 */
114 public void sendPromise(int currentAgreementIdx[]) {
115 setSendOfferFlag(true);
116 sendMessage(AutomatedAgentMessages.PROMISE, currentAgreementIdx);
117 }
118
119 /**
120 * Called when you want to send a counter offer
121 * @param currentAgreementIdx - the indices of the counter offer
122 */
123 public void sendCounterOffers(int currentAgreementIdx[]) {
124 setSendOfferFlag(true);
125 sendMessage(AutomatedAgentMessages.COUNTER_OFFER, currentAgreementIdx);
126 }
127
128 /**
129 * Called when you want to send a comment
130 * @param sMessage - the comment to be sent
131 */
132 public void sendComment(String sMessage) {
133 sMessage = agent.formatMessage(AutomatedAgentMessages.COMMENT, sMessage);
134 agent.printMessageToServer(sMessage);
135 }
136
137 /**
138 * Called when you want to send a threat
139 * @param sMessage - the threat to be sent
140 */
141 public void sendThreat(String sMessage) {
142 sMessage = agent.formatMessage(AutomatedAgentMessages.THREAT, sMessage);
143 agent.printMessageToServer(sMessage);
144 }
145 /***********************************************
146 * @@ End of methods for sending message
147 ***********************************************/
148
149 // helper function
150 /**
151 * Get the total number of turns in the negotiation
152 * @return total number of turns
153 */
154 public int getTurnsNumber() {
155 return agent.getMaxTurns();
156 }
157
158 /**
159 * Get the current turn number
160 * @return the current turn
161 */
162 public int getCurrentTurn() {
163 return agent.getCurrentTurn();
164 }
165
166 // utility functions
167 /**
168 * @param agentType - the agent's type
169 * @param CurrentAgreementIdx - the agreement indices
170 * @param nCurrentTurn - the current turn for calculations
171 * @return the value of a given agreement for the agent at a given turn
172 */
173 public double getAgreementValue(AutomatedAgentType agentType, int[] CurrentAgreementIdx, int nCurrentTurn) {
174 return agentType.getAgreementValue(CurrentAgreementIdx, nCurrentTurn);
175 }
176
177
178 /**
179 * Return the best agreement as string
180 * @param agentType - the agent's type
181 * @return the best agreement as String
182 */
183 public String getBestAgreementStr(AutomatedAgentType agentType) {
184 return agentType.getBestAgreementStr();
185 }
186
187 /**
188 * Return the best agreement value for a given agent
189 * @param agentType - the agent's type
190 * @return the best agreement value computed for the current turn
191 */
192 public double getBestAgreementValue(AutomatedAgentType agentType) {
193 return agentType.getBestAgreementValue();
194 }
195
196 /**
197 * Return the worst agreement as a String
198 * @param agentType - the agent's type
199 * @return the worst agreement as String
200 */
201 public String getWorstAgreementStr(AutomatedAgentType agentType) {
202 return agentType.getWorstAgreementStr();
203 }
204
205 /**
206 * Return the worst agreement for a given agent
207 * @param agentType - the agent's type
208 * @return the worst agreement value computed for the current turn
209 */
210 public double getWorstAgreementValue(AutomatedAgentType agentType) {
211 return agentType.getWorstAgreementValue();
212 }
213
214 /**
215 * Sets the best agreement value for a given agent
216 * @param agentType - the agent's type
217 * @param value - the value
218 */
219 public void setBestAgreementValue(AutomatedAgentType agentType, double value) {
220 agentType.setBestAgreementValue(value);
221 }
222
223 /**
224 * Sets the best agreement indices for a given agent
225 * @param agentType - the agent's type
226 * @param currentAgreementIdx - the agreement indices
227 */
228 public void setBestAgreementIndices(AutomatedAgentType agentType, int[] currentAgreementIdx) {
229 agentType.setBestAgreementIndices(currentAgreementIdx);
230 }
231
232 /**
233 * Sets the worst agreement value for a given agent
234 * @param agentType - the agent's type
235 * @param value - the value
236 */
237 public void setWorstAgreementValue(AutomatedAgentType agentType, double value) {
238 agentType.setWorstAgreementValue(value);
239 }
240
241 /**
242 * Sets the worst agreement indices for a given agent
243 * @param agentType - the agent's type
244 * @param currentAgreementIdx - the agreement indices
245 */
246 public void setWorstAgreementIndices(AutomatedAgentType agentType, int[] currentAgreementIdx) {
247 agentType.setWorstAgreementIndices(currentAgreementIdx);
248 }
249 /**
250 * Initializes the best agreement -
251 * inits the indices and sets minimal value
252 * @param agentType - the agent's type
253 */
254 public void initializeBestAgreement(AutomatedAgentType agentType) {
255 agentType.setBestAgreementValue(AutomatedAgentType.VERY_SMALL_NUMBER);
256 agentType.initializeBestAgreementIndices();
257 }
258
259 /**
260 * Initializes the worst agreement -
261 * inits the indices and sets maximal value
262 * @param agentType - the agent's type
263 */
264 public void initializeWorstAgreement(AutomatedAgentType agentType) {
265 agentType.setWorstAgreementValue(AutomatedAgentType.VERY_HIGH_NUMBER);
266 agentType.initializeWorstAgreementIndices();
267
268 }
269
270 /**
271 * Return the time effect for the entire agreement
272 * @param agentType - the agent's type
273 * @return the time effect for the entire agreement
274 */
275 public double getAgreementTimeEffect(AutomatedAgentType agentType) {
276 return agentType.getAgreementTypeEffect();
277 }
278
279 /**
280 * Return the SQ value for a given agent
281 * @param agentType - the agent's type
282 * @return the status quo value computed for the current turn
283 * for a given agent type
284 */
285 public double getSQValue(AutomatedAgentType agentType) {
286 return agentType.getSQValue();
287 }
288
289 /**
290 * Return the opting out value for a given agent
291 * @param agentType - the agent's type
292 * @return the opting out value computed for the current turn
293 */
294 public double getOptOutValue(AutomatedAgentType agentType) {
295 return agentType.getOptOutValue();
296 }
297
298 /**
299 * @return the total number of agreement
300 */
301 public int getTotalAgreements(AutomatedAgentType agentType) {
302 return agentType.getTotalAgreements();
303 }
304
305
306 /**
307 * Set the automated agent type
308 * Possible types: COMPROMISE_TYPE, SHORT_TERM_TYPE and LONG_TERM_TYPE
309 *
310 */
311 public void setAutomatedAgentType(String side) {
312 // @@EXAMPLE@@
313 // using the short term type for the automated agent,
314 // no matter which side it plays
315 agent.setAgentType(side, AutomatedAgentsCore.LONG_TERM_TYPE_IDX);
316 }
317
318 public String getAgentSide() {
319 return agent.getAgentSide();
320 }
321
322 /**
323 *
324 * @return the selected offer of the agent at a given turn
325 */
326 public String getSelectedOffer() {
327 String sAutomatedAgentAgreement = agent.getAutomatedAgentAgreement();
328
329 return sAutomatedAgentAgreement;
330 }
331
332 /**
333 *
334 * @return the value of the selected offer of the agent at a given turn
335 */
336 public double getSelectedOfferValue() {
337 String sAutomatedAgentAgreement = agent.getAutomatedAgentAgreement();
338
339 int nextAgreementIndices[] = new int[AutomatedAgentType.MAX_ISSUES];
340 nextAgreementIndices = agent.getAgreementIndices(sAutomatedAgentAgreement);
341
342 double dNextAgreementValue = agent.getAgreementValue(nextAgreementIndices);
343
344 return dNextAgreementValue;
345 }
346
347 /**
348 *
349 * @return the value of previously accepted agreement
350 */
351 public double getAcceptedAgreementsValue() {
352 // The accepted agreement is saved in agent.m_PreviosAcceptedOffer
353 // Note: agreements can be incremental. The m_PreviosAcceptedOffer saves the whole agreement
354 int previousAcceptedAgreementsIndices[] = new int[AutomatedAgentType.MAX_ISSUES];
355 previousAcceptedAgreementsIndices = agent.getPreviousAcceptedAgreementsIndices();
356
357 double dAcceptedAgreementValue = agent.getAgreementValue(previousAcceptedAgreementsIndices);
358
359 return dAcceptedAgreementValue;
360
361 }
362
363 public int[] getAcceptedAgreementIdx() {
364 // The accepted agreement is saved in agent.m_PreviosAcceptedOffer
365 // Note: agreements can be incremental. The m_PreviosAcceptedOffer saves the whole agreement
366 int previousAcceptedAgreementsIndices[] = new int[AutomatedAgentType.MAX_ISSUES];
367 previousAcceptedAgreementsIndices = agent.getPreviousAcceptedAgreementsIndices();
368
369 return previousAcceptedAgreementsIndices;
370
371 }
372
373 /**
374 * calculate the selected offer the agent will propose
375 * in the following turn
376 *
377 */
378 public void calculateNextTurnOffer() {
379 agent.calculateNextTurnOffer();
380 }
381
382 /**
383 *
384 * @return the value of the selected offer the agent will propose
385 * in the following turn
386 */
387 public double getNextTurnOfferValue() {
388 double dAutomatedAgentNextOfferValueForAgent = agent.getNextTurnAutomatedAgentOfferValue();
389 return dAutomatedAgentNextOfferValueForAgent;
390 }
391
392 /**
393 * Iterator for going over all possible agreements
394 * @param totalIssuesNum - the total number of issues in the negotiatoin
395 * @param currentAgreementIdx - the current agreement indices
396 * @param maxIssueValues - the maximal issue value
397 */
398 public void getNextAgreement(int totalIssuesNum, int[] currentAgreementIdx, int[] maxIssueValues) {
399 //TODO:DEBUG THIS
400 // receiveMessage issue values indices for evaluating the next agreement
401 boolean bFinishUpdate = false;
402 for (int k = totalIssuesNum-1; k >= 0 && !bFinishUpdate; --k)
403 {
404 if (currentAgreementIdx[k]+1 >= maxIssueValues[k])
405 {
406 currentAgreementIdx[k] = 0;
407 }
408 else
409 {
410 currentAgreementIdx[k]++;
411 bFinishUpdate = true;
412 }
413 }
414 }
415
416 /**
417 * Get the opponent's side
418 * @param sideName - the type of side (A or B)
419 * @param type - the type (compromise, short, long)
420 * @return
421 */
422 public AutomatedAgentType getNextTurnSideAgentType(String sideName, int type) {
423 AutomatedAgentType agentType = null;
424 agentType = agent.getNextTurnSideAgentType(sideName, type);
425 return agentType;
426 }
427
428 /**
429 * Get the opponent's side
430 * @param sideName - the type of side (A or B)
431 * @param type - the type (compromise, short, long)
432 * @return
433 */
434 public AutomatedAgentType getCurrentTurnSideAgentType(String sideName, int type) {
435 AutomatedAgentType agentType = null;
436 agentType = agent.getCurrentTurnSideAgentType(sideName, type);
437 return agentType;
438 }
439
440 /**
441 *
442 * @return the value of the selected offer for the next turn
443 */
444 public double getNextTurnAutomatedAgentValue() {
445 return agent.getNextTurnAutomatedAgentValue();
446 }
447
448 /**
449 *
450 * @return the value of the selected offer for the current turn
451 */
452 public double getCurrentTurnAutomatedAgentValue() {
453 return agent.getCurrentTurnAutomatedAgentValue();
454 }
455
456
457
458 /**
459 * Sets the value of the selected offer for the current turn
460 * @param agreementValue - the agreement's value
461 */
462 public void setCurrentTurnAutomatedAgentValue(double agreementValue) {
463 agent.setCurrentTurnAutomatedAgentValue(agreementValue);
464 }
465
466 /**
467 * Sets the value of the selected offer for the following turn
468 * @param agreementValue - the agreement's value
469 */
470 public void setNextTurnAutomatedAgentSelectedValue(double agreementValue) {
471 agent.setNextTurnAutomatedAgentSelectedValue(agreementValue);
472 }
473
474 /**
475 * Sets the value of the selected offer for the following turn
476 * for the opponent
477 * @param agreementValue - the agreement's value
478 */
479 public void setNextTurnOpponentSelectedValue(double agreementValue) {
480 agent.setNextTurnOpponentSelectedValue(agreementValue);
481 }
482
483 /**
484 * Sets the value of the selected offer for the current turn
485 * for the opponent
486 * @param agreementValue - the agreement's value
487 */
488 public void setCurrentTurnOpponentSelectedValue(double agreementValue) {
489 agent.setCurrentTurnOpponentSelectedValue(agreementValue);
490 }
491
492 /**
493 * Sets the String of the selected offer for the following turn
494 * @param agreementStr - the agreement as String
495 */
496 public void setNextTurnAgreementString(String agreementStr) {
497 agent.setNextTurnAgreementString(agreementStr);
498 }
499
500 /**
501 * Sets the String of the selected offer for the current turn
502 * @param agreementStr - the agreement as String
503 */
504 public void setCurrentTurnAgreementString(String agreementStr) {
505 agent.setCurrentTurnAgreementString(agreementStr);
506 }
507
508 /**
509 * Sets the side of the opponent (Side A or B)
510 * @param agreementStr - the agreement as String
511 */
512 public void setNextTurnOpponentType(int type) {
513 agent.setNextTurnOpponentType(type);
514 }
515
516 /**
517 * Calculating the response to a given proposal.
518 * This method eventually calls AbstractAutomatedAgent.calculateResponse()
519 * @see AbstractAutomatedAgent#calculateResponse
520 *
521 */
522 public void calculateResponse(int messageType, int[] currentAgreementIdx, String message) {
523 agent.calculateResponse(messageType, currentAgreementIdx, message);
524 }
525
526 /**
527 * @return string of a given agreement for the current agent
528 */
529 public String getMessageByIndices(int[] currentAgreementIdx) {
530 return agent.getAgreementStr(currentAgreementIdx);
531 }
532
533 /**
534 * @return indices of a given agreement for the current agent
535 */
536 public int[] getMessageIndicesByMessage(String currentAgreementStr) {
537 return agent.getAgreementIndices(currentAgreementStr);
538 }
539
540 /**
541 * @return value of a given agreement for the current agent
542 */
543 public double getAgreementValue(int[] currentAgreementIdx) {
544 double dAgreementValue = agent.getAgreementValue(currentAgreementIdx);
545 return dAgreementValue;
546 }
547
548 /**
549 *
550 * @return the total number of seconds per each turn
551 */
552 public double getSecondPerTurn() {
553 return agent.getSecondsForTurn();
554 }
555
556 /**
557 *
558 * @return the total number of issues for negotiation
559 */
560 public int getTotalIssues(AutomatedAgentType agentType) {
561 return agentType.getIssuesNum();
562 }
563
564 /**
565 * return the maximal value for the agent for issue i
566 * @param agentType - the agent's type
567 * @param issueNum - the issue number
568 * @return the maximal value per that issue
569 */
570 public int getMaxValuePerIssue(AutomatedAgentType agentType, int issueNum) {
571 return agentType.getMaxIssueValue(issueNum);
572 }
573
574 /**
575 *
576 * @return whether the flag for sending offers/queries/promises
577 * is true or not
578 */
579 public boolean getSendOfferFlag() {
580 return agent.getSendOfferFlag();
581 }
582
583 /**
584 * Sets the boolean flag of sending offers/queries/promises
585 * @param flag - true if wanting to send message, false - o/w
586 */
587 public void setSendOfferFlag(boolean flag) {
588 agent.setSendOfferFlag(flag);
589 }
590}
Note: See TracBrowser for help on using the repository browser.