1 | package geniusweb.exampleparties.randomparty;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.math.BigInteger;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Random;
|
---|
8 | import java.util.logging.Level;
|
---|
9 |
|
---|
10 | import geniusweb.actions.Accept;
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.Offer;
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import geniusweb.bidspace.AllPartialBidsList;
|
---|
15 | import geniusweb.issuevalue.Bid;
|
---|
16 | import geniusweb.party.Capabilities;
|
---|
17 | import geniusweb.party.DefaultParty;
|
---|
18 | import geniusweb.party.inform.ActionDone;
|
---|
19 | import geniusweb.party.inform.Finished;
|
---|
20 | import geniusweb.party.inform.Inform;
|
---|
21 | import geniusweb.party.inform.Settings;
|
---|
22 | import geniusweb.party.inform.YourTurn;
|
---|
23 | import geniusweb.profile.PartialOrdering;
|
---|
24 | import geniusweb.profile.Profile;
|
---|
25 | import geniusweb.profile.utilityspace.UtilitySpace;
|
---|
26 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
27 | import geniusweb.profileconnection.ProfileInterface;
|
---|
28 | import geniusweb.progress.Progress;
|
---|
29 | import geniusweb.progress.ProgressRounds;
|
---|
30 | import 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 | */
|
---|
36 | public 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 | }
|
---|