source: src/main/java/agents/TestAgent.java@ 209

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