package onetomany.bargainingchipsgame; import java.util.List; import java.util.ArrayList; import onetomany.bargainingchipsgame.interactions.Offer; import onetomany.bargainingchipsgame.players.Agent; import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction; /** * This class generates 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. */ public class OutcomeSpace { /** List of all possible bids in the domain */ protected List allBids = new ArrayList(); /** * Creates an unsorted outcome space. Warning: this call iterates over ALL * possible bids. * * @param utilSpace */ public OutcomeSpace(Domain domain) { generateAllBids(); } /** * Generates all the possible bids in the domain * * @param utilSpace */ private void generateAllBids() { allBids.add(Offer.getSampleOffer("Green", 1).getBundle()); allBids.add(Offer.getSampleOffer("Green", 2).getBundle()); allBids.add(Offer.getSampleOffer("Green", 3).getBundle()); allBids.add(Offer.getSampleOffer("Green", 4).getBundle()); allBids.add(Offer.getSampleOffer("Green", 5).getBundle()); allBids.add(Offer.getSampleOffer("Green", 6).getBundle()); allBids.add(Offer.getSampleOffer("Green", 7).getBundle()); allBids.add(Offer.getSampleOffer("Green", 8).getBundle()); } /** * 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. */ private 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. */ private 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 + "]"; } }