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

Last change on this file since 339 was 339, checked in by Faria Nassiri Mofakham, 5 years ago

BargainingChips creates bob calling BargainingBuyer. OutcomeSpace is Iterable<Bundle>, also getAllBids, getRandom, getRandom(r), getAverageUtility(u), size(), checkReadyForNegotiation, getName(), getUtility(u, b), getReservationValue(u), and getDiscountFactor added. An update on Bid getSampleBid type. An update on Agent. A history' package of BidEvent, BidEventHistory, BidEventHisoryKeeper, BidEventSortedUtility, and BidEventStrictSortedUtility added. HistoryAgent added. An analysis' package of BundleUtilityPoint, BundleUtilitySpace, and ParetoFrontier added. BargainingAgent added. TitForTatNegotiationAgent. Now, Buyer extended to BargainingBuyer which is created using Boulware or TitForTatNegotiationAgent using one among 9 utility functions. Name of strategy added toDescription in BoulwareAgent. A few small updates on several utility function classes.

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