import java.math.BigDecimal; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.concurrent.ThreadLocalRandom; import java.util.logging.Level; import java.util.stream.Collectors; import java.util.stream.StreamSupport; import geniusweb.actions.Accept; import geniusweb.actions.Action; import geniusweb.actions.EndNegotiation; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.boa.BoaState; import geniusweb.boa.biddingstrategy.BiddingStrategy; import geniusweb.boa.biddingstrategy.ExtendedUtilSpace; import geniusweb.inform.Settings; import geniusweb.issuevalue.Bid; import geniusweb.opponentmodel.FrequencyOpponentModel; import geniusweb.opponentmodel.OpponentModel; import geniusweb.profile.Profile; import geniusweb.profile.utilityspace.LinearAdditive; import tudelft.utilities.immutablelist.ImmutableList; import javax.swing.*; // Adapted Time dependent bidding strategy, given that class isn't subclassable public class Group5BiddingStrategy implements BiddingStrategy { // bidSpace=null means we're not yet initialized. private ExtendedUtilSpace bidSpace = null; private Double e, k, min, max; // min, max attainable utility private PartyId me; protected ImmutableList getPossibleOptions(BoaState state) { if (bidSpace == null) { init(state); } double utilityGoal = p( state.getProgress().get(System.currentTimeMillis())); // if there is no opponent model available return bidSpace.getBids((BigDecimal.valueOf(utilityGoal))); } protected BigDecimal bidToUtility(Bid bid, OpponentModel model) { if (!(model instanceof FrequencyOpponentModel)) { return BigDecimal.ZERO; } return ((FrequencyOpponentModel) model).getUtility(bid); } protected Bid rouletteWheel(Group5BoaState state, ImmutableList bidOptions) { Collection opponentModels = state.getOpponentModels().values(); BigDecimal numOpponents = BigDecimal.valueOf(opponentModels.size()); if (numOpponents.equals(BigDecimal.ZERO)) { return bidOptions.get(0); } List utilities = StreamSupport.stream(bidOptions.spliterator(), false) .map(bid -> opponentModels.stream() .map(model -> bidToUtility(bid, model)) .reduce(BigDecimal.ZERO, BigDecimal::add) .divide(numOpponents, 4, BigDecimal.ROUND_HALF_UP)) .collect(Collectors.toList()); // Perform Roulette Wheel Selection BigDecimal randomNumber = BigDecimal.valueOf(ThreadLocalRandom.current().nextDouble()); BigDecimal utilitiesSum = utilities.stream().reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal cumSum = BigDecimal.ZERO; for (int i = 0; i < utilities.size(); i++) { cumSum = cumSum.add(utilities.get(i)); BigDecimal cumRelativeSum = cumSum.divide(utilitiesSum, 4, BigDecimal.ROUND_HALF_UP); if (randomNumber.compareTo(cumRelativeSum) < 0) { // RandomNumber is smaller than cumRelativeSum return bidOptions.get(i > 0 ? i - 1 : 0); } } return bidOptions.get(utilities.size() - 1); } public Action getAction(BoaState state) { ImmutableList bidOptions = getPossibleOptions(state); if (!(state instanceof Group5BoaState) || bidOptions.size().intValue() == 0) { // should not happen, emergency exit state.getReporter().log(Level.WARNING, "No viable bids found around current utility target"); Bid lastBid = getLastBid(state.getActionHistory()); if (lastBid == null) return new EndNegotiation(me); return new Accept(me, lastBid); } return new Offer(me, rouletteWheel((Group5BoaState)state, bidOptions)); } /** * Overrideable for hard configuring this component. * * @param state the {@link BoaState} * @return the parameter e for the time depemdency function * {@link #f(double)}. The parameter is 1 by default, or the value * for the "e" parameter in the {@link Settings} if available. */ protected Double getE(BoaState state) { HashMap params = state.getSettings().getParameters(); Double e = 1d; if (params.containsKey("e")) { e = (Double) params.get("e"); if (e < 0 || e > 1) throw new IllegalArgumentException( "e must be in [0,1] but got " + this.e); } return e; } /** * Overrideable for hard configuring this component. * * @param state the {@link BoaState} * @return the parameter k for the time dependency function * {@link #f(double)}. The parameter is 0 by default, or the value * for the "k" parameter in the {@link Settings} if available. */ protected Double getK(BoaState state) { HashMap params = state.getSettings().getParameters(); Double k = 0d; // default if (params.containsKey("k")) { k = (Double) params.get("k"); if (k < 0 || k > 1) throw new IllegalArgumentException( "k must be in [0,1] but got " + this.e); } return k; } /** * Assumes {@link #bidSpace} has been initialized. *

