source: src/main/java/agents/SimilarityAgent.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: 8.2 KB
Line 
1package agents;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.Map.Entry;
6import java.util.Random;
7
8import agents.similarity.Similarity;
9import genius.core.Agent;
10import genius.core.Bid;
11import genius.core.BidIterator;
12import genius.core.DomainImpl;
13import genius.core.SupportedNegotiationSetting;
14import genius.core.actions.Accept;
15import genius.core.actions.Action;
16import genius.core.actions.EndNegotiation;
17import genius.core.actions.Offer;
18
19public class SimilarityAgent extends Agent {
20
21 private Action messageOpponent;
22 private Bid myLastBid = null;
23 private Action myLastAction = null;
24 private Similarity fSimilarity;
25
26 private enum ACTIONTYPE {
27 START, OFFER, ACCEPT, BREAKOFF
28 };
29
30 private enum STRATEGY {
31 SMART, SERIAL, RESPONSIVE, RANDOM
32 };
33
34 private STRATEGY fStrategy = STRATEGY.SMART;
35 private int fSmartSteps;
36 private static final double CONCESSIONFACTOR = 0.035;
37 private static final double ALLOWED_UTILITY_DEVIATION = 0.01;
38 private static final int NUMBER_OF_SMART_STEPS = 0;
39 private HashMap<Bid, Double> utilityCash;
40
41 // Class constructor
42 public SimilarityAgent() {
43 super();
44 }
45
46 @Override
47 public void init() {
48 messageOpponent = null;
49 myLastBid = null;
50 myLastAction = null;
51 fSmartSteps = 0;
52 // load similarity info from the utility space
53 fSimilarity = new Similarity(utilitySpace.getDomain());
54 // HACK we assume DomainImpl is used
55 fSimilarity.loadFromXML(
56 ((DomainImpl) utilitySpace.getDomain()).getXMLRoot());
57 // build utility cash
58 utilityCash = new HashMap<Bid, Double>();
59 BidIterator lIter = new BidIterator(utilitySpace.getDomain());
60 try {
61 while (lIter.hasNext()) {
62 Bid tmpBid = lIter.next();
63 utilityCash.put(tmpBid,
64 new Double(utilitySpace.getUtility(tmpBid)));
65 } // while
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69
70 }
71
72 // Class methods
73 @Override
74 public void ReceiveMessage(Action opponentAction) {
75 messageOpponent = opponentAction;
76 }
77
78 @Override
79 public String getVersion() {
80 return "1.0";
81 };
82
83 private Action proposeInitialBid() {
84 Bid lBid = null;
85 /*
86 * Value[] values = new Value[4]; if(myName.equals("Buyer")) { values[0]
87 * = new ValueReal(0.6); values[1] = new ValueReal(0.9); values[2] = new
88 * ValueReal(0.6); values[3] = new ValueReal(1); } else { values[0] =
89 * new ValueReal(0); values[1] = new ValueReal(0.2); values[2] = new
90 * ValueReal(0); values[3] = new ValueReal(0.5); } lBid = new
91 * Bid(utilitySpace.getDomain(), values);
92 */
93 // Return (one of the) possible bid(s) with maximal utility.
94 try {
95 lBid = utilitySpace.getMaxUtilityBid();
96 Bid lBid2 = getBidRandomWalk(utilitySpace.getUtility(lBid) * 0.98,
97 utilitySpace.getUtility(lBid));
98 if (lBid != null) {
99 lBid = lBid2;
100 }
101 } catch (Exception e) {
102 e.printStackTrace();
103 }
104 fSmartSteps = NUMBER_OF_SMART_STEPS + 1;
105 myLastBid = lBid;
106
107 return new Offer(getAgentID(), lBid);
108
109 }
110
111 private Bid getNextBidSmart(Bid pOppntBid) {
112 double lMyUtility = 0, lOppntUtility = 0, lTargetUtility;
113 // Both parties have made an initial bid. Compute associated utilities
114 // from my point of view.
115 try {
116 lMyUtility = utilitySpace.getUtility(myLastBid);
117 lOppntUtility = utilitySpace.getUtility(pOppntBid);
118 } catch (Exception e) {
119 e.printStackTrace();
120 }
121 if (fSmartSteps >= NUMBER_OF_SMART_STEPS) {
122 lTargetUtility = getTargetUtility(lMyUtility, lOppntUtility);
123 fSmartSteps = 0;
124 } else {
125 lTargetUtility = lMyUtility;
126 fSmartSteps++;
127 }
128 Bid lMyLastBid = myLastBid;
129 Bid lBid = getTradeOffExhaustive(lTargetUtility, pOppntBid);
130 if (Math.abs(fSimilarity.getSimilarity(lMyLastBid, lBid)) > 0.993) {
131 lTargetUtility = getTargetUtility(lMyUtility, lOppntUtility);
132 fSmartSteps = 0;
133 lBid = getTradeOffExhaustive(lTargetUtility, pOppntBid);
134 }
135 return lBid;
136 }
137
138 private Bid getTradeOffExhaustive(double pUtility, Bid pOppntBid) {
139 Bid lBid = null;
140 double lSim = -1;
141 // BidIterator lIter = new BidIterator(utilitySpace.getDomain());
142 // while(lIter.hasNext()) {
143 for (Entry<Bid, Double> entry : utilityCash.entrySet()) {
144 Bid tmpBid = entry.getKey();
145 double lUtil = entry.getValue();
146 if (Math.abs(lUtil - pUtility) < ALLOWED_UTILITY_DEVIATION) {
147 double lTmpSim = fSimilarity.getSimilarity(tmpBid, pOppntBid);
148 if (lTmpSim > lSim) {
149 lSim = lTmpSim;
150 lBid = tmpBid;
151 }
152 }
153 } // while
154 return lBid;
155 }
156
157 private Action proposeNextBid(Bid pOppntBid) {
158 Bid lBid = null;
159 switch (fStrategy) {
160 case SMART:
161 lBid = getNextBidSmart(pOppntBid);
162 break;
163 }
164 myLastBid = lBid;
165 return new Offer(getAgentID(), lBid);
166 }
167
168 @Override
169 public Action chooseAction() {
170 Action lAction = null;
171 ACTIONTYPE lActionType;
172 Bid lOppntBid = null;
173
174 lActionType = getActionType(messageOpponent);
175 try {
176 switch (lActionType) {
177 case OFFER: // Offer received from opponent
178 lOppntBid = ((Offer) messageOpponent).getBid();
179 if (myLastAction == null)
180 // Other agent started, lets propose my initial bid.
181 lAction = proposeInitialBid();
182 else if (utilitySpace.getUtility(lOppntBid) >= utilitySpace
183 .getUtility(myLastBid))
184 // Opponent bids equally, or outbids my previous bid, so
185 // lets
186 // accept
187 lAction = new Accept(getAgentID(), lOppntBid);
188 else
189 // Propose counteroffer. Get next bid.
190 lAction = proposeNextBid(lOppntBid);
191 // Check if utility of the new bid is lower than utility of the
192 // opponent's last bid
193 // if yes then accept last bid of the opponent.
194 if (utilitySpace.getUtility(lOppntBid) >= utilitySpace
195 .getUtility(myLastBid))
196 // Opponent bids equally, or outbids my previous bid, so
197 // lets
198 // accept
199 lAction = new Accept(getAgentID(), lOppntBid);
200 break;
201 case ACCEPT: // Presumably, opponent accepted last bid, but let's
202 // check...
203 break;
204 case BREAKOFF:
205 // nothing left to do. Negotiation ended, which should be
206 // checked by
207 // Negotiator...
208 break;
209 default:
210 // I am starting, but not sure whether Negotiator checks this,
211 // so
212 // lets check also myLastAction...
213 if (myLastAction == null)
214 lAction = proposeInitialBid();
215 else
216 // simply repeat last action
217 lAction = myLastAction;
218 break;
219 }
220 } catch (Exception e) {
221 e.printStackTrace();
222 }
223 myLastAction = lAction;
224 return lAction;
225 }
226
227 private ACTIONTYPE getActionType(Action lAction) {
228 ACTIONTYPE lActionType = ACTIONTYPE.START;
229 if (lAction instanceof Offer)
230 lActionType = ACTIONTYPE.OFFER;
231 else if (lAction instanceof Accept)
232 lActionType = ACTIONTYPE.ACCEPT;
233 else if (lAction instanceof EndNegotiation)
234 lActionType = ACTIONTYPE.BREAKOFF;
235 return lActionType;
236 }
237
238 private Bid getBidRandomWalk(double lowerBound, double upperBoud)
239 throws Exception {
240 // find all suitable bids
241 ArrayList<Bid> lBidsRange = new ArrayList<Bid>();
242 BidIterator lIter = new BidIterator(utilitySpace.getDomain());
243 while (lIter.hasNext()) {
244 Bid tmpBid = lIter.next();
245 double lUtil = 0;
246 try {
247 lUtil = utilitySpace.getUtility(tmpBid);
248 if (lUtil >= lowerBound && lUtil <= upperBoud)
249 lBidsRange.add(tmpBid);
250 } catch (Exception e) {
251 e.printStackTrace();
252 }
253 } // while
254 // Return bid that gets closest to target utility in a "random walk"
255 // search.
256 /*
257 * lBestBid = utilitySpace.getDomain().getRandomBid(); while(true) {
258 * lBid = utilitySpace.getDomain().getRandomBid(); if
259 * ((utilitySpace.getUtility(lBid) > lowerBound)&&
260 * (utilitySpace.getUtility(lBestBid) < upperBoud)) { lBestBid = lBid;
261 * break; }*
262 *
263 * }
264 */
265 if (lBidsRange.size() < 1) {
266 return null;
267 }
268 if (lBidsRange.size() < 2) {
269 return lBidsRange.get(0);
270 } else {
271 int lIndex = (new Random()).nextInt(lBidsRange.size() - 1);
272 return lBidsRange.get(lIndex);
273 }
274 }
275
276 private double getTargetUtility(double myUtility, double oppntUtility) {
277 return myUtility - getConcessionFactor();
278 }
279
280 private double getConcessionFactor() {
281 // The more the agent is willing to concess on its aspiration value, the
282 // higher this factor.
283 return CONCESSIONFACTOR;
284 }
285
286 @Override
287 public SupportedNegotiationSetting getSupportedNegotiationSetting() {
288 return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
289 }
290
291 @Override
292 public String getDescription() {
293 return "Tries to make bids similar to received offers";
294 }
295
296}
Note: See TracBrowser for help on using the repository browser.