1 | package negotiator.ahbuneagent;
|
---|
2 |
|
---|
3 | import geniusweb.actions.*;
|
---|
4 | import geniusweb.bidspace.AllBidsList;
|
---|
5 | import geniusweb.issuevalue.Bid;
|
---|
6 | import geniusweb.party.Capabilities;
|
---|
7 | import geniusweb.party.DefaultParty;
|
---|
8 | import geniusweb.party.inform.*;
|
---|
9 | import geniusweb.profile.FullOrdering;
|
---|
10 | import geniusweb.profile.PartialOrdering;
|
---|
11 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
12 | import geniusweb.profileconnection.ProfileInterface;
|
---|
13 | import geniusweb.progress.Progress;
|
---|
14 | import geniusweb.progress.ProgressRounds;
|
---|
15 | import negotiator.ahbuneagent.impmap.SimilarityMap;
|
---|
16 | import negotiator.ahbuneagent.impmap.OppSimilarityMap;
|
---|
17 | import negotiator.ahbuneagent.linearorder.OppSimpleLinearOrdering;
|
---|
18 | import negotiator.ahbuneagent.linearorder.SimpleLinearOrdering;
|
---|
19 | import tudelft.utilities.logging.Reporter;
|
---|
20 |
|
---|
21 | import javax.websocket.DeploymentException;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.math.BigDecimal;
|
---|
24 | import java.math.BigInteger;
|
---|
25 | import java.util.*;
|
---|
26 | import java.util.logging.Level;
|
---|
27 |
|
---|
28 | import static java.lang.Math.*;
|
---|
29 |
|
---|
30 | public class AhBuNeAgent extends DefaultParty {
|
---|
31 |
|
---|
32 | private final Random random = new Random();
|
---|
33 |
|
---|
34 | private int ourNumFirstBids;
|
---|
35 | private int ourNumLastBids;
|
---|
36 | private int oppNumFirstBids;
|
---|
37 | private int ourKnownBidNum = 0;
|
---|
38 | private int oppKnownBidNum = 0;
|
---|
39 |
|
---|
40 |
|
---|
41 | protected ProfileInterface profileInterface;
|
---|
42 | private PartyId partyId;
|
---|
43 | private Progress progress;
|
---|
44 | private double time = 0.0;
|
---|
45 |
|
---|
46 | private AllBidsList allPossibleBids;
|
---|
47 | private BigInteger allPossibleBidsSize;
|
---|
48 | private SimpleLinearOrdering ourLinearPartialOrdering = null;
|
---|
49 | private OppSimpleLinearOrdering oppLinearPartialOrdering = null;
|
---|
50 | private SimilarityMap ourSimilarityMap = null;
|
---|
51 | private OppSimilarityMap oppSimilarityMap = null;
|
---|
52 |
|
---|
53 | private Bid lastReceivedBid = null;
|
---|
54 | private double utilityLowerBound = 1.0;
|
---|
55 | private final double ourMaxCompromise = 0.1;
|
---|
56 |
|
---|
57 | // Initially no loss
|
---|
58 | private double lostElicitScore = 0.00;
|
---|
59 | //Set default as 0.01 by Genius Framework
|
---|
60 | private double elicitationCost = 0.01;
|
---|
61 | private final BigDecimal maxElicitationLost = new BigDecimal("0.05");
|
---|
62 | private int leftElicitationNumber = 0;
|
---|
63 | Bid elicitationBid = null;
|
---|
64 | ArrayList<Map.Entry<Bid, Integer>> mostCompromisedBids = new ArrayList<>();
|
---|
65 | ArrayList<Bid> oppElicitatedBid = new ArrayList<>();
|
---|
66 | private Bid reservationBid = null;
|
---|
67 |
|
---|
68 | public AhBuNeAgent() {
|
---|
69 | }
|
---|
70 |
|
---|
71 | public AhBuNeAgent(Reporter reporter) {
|
---|
72 | super(reporter);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public Capabilities getCapabilities() {
|
---|
77 | return new Capabilities(new HashSet<>(Arrays.asList("SHAOP", "SAOP")));
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public String getDescription() {
|
---|
82 | return "AhBuNe Agent";
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public void notifyChange(Inform info) {
|
---|
87 | try {
|
---|
88 | if (info instanceof Settings) {
|
---|
89 | Settings settings = (Settings) info;
|
---|
90 | init(settings);
|
---|
91 | } else if (info instanceof ActionDone) {
|
---|
92 | Action lastReceivedAction = ((ActionDone) info).getAction();
|
---|
93 | if (lastReceivedAction instanceof Offer) {
|
---|
94 | lastReceivedBid = ((Offer) lastReceivedAction).getBid();
|
---|
95 | } else if (lastReceivedAction instanceof Comparison) {
|
---|
96 | ourLinearPartialOrdering = ourLinearPartialOrdering.with(((Comparison) lastReceivedAction).getBid(), ((Comparison) lastReceivedAction).getWorse());
|
---|
97 | myTurn();
|
---|
98 | }
|
---|
99 | } else if (info instanceof YourTurn) {
|
---|
100 | if (progress instanceof ProgressRounds) {
|
---|
101 | progress = ((ProgressRounds) progress).advance();
|
---|
102 | }
|
---|
103 | myTurn();
|
---|
104 | } else if (info instanceof Finished) {
|
---|
105 | }
|
---|
106 | } catch (Exception exception) {
|
---|
107 | throw new RuntimeException("Failed to handle info", exception);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void init(Settings settings) throws IOException, DeploymentException {
|
---|
112 | this.partyId = settings.getID();
|
---|
113 | this.progress = settings.getProgress();
|
---|
114 | this.profileInterface = ProfileConnectionFactory.create(settings.getProfile().getURI(), getReporter());
|
---|
115 |
|
---|
116 | if (profileInterface.getProfile() instanceof FullOrdering) {
|
---|
117 | throw new UnsupportedOperationException("Only <DefaultPartialOrdering> is supported");
|
---|
118 | } else if (profileInterface.getProfile() instanceof PartialOrdering) {
|
---|
119 | PartialOrdering partialProfile = (PartialOrdering) profileInterface.getProfile();
|
---|
120 | this.allPossibleBids = new AllBidsList(partialProfile.getDomain());
|
---|
121 | this.allPossibleBidsSize = allPossibleBids.size();
|
---|
122 | this.ourSimilarityMap = new SimilarityMap(partialProfile);
|
---|
123 | this.oppSimilarityMap = new OppSimilarityMap(partialProfile);
|
---|
124 | this.ourLinearPartialOrdering = new SimpleLinearOrdering(profileInterface.getProfile());
|
---|
125 | this.oppLinearPartialOrdering = new OppSimpleLinearOrdering();
|
---|
126 | this.ourSimilarityMap.update(ourLinearPartialOrdering);
|
---|
127 | getReservationRatio();
|
---|
128 | getElicitationCost(settings);
|
---|
129 | } else {
|
---|
130 | throw new UnsupportedOperationException("Only <DefaultPartialOrdering> is supported");
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | private Action selectAction() {
|
---|
136 | if (doWeMakeElicitation()) {
|
---|
137 | lostElicitScore += elicitationCost;
|
---|
138 | leftElicitationNumber -= 1;
|
---|
139 | return new ElicitComparison(partyId, elicitationBid, ourLinearPartialOrdering.getBids());
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (lastReceivedBid == null) {
|
---|
143 | return makeAnOffer();
|
---|
144 | }
|
---|
145 | if (doWeEndTheNegotiation()) {
|
---|
146 | return new EndNegotiation(partyId);
|
---|
147 | } else if (doWeAccept(lastReceivedBid)) {
|
---|
148 | return new Accept(partyId, lastReceivedBid);
|
---|
149 | }
|
---|
150 | return makeAnOffer();
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void myTurn() throws IOException {
|
---|
155 | time = progress.get(System.currentTimeMillis());
|
---|
156 | strategySelection();
|
---|
157 | Action action = selectAction();
|
---|
158 | getConnection().send(action);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private boolean doWeEndTheNegotiation() {
|
---|
162 | if (reservationBid != null && ourSimilarityMap.isCompatibleWithSimilarity(reservationBid, ourNumFirstBids, ourNumLastBids, 0.9 - time * 0.1)) {
|
---|
163 | return true;
|
---|
164 | }
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | private Bid elicitationRandomBidGenerator() {
|
---|
169 | Bid foundBid = allPossibleBids.get(random.nextInt(allPossibleBids.size().intValue()));
|
---|
170 | while (ourLinearPartialOrdering.getBids().contains(foundBid)) {
|
---|
171 | foundBid = allPossibleBids.get(random.nextInt(allPossibleBids.size().intValue()));
|
---|
172 | }
|
---|
173 | return foundBid;
|
---|
174 | }
|
---|
175 |
|
---|
176 | private Action makeAnOffer() {
|
---|
177 | if (time > 0.96) {
|
---|
178 | for (int i = ourLinearPartialOrdering.getKnownBidsSize() - 1; i >= 0; i--) {
|
---|
179 | Bid testBid = ourLinearPartialOrdering.getBidByIndex(i);
|
---|
180 | if (oppElicitatedBid.contains(testBid) && doWeAccept(testBid)) {
|
---|
181 | return new Offer(partyId, testBid);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | Bid oppMaxBid = oppLinearPartialOrdering.getMaxBid();
|
---|
186 | Bid ourOffer = ourSimilarityMap.findBidCompatibleWithSimilarity(ourNumFirstBids, ourNumLastBids, utilityLowerBound, oppMaxBid);
|
---|
187 | if (time < 0.015) {
|
---|
188 | if (oppLinearPartialOrdering.isAvailable()) {
|
---|
189 | int count = 0;
|
---|
190 | while (count < 500 && !oppSimilarityMap.isCompromised(ourOffer, oppNumFirstBids, 0.85) && ourOffer.equals(ourLinearPartialOrdering.getMaxBid())) {
|
---|
191 | ourOffer = ourSimilarityMap.findBidCompatibleWithSimilarity(ourNumFirstBids, ourNumLastBids, 0.85, oppMaxBid);
|
---|
192 | count++;
|
---|
193 | }
|
---|
194 | } else {
|
---|
195 | int count = 0;
|
---|
196 | while (count < 500 && ourOffer.equals(ourLinearPartialOrdering.getMaxBid())) {
|
---|
197 | ourOffer = ourSimilarityMap.findBidCompatibleWithSimilarity(ourNumFirstBids, ourNumLastBids, 0.85, oppMaxBid);
|
---|
198 | count++;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | } else if (lastReceivedBid != null) {
|
---|
202 | if (ourSimilarityMap.isCompatibleWithSimilarity(lastReceivedBid, ourNumFirstBids, ourNumLastBids, 0.9)) {
|
---|
203 | return new Offer(partyId, lastReceivedBid);
|
---|
204 | }
|
---|
205 | if (ourSimilarityMap.isCompatibleWithSimilarity(oppMaxBid, ourNumFirstBids, ourNumLastBids, 0.9)) {
|
---|
206 | return new Offer(partyId, oppMaxBid);
|
---|
207 | }
|
---|
208 | int count = 0;
|
---|
209 | while (count < 500 && oppLinearPartialOrdering.isAvailable() && !oppSimilarityMap.isCompromised(ourOffer, oppNumFirstBids, utilityLowerBound)) {
|
---|
210 | ourOffer = ourSimilarityMap.findBidCompatibleWithSimilarity(ourNumFirstBids, ourNumLastBids, utilityLowerBound, oppMaxBid);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | return new Offer(partyId, ourOffer);
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | private boolean doWeAccept(Bid bid) {
|
---|
218 | if (ourSimilarityMap.isCompatibleWithSimilarity(bid, ourNumFirstBids, ourNumLastBids, 0.9)) {
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | double startUtilitySearch = utilityLowerBound;
|
---|
223 |
|
---|
224 | if (time >= 0.98) {
|
---|
225 | startUtilitySearch = utilityLowerBound - ourMaxCompromise;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (oppLinearPartialOrdering.isAvailable()) {
|
---|
229 | for (int i = (int) (startUtilitySearch * 100); i <= 95; i += 5) {
|
---|
230 | double utilityTest = (double) i / 100.0;
|
---|
231 | if (oppSimilarityMap.isCompromised(bid, oppNumFirstBids, utilityTest)) {
|
---|
232 | if (ourSimilarityMap.isCompatibleWithSimilarity(bid, ourNumFirstBids, ourNumLastBids, utilityTest)) {
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 |
|
---|
242 | private boolean doWeMakeElicitation() {
|
---|
243 | if (leftElicitationNumber == 0) {
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 | if (allPossibleBidsSize.intValue() <= 100) {
|
---|
247 | if (ourLinearPartialOrdering.getKnownBidsSize() < allPossibleBidsSize.intValue() * 0.1) {
|
---|
248 | elicitationBid = elicitationRandomBidGenerator();
|
---|
249 | return true;
|
---|
250 | }
|
---|
251 | } else if (ourLinearPartialOrdering.getKnownBidsSize() < 10) {
|
---|
252 | elicitationBid = elicitationRandomBidGenerator();
|
---|
253 | return true;
|
---|
254 | } else if (time > 0.98 && oppLinearPartialOrdering.isAvailable()) {
|
---|
255 | if (mostCompromisedBids.size() > 0) {
|
---|
256 | elicitationBid = mostCompromisedBids.remove(mostCompromisedBids.size() - 1).getKey();
|
---|
257 | oppElicitatedBid.add(elicitationBid);
|
---|
258 | return true;
|
---|
259 | } else {
|
---|
260 | LinkedHashMap<Bid, Integer> mostCompromisedBidsHash = oppSimilarityMap.mostCompromisedBids();
|
---|
261 | Set<Map.Entry<Bid, Integer>> sortedCompromiseMapSet = mostCompromisedBidsHash.entrySet();
|
---|
262 | mostCompromisedBids = new ArrayList<>(sortedCompromiseMapSet);
|
---|
263 | elicitationBid = mostCompromisedBids.remove(mostCompromisedBids.size() - 1).getKey();
|
---|
264 | oppElicitatedBid.add(elicitationBid);
|
---|
265 | return true;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void strategySelection() {
|
---|
272 | utilityLowerBound = getUtilityLowerBound(time, lostElicitScore);
|
---|
273 | ourKnownBidNum = ourLinearPartialOrdering.getKnownBidsSize();
|
---|
274 | oppKnownBidNum = oppLinearPartialOrdering.getKnownBidsSize();
|
---|
275 | ourNumFirstBids = getNumFirst(utilityLowerBound, ourKnownBidNum);
|
---|
276 | ourNumLastBids = getNumLast(utilityLowerBound, getUtilityLowerBound(1.0, lostElicitScore), ourKnownBidNum);
|
---|
277 | if (lastReceivedBid != null) {
|
---|
278 | oppLinearPartialOrdering.updateBid(lastReceivedBid);
|
---|
279 | oppSimilarityMap.update(oppLinearPartialOrdering);
|
---|
280 | oppNumFirstBids = getOppNumFirst(utilityLowerBound, oppKnownBidNum);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void getElicitationCost(Settings settings) {
|
---|
285 | try {
|
---|
286 | elicitationCost = Double.parseDouble(settings.getParameters().get("elicitationcost").toString());
|
---|
287 | leftElicitationNumber = (int) (maxElicitationLost.doubleValue() / elicitationCost);
|
---|
288 | reporter.log(Level.INFO, "leftElicitationNumber: " + leftElicitationNumber);
|
---|
289 | } catch (Exception e) {
|
---|
290 | elicitationCost = 0.01;
|
---|
291 | leftElicitationNumber = (int) (maxElicitationLost.doubleValue() / elicitationCost);
|
---|
292 | reporter.log(Level.INFO, "catch leftElicitationNumber: " + leftElicitationNumber);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | private void getReservationRatio() {
|
---|
297 | try {
|
---|
298 | reservationBid = profileInterface.getProfile().getReservationBid();
|
---|
299 | } catch (Exception e) {
|
---|
300 | reservationBid = null;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | double getUtilityLowerBound(double time, double lostElicitScore) {
|
---|
305 | if (time < 0.5) {
|
---|
306 | return -pow((time - 0.25), 2) + 0.9 + lostElicitScore;
|
---|
307 | } else if (time < 0.7) {
|
---|
308 | return -pow((1.5 * (time - 0.7)), 2) + 0.9 + lostElicitScore;
|
---|
309 | }
|
---|
310 | return (3.25 * time * time) - (6.155 * time) + 3.6105 + lostElicitScore;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int getNumFirst(double utilityLowerBound, int knownBidNum) {
|
---|
314 | return ((int) (knownBidNum * (1 - utilityLowerBound)) + 1);
|
---|
315 | }
|
---|
316 |
|
---|
317 | int getNumLast(double utilityLowerBound, double minUtilityLowerBound, int ourKnownBidNum) {
|
---|
318 | return ((int) (ourKnownBidNum * (1 - minUtilityLowerBound)) - (int) (ourKnownBidNum * (1 - utilityLowerBound)) + 1);
|
---|
319 | }
|
---|
320 |
|
---|
321 | int getOppNumFirst(double utilityLowerBound, int oppKnownBidNum) {
|
---|
322 | return ((int) (oppKnownBidNum * (1 - utilityLowerBound)) + 1);
|
---|
323 | }
|
---|
324 | }
|
---|