[1] | 1 | package geniusweb.exampleparties.randomparty;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.math.BigInteger;
|
---|
| 5 | import java.util.Arrays;
|
---|
[24] | 6 | import java.util.Collections;
|
---|
[1] | 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.Offer;
|
---|
| 14 | import geniusweb.actions.PartyId;
|
---|
| 15 | import geniusweb.bidspace.AllPartialBidsList;
|
---|
[24] | 16 | import geniusweb.inform.ActionDone;
|
---|
| 17 | import geniusweb.inform.Finished;
|
---|
| 18 | import geniusweb.inform.Inform;
|
---|
| 19 | import geniusweb.inform.Settings;
|
---|
| 20 | import geniusweb.inform.YourTurn;
|
---|
[1] | 21 | import geniusweb.issuevalue.Bid;
|
---|
| 22 | import geniusweb.party.Capabilities;
|
---|
| 23 | import geniusweb.party.DefaultParty;
|
---|
| 24 | import geniusweb.profile.PartialOrdering;
|
---|
| 25 | import geniusweb.profile.Profile;
|
---|
| 26 | import geniusweb.profile.utilityspace.UtilitySpace;
|
---|
| 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 party that places random bids and accepts when it receives an offer
|
---|
| 35 | * with sufficient utility.
|
---|
| 36 | */
|
---|
| 37 | public 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;
|
---|
[24] | 57 | this.profileint = ProfileConnectionFactory.create(settings.getProfile().getURI(), getReporter());
|
---|
[1] | 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() {
|
---|
[24] | 80 | return new Capabilities(new HashSet<>(Arrays.asList("SAOP")), Collections.singleton(Profile.class));
|
---|
[1] | 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
|
---|
[24] | 94 | AllPartialBidsList bidspace = new AllPartialBidsList(profileint.getProfile().getDomain());
|
---|
[1] | 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) {
|
---|
[24] | 114 | return ((PartialOrdering) profile).isPreferredOrEqual(bid, profile.getReservationBid());
|
---|
[1] | 115 | }
|
---|
[24] | 116 | throw new IllegalArgumentException("Can not handle profile type " + profile.getClass());
|
---|
[1] | 117 | }
|
---|
| 118 |
|
---|
| 119 | }
|
---|