1 | package agents;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import java.util.StringTokenizer;
|
---|
5 |
|
---|
6 | import agents.qoagent2.QAgentsCore;
|
---|
7 | import agents.qoagent2.QMessages;
|
---|
8 | import genius.core.Agent;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.actions.Accept;
|
---|
11 | import genius.core.actions.Action;
|
---|
12 | import genius.core.actions.EndNegotiation;
|
---|
13 | import genius.core.actions.Offer;
|
---|
14 | import genius.core.issue.Issue;
|
---|
15 | import genius.core.issue.IssueDiscrete;
|
---|
16 | import genius.core.issue.Value;
|
---|
17 | import genius.core.issue.ValueDiscrete;
|
---|
18 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
19 |
|
---|
20 | public class QOAgent extends Agent {
|
---|
21 | private enum ACTIONTYPE {
|
---|
22 | START, OFFER, ACCEPT, BREAKOFF
|
---|
23 | };
|
---|
24 |
|
---|
25 | private agents.qoagent2.QOAgent m_QOAgent;
|
---|
26 | private boolean fFirstOffer;
|
---|
27 | private Action fNextAction;
|
---|
28 | private int fMessageId;
|
---|
29 | public AdditiveUtilitySpace[] opponentModels;
|
---|
30 | private Bid lOppntBid = null; // last opponent bid
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public Action chooseAction() {
|
---|
34 |
|
---|
35 | if (fFirstOffer) {
|
---|
36 | m_QOAgent.calculateFirstOffer();
|
---|
37 | fFirstOffer = false;
|
---|
38 | } else {
|
---|
39 | // m_QOAgent.incrementCurrentTurn();
|
---|
40 | }
|
---|
41 | return fNextAction;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public String getVersion() {
|
---|
46 | return "1.0";
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void init() {
|
---|
51 | fFirstOffer = true;
|
---|
52 | fMessageId = 1;
|
---|
53 | opponentModels = new AdditiveUtilitySpace[3];
|
---|
54 | try {
|
---|
55 | opponentModels[0] = new AdditiveUtilitySpace(
|
---|
56 | utilitySpace.getDomain(), getName() + "_long_term.xml");
|
---|
57 | opponentModels[1] = new AdditiveUtilitySpace(
|
---|
58 | utilitySpace.getDomain(), getName() + "_short_term.xml");
|
---|
59 | opponentModels[2] = new AdditiveUtilitySpace(
|
---|
60 | utilitySpace.getDomain(), getName() + "_compromise.xml");
|
---|
61 | } catch (Exception e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | }
|
---|
64 | m_QOAgent = new agents.qoagent2.QOAgent(this, false, "Zimbabwe", "no",
|
---|
65 | "QOAgent", "1");
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void ReceiveMessage(Action opponentAction) {
|
---|
71 | String sMessage = "";
|
---|
72 | ACTIONTYPE lActionType;
|
---|
73 | lActionType = getActionType(opponentAction);
|
---|
74 | switch (lActionType) {
|
---|
75 | case OFFER: // Offer received from opponent
|
---|
76 | try {
|
---|
77 | lOppntBid = ((Offer) opponentAction).getBid();
|
---|
78 | if (fFirstOffer) {
|
---|
79 | sMessage = "type offer source 1 target 2 tag "
|
---|
80 | + String.valueOf(fMessageId) + " issueSet ";
|
---|
81 | } else {
|
---|
82 | sMessage = "type counter_offer source 1 target 2 tag "
|
---|
83 | + String.valueOf(fMessageId) + " issueSet ";
|
---|
84 | }
|
---|
85 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
86 | sMessage = sMessage
|
---|
87 | + lOppntBid.getValue(lIssue.getNumber()) + "*"
|
---|
88 | + lIssue.getName() + "*";
|
---|
89 | }
|
---|
90 |
|
---|
91 | } catch (Exception e) {
|
---|
92 | e.printStackTrace();
|
---|
93 | }
|
---|
94 | break;
|
---|
95 | case ACCEPT: // Presumably, opponent accepted last bid, but let's
|
---|
96 | // check...
|
---|
97 | case BREAKOFF:
|
---|
98 | // nothing left to do. Negotiation ended, which should be checked by
|
---|
99 | // Negotiator...
|
---|
100 | break;
|
---|
101 | default:
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | m_QOAgent.receivedMessage(sMessage);
|
---|
105 | }
|
---|
106 |
|
---|
107 | private ACTIONTYPE getActionType(Action lAction) {
|
---|
108 | ACTIONTYPE lActionType = ACTIONTYPE.START;
|
---|
109 | if (lAction instanceof Offer)
|
---|
110 | lActionType = ACTIONTYPE.OFFER;
|
---|
111 | else if (lAction instanceof Accept)
|
---|
112 | lActionType = ACTIONTYPE.ACCEPT;
|
---|
113 | else if (lAction instanceof EndNegotiation)
|
---|
114 | lActionType = ACTIONTYPE.BREAKOFF;
|
---|
115 | return lActionType;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void prepareAction(int pMessageType, String pMessage) {
|
---|
119 | String sFormattedMsg = "";
|
---|
120 | Action lAction = null;
|
---|
121 |
|
---|
122 | switch (pMessageType) {
|
---|
123 | case QMessages.OFFER:
|
---|
124 | case QMessages.COUNTER_OFFER: {
|
---|
125 | /*
|
---|
126 | * sFormattedMsg = "type offer" + " source " + m_agent.getPartyId()
|
---|
127 | * + " target " + m_agent.getOpponentAgentId() + " tag " +
|
---|
128 | * m_agent.getMsgId() + " issueSet ";
|
---|
129 | *
|
---|
130 | * sFormattedMsg += sMsgBody;
|
---|
131 | */
|
---|
132 | HashMap<Integer, Value> lValues = new HashMap<Integer, Value>();
|
---|
133 |
|
---|
134 | String sOffer = pMessage
|
---|
135 | .substring(pMessage.indexOf("issueSet ") + 9);
|
---|
136 | // tokenize the agreement by issue separator
|
---|
137 | StringTokenizer st = new StringTokenizer(sOffer,
|
---|
138 | QAgentsCore.ISSUE_SEPARATOR_STR);
|
---|
139 |
|
---|
140 | // the agreement string has the following structure:
|
---|
141 | // issue_value SEPARATOR issue_name SEPARATOR...
|
---|
142 | while (st.hasMoreTokens()) {
|
---|
143 | // get issue value
|
---|
144 | String sCurrentIssueValue = st.nextToken();
|
---|
145 |
|
---|
146 | String sCurrentIssueName = st.nextToken();
|
---|
147 |
|
---|
148 | // find the issue name and set the index in the returned array
|
---|
149 | Issue lIssue = null;
|
---|
150 | for (Issue lTmp : utilitySpace.getDomain().getIssues()) {
|
---|
151 | if (lTmp.getName().equals(sCurrentIssueName)) {
|
---|
152 | lIssue = lTmp;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | IssueDiscrete lIssueDisc = (IssueDiscrete) lIssue;
|
---|
157 | // find the value
|
---|
158 | ValueDiscrete lValue = null;
|
---|
159 | for (ValueDiscrete lTmp : lIssueDisc.getValues()) {
|
---|
160 | if (lTmp.getValue().equals(sCurrentIssueValue)) {
|
---|
161 | lValue = lTmp;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | lValues.put(lIssue.getNumber(), lValue);
|
---|
166 | } // end while - has more tokens
|
---|
167 | try {
|
---|
168 | Bid lBid = new Bid(utilitySpace.getDomain(), lValues);
|
---|
169 | lAction = new Offer(this.getAgentID(), lBid);
|
---|
170 | } catch (Exception e) {
|
---|
171 | e.printStackTrace();
|
---|
172 | }
|
---|
173 | }
|
---|
174 | break;
|
---|
175 | case QMessages.ACCEPT: {
|
---|
176 | lAction = new Accept(this.getAgentID(), lOppntBid);
|
---|
177 | }
|
---|
178 | break;
|
---|
179 | case QMessages.REJECT: {
|
---|
180 | m_QOAgent.incrementCurrentTurn();
|
---|
181 |
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | default: {
|
---|
186 | System.out.println("[QO]ERROR: Invalid message kind: "
|
---|
187 | + pMessageType + " [QMessages::formatMessage(199)]");
|
---|
188 |
|
---|
189 | }
|
---|
190 | break;
|
---|
191 | }
|
---|
192 |
|
---|
193 | fNextAction = lAction;
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|