[330] | 1 | package bargainingchips.players;
|
---|
| 2 |
|
---|
| 3 | import java.util.concurrent.BlockingQueue;
|
---|
| 4 | import java.util.concurrent.LinkedBlockingQueue;
|
---|
| 5 |
|
---|
| 6 | import bargainingchips.Bundle;
|
---|
| 7 | import bargainingchips.NegotiationContext;
|
---|
| 8 | import bargainingchips.OutcomeSpace;
|
---|
| 9 | import bargainingchips.actions.Accept;
|
---|
| 10 | import bargainingchips.actions.Bid;
|
---|
| 11 | import bargainingchips.actions.Offer;
|
---|
| 12 | import bargainingchips.actions.OfferBy;
|
---|
| 13 | import bargainingchips.outcomes.Outcome;
|
---|
| 14 | import bargainingchips.utilityfunctions.UtilityFunction;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Basic agent, by Tim Baarslag
|
---|
| 18 | */
|
---|
| 19 | public class BasicAgent extends Agent
|
---|
| 20 | {
|
---|
| 21 | private Offer lastReceivedOffer = null;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * For sellers, dummy coordinator queues are created but are never linked up
|
---|
| 25 | */
|
---|
| 26 | public BasicAgent(String name, UtilityFunction u, NegotiationContext nc,
|
---|
| 27 | BlockingQueue<Offer> in, BlockingQueue<OfferBy> out)
|
---|
| 28 | {
|
---|
| 29 | super(name, u, nc, in, out,
|
---|
| 30 | new LinkedBlockingQueue<CoordinationMessage>(),
|
---|
| 31 | new LinkedBlockingQueue<StatusMessage>());
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
| 35 | protected Offer sendOffer()
|
---|
| 36 | {
|
---|
| 37 | // Nothing received yet
|
---|
| 38 | if (lastReceivedOffer == null)
|
---|
| 39 | return new Bid(getNextBid());
|
---|
| 40 |
|
---|
| 41 | // Our last bid got accepted. We are also accepting (and we should notify the coordinator).
|
---|
| 42 | if (lastReceivedOffer.isAccept())
|
---|
| 43 | return new Accept(lastReceivedOffer);
|
---|
| 44 |
|
---|
| 45 | Bundle lastReceivedBid = lastReceivedOffer.getBundle();
|
---|
| 46 | Bundle nextBid = getNextBid();
|
---|
| 47 |
|
---|
| 48 | double lastUtil = lastReceivedBid != null ? u.getUtility(lastReceivedBid) : 0;
|
---|
| 49 | double nextUtil = nextBid != null ? u.getUtility(nextBid) : 0;
|
---|
| 50 |
|
---|
| 51 | // Accept
|
---|
| 52 | if (nextUtil <= lastUtil)
|
---|
| 53 | return new Accept(lastReceivedOffer);
|
---|
| 54 | // Counter offer based actions
|
---|
| 55 | else
|
---|
| 56 | {
|
---|
| 57 | try {
|
---|
| 58 | Thread.sleep(5000);
|
---|
| 59 | } catch (InterruptedException e) {
|
---|
| 60 | // TODO Auto-generated catch block
|
---|
| 61 | e.printStackTrace();
|
---|
| 62 | }
|
---|
| 63 | return new Bid(nextBid);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Get the next bid we should do
|
---|
| 70 | */
|
---|
| 71 | protected Bundle getNextBid()
|
---|
| 72 | {
|
---|
| 73 | OutcomeSpace outcomeSpace = negotiationContext.getOutcomeSpace();
|
---|
| 74 | return outcomeSpace.getBidNearUtility(getTargetUtility(), u, this);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
| 78 | protected void receiveOffer(Offer o)
|
---|
| 79 | {
|
---|
| 80 | lastReceivedOffer = o;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | @Override
|
---|
| 84 | protected void receiveOutcome(Outcome outcome)
|
---|
| 85 | {
|
---|
| 86 | System.out.println(this + " is done with outcome " + outcome + ". Should clean up now.");
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Only the best bid is good enough.
|
---|
| 91 | */
|
---|
| 92 | private double getTargetUtility()
|
---|
| 93 | {
|
---|
| 94 | return 1.0;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | @Override
|
---|
| 98 | protected Offer sendOpeningOffer()
|
---|
| 99 | {
|
---|
| 100 | return null;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Messaging with the coordinator can happen below
|
---|
| 105 | */
|
---|
| 106 | @Override
|
---|
| 107 | protected void receiveCoordinationMessage(CoordinationMessage cpoll)
|
---|
| 108 | {
|
---|
| 109 | // Update the utility function
|
---|
| 110 | u = cpoll.getUtilityFunction();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | @Override
|
---|
| 114 | protected StatusMessage sendStatusMessage()
|
---|
| 115 | {
|
---|
| 116 | return null;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|