[127] | 1 | package negotiator.boaframework.omstrategy;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Collections;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.HashSet;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 | import java.util.Random;
|
---|
| 10 | import java.util.Set;
|
---|
| 11 |
|
---|
| 12 | import genius.core.Bid;
|
---|
| 13 | import genius.core.bidding.BidDetails;
|
---|
| 14 | import genius.core.bidding.BidDetailsSorterUtility;
|
---|
| 15 | import genius.core.boaframework.BOAparameter;
|
---|
| 16 | import genius.core.boaframework.NegotiationSession;
|
---|
| 17 | import genius.core.boaframework.OMStrategy;
|
---|
| 18 | import 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 | */
|
---|
| 31 | public 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 | } |
---|