source: src/main/java/bargainingchips/players/BasicAgent.java@ 330

Last change on this file since 330 was 330, checked in by Tim Baarslag, 5 years ago

BasicAgent

File size: 2.8 KB
Line 
1package bargainingchips.players;
2
3import java.util.concurrent.BlockingQueue;
4import java.util.concurrent.LinkedBlockingQueue;
5
6import bargainingchips.Bundle;
7import bargainingchips.NegotiationContext;
8import bargainingchips.OutcomeSpace;
9import bargainingchips.actions.Accept;
10import bargainingchips.actions.Bid;
11import bargainingchips.actions.Offer;
12import bargainingchips.actions.OfferBy;
13import bargainingchips.outcomes.Outcome;
14import bargainingchips.utilityfunctions.UtilityFunction;
15
16/**
17 * Basic agent, by Tim Baarslag
18 */
19public 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}
Note: See TracBrowser for help on using the repository browser.