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