source: src/main/java/bargainingchips/players/HistoryAgent.java@ 340

Last change on this file since 340 was 340, checked in by Tim Baarslag, 4 years ago

Change BilateralProtocol loop to avoid busy wait; note that this fixes only a part of the busy wait problem.

Package structure now in line with latest Acumex discussions.

Pinpointed error avoidance in Agent.

File size: 6.2 KB
Line 
1/**
2 *
3 */
4package bargainingchips.players;
5
6
7import java.util.concurrent.BlockingQueue;
8
9import bargainingchips.Bundle;
10import bargainingchips.NegotiationContext;
11import bargainingchips.OutcomeSpace;
12import bargainingchips.actions.Bid;
13import bargainingchips.actions.FinalAccept;
14import bargainingchips.actions.Offer;
15import bargainingchips.actions.OfferBy;
16import bargainingchips.actions.OfferType;
17import bargainingchips.actions.PreAccept;
18import bargainingchips.messaging.coordination.CoordinationMessage;
19import bargainingchips.messaging.status.StatusMessage;
20import bargainingchips.outcomes.Outcome;
21import bargainingchips.players.history.BidEvent;
22import bargainingchips.players.history.BidEventHistory;
23import bargainingchips.players.history.BidEventHistoryKeeper;
24import bargainingchips.utilityfunctions.UtilityFunction;
25
26/**
27 *
28 * The bilateral history agent is a model-based agent [1]; that is, it has memory (i.e., the bidding history).
29 * It keeps separate copies of the bids made by itself as well as its opponent. It is designed for Bargaining Chips,
30 * in which, besides its opponent, each such bilateral agent also communicates with its {@link coordinator} agent.
31 * This skeleton memory-based agent adapts some functionalities in both
32 * {@link /NegotiatorGUI/src/main/java/agents/anac/y2011/Nice_Tit_for_Tat/BilateralAgent.java} and
33 * {@link /NegotiatorGUI/src/main/java/bargainingchips/players/TimeDependentNegotiationAgent.java}.
34 *
35 * [1] Stuart Russell and Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (2020).
36 *
37 *
38 * @author Faria Nassiri-Mofakham
39 *
40 */
41public abstract class HistoryAgent extends Agent implements BidEventHistoryKeeper {
42
43 protected OutcomeSpace outcomeSpace;
44 protected BidEventHistory myHistory;
45 protected BidEventHistory opponentHistory;
46
47 private Offer lastReceivedOffer = null;
48
49 public HistoryAgent(String name, UtilityFunction u, NegotiationContext nc,
50 BlockingQueue<Offer> in,
51 BlockingQueue<OfferBy> out,
52 BlockingQueue<CoordinationMessage> cin,
53 BlockingQueue<StatusMessage> cout) {
54 super(name, u, nc, in, out, cin, cout);
55 }
56
57 @Override
58 protected void receiveOutcome(Outcome outcome)
59 {
60 System.out.println(this + " is done with outcome " + outcome + ". Should clean up now.");
61 }
62
63 /**
64 * Messaging with the coordinator can happen below
65 */
66 @Override
67 protected void receiveCoordinationMessage(CoordinationMessage cpoll)
68 {
69 // Update the utility function
70 u = cpoll.getUtilityFunction();
71 }
72
73 @Override
74 protected StatusMessage sendStatusMessage()
75 {
76 return null;
77 }
78
79 @Override
80 protected void receiveOffer(Offer opponentOffer)
81 {
82 lastReceivedOffer = opponentOffer;
83 if (opponentOffer.isBid())
84 {
85 Bundle b = lastReceivedOffer.getBundle();
86 double time = getTime();
87 double myUtility = u.getUtility(b);
88 opponentHistory.add(new BidEvent(b,myUtility,time)); // added the event of opponent's bid in the history
89 }
90 }
91
92 //**
93 @Override
94 protected Offer sendOffer()
95 {
96 // Nothing received yet
97 if (lastReceivedOffer == null)
98 {
99 Bundle myBid = getNextBid();
100 remember(new Offer(myBid,OfferType.BID));
101 return new Bid(myBid);
102 }
103
104 // Our last bid got accepted. We are also accepting (and we should notify the coordinator).
105 if (lastReceivedOffer.isPreAccept())
106 return new FinalAccept(lastReceivedOffer);
107
108 Bundle lastReceivedBid = lastReceivedOffer.getBundle();
109
110 // Accept
111 if (isAcceptable(lastReceivedBid))
112 return new PreAccept(lastReceivedOffer);
113 // Counter offer based actions
114 else
115 {
116 Bundle nextBid = getNextBid();
117 remember(new Offer(nextBid,OfferType.BID));
118 return new Bid(nextBid);
119 }
120 }
121
122 //**
123 /**
124 * Get the next bid we should do
125 */
126 protected abstract Bundle getNextBid();
127
128 //**
129 @Override
130 protected Offer sendOpeningOffer()
131 {
132 return sendOffer();
133 }
134
135 public void init() {
136 outcomeSpace = negotiationContext.getOutcomeSpace();
137 myHistory = new BidEventHistory();
138 opponentHistory = new BidEventHistory();
139 }
140
141 public double getTime()
142 {
143 // time runs from 0.0 to 1.0
144 int totalrounds = NegotiationContext.DEADLINE;
145 return (double) k / totalrounds;
146 }
147
148 protected double getUtility(Bundle bid) {
149 double myUtility = 0;
150 try {
151 myUtility = u.getUtility(bid);
152 } catch (Exception e) {
153 e.printStackTrace();
154 }
155 return myUtility;
156 }
157
158
159 /**
160 * Remember our offer, if it was a bid (i.e., of BID type)
161 */
162 protected void remember(Offer myAction) {
163 if (myAction.isBid()) {
164 Bundle myLastBid = myAction.getBundle();
165 double time = getTime();
166 double myUtility = getUtility(myLastBid);
167 BidEvent bidEvent = new BidEvent(myLastBid,
168 myUtility, time);
169 myHistory.add(bidEvent);
170 }
171 }
172
173 /**
174 * At some point, one of the parties has to accept an offer to end the
175 * negotiation. Use this method to decide whether to accept the last offer
176 * by the opponent.
177 *
178 * @param plannedBid
179 */
180 public abstract boolean isAcceptable(Bundle plannedBid);
181
182 public abstract Offer makeAcceptAction();
183
184 /**
185 * The opponent has already made a bid. Use this method to make an counter
186 * bid.
187 */
188 public abstract Bundle chooseCounterBid();
189
190 /**
191 * Use this method to make an opening bid.
192 */
193 public abstract Bundle chooseOpeningBid();
194
195 /**
196 * Use this method to make the first counter-bid.
197 */
198 public abstract Bundle chooseFirstCounterBid();
199
200
201 //protected void prepareOpponentModel() {};
202
203 //public Offer chooseOffer() {};
204
205 //protected Offer makeAcceptOffer() {};
206
207
208 public Bundle getMyLastBid() {
209 return myHistory.getLastBid();
210 }
211
212 public Bundle getMySecondLastBid() {
213 return myHistory.getSecondLastBid();
214 }
215
216 public BidEventHistory getOpponentHistory() {
217 return opponentHistory;
218 }
219
220 public Bundle getOpponentLastBid() {
221 return opponentHistory.getLastBid();
222 }
223
224 /**
225 * Returns the current round number, starting at 0. This is equal to the
226 * total number of placed bids.
227 */
228 public int getRound() {
229 return myHistory.size() + opponentHistory.size();
230 }
231
232 protected static void log(String s) {
233 System.out.println(s);
234 }
235
236}
237
Note: See TracBrowser for help on using the repository browser.