source: anac2020/agentxx/src/main/java/geniusweb/exampleparties/randomparty/RandomParty.java@ 3

Last change on this file since 3 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 3.6 KB
Line 
1package geniusweb.exampleparties.randomparty;
2
3import java.io.IOException;
4import java.math.BigInteger;
5import java.util.Arrays;
6import java.util.HashSet;
7import java.util.Random;
8import java.util.logging.Level;
9
10import geniusweb.actions.Accept;
11import geniusweb.actions.Action;
12import geniusweb.actions.Offer;
13import geniusweb.actions.PartyId;
14import geniusweb.bidspace.AllPartialBidsList;
15import geniusweb.issuevalue.Bid;
16import geniusweb.party.Capabilities;
17import geniusweb.party.DefaultParty;
18import geniusweb.party.inform.ActionDone;
19import geniusweb.party.inform.Finished;
20import geniusweb.party.inform.Inform;
21import geniusweb.party.inform.Settings;
22import geniusweb.party.inform.YourTurn;
23import geniusweb.profile.PartialOrdering;
24import geniusweb.profile.Profile;
25import geniusweb.profile.utilityspace.UtilitySpace;
26import geniusweb.profileconnection.ProfileConnectionFactory;
27import geniusweb.profileconnection.ProfileInterface;
28import geniusweb.progress.Progress;
29import geniusweb.progress.ProgressRounds;
30import tudelft.utilities.logging.Reporter;
31
32/**
33 * A simple party that places random bids and accepts when it receives an offer
34 * with sufficient utility.
35 */
36public class RandomParty extends DefaultParty {
37
38 private Bid lastReceivedBid = null;
39 private PartyId me;
40 private final Random random = new Random();
41 protected ProfileInterface profileint;
42 private Progress progress;
43
44 public RandomParty() {
45 }
46
47 public RandomParty(Reporter reporter) {
48 super(reporter); // for debugging
49 }
50
51 @Override
52 public void notifyChange(Inform info) {
53 try {
54 if (info instanceof Settings) {
55 Settings settings = (Settings) info;
56 this.profileint = ProfileConnectionFactory
57 .create(settings.getProfile().getURI(), getReporter());
58 this.me = settings.getID();
59 this.progress = settings.getProgress();
60 } else if (info instanceof ActionDone) {
61 Action otheract = ((ActionDone) info).getAction();
62 if (otheract instanceof Offer) {
63 lastReceivedBid = ((Offer) otheract).getBid();
64 }
65 } else if (info instanceof YourTurn) {
66 myTurn();
67 if (progress instanceof ProgressRounds) {
68 progress = ((ProgressRounds) progress).advance();
69 }
70 } else if (info instanceof Finished) {
71 getReporter().log(Level.INFO, "Final ourcome:" + info);
72 }
73 } catch (Exception e) {
74 throw new RuntimeException("Failed to handle info", e);
75 }
76 }
77
78 @Override
79 public Capabilities getCapabilities() {
80 return new Capabilities(new HashSet<>(Arrays.asList("SAOP")));
81 }
82
83 @Override
84 public String getDescription() {
85 return "places random bids until it can accept an offer with utility >0.6";
86 }
87
88 private void myTurn() throws IOException {
89 Action action;
90 if (isGood(lastReceivedBid)) {
91 action = new Accept(me, lastReceivedBid);
92 } else {
93 // for demo. Obviously full bids have higher util in general
94 AllPartialBidsList bidspace = new AllPartialBidsList(
95 profileint.getProfile().getDomain());
96 Bid bid = null;
97 for (int attempt = 0; attempt < 20 && !isGood(bid); attempt++) {
98 long i = random.nextInt(bidspace.size().intValue());
99 bid = bidspace.get(BigInteger.valueOf(i));
100 }
101 action = new Offer(me, bid);
102 }
103 getConnection().send(action);
104
105 }
106
107 private boolean isGood(Bid bid) throws IOException {
108 if (bid == null)
109 return false;
110 Profile profile = profileint.getProfile();
111 if (profile instanceof UtilitySpace) {
112 return ((UtilitySpace) profile).getUtility(bid).doubleValue() > 0.6;
113 }
114 if (profile instanceof PartialOrdering) {
115 return ((PartialOrdering) profile).isPreferredOrEqual(bid,
116 profile.getReservationBid());
117 }
118 throw new IllegalArgumentException(
119 "Can not handle profile type " + profile.getClass());
120 }
121
122}
Note: See TracBrowser for help on using the repository browser.