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

Last change on this file since 31 was 31, checked in by bart, 3 years ago

New protocols Learn and APPLearn. Fixed memory leak.

File size: 4.7 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.Collections;
8import java.util.HashSet;
9import java.util.Random;
10import java.util.logging.Level;
11
12import geniusweb.actions.Accept;
13import geniusweb.actions.Action;
14import geniusweb.actions.Comparison;
15import geniusweb.actions.ElicitComparison;
16import geniusweb.actions.Offer;
17import geniusweb.actions.PartyId;
18import geniusweb.bidspace.AllBidsList;
19import geniusweb.inform.ActionDone;
20import geniusweb.inform.Finished;
21import geniusweb.inform.Inform;
22import geniusweb.inform.Settings;
23import geniusweb.inform.YourTurn;
24import geniusweb.issuevalue.Bid;
25import geniusweb.party.Capabilities;
26import geniusweb.party.DefaultParty;
27import geniusweb.profile.DefaultPartialOrdering;
28import geniusweb.profile.PartialOrdering;
29import geniusweb.profileconnection.ProfileConnectionFactory;
30import geniusweb.profileconnection.ProfileInterface;
31import geniusweb.progress.Progress;
32import geniusweb.progress.ProgressRounds;
33import 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 */
46public 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 }
87 } catch (Exception e) {
88 throw new RuntimeException("Failed to handle info", e);
89 }
90 }
91
92 @Override
93 public Capabilities getCapabilities() {
94 return new Capabilities(new HashSet<>(Arrays.asList("SHAOP")),
95 Collections.singleton(DefaultPartialOrdering.class));
96 }
97
98 @Override
99 public String getDescription() {
100 return "Communicates with COB party to figure out which bids are good. Accepts bids with utility > 0.9. Offers random bids. Requires partial profile";
101 }
102
103 /**
104 * Called when it's (still) our turn and we should take some action. Also
105 * Updates the progress if necessary.
106 */
107 private void myTurn() throws IOException {
108 Action action = null;
109 if (estimatedProfile == null) {
110 estimatedProfile = new SimpleLinearOrdering(
111 profileint.getProfile());
112 }
113
114 if (lastReceivedBid != null) {
115 // then we do the action now, no need to ask user
116 if (estimatedProfile.contains(lastReceivedBid)) {
117 if (isGood(lastReceivedBid)) {
118 action = new Accept(me, lastReceivedBid);
119 }
120 } else {
121 // we did not yet assess the received bid
122 action = new ElicitComparison(me, lastReceivedBid,
123 estimatedProfile.getBids());
124 }
125 if (progress instanceof ProgressRounds) {
126 progress = ((ProgressRounds) progress).advance();
127 }
128 }
129 // Otherwise just offer a Random bid
130 // FIXME this is weird, can't we do better than random?
131 if (action == null)
132 action = randomBid();
133 getConnection().send(action);
134 }
135
136 private Offer randomBid() throws IOException {
137 AllBidsList bidspace = new AllBidsList(
138 profileint.getProfile().getDomain());
139 long i = random.nextInt(bidspace.size().intValue());
140 Bid bid = bidspace.get(BigInteger.valueOf(i));
141 return new Offer(me, bid);
142 }
143
144 private boolean isGood(Bid bid) {
145 if (bid == null)
146 return false;
147 return estimatedProfile.getUtility(bid).compareTo(N09) >= 0;
148 }
149
150}
Note: See TracBrowser for help on using the repository browser.