source: src/main/java/agents/SimpleAgent.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: 6.7 KB
Line 
1package agents;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Agent;
8import genius.core.Bid;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.ActionWithBid;
12import genius.core.actions.Offer;
13import genius.core.issue.Issue;
14import genius.core.issue.IssueDiscrete;
15import genius.core.issue.IssueInteger;
16import genius.core.issue.IssueReal;
17import genius.core.issue.Value;
18import genius.core.issue.ValueInteger;
19import genius.core.issue.ValueReal;
20import genius.core.timeline.Timeline;
21
22/**
23 * @author W.Pasman Some improvements over the standard SimpleAgent.
24 *
25 * Random Walker, Zero Intelligence Agent
26 */
27public class SimpleAgent extends Agent {
28 private Action actionOfPartner = null;
29 /**
30 * Note: {@link SimpleAgent} does not account for the discount factor in its
31 * computations
32 */
33 private static double MINIMUM_BID_UTILITY = 0.0;
34
35 /**
36 * init is called when a next session starts with the same opponent.
37 */
38 @Override
39 public void init() {
40 MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
41 }
42
43 @Override
44 public String getVersion() {
45 return "3.1";
46 }
47
48 @Override
49 public String getName() {
50 return "Simple Agent";
51 }
52
53 @Override
54 public void ReceiveMessage(Action opponentAction) {
55 actionOfPartner = opponentAction;
56 }
57
58 @Override
59 public Action chooseAction() {
60 Action action = null;
61 Bid partnerBid = null;
62 try {
63 if (actionOfPartner == null)
64 action = chooseRandomBidAction();
65 if (actionOfPartner instanceof Offer) {
66 partnerBid = ((Offer) actionOfPartner).getBid();
67 double offeredUtilFromOpponent = getUtility(partnerBid);
68 // get current time
69 double time = timeline.getTime();
70 action = chooseRandomBidAction();
71
72 Bid myBid = ((Offer) action).getBid();
73 double myOfferedUtil = getUtility(myBid);
74
75 // accept under certain circumstances
76 if (isAcceptable(offeredUtilFromOpponent, myOfferedUtil, time))
77 action = new Accept(getAgentID(), partnerBid);
78
79 }
80 if (timeline.getType().equals(Timeline.Type.Time)) {
81 sleep(0.005); // just for fun
82 }
83
84 } catch (Exception e) {
85 System.out.println("Exception in ChooseAction:" + e.getMessage());
86 action = new Accept(getAgentID(), partnerBid); // best guess if
87 // things go wrong.
88 }
89 return action;
90 }
91
92 private boolean isAcceptable(double offeredUtilFromOpponent,
93 double myOfferedUtil, double time) throws Exception {
94 double P = Paccept(offeredUtilFromOpponent, time);
95 if (P > Math.random())
96 return true;
97 return false;
98 }
99
100 /**
101 * Wrapper for getRandomBid, for convenience.
102 *
103 * @return new Action(Bid(..)), with bid utility > MINIMUM_BID_UTIL. If a
104 * problem occurs, it returns an Accept() action.
105 */
106 private Action chooseRandomBidAction() {
107 Bid nextBid = null;
108 try {
109 nextBid = getRandomBid();
110 } catch (Exception e) {
111 System.out.println("Problem with received bid:" + e.getMessage()
112 + ". cancelling bidding");
113 }
114 if (nextBid == null)
115 return new Accept(getAgentID(),
116 ((ActionWithBid) actionOfPartner).getBid());
117 return (new Offer(getAgentID(), nextBid));
118 }
119
120 /**
121 * @return a random bid with high enough utility value.
122 * @throws Exception
123 * if we can't compute the utility (eg no evaluators have been
124 * set) or when other evaluators than a DiscreteEvaluator are
125 * present in the util space.
126 */
127 private Bid getRandomBid() throws Exception {
128 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
129 // <issuenumber,chosen
130 // value
131 // string>
132 List<Issue> issues = utilitySpace.getDomain().getIssues();
133 Random randomnr = new Random();
134
135 // createFrom a random bid with utility>MINIMUM_BID_UTIL.
136 // note that this may never succeed if you set MINIMUM too high!!!
137 // in that case we will search for a bid till the time is up (3 minutes)
138 // but this is just a simple agent.
139 Bid bid = null;
140 do {
141 for (Issue lIssue : issues) {
142 switch (lIssue.getType()) {
143 case DISCRETE:
144 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
145 int optionIndex = randomnr
146 .nextInt(lIssueDiscrete.getNumberOfValues());
147 values.put(lIssue.getNumber(),
148 lIssueDiscrete.getValue(optionIndex));
149 break;
150 case REAL:
151 IssueReal lIssueReal = (IssueReal) lIssue;
152 int optionInd = randomnr.nextInt(
153 lIssueReal.getNumberOfDiscretizationSteps() - 1);
154 values.put(lIssueReal.getNumber(), new ValueReal(lIssueReal
155 .getLowerBound()
156 + (lIssueReal.getUpperBound()
157 - lIssueReal.getLowerBound()) * (optionInd)
158 / (lIssueReal
159 .getNumberOfDiscretizationSteps())));
160 break;
161 case INTEGER:
162 IssueInteger lIssueInteger = (IssueInteger) lIssue;
163 int optionIndex2 = lIssueInteger.getLowerBound()
164 + randomnr.nextInt(lIssueInteger.getUpperBound()
165 - lIssueInteger.getLowerBound());
166 values.put(lIssueInteger.getNumber(),
167 new ValueInteger(optionIndex2));
168 break;
169 default:
170 throw new Exception("issue type " + lIssue.getType()
171 + " not supported by SimpleAgent2");
172 }
173 }
174 bid = new Bid(utilitySpace.getDomain(), values);
175 } while (getUtility(bid) < MINIMUM_BID_UTILITY);
176
177 return bid;
178 }
179
180 /**
181 * This function determines the accept probability for an offer. At t=0 it
182 * will prefer high-utility offers. As t gets closer to 1, it will accept
183 * lower utility offers with increasing probability. it will never accept
184 * offers with utility 0.
185 *
186 * @param u
187 * is the utility
188 * @param t
189 * is the time as fraction of the total available time (t=0 at
190 * start, and t=1 at end time)
191 * @return the probability of an accept at time t
192 * @throws Exception
193 * if you use wrong values for u or t.
194 *
195 */
196 double Paccept(double u, double t1) throws Exception {
197 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
198 if (u < 0 || u > 1.05)
199 throw new Exception("utility " + u + " outside [0,1]");
200 // normalization may be slightly off, therefore we have a broad boundary
201 // up to 1.05
202 if (t < 0 || t > 1)
203 throw new Exception("time " + t + " outside [0,1]");
204 if (u > 1.)
205 u = 1;
206 if (t == 0.5)
207 return u;
208 return (u - 2. * u * t
209 + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u * (-1. + 2 * t))))
210 / (-1. + 2 * t);
211 }
212
213 double sq(double x) {
214 return x * x;
215 }
216
217 @Override
218 public String getDescription() {
219 return "Random bids until good offer is received";
220 }
221
222}
Note: See TracBrowser for help on using the repository browser.