* Overrideable for hard configuring this component. * * @param state the {@link BoaState} * @return the min value for {@link #p(double)}. We use the "min" parameter * in the {@link Settings} if available. If not available, the * parameter is computed as the utility of the reservation bid. If * there is no reservation bid, we use the minimum utility of the * available profile. */ protected Double getMin(BoaState state) { HashMap params = state.getSettings().getParameters(); if (params.containsKey("min")) { return Math.max(0f, Math.min(1f, (Double) params.get("min"))); } LinearAdditive profile = (LinearAdditive) state.getProfile(); if (profile.getReservationBid() != null) { return profile.getUtility(profile.getReservationBid()) .doubleValue(); } return bidSpace.getMin().doubleValue(); } /** * Assumes {@link #bidSpace} has been initialized. *

* Overrideable for hard configuring this component. * * @param state the {@link BoaState} * @return the max value for {@link #p(double)}. We use the "max" parameter * in the {@link Settings} if available. If not available, we use * the maximum utility of the available profile. */ protected Double getMax(BoaState state) { HashMap params = state.getSettings().getParameters(); if (params.containsKey("max")) { return Math.max(0f, Math.min(1f, (Double) params.get("max"))); } return bidSpace.getMax().doubleValue(); } /** * @return the most recent bid that was offered, or null if no offer has * been done yet. */ private Bid getLastBid(List history) { for (int n = history.size() - 1; n >= 0; n--) { Action action = history.get(n); if (action instanceof Offer) { return ((Offer) action).getBid(); } } return null; } /** * initializes bidSpace * * @param state */ private void init(BoaState state) { this.me = state.getSettings().getID(); Profile prof = state.getProfile(); if (!(prof instanceof LinearAdditive)) throw new IllegalArgumentException( "Requires a LinearAdditive space but got " + prof); LinearAdditive profile = (LinearAdditive) prof; this.bidSpace = getBidSpace(profile); this.e = getE(state); this.k = getK(state); this.min = getMin(state); this.max = getMax(state); state.getReporter().log(Level.INFO, "BOA biddingstrategy min util = " + this.min); } /** * Makes sure the target utility with in the acceptable range according to * the domain * * @param t the normalized current time in the negotiation, where t=9 at * start of negotiation and t=1 at end of negotiation. * @return double */ private double p(double t) { return this.min + (this.max - this.min) * (1.0 - f(t)); } /** * From the paper: * * A wide range of time dependent functions can be defined by varying the * way in which f(t) is computed. However, functions must ensure that 0 <= * f(t) <= 1, f(0) = k, and f(1) = 1. * * That is, the offer will always be between the value range, at the * beginning it will give the initial constant and when the deadline is * reached, it will offer the reservation value. * * @param t the normalized current time in the negotiation, where t=9 at * start of negotiation and t=1 at end of negotiation. * @return */ private double f(double t) { double ft = k + (1.0 - k) * Math.pow(t, 1.0 / e); return ft; } /** * Factory method to get the {@link ExtendedUtilSpace} helper class. * Overridable eg for tests * * @param profile a {@link LinearAdditive} profile * * @return {@link ExtendedUtilSpace} */ protected ExtendedUtilSpace getBidSpace(LinearAdditive profile) { return new ExtendedUtilSpace(profile); } }