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