source: src/main/java/agents/anac/y2014/AgentTD/AgentTD.java

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

Initial import : Genius 9.0.0

File size: 7.6 KB
Line 
1package agents.anac.y2014.AgentTD;
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.EndNegotiation;
13import genius.core.actions.Offer;
14import genius.core.issue.Issue;
15import genius.core.issue.IssueDiscrete;
16import genius.core.issue.IssueInteger;
17import genius.core.issue.IssueReal;
18import genius.core.issue.Value;
19import genius.core.issue.ValueInteger;
20import genius.core.issue.ValueReal;
21import genius.core.timeline.Timeline;
22
23public class AgentTD extends Agent {
24 private Action actionOfPartner = null;
25 private static double MINIMUM_BID_UTILITY = 0.0;
26 private Bid maximumBidFromPartner = null;
27 private Action temp_action = null;
28 private Bid temp_bid = null;
29 private static double DISCOUNT_FACTOR = 0.0;
30 private static boolean first = true;
31
32 @Override
33 public void init() {
34 MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
35 DISCOUNT_FACTOR = utilitySpace.getDiscountFactor();
36 }
37
38 @Override
39 public String getVersion() {
40 return "3.1";
41 }
42
43 @Override
44 public String getName() {
45 return "AgentTD";
46 }
47
48 @Override
49 public void ReceiveMessage(Action opponentAction) {
50 actionOfPartner = opponentAction;
51 }
52
53 @Override
54 public Action chooseAction() {
55 Action action = null;
56 try {
57 if (actionOfPartner == null || first) {
58 temp_action = chooseFirstBidAction();
59 action = temp_action;
60 first = false;
61 }
62 if (actionOfPartner instanceof Offer) {
63 Bid partnerBid = ((Offer) actionOfPartner).getBid();
64 double offeredUtilFromOpponent = getUtility(partnerBid);
65 if (offeredUtilFromOpponent > MINIMUM_BID_UTILITY) {
66 MINIMUM_BID_UTILITY = offeredUtilFromOpponent;
67 }
68
69 if (maximumBidFromPartner == null) {
70 maximumBidFromPartner = partnerBid;
71 } else {
72 if (getUtility(maximumBidFromPartner) < getUtility(
73 partnerBid))
74 maximumBidFromPartner = partnerBid;
75 }
76
77 double time = timeline.getTime();
78 if (time < 0.95) {
79 action = chooseRandomBidAction();
80 } else {
81 action = new Offer(getAgentID(), maximumBidFromPartner);
82 }
83
84 if (isAcceptable(offeredUtilFromOpponent, time)) {
85 action = new Accept(getAgentID(), partnerBid);
86 }
87 if (isEndNegotiation(time)) {
88 action = new EndNegotiation(getAgentID());
89 }
90
91 }
92
93 if (timeline.getType().equals(Timeline.Type.Time)) {
94 sleep(0.005);
95 }
96 } catch (Exception e) {
97 System.out.println("Exception in ChooseAction:" + e.getMessage());
98 action = new Accept(getAgentID(),
99 ((ActionWithBid) actionOfPartner).getBid());
100 }
101
102 return action;
103 }
104
105 private boolean isAcceptable(double offeredUtilFromOpponent, double time)
106 throws Exception {
107
108 if (offeredUtilFromOpponent > getUtility(maximumBidFromPartner)) {
109 if (time < 0.7) {
110 if (offeredUtilFromOpponent > 0.85)
111 return true;
112 } else if (time < 0.98) {
113 if (offeredUtilFromOpponent > 0.75)
114 return true;
115 } else {
116 return true;
117 }
118 }
119 return false;
120 }
121
122 private boolean isEndNegotiation(double time) throws Exception {
123 if (utilitySpace.getUtilityWithDiscount(maximumBidFromPartner,
124 time) < utilitySpace.getReservationValueWithDiscount(time))
125 return true;
126 return false;
127 }
128
129 private Action chooseRandomBidAction() {
130 Bid nextBid = null;
131 try {
132 nextBid = getRandomBid();
133 } catch (Exception e) {
134 System.out.println("Problem with received bid:" + e.getMessage()
135 + ". cancelling bidding");
136 }
137 if (nextBid == null)
138 return (new Accept(getAgentID(),
139 ((ActionWithBid) actionOfPartner).getBid()));
140 return (new Offer(getAgentID(), nextBid));
141 }
142
143 private Action chooseFirstBidAction() {
144 Bid nextBid = null;
145 try {
146 nextBid = getFirstBid();
147 } catch (Exception e) {
148 System.out.println("Problem with received bid:" + e.getMessage()
149 + ". cancelling bidding");
150 }
151 if (nextBid == null) {
152 return (new Accept(getAgentID(),
153 ((ActionWithBid) actionOfPartner).getBid()));
154 } else {
155 temp_bid = nextBid;
156 }
157 return (new Offer(getAgentID(), nextBid));
158 }
159
160 private Bid getFirstBid() throws Exception {
161 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
162 // <issuenumber,chosen
163 // value
164 // string>
165 List<Issue> issues = utilitySpace.getDomain().getIssues();
166 Random randomnr = new Random();
167 Bid bid = null;
168 Bid max_bid = null;
169 double max_bid_utility = 0;
170 int count = 0;
171
172 do {
173 for (Issue lIssue : issues) {
174 switch (lIssue.getType()) {
175 case DISCRETE:
176 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
177 int optionIndex = randomnr
178 .nextInt(lIssueDiscrete.getNumberOfValues());
179 values.put(lIssue.getNumber(),
180 lIssueDiscrete.getValue(optionIndex));
181 break;
182 case REAL:
183 IssueReal lIssueReal = (IssueReal) lIssue;
184 int optionInd = randomnr.nextInt(
185 lIssueReal.getNumberOfDiscretizationSteps() - 1);
186 values.put(lIssueReal.getNumber(), new ValueReal(lIssueReal
187 .getLowerBound()
188 + (lIssueReal.getUpperBound()
189 - lIssueReal.getLowerBound()) * (optionInd)
190 / (lIssueReal
191 .getNumberOfDiscretizationSteps())));
192 break;
193 case INTEGER:
194 IssueInteger lIssueInteger = (IssueInteger) lIssue;
195 int optionIndex2 = lIssueInteger.getLowerBound()
196 + randomnr.nextInt(lIssueInteger.getUpperBound()
197 - lIssueInteger.getLowerBound());
198 values.put(lIssueInteger.getNumber(),
199 new ValueInteger(optionIndex2));
200 break;
201 default:
202 throw new Exception("issue type " + lIssue.getType()
203 + " not supported ");
204 }
205 }
206 bid = new Bid(utilitySpace.getDomain(), values);
207 if (getUtility(bid) >= max_bid_utility) {
208 max_bid_utility = getUtility(bid);
209 max_bid = bid;
210 }
211
212 count++;
213 } while (count < 1000000);
214 return max_bid;
215 }
216
217 private Bid getRandomBid() throws Exception {
218 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
219 List<Issue> issues = utilitySpace.getDomain().getIssues();
220 Random randomnr = new Random();
221
222 Bid bid = null;
223 double temp = 0.0;
224 int count = 0;
225
226 if (MINIMUM_BID_UTILITY > 0.5) {
227 temp = MINIMUM_BID_UTILITY;
228 } else {
229 temp = getUtility(temp_bid) - 0.1;
230 }
231
232 do {
233 for (Issue lIssue : issues) {
234 switch (lIssue.getType()) {
235 case DISCRETE:
236 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
237 int optionIndex = randomnr
238 .nextInt(lIssueDiscrete.getNumberOfValues());
239 values.put(lIssue.getNumber(),
240 lIssueDiscrete.getValue(optionIndex));
241 break;
242 case REAL:
243 IssueReal lIssueReal = (IssueReal) lIssue;
244 int optionInd = randomnr.nextInt(
245 lIssueReal.getNumberOfDiscretizationSteps() - 1);
246 values.put(lIssueReal.getNumber(), new ValueReal(lIssueReal
247 .getLowerBound()
248 + (lIssueReal.getUpperBound()
249 - lIssueReal.getLowerBound()) * (optionInd)
250 / (lIssueReal
251 .getNumberOfDiscretizationSteps())));
252 break;
253 case INTEGER:
254 IssueInteger lIssueInteger = (IssueInteger) lIssue;
255 int optionIndex2 = lIssueInteger.getLowerBound()
256 + randomnr.nextInt(lIssueInteger.getUpperBound()
257 - lIssueInteger.getLowerBound());
258 values.put(lIssueInteger.getNumber(),
259 new ValueInteger(optionIndex2));
260 break;
261 default:
262 throw new Exception("issue type " + lIssue.getType()
263 + " not supported ");
264 }
265 }
266 bid = new Bid(utilitySpace.getDomain(), values);
267 } while (getUtility(bid) < temp);
268 return bid;
269 }
270
271 double sq(double x) {
272 return x * x;
273 }
274
275 @Override
276 public String getDescription() {
277 return "ANAC2014 compatible with non-linear utility spaces";
278 }
279}
Note: See TracBrowser for help on using the repository browser.