source: src/main/java/negotiator/boaframework/omstrategy/OfferBestN.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 4.7 KB
RevLine 
[127]1package negotiator.boaframework.omstrategy;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Map;
9import java.util.Random;
10import java.util.Set;
11
12import genius.core.Bid;
13import genius.core.bidding.BidDetails;
14import genius.core.bidding.BidDetailsSorterUtility;
15import genius.core.boaframework.BOAparameter;
16import genius.core.boaframework.NegotiationSession;
17import genius.core.boaframework.OMStrategy;
18import genius.core.boaframework.OpponentModel;
19
20/**
21 * This class uses an opponent model to determine the next bid for the opponent,
22 * while taking the opponent's preferences into account. The opponent model is
23 * used to select the N best bids. Following, a random bid is selected from this
24 * subset. Setting N > 1 is rational, as opponent models cannot be assumed to be
25 * perfect.
26 *
27 * Due to performance reasons, it is recommended to use BestBid if N = 1.
28 *
29 * @author Mark Hendrikx
30 */
31public class OfferBestN extends OMStrategy {
32
33 private Random rand;
34 /** parameter which determines which n best bids should be considered */
35 private int bestN;
36 /** used to sort the opponent's bid with regard to utility */
37 private BidDetailsSorterUtility comp = new BidDetailsSorterUtility();
38 /** when to stop updating */
39 double updateThreshold = 1.1;
40
41 /**
42 * Initializes the agent by storing the size of the domain, and checking if
43 * the domain is large.
44 *
45 * @param negotiationSession
46 * state of the negotiation.
47 * @param model
48 * opponent model used in conjunction with this opponent modeling
49 * strategy.
50 * @param parameters
51 * set of parameters for this opponent model strategy.
52 */
53 @Override
54 public void init(NegotiationSession negotiationSession, OpponentModel model, Map<String, Double> parameters) {
55 super.init(negotiationSession, model, parameters);
56 this.negotiationSession = negotiationSession;
57 this.model = model;
58 if (parameters.get("n") != null) {
59 int n = parameters.get("n").intValue();
60 if (n == 1) {
61 throw new IllegalArgumentException("For \"n\"=1 use BestBid instead.");
62 }
63 initializeAgent(negotiationSession, model, n);
64 } else {
65 throw new IllegalArgumentException("Constant \"n\" for amount of best bids was not set.");
66 }
67 if (parameters.get("t") != null) {
68 updateThreshold = parameters.get("t").doubleValue();
69 } else {
70 System.out.println("OMStrategy assumed t = 1.1");
71 }
72 }
73
74 private void initializeAgent(NegotiationSession negotiationSession, OpponentModel model, int n) {
75 try {
76 super.init(negotiationSession, model, new HashMap<String, Double>());
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 this.rand = new Random();
81 this.bestN = n;
82 }
83
84 /**
85 * First this method determines the N best bids given the array of similarly
86 * preferred bids. Next, a random bid is offered from this set.
87 *
88 * @param allBids
89 * list of similarly preferred bids.
90 * @return random bid from the subset of N best bids in the given set.
91 */
92 @Override
93 public BidDetails getBid(List<BidDetails> allBids) {
94 // 1. Determine the utility for the opponent for each of the bids
95 ArrayList<BidDetails> oppBids = new ArrayList<BidDetails>(allBids.size());
96 for (BidDetails bidDetail : allBids) {
97 Bid bid = bidDetail.getBid();
98 BidDetails newBid = new BidDetails(bid, model.getBidEvaluation(bid), negotiationSession.getTime());
99 oppBids.add(newBid);
100 }
101
102 // 2. Sort the bids on the utility for the opponent
103 Collections.sort(oppBids, comp);
104
105 // 3. Select a random bid from the N best bids and offer this bid
106 int entry = rand.nextInt(Math.min(bestN, oppBids.size()));
107 Bid opponentBestBid = oppBids.get(entry).getBid();
108 BidDetails nextBid = null;
109 try {
110 nextBid = new BidDetails(opponentBestBid, negotiationSession.getUtilitySpace().getUtility(opponentBestBid),
111 negotiationSession.getTime());
112 } catch (Exception e) {
113 e.printStackTrace();
114 }
115 return nextBid;
116 }
117
118 /**
119 * Specifies that the opponent model may be updated when the current time is
120 * smaller than the deadline.
121 *
122 * @return true if the model may be updated
123 */
124 @Override
125 public boolean canUpdateOM() {
126 return negotiationSession.getTime() < updateThreshold;
127 }
128
129 @Override
130 public Set<BOAparameter> getParameterSpec() {
131 Set<BOAparameter> set = new HashSet<BOAparameter>();
132 set.add(new BOAparameter("n", 3.0, "A random bid is selected from the best n bids"));
133 set.add(new BOAparameter("t", 1.1, "Time after which the OM should not be updated"));
134 return set;
135 }
136
137 @Override
138 public String getName() {
139 return "Offer Best N";
140 }
[1]141}
Note: See TracBrowser for help on using the repository browser.