source: src/main/java/agents/RandomIncreasingUtilAgent.java@ 200

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