source: exampleparties/simpleshaop/src/main/java/geniusweb/exampleparties/simpleshaop/ShaopParty.java@ 11

Last change on this file since 11 was 11, checked in by bart, 4 years ago

Tournament overview table, few bug fixes

File size: 4.6 KB
Line 
1package geniusweb.exampleparties.simpleshaop;
2
3import java.io.IOException;
4import java.math.BigDecimal;
5import java.math.BigInteger;
6import java.util.Arrays;
7import java.util.HashSet;
8import java.util.Random;
9import java.util.logging.Level;
10
11import geniusweb.actions.Accept;
12import geniusweb.actions.Action;
13import geniusweb.actions.Comparison;
14import geniusweb.actions.ElicitComparison;
15import geniusweb.actions.Offer;
16import geniusweb.actions.PartyId;
17import geniusweb.bidspace.AllBidsList;
18import geniusweb.issuevalue.Bid;
19import geniusweb.party.Capabilities;
20import geniusweb.party.DefaultParty;
21import geniusweb.party.inform.ActionDone;
22import geniusweb.party.inform.Finished;
23import geniusweb.party.inform.Inform;
24import geniusweb.party.inform.Settings;
25import geniusweb.party.inform.YourTurn;
26import geniusweb.profile.PartialOrdering;
27import geniusweb.profileconnection.ProfileConnectionFactory;
28import geniusweb.profileconnection.ProfileInterface;
29import geniusweb.progress.Progress;
30import geniusweb.progress.ProgressRounds;
31import 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 */
44public 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.";
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}
Note: See TracBrowser for help on using the repository browser.