source: src/main/java/agents/SimpleAgentSavingBidHistory.java@ 1

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

Initial import : Genius 9.0.0

File size: 9.1 KB
Line 
1package agents;
2
3import java.io.Serializable;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Random;
7
8import genius.core.Agent;
9import genius.core.Bid;
10import genius.core.BidHistory;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.EndNegotiation;
14import genius.core.actions.Offer;
15import genius.core.bidding.BidDetails;
16import genius.core.issue.Issue;
17import genius.core.issue.IssueDiscrete;
18import genius.core.issue.IssueInteger;
19import genius.core.issue.IssueReal;
20import genius.core.issue.Value;
21import genius.core.issue.ValueInteger;
22import genius.core.issue.ValueReal;
23
24/**
25 * @author S. Hourmann Some improvements over the standard Agent. Saving Bid
26 * History for the session.
27 *
28 * Random Walker, Zero Intelligence Agent
29 */
30public class SimpleAgentSavingBidHistory extends Agent {
31 // "state" represents "where am I in the code".
32 // I want to print "state" when I print a message about saving data.
33 private String state;
34 private Action actionOfPartner = null;
35
36 /**
37 * Note: {@link SimpleAgentSavingBidHistory} does not account for the
38 * discount factor in its computations
39 */
40 private static double MINIMUM_BID_UTILITY = 0.0;
41
42 private BidHistory currSessOppBidHistory;
43 private BidHistory prevSessOppBidHistory;
44
45 public SimpleAgentSavingBidHistory() {
46 super();
47 this.currSessOppBidHistory = new BidHistory();
48 }
49
50 /**
51 * init is called when a next session starts with the same opponent.
52 */
53 public void init() {
54 MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
55 myBeginSession();
56 }
57
58 public void myBeginSession() {
59 System.out.println("Starting match num: " + sessionNr);
60
61 // ---- Code for trying save and load functionality
62 // First try to load saved data
63 // ---- Loading from agent's function "loadSessionData"
64 Serializable prev = this.loadSessionData();
65 if (!(prev == null)) {
66 prevSessOppBidHistory = (BidHistory) prev;
67 System.out
68 .println("---------/////////// NEW NEW NEW /////////////----------");
69 System.out.println("The size of the previous BidHistory is: "
70 + prevSessOppBidHistory.size());
71 } else {
72 // If didn't succeed, it means there is no data for this preference
73 // profile
74 // in this domain.
75 System.out.println("There is no history yet.");
76 }
77 }
78
79 @Override
80 public String getVersion() {
81 return "3.1";
82 }
83
84 @Override
85 public String getName() {
86 return "Simple Agent Saving Bid History";
87 }
88
89 public void ReceiveMessage(Action opponentAction) {
90 actionOfPartner = opponentAction;
91 if (opponentAction instanceof Offer) {
92 Bid bid = ((Offer) opponentAction).getBid();
93 // 2. store the opponent's trace
94 try {
95 BidDetails opponentBid = new BidDetails(bid,
96 utilitySpace.getUtility(bid), timeline.getTime());
97 currSessOppBidHistory.add(opponentBid);
98 } catch (Exception e) {
99 e.printStackTrace();
100 }
101 }
102 }
103
104 public Action chooseAction() {
105 Action action = null;
106 Bid partnerBid = null;
107 try {
108 if (actionOfPartner == null)
109 action = chooseRandomBidAction();
110 if (actionOfPartner instanceof Offer) {
111 partnerBid = ((Offer) actionOfPartner).getBid();
112 double offeredUtilFromOpponent = getUtility(partnerBid);
113 // get current time
114 double time = timeline.getTime();
115 action = chooseRandomBidAction();
116
117 Bid myBid = ((Offer) action).getBid();
118 double myOfferedUtil = getUtility(myBid);
119
120 // accept under certain circumstances
121 if (isAcceptable(offeredUtilFromOpponent, myOfferedUtil, time)) {
122 action = new Accept(getAgentID(), partnerBid);
123
124 // ---- Code for trying save and load functionality
125 // /////////////////////////////////
126 state = "I accepted so I'm trying to save. ";
127 tryToSaveAndPrintState();
128 // /////////////////////////////////
129
130 }
131 }
132 if (actionOfPartner instanceof EndNegotiation) {
133
134 // ---- Code for trying save and load functionality
135 // /////////////////////////////////
136 state = "Got EndNegotiation from opponentttttttttt. ";
137 tryToSaveAndPrintState();
138 // /////////////////////////////////
139 }
140 sleep(0.005); // just for fun
141 } catch (Exception e) {
142 System.out.println("Exception in ChooseAction:" + e.getMessage());
143
144 // ---- Code for trying save and load functionality
145 // /////////////////////////////////
146 state = "Got Exceptionnnnnnnnnn. ";
147 tryToSaveAndPrintState();
148 // /////////////////////////////////
149 // best guess if things go wrong.
150 action = new Accept(getAgentID(), partnerBid);
151 }
152 return action;
153 }
154
155 // ---- Code for trying save and load functionality
156 private void tryToSaveAndPrintState() {
157
158 // ---- Saving from agent's function "saveSessionData"
159 this.saveSessionData(currSessOppBidHistory);
160 System.out.println(state + "The size of the BidHistory I'm saving is: "
161 + currSessOppBidHistory.size());
162 }
163
164 private boolean isAcceptable(double offeredUtilFromOpponent,
165 double myOfferedUtil, double time) throws Exception {
166 double P = Paccept(offeredUtilFromOpponent, time);
167 if (P > Math.random())
168 return true;
169 return false;
170 }
171
172 /**
173 * Wrapper for getRandomBid, for convenience.
174 *
175 * @return new Action(Bid(..)), with bid utility > MINIMUM_BID_UTIL. If a
176 * problem occurs, it returns an Accept() action.
177 */
178 private Action chooseRandomBidAction() {
179 Bid nextBid = null;
180 try {
181 nextBid = getRandomBid();
182 } catch (Exception e) {
183 System.out.println("Problem with received bid:" + e.getMessage()
184 + ". cancelling bidding");
185 }
186 if (nextBid == null)
187 return (new Accept(getAgentID(), currSessOppBidHistory.getLastBid()));
188 return (new Offer(getAgentID(), nextBid));
189 }
190
191 /**
192 * @return a random bid with high enough utility value.
193 * @throws Exception
194 * if we can't compute the utility (eg no evaluators have been
195 * set) or when other evaluators than a DiscreteEvaluator are
196 * present in the util space.
197 */
198 private Bid getRandomBid() throws Exception {
199 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
200 // <issuenumber,chosen
201 // value
202 // string>
203 List<Issue> issues = utilitySpace.getDomain().getIssues();
204 Random randomnr = new Random();
205
206 // createFrom a random bid with utility>MINIMUM_BID_UTIL.
207 // note that this may never succeed if you set MINIMUM too high!!!
208 // in that case we will search for a bid till the time is up (3 minutes)
209 // but this is just a simple agent.
210 Bid bid = null;
211 do {
212 for (Issue lIssue : issues) {
213 switch (lIssue.getType()) {
214 case DISCRETE:
215 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
216 int optionIndex = randomnr.nextInt(lIssueDiscrete
217 .getNumberOfValues());
218 values.put(lIssue.getNumber(),
219 lIssueDiscrete.getValue(optionIndex));
220 break;
221 case REAL:
222 IssueReal lIssueReal = (IssueReal) lIssue;
223 int optionInd = randomnr.nextInt(lIssueReal
224 .getNumberOfDiscretizationSteps() - 1);
225 values.put(
226 lIssueReal.getNumber(),
227 new ValueReal(lIssueReal.getLowerBound()
228 + (lIssueReal.getUpperBound() - lIssueReal
229 .getLowerBound())
230 * (double) (optionInd)
231 / (double) (lIssueReal
232 .getNumberOfDiscretizationSteps())));
233 break;
234 case INTEGER:
235 IssueInteger lIssueInteger = (IssueInteger) lIssue;
236 int optionIndex2 = lIssueInteger.getLowerBound()
237 + randomnr.nextInt(lIssueInteger.getUpperBound()
238 - lIssueInteger.getLowerBound());
239 values.put(lIssueInteger.getNumber(), new ValueInteger(
240 optionIndex2));
241 break;
242 default:
243 throw new Exception("issue type " + lIssue.getType()
244 + " not supported by SamantaAgent2");
245 }
246 }
247 bid = new Bid(utilitySpace.getDomain(), values);
248 } while (getUtility(bid) < MINIMUM_BID_UTILITY);
249
250 return bid;
251 }
252
253 /**
254 * This function determines the accept probability for an offer. At t=0 it
255 * will prefer high-utility offers. As t gets closer to 1, it will accept
256 * lower utility offers with increasing probability. it will never accept
257 * offers with utility 0.
258 *
259 * @param u
260 * is the utility
261 * @param t
262 * is the time as fraction of the total available time (t=0 at
263 * start, and t=1 at end time)
264 * @return the probability of an accept at time t
265 * @throws Exception
266 * if you use wrong values for u or t.
267 *
268 */
269 double Paccept(double u, double t1) throws Exception {
270 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
271 if (u < 0 || u > 1.05)
272 throw new Exception("utility " + u + " outside [0,1]");
273 // normalization may be slightly off, therefore we have a broad boundary
274 // up to 1.05
275 if (t < 0 || t > 1)
276 throw new Exception("time " + t + " outside [0,1]");
277 if (u > 1.)
278 u = 1;
279 if (t == 0.5)
280 return u;
281 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u
282 * (-1. + 2 * t))))
283 / (-1. + 2 * t);
284 }
285
286 double sq(double x) {
287 return x * x;
288 }
289}
Note: See TracBrowser for help on using the repository browser.