1 | package geniusweb.exampleparties.simpleshaop;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.math.BigDecimal;
|
---|
5 | import java.math.BigInteger;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.Random;
|
---|
10 | import java.util.logging.Level;
|
---|
11 |
|
---|
12 | import geniusweb.actions.Accept;
|
---|
13 | import geniusweb.actions.Action;
|
---|
14 | import geniusweb.actions.Comparison;
|
---|
15 | import geniusweb.actions.ElicitComparison;
|
---|
16 | import geniusweb.actions.Offer;
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import geniusweb.bidspace.AllBidsList;
|
---|
19 | import geniusweb.inform.ActionDone;
|
---|
20 | import geniusweb.inform.Finished;
|
---|
21 | import geniusweb.inform.Inform;
|
---|
22 | import geniusweb.inform.Settings;
|
---|
23 | import geniusweb.inform.YourTurn;
|
---|
24 | import geniusweb.issuevalue.Bid;
|
---|
25 | import geniusweb.party.Capabilities;
|
---|
26 | import geniusweb.party.DefaultParty;
|
---|
27 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
28 | import geniusweb.profile.PartialOrdering;
|
---|
29 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
30 | import geniusweb.profileconnection.ProfileInterface;
|
---|
31 | import geniusweb.progress.Progress;
|
---|
32 | import geniusweb.progress.ProgressRounds;
|
---|
33 | import tudelft.utilities.logging.Reporter;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * A simple implementation of a SHAOP party that can handle only bilateral
|
---|
37 | * negotiations (1 other party). It will ignore all other parties except the one
|
---|
38 | * that has the turn right before us. It estimates the utilities of bids by
|
---|
39 | * assigning a linear increasing utility from the orderings that have been
|
---|
40 | * created.
|
---|
41 | * <p>
|
---|
42 | * <b>Requirement</b> the initial {@link PartialOrdering} must contain at least
|
---|
43 | * the bids with lowest utility and highest utility, and the proper comparison
|
---|
44 | * info for these two bids.
|
---|
45 | */
|
---|
46 | public class ShaopParty extends DefaultParty {
|
---|
47 |
|
---|
48 | private static final BigDecimal N09 = new BigDecimal("0.9");
|
---|
49 | private Bid lastReceivedBid = null; // we ignore all others
|
---|
50 | private PartyId me;
|
---|
51 | private final Random random = new Random();
|
---|
52 | protected ProfileInterface profileint;
|
---|
53 | private Progress progress;
|
---|
54 | private SimpleLinearOrdering estimatedProfile = null;
|
---|
55 |
|
---|
56 | public ShaopParty() {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ShaopParty(Reporter reporter) {
|
---|
60 | super(reporter); // for debugging
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void notifyChange(Inform info) {
|
---|
65 | try {
|
---|
66 | if (info instanceof Settings) {
|
---|
67 | Settings settings = (Settings) info;
|
---|
68 | this.profileint = ProfileConnectionFactory
|
---|
69 | .create(settings.getProfile().getURI(), getReporter());
|
---|
70 | this.me = settings.getID();
|
---|
71 | this.progress = settings.getProgress();
|
---|
72 | } else if (info instanceof ActionDone) {
|
---|
73 | Action otheract = ((ActionDone) info).getAction();
|
---|
74 | if (otheract instanceof Offer) {
|
---|
75 | lastReceivedBid = ((Offer) otheract).getBid();
|
---|
76 | } else if (otheract instanceof Comparison) {
|
---|
77 | estimatedProfile = estimatedProfile.with(
|
---|
78 | ((Comparison) otheract).getBid(),
|
---|
79 | ((Comparison) otheract).getWorse());
|
---|
80 | myTurn();
|
---|
81 | }
|
---|
82 | } else if (info instanceof YourTurn) {
|
---|
83 | myTurn();
|
---|
84 | } else if (info instanceof Finished) {
|
---|
85 | getReporter().log(Level.INFO, "Final ourcome:" + info);
|
---|
86 | terminate(); // stop this party and free resources.
|
---|
87 | }
|
---|
88 | } catch (Exception e) {
|
---|
89 | throw new RuntimeException("Failed to handle info", e);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Capabilities getCapabilities() {
|
---|
95 | return new Capabilities(new HashSet<>(Arrays.asList("SHAOP")),
|
---|
96 | Collections.singleton(DefaultPartialOrdering.class));
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public String getDescription() {
|
---|
101 | return "Communicates with COB party to figure out which bids are good. Accepts bids with utility > 0.9. Offers random bids. Requires partial profile";
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Called when it's (still) our turn and we should take some action. Also
|
---|
106 | * Updates the progress if necessary.
|
---|
107 | */
|
---|
108 | private void myTurn() throws IOException {
|
---|
109 | Action action = null;
|
---|
110 | if (estimatedProfile == null) {
|
---|
111 | estimatedProfile = new SimpleLinearOrdering(
|
---|
112 | (DefaultPartialOrdering) profileint.getProfile());
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (lastReceivedBid != null) {
|
---|
116 | // then we do the action now, no need to ask user
|
---|
117 | if (estimatedProfile.contains(lastReceivedBid)) {
|
---|
118 | if (isGood(lastReceivedBid)) {
|
---|
119 | action = new Accept(me, lastReceivedBid);
|
---|
120 | }
|
---|
121 | } else {
|
---|
122 | // we did not yet assess the received bid
|
---|
123 | action = new ElicitComparison(me, lastReceivedBid,
|
---|
124 | estimatedProfile.getBids());
|
---|
125 | }
|
---|
126 | if (progress instanceof ProgressRounds) {
|
---|
127 | progress = ((ProgressRounds) progress).advance();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | // Otherwise just offer a Random bid
|
---|
131 | // FIXME this is weird, can't we do better than random?
|
---|
132 | if (action == null)
|
---|
133 | action = randomBid();
|
---|
134 | getConnection().send(action);
|
---|
135 | }
|
---|
136 |
|
---|
137 | private Offer randomBid() throws IOException {
|
---|
138 | AllBidsList bidspace = new AllBidsList(
|
---|
139 | profileint.getProfile().getDomain());
|
---|
140 | long i = random.nextInt(bidspace.size().intValue());
|
---|
141 | Bid bid = bidspace.get(BigInteger.valueOf(i));
|
---|
142 | return new Offer(me, bid);
|
---|
143 | }
|
---|
144 |
|
---|
145 | private boolean isGood(Bid bid) {
|
---|
146 | if (bid == null)
|
---|
147 | return false;
|
---|
148 | return estimatedProfile.getUtility(bid).compareTo(N09) >= 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|