source: src/main/java/parties/in4010/q12015/group11/Group11.java@ 3

Last change on this file since 3 was 1, checked in by Wouter Pasman, 7 years ago

Initial import : Genius 9.0.0

File size: 9.5 KB
Line 
1package parties.in4010.q12015.group11;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Map.Entry;
6
7import genius.core.AgentID;
8import genius.core.Bid;
9import genius.core.BidHistory;
10import genius.core.BidIterator;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.DefaultAction;
14import genius.core.actions.Offer;
15import genius.core.bidding.BidDetails;
16import genius.core.issue.Issue;
17import genius.core.issue.IssueDiscrete;
18import genius.core.issue.Objective;
19import genius.core.issue.ValueDiscrete;
20import genius.core.parties.AbstractNegotiationParty;
21import genius.core.parties.NegotiationInfo;
22import genius.core.utility.AdditiveUtilitySpace;
23import genius.core.utility.Evaluator;
24import genius.core.utility.EvaluatorDiscrete;
25
26/************************************************
27 * Assignment AI Technique - Negotiation Agent By: J.K. van Schoubroeck
28 * (4329996) H.H. Choiri (4468457) T. Smit (4242785)
29 ************************************************/
30
31public class Group11 extends AbstractNegotiationParty {
32
33 // opponent model & bidding history for each opponent agent
34 private HashMap<Object, AdditiveUtilitySpace> opponentUtilitySpace = new HashMap<Object, AdditiveUtilitySpace>();
35 private HashMap<Object, BidHistory> bidHistory = new HashMap<Object, BidHistory>();
36
37 private Bid lastBid;
38
39 private int amountOfIssues;
40 private double learningRate = 0.2;
41 private int learnValueAddition;
42
43 @Override
44 public void init(NegotiationInfo info) {
45
46 super.init(info);
47 lastBid = new Bid(info.getUtilitySpace().getDomain());
48 }
49
50 @Override
51 public Action chooseAction(List<Class<? extends Action>> validActions) {
52
53 if (bidHistory.isEmpty()) {
54 // Opening Bid
55 try {
56 System.out.println("opening bid util:" + getUtility(utilitySpace.getMaxUtilityBid()));
57 return new Offer(getPartyId(), utilitySpace.getMaxUtilityBid());
58 } catch (Exception e) {
59 System.out.println("Fail to send offer");
60 e.printStackTrace();
61 }
62 }
63 Bid nextBid = determineNextBid();
64 double nextMyBidUtil = getUtility(nextBid);
65 // double time = getTimeLine().getTime();
66
67 // use AC Next approach, and accept if this agent get the highest
68 // utility
69 if ((getUtility(lastBid) >= nextMyBidUtil) && isGetHighest(lastBid)) {
70 return new Accept(getPartyId(), lastBid);
71 }
72
73 return new Offer(getPartyId(), nextBid);
74 }
75
76 @Override
77 public void receiveMessage(AgentID sender, Action action) {
78 super.receiveMessage(sender, action);
79 Bid currentOpBid = DefaultAction.getBidFromAction(action);
80 if (!(sender == null) && getUtility(currentOpBid) > 0) {
81 lastBid = currentOpBid;
82 try {
83 // System.out.println("Sender is "+sender.toString());
84 // Add the bid to the bidding history
85 BidHistory bidH = new BidHistory();
86 if (bidHistory.containsKey(sender.toString())) {
87 bidH = bidHistory.get(sender.toString());
88 }
89 bidH.add(new BidDetails(currentOpBid, getUtility(currentOpBid)));
90 bidHistory.put(sender.toString(), bidH);
91 } catch (Exception e) {
92 System.out.println("Error to add bid history of " + sender.toString());
93 e.printStackTrace();
94 }
95 updateModel(sender, currentOpBid, getTimeLine().getTime());
96 }
97 }
98
99 public void updateModel(Object sender, Bid opponentBid, double time) {
100
101 // use time-dependent learning value. 10*(1-t)
102 learnValueAddition = (int) Math.round((1 - getTimeLine().getTime()) * 10);
103
104 // This function handles the Opponent modelling
105 BidHistory bidHist = bidHistory.get(sender.toString());
106 if (!opponentUtilitySpace.containsKey(sender.toString())) {
107 // initialize opponent model's weight
108 AdditiveUtilitySpace oppUSpace = (AdditiveUtilitySpace) getUtilitySpace().copy();
109 amountOfIssues = oppUSpace.getDomain().getIssues().size();
110 for (Entry<Objective, Evaluator> e : oppUSpace.getEvaluators()) {
111 // set the issue weights equally to 1/#OfIssues
112 oppUSpace.unlock(e.getKey());
113 e.getValue().setWeight(1D / (double) amountOfIssues);
114 try {
115 // set all value weights to 10
116 for (ValueDiscrete vd : ((IssueDiscrete) e.getKey()).getValues())
117 ((EvaluatorDiscrete) e.getValue()).setEvaluation(vd, 10);
118 } catch (Exception ex) {
119 System.out.println("Fail to initialize opponent model");
120 ex.printStackTrace();
121 }
122 }
123 opponentUtilitySpace.put(sender.toString(), oppUSpace);
124 System.out.println("Opponent model successfully created");
125 return;
126 } else if (bidHist.size() < 2) {
127 return;
128 }
129
130 // Update existing opponent model
131 int numberOfUnchanged = 0;
132 BidDetails oppBid = bidHist.getHistory().get(bidHist.size() - 1);
133 BidDetails prevOppBid = bidHist.getHistory().get(bidHist.size() - 2);
134 HashMap<Integer, Integer> lastDiffSet = determineDifference(sender, prevOppBid, oppBid);
135
136 // count the number of changes in value
137 for (Integer i : lastDiffSet.keySet()) {
138 if (lastDiffSet.get(i) == 0)
139 numberOfUnchanged++;
140 }
141
142 double goldenValue = learningRate / (double) amountOfIssues;
143 double totalSum = 1D + goldenValue * (double) numberOfUnchanged;
144 double maximumWeight = 1D - ((double) amountOfIssues) * goldenValue / totalSum;
145
146 AdditiveUtilitySpace oppUSpace = opponentUtilitySpace.get(sender.toString());
147
148 // update and normalize issue weights
149 for (Integer i : lastDiffSet.keySet()) {
150 if (lastDiffSet.get(i) == 0 && oppUSpace.getWeight(i) < maximumWeight)
151 oppUSpace.setWeight(oppUSpace.getDomain().getObjectivesRoot().getObjective(i),
152 (oppUSpace.getWeight(i) + goldenValue) / totalSum);
153 else
154 oppUSpace.setWeight(oppUSpace.getDomain().getObjectivesRoot().getObjective(i),
155 oppUSpace.getWeight(i) / totalSum);
156 }
157
158 // update the issue value weights
159 try {
160 for (Entry<Objective, Evaluator> e : oppUSpace.getEvaluators()) {
161 ((EvaluatorDiscrete) e.getValue()).setEvaluation(
162 oppBid.getBid().getValue(((IssueDiscrete) e.getKey()).getNumber()),
163 (learnValueAddition + ((EvaluatorDiscrete) e.getValue()).getEvaluationNotNormalized(
164 ((ValueDiscrete) oppBid.getBid().getValue(((IssueDiscrete) e.getKey()).getNumber())))));
165 }
166 opponentUtilitySpace.put(sender.toString(), oppUSpace);
167 } catch (Exception ex) {
168 ex.printStackTrace();
169 }
170 }
171
172 private HashMap<Integer, Integer> determineDifference(Object sender, BidDetails first, BidDetails second) {
173 // get the value differences between 2 bids
174 HashMap<Integer, Integer> diff = new HashMap<Integer, Integer>();
175 try {
176 for (Issue i : opponentUtilitySpace.get(sender.toString()).getDomain().getIssues()) {
177 diff.put(i.getNumber(), (((ValueDiscrete) first.getBid().getValue(i.getNumber()))
178 .equals((ValueDiscrete) second.getBid().getValue(i.getNumber()))) ? 0 : 1);
179 }
180 } catch (Exception ex) {
181 ex.printStackTrace();
182 }
183
184 return diff;
185 }
186
187 public Bid determineNextBid() {
188 // This function handles Bidding strategy
189 Bid bestBid;
190 double time = getTimeLine().getTime();
191 double utilityGoal;
192 // calculate target utility
193 utilityGoal = (1 - Math.pow(time, 2)) * 0.5 + 0.5;
194 if (utilityGoal < 0.85) {
195 utilityGoal = 0.85;
196 }
197
198 if (time < 0.6) {
199 utilityGoal = (1 - Math.pow(time, 2)) * 0.5 + 0.5;
200 } else {
201 utilityGoal = 0.85;
202 }
203 try {
204 bestBid = getBidNearUtility(utilityGoal, 0.05);
205 return bestBid;
206 } catch (Exception e) {
207 System.out.println("Fail tp get bid near utility");
208 e.printStackTrace();
209 }
210
211 return null;
212 }
213
214 private Bid getBidNearUtility(double target, double delta) throws Exception {
215 // System.out.println("target utility:"+target+", delta: "+delta);
216 // This function searches the best bid based on target utility and
217 // tolerance
218 BidIterator iter = new BidIterator(utilitySpace.getDomain());
219 Bid bestBid = null;
220 double maxOpUtil = -1;
221 while (iter.hasNext()) {
222 Bid nBid = iter.next();
223 // check all bids
224 try {
225 double currMyU = getUtility(nBid);
226 if (Math.abs(currMyU - target) < delta) {
227 // bid's utility is in the range, check the opponent's
228 // utility
229 double oppUtil = 0;
230 boolean Iwin = true;
231 for (Entry<Object, AdditiveUtilitySpace> opU : opponentUtilitySpace.entrySet()) {
232 // sum all opponent's utility
233 double currOpU = opU.getValue().getUtility(nBid);
234 // make sure agent's utility is the highest
235 if (Iwin && (currMyU >= currOpU)) {
236 oppUtil += currOpU;
237 } else {
238 Iwin = false;
239 break;
240 }
241 }
242
243 if (Iwin && (oppUtil > maxOpUtil)) {
244 // choose maximum opponent total utility
245 bestBid = nBid;
246 maxOpUtil = oppUtil;
247 }
248 }
249 } catch (Exception e) {
250 System.out.println("Fail to get opponent utility space 2");
251 e.printStackTrace();
252 }
253 }
254 if (maxOpUtil == -1) {
255 // searching file, add the tolerance value
256 return getBidNearUtility(target, delta + 0.05);
257 }
258 return bestBid;
259 }
260
261 public boolean isGetHighest(Bid bid) {
262 double myUtil = getUtility(bid);
263 // compare the utility with opponent's utility and check if the agent
264 // gets the highest utility
265 for (Entry<Object, AdditiveUtilitySpace> opU : opponentUtilitySpace.entrySet()) {
266 try {
267 if (myUtil < opU.getValue().getUtility(bid)) {
268 return false;
269 }
270 } catch (Exception e) {
271 System.out.println("Fail to get opponent utility space 3");
272 e.printStackTrace();
273 }
274 }
275
276 return true;
277 }
278
279 @Override
280 public String getDescription() {
281 return "Agent11 - Multi party negotiation agent";
282 }
283
284}
Note: See TracBrowser for help on using the repository browser.