source: anac2020/agentxx/src/main/java/geniusweb/exampleparties/randomparty/RandomParty.java

Last change on this file was 24, checked in by wouter, 3 years ago

#3

File size: 3.6 KB
Line 
1package geniusweb.exampleparties.randomparty;
2
3import java.io.IOException;
4import java.math.BigInteger;
5import java.util.Arrays;
6import java.util.Collections;
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.Offer;
14import geniusweb.actions.PartyId;
15import geniusweb.bidspace.AllPartialBidsList;
16import geniusweb.inform.ActionDone;
17import geniusweb.inform.Finished;
18import geniusweb.inform.Inform;
19import geniusweb.inform.Settings;
20import geniusweb.inform.YourTurn;
21import geniusweb.issuevalue.Bid;
22import geniusweb.party.Capabilities;
23import geniusweb.party.DefaultParty;
24import geniusweb.profile.PartialOrdering;
25import geniusweb.profile.Profile;
26import geniusweb.profile.utilityspace.UtilitySpace;
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 party that places random bids and accepts when it receives an offer
35 * with sufficient utility.
36 */
37public class RandomParty extends DefaultParty {
38
39 private Bid lastReceivedBid = null;
40 private PartyId me;
41 private final Random random = new Random();
42 protected ProfileInterface profileint;
43 private Progress progress;
44
45 public RandomParty() {
46 }
47
48 public RandomParty(Reporter reporter) {
49 super(reporter); // for debugging
50 }
51
52 @Override
53 public void notifyChange(Inform info) {
54 try {
55 if (info instanceof Settings) {
56 Settings settings = (Settings) info;
57 this.profileint = ProfileConnectionFactory.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")), Collections.singleton(Profile.class));
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(profileint.getProfile().getDomain());
95 Bid bid = null;
96 for (int attempt = 0; attempt < 20 && !isGood(bid); attempt++) {
97 long i = random.nextInt(bidspace.size().intValue());
98 bid = bidspace.get(BigInteger.valueOf(i));
99 }
100 action = new Offer(me, bid);
101 }
102 getConnection().send(action);
103
104 }
105
106 private boolean isGood(Bid bid) throws IOException {
107 if (bid == null)
108 return false;
109 Profile profile = profileint.getProfile();
110 if (profile instanceof UtilitySpace) {
111 return ((UtilitySpace) profile).getUtility(bid).doubleValue() > 0.6;
112 }
113 if (profile instanceof PartialOrdering) {
114 return ((PartialOrdering) profile).isPreferredOrEqual(bid, profile.getReservationBid());
115 }
116 throw new IllegalArgumentException("Can not handle profile type " + profile.getClass());
117 }
118
119}
Note: See TracBrowser for help on using the repository browser.