package bargainingchips; import java.util.List; import java.util.Random; import java.util.ArrayList; import java.util.Iterator; import bargainingchips.actions.Offer; import bargainingchips.players.Agent; import bargainingchips.utilityfunctions.UtilityFunction; /** * * This class represents the complete outcome space and is therefore useful if * someone wants to quickly implement an agent. Note that working with a sorted * outcomespace class would be faster during the negotiation. * * * @author Tim Baarslag and Faria Nassiri-Mofakham * */ public class OutcomeSpace implements Iterable { /** List of all possible bids in the domain */ protected List allBids = new ArrayList(); protected String fileName; private double discountFactor = 1; /** * Creates a test outcome space. */ public OutcomeSpace() { generateAllBids(); } /** * Generates all the possible bids in the domain * * @param utilSpace */ private void generateAllBids() { for (int g = 1; g < 10; g++) allBids.add(Offer.getSampleOffer("Green", g).getBundle()); } public List getAllBids() { return allBids; } /** * gets a {@link Bundle} which is closest to the given utility * * @param utility to which the found bid must be closest. */ public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a) { int index = getIndexOfBidNearUtility(utility, u); Bundle bundle = allBids.get(index); System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle)); return bundle; } /** * @return best bid in the domain. */ public Bundle getMaxBidPossible(UtilityFunction u) { Bundle maxBid = allBids.get(0); for (Bundle bid : allBids) { if (u.getUtility(bid) > u.getUtility(maxBid)) { maxBid = bid; } } return maxBid; } /** * @return worst bid in the domain. */ public Bundle getMinBidPossible(UtilityFunction u) { Bundle minBid = allBids.get(0); for (Bundle bid : allBids) { if (u.getUtility(bid) < u.getUtility(minBid)) { minBid = bid; } } return minBid; } /** * @param utility * to which the found bid must be closest. * @return index of the bid with the utility closest to the given utilty. */ private int getIndexOfBidNearUtility(double utility, UtilityFunction u) { double closesDistance = 1; int best = 0; for (int i = 0; i < allBids.size(); i++) { if (Math.abs(u.getUtility(allBids.get(i)) - utility) < closesDistance) { closesDistance = Math.abs(u.getUtility(allBids.get(i)) - utility); best = i; } } return best; } @Override public String toString() { String all = ""; for (Bundle b : allBids) { all += b.toString() + "\n,"; } return "OutcomeSpace[" + all + "]"; } @Override public Iterator iterator() { return getAllBids().iterator(); } public Bundle getRandom() { int size = size(); if (size == 0) return null; int index = (new Random()).nextInt(size); return allBids.get(index); } public Bundle getRandom(Random r) { int size = size(); if (size == 0) return null; int index = r.nextInt(size); return allBids.get(index); } public double getAverageUtility(UtilityFunction u) { int size = size(); if (size == 0) return 0; double totalUtil = 0; for (Bundle b : allBids) totalUtil += u.getUtility(b); return totalUtil / size; } public int size() { return allBids.size(); } /** * Check if this utility space is ready for negotiation. To be so, the * domain must match the given domain and the space must be complete. * * @param dom * is the domain in which nego is taking place * @throws Exception * if utility space is incomplete (@see isComplete()) */ public void checkReadyForNegotiation(List domain) throws Exception { if (!(domain.equals(getAllBids()))) throw new IllegalStateException( "domain does not match the negotiation domain"); else throw new IllegalStateException( "utility space is incomplete:"); } public String getName() { return getClass().getSimpleName(); } public double getUtility(UtilityFunction u, Bundle b) { return u.getUtility(b); } public double getReservationValue(UtilityFunction u) { try { double fReservationValue = u.getUtility(getMinBidPossible(u)); return fReservationValue; } catch (Exception e) { e.printStackTrace(); return 0; } } public final double getDiscountFactor() { return discountFactor; } // public void setDiscount(double newDiscount) { // discountFactor = validateDiscount(newDiscount); // } // // protected double validateDiscount(double df) { // if (df < 0 || df > 1) { // System.err.println( // "Warning: discount factor = " + df + " was discarded."); // } // // if (df <= 0 || df > 1) { // df = 1; // } // return df; // } }