1 | package parties.in4010.q12015.group3;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import genius.core.AgentID;
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.actions.Accept;
|
---|
10 | import genius.core.actions.Action;
|
---|
11 | import genius.core.actions.ActionWithBid;
|
---|
12 | import genius.core.actions.DefaultAction;
|
---|
13 | import genius.core.actions.EndNegotiation;
|
---|
14 | import genius.core.actions.Offer;
|
---|
15 | import genius.core.bidding.BidDetails;
|
---|
16 | import genius.core.boaframework.OutcomeSpace;
|
---|
17 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
18 | import genius.core.issue.Value;
|
---|
19 | import genius.core.parties.AbstractNegotiationParty;
|
---|
20 | import genius.core.parties.NegotiationInfo;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This is your negotiation party.
|
---|
24 | */
|
---|
25 | public class Group3 extends AbstractNegotiationParty {
|
---|
26 | private double alfa;
|
---|
27 | private double beta;
|
---|
28 |
|
---|
29 | private double MIN_BID_UTILITY;
|
---|
30 | private double LEARNING_TIME;
|
---|
31 | private SortedOutcomeSpace sortedBids;
|
---|
32 | private OutcomeSpace space;
|
---|
33 |
|
---|
34 | private double opponentWeights[];
|
---|
35 | private double WEIGHT_PARAMETER = 0.1;
|
---|
36 |
|
---|
37 | private List<Action> opponentActions;
|
---|
38 | private Bid previousBidOpponent;
|
---|
39 | private double HIGH_WEIGHT;
|
---|
40 | private int HAMMING_THRESHOLD;
|
---|
41 |
|
---|
42 | private List<Double> opponentUtils = new ArrayList<Double>();
|
---|
43 | private double ACCEPTANCE_THRESHOLD;
|
---|
44 |
|
---|
45 | private int roundsCounter;
|
---|
46 |
|
---|
47 | public void receiveMessage(AgentID sender, Action action) {
|
---|
48 | super.receiveMessage(sender, action);
|
---|
49 | this.opponentActions.add(action);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public String getDescription() {
|
---|
53 | return "Group 3.oct-19-4.2";
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void init(NegotiationInfo info) {
|
---|
58 | super.init(info);
|
---|
59 | System.out.println("\n");
|
---|
60 | System.out.println("####### STARTING SESSION #########");
|
---|
61 | System.out.println("Initializing Agent");
|
---|
62 |
|
---|
63 | this.space = new OutcomeSpace(info.getUtilitySpace());
|
---|
64 | sortedBids = new SortedOutcomeSpace(info.getUtilitySpace());
|
---|
65 | this.opponentActions = new ArrayList<Action>();
|
---|
66 |
|
---|
67 | super.timeline = timeline;
|
---|
68 | super.utilitySpace = info.getUtilitySpace();
|
---|
69 |
|
---|
70 | this.LEARNING_TIME = 0.1 * info.getDeadline().getValue();
|
---|
71 | this.HAMMING_THRESHOLD = 3;
|
---|
72 | this.HIGH_WEIGHT = 0.15;
|
---|
73 | this.ACCEPTANCE_THRESHOLD = 0.75;
|
---|
74 |
|
---|
75 | this.alfa = 0.05;
|
---|
76 | this.beta = -Math.log(alfa) / info.getDeadline().getValue();
|
---|
77 | this.MIN_BID_UTILITY = 0.8;
|
---|
78 |
|
---|
79 | opponentWeights = new double[info.getUtilitySpace().getDomain().getIssues().size()];
|
---|
80 | this.previousBidOpponent = null;
|
---|
81 |
|
---|
82 | for (int i = 0; i < info.getUtilitySpace().getDomain().getIssues().size(); i++)
|
---|
83 | opponentWeights[i] = 1 / (double) (info.getUtilitySpace().getDomain().getIssues().size());
|
---|
84 |
|
---|
85 | roundsCounter = 0;
|
---|
86 |
|
---|
87 | System.out.println("Initialization Completed!");
|
---|
88 | System.out.println("Opponent Model: " + Arrays.toString(opponentWeights));
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 |
|
---|
93 | */
|
---|
94 | @Override
|
---|
95 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
96 | Action action = null;
|
---|
97 | Action lastBidResponse = null;
|
---|
98 | Bid nextBid = null;
|
---|
99 |
|
---|
100 | double opponentUtility, myUtility;
|
---|
101 |
|
---|
102 | System.out.println("\nROUND #" + roundsCounter++);
|
---|
103 |
|
---|
104 | if (opponentActions.isEmpty())
|
---|
105 | return new Offer(getPartyId(), getNextBid(null));
|
---|
106 |
|
---|
107 | try {
|
---|
108 | lastBidResponse = opponentActions.get(0);
|
---|
109 |
|
---|
110 | Bid toBeAccepted = DefaultAction.getBidFromAction(opponentActions.get(opponentActions.size() - 1));
|
---|
111 |
|
---|
112 | Bid toBeModeled = DefaultAction.getBidFromAction(lastBidResponse);
|
---|
113 |
|
---|
114 | /* First we update the model of our opponent */
|
---|
115 | if (lastBidResponse instanceof Offer)
|
---|
116 | opponentWeights = this.updateModel(toBeModeled, opponentWeights);
|
---|
117 |
|
---|
118 | if (lastBidResponse instanceof Accept) {
|
---|
119 | System.out.println("Reoffering previous Bid:");
|
---|
120 |
|
---|
121 | toBeModeled = this.previousBidOpponent;
|
---|
122 | }
|
---|
123 |
|
---|
124 | System.out.println("Opponent Model: " + Arrays.toString(opponentWeights));
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Second, we choose the next bid wrt the preferences of the next
|
---|
128 | * opponent
|
---|
129 | */
|
---|
130 | nextBid = this.getNextBid(toBeModeled);
|
---|
131 |
|
---|
132 | /* Last, we decide whether to accept the last bid or propose ours */
|
---|
133 | myUtility = getUtility(nextBid);
|
---|
134 | opponentUtility = getUtility(toBeAccepted);
|
---|
135 |
|
---|
136 | if (isAcceptable(opponentUtility, myUtility)) {
|
---|
137 | System.out.println("Accepting Bid with utility: " + opponentUtility);
|
---|
138 | System.out.println(toBeAccepted.toString());
|
---|
139 | action = new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
|
---|
140 | } else
|
---|
141 | action = new Offer(getPartyId(), nextBid);
|
---|
142 |
|
---|
143 | /* Update parameters */
|
---|
144 | opponentUtils.add(opponentUtility);
|
---|
145 |
|
---|
146 | this.previousBidOpponent = toBeModeled;
|
---|
147 | } catch (Exception e) {
|
---|
148 | System.out.println("CHOOSE ACTION");
|
---|
149 | e.printStackTrace();
|
---|
150 | System.out.println("List of opponent actions:\n" + opponentActions.toString());
|
---|
151 | action = new EndNegotiation(getPartyId());
|
---|
152 | }
|
---|
153 |
|
---|
154 | this.opponentActions.clear();
|
---|
155 |
|
---|
156 | return action;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /************************
|
---|
160 | * ACCEPTANCE STRATEGY
|
---|
161 | ****************************************/
|
---|
162 |
|
---|
163 | // ACcombi(T, MAXw)
|
---|
164 | private boolean isAcceptable(double offeredUtilFromOpponent, double myOfferedUtil) {
|
---|
165 | double time = timeline.getCurrentTime();
|
---|
166 | double timeThreshold = ACCEPTANCE_THRESHOLD * getDeadlines().getValue();
|
---|
167 |
|
---|
168 | // ACnext
|
---|
169 | if (offeredUtilFromOpponent > myOfferedUtil)
|
---|
170 | return true;
|
---|
171 |
|
---|
172 | // ACtime
|
---|
173 | if (time > timeThreshold) {
|
---|
174 | // ACmaxW
|
---|
175 | if (offeredUtilFromOpponent > getMaxValue(opponentUtils))
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public double getMaxValue(List<Double> values) {
|
---|
183 | double maxValue = Double.MIN_VALUE;
|
---|
184 |
|
---|
185 | try {
|
---|
186 | for (Double d : values) {
|
---|
187 | if (d > maxValue) {
|
---|
188 | maxValue = d;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | } catch (Exception e) {
|
---|
192 | System.out.println("GETMAXVALUE");
|
---|
193 | e.printStackTrace();
|
---|
194 | }
|
---|
195 |
|
---|
196 | return maxValue;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /************************
|
---|
200 | * BIDDING STRATEGY
|
---|
201 | ****************************************/
|
---|
202 | private boolean compareValues(Value v1, Value v2) {
|
---|
203 | String v1Str = v1.toString();
|
---|
204 | String v2Str = v2.toString();
|
---|
205 |
|
---|
206 | return v1Str.equals(v2Str);
|
---|
207 | }
|
---|
208 |
|
---|
209 | private int computeHammingDistance(Bid b1, Bid b2) {
|
---|
210 | int nIssues = b1.getIssues().size();
|
---|
211 | int distance = 0;
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * IF issues are different IF weight for that issue is small distance =
|
---|
215 | * 1 ELSE distance = 2
|
---|
216 | */
|
---|
217 |
|
---|
218 | for (int i = 1; i <= nIssues; i++) {
|
---|
219 | try {
|
---|
220 | if (!compareValues(b1.getValue(i), b2.getValue(i)))
|
---|
221 | distance += opponentWeights[i - 1] > HIGH_WEIGHT ? 2 : 1;
|
---|
222 | } catch (Exception e) {
|
---|
223 | System.out.println("HAMMING");
|
---|
224 | e.printStackTrace();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | return distance;
|
---|
229 | }
|
---|
230 |
|
---|
231 | private Bid getNextBid(Bid opponent) {
|
---|
232 | double time = timeline.getCurrentTime();
|
---|
233 | double targetUtility;
|
---|
234 |
|
---|
235 | if (opponent == null) {
|
---|
236 | System.out.println("No previous bids to evaluate.");
|
---|
237 | return space.getMaxBidPossible().getBid();
|
---|
238 | }
|
---|
239 |
|
---|
240 | List<BidDetails> allBids = this.sortedBids.getAllOutcomes();// space.getAllOutcomes();
|
---|
241 |
|
---|
242 | double maxUtility = 0;
|
---|
243 | Bid maxUtilityBid = null;
|
---|
244 |
|
---|
245 | if (time < LEARNING_TIME) {
|
---|
246 | System.out.println("Learning period: bidding max.");
|
---|
247 | return space.getMaxBidPossible().getBid();
|
---|
248 | }
|
---|
249 |
|
---|
250 | for (java.util.Iterator<BidDetails> i = allBids.iterator(); i.hasNext();) {
|
---|
251 | Bid bid = i.next().getBid();
|
---|
252 |
|
---|
253 | if (getUtility(bid) < MIN_BID_UTILITY)
|
---|
254 | break;
|
---|
255 |
|
---|
256 | if (computeHammingDistance(opponent, bid) < HAMMING_THRESHOLD)
|
---|
257 |
|
---|
258 | if (getUtility(bid) > maxUtility) {
|
---|
259 | maxUtility = getUtility(bid);
|
---|
260 | maxUtilityBid = bid;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (maxUtility > MIN_BID_UTILITY) {
|
---|
265 | System.out.println("Hamming Utility: " + maxUtility);
|
---|
266 | return maxUtilityBid;
|
---|
267 | }
|
---|
268 |
|
---|
269 | targetUtility = 1 - alfa * Math.exp(beta * (time - LEARNING_TIME));
|
---|
270 |
|
---|
271 | System.out.println("Exponential decay: " + targetUtility);
|
---|
272 |
|
---|
273 | if (targetUtility < MIN_BID_UTILITY) {
|
---|
274 | System.out.println("Lower threshold reached.");
|
---|
275 | targetUtility = MIN_BID_UTILITY;
|
---|
276 | }
|
---|
277 |
|
---|
278 | return sortedBids.getBidNearUtility(targetUtility).getBid();
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** OPPONENT MODELING */
|
---|
282 | private double[] updateModel(Bid newBid, double[] currentWeights) {
|
---|
283 | double sum = 0;
|
---|
284 |
|
---|
285 | if (previousBidOpponent == null)
|
---|
286 | return currentWeights;
|
---|
287 |
|
---|
288 | for (int i = 1; i <= newBid.getIssues().size(); i++) {
|
---|
289 | try {
|
---|
290 | if (newBid.getValue(i) == previousBidOpponent.getValue(i))
|
---|
291 | currentWeights[i - 1] += WEIGHT_PARAMETER;
|
---|
292 |
|
---|
293 | sum += currentWeights[i - 1];
|
---|
294 | } catch (Exception e) {
|
---|
295 | System.out.println("UPDATE 1");
|
---|
296 | e.printStackTrace();
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | try {
|
---|
301 | for (int i = 0; i < newBid.getIssues().size(); i++)
|
---|
302 | currentWeights[i] = currentWeights[i] / sum;
|
---|
303 | } catch (Exception e) {
|
---|
304 | System.out.println("UPDATE 2");
|
---|
305 | e.printStackTrace();
|
---|
306 | }
|
---|
307 |
|
---|
308 | return currentWeights;
|
---|
309 | }
|
---|
310 |
|
---|
311 | }
|
---|