/** * */ package bargainingchips.utilityfunctions; import bargainingchips.Bundle; import bargainingchips.BundleBuilder; import bargainingchips.Chip; import bargainingchips.ChipIssueValue; import bargainingchips.ChipIssueValueBuilder; import bargainingchips.wishlist.WishList; import bargainingchips.wishlist.WishListBuilder; /** * * This utility function evaluates the utility of an offer based on its separate utilities regarding the ratio of its total price {@link UF_LessPrice} * with respect to the break even prices as well as the total deviation of the chips quantities {@link UF_CloseToQuantity} by considering the importance of price vs. quantity. * It calculates two separate utility functions uP for total offered vs. desired prices as well as uQ for total offered vs desired quantities. * The utility of the offered bundle is then is calculated by uP * uQ (dependent values). * * * @author Faria Nassiri-Mofakham * */ public class UF_LessPriceCloseToQuantity implements UtilityFunction { private WishList wishlist; private ChipIssueValue breakEvenPrices; public UF_LessPriceCloseToQuantity(WishList w, ChipIssueValue bEP) { this.wishlist = w; this.breakEvenPrices = bEP; } @Override public Double getUtility(Bundle b) { int totalDeviationQ = 0; double desiredP=0.0; for (Chip c : wishlist) { int desiredQ = wishlist.getQuantity(c); Integer offeredQ = b.getQuantity(c); if (offeredQ == null) offeredQ = 0; int deviationOfferedQ = Math.abs(desiredQ - offeredQ); totalDeviationQ += deviationOfferedQ; desiredP += ((c!=null) ? breakEvenPrices.getUnitValue(c) : 0.0); } double offeredP = ((b!=null) ? b.getTotalPrice() : 0.0); if (offeredP > desiredP) { //System.out.println("offeredPrice "+ offeredP + " > desiredPrice "+ desiredP); return 0.0; } double uP = 1.0 - offeredP/desiredP; double uQ = 1.0 / (1 + totalDeviationQ); //System.out.println("uP "+uP+"\n"+"uQ "+uQ); double u = uP * uQ; return ( Double.isNaN(u)? 0.0 : ( (u>1) ? 1 : ( u < 0) ? 0 : u)); } @Override public String toString() { return this.getClass().getSimpleName() + ": WishList " + wishlist.toString() + ": BreakEvenPrices "+ breakEvenPrices.toString(); } public static void main(String[] args) { WishList wishlist1 = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build(); ChipIssueValue prices1 = new ChipIssueValueBuilder().addIssue("Red", 100.0).addIssue("Green", 200.0).addIssue("Blue", 700.0).build(); UF_LessPriceCloseToQuantity u1 = new UF_LessPriceCloseToQuantity(wishlist1, prices1); WishList wishlist2 = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build(); ChipIssueValue prices2 = new ChipIssueValueBuilder().addIssue("Red", 10.0).addIssue("Green", 20.0).addIssue("Blue", 70.0).build(); UF_LessPriceCloseToQuantity u2 = new UF_LessPriceCloseToQuantity(wishlist2, prices2); WishList wishlist3 = new WishListBuilder().addWish("Green", 4).addWish("Yellow", 6).addWish("Orange", 40).build(); ChipIssueValue prices3 = new ChipIssueValueBuilder().addIssue("Green", 3.0).addIssue("Yellow", 5.0).addIssue("Orange", 1.0).build(); UF_LessPriceCloseToQuantity u3 = new UF_LessPriceCloseToQuantity(wishlist3, prices3); Bundle offer1 = new BundleBuilder() .addStack("Red", 1.0, 6) .addStack("Green", 3.0, 15) .addStack("Purple", 0.10, 10) .build(); Bundle offer2 = new BundleBuilder() .addStack("Red", 1.0, 1) .addStack("Green", 3.0, 1) .addStack("Purple", 0.10, 1) .build(); Bundle offer3 = new BundleBuilder() // .addStack("Green", 2.0, 3) // .addStack("Yellow", 5.0, 4) // .addStack("Orange", 1.0, 17) //--- .addStack("Green", 3.0, 4) .addStack("Yellow", 5.0, 6) .addStack("Orange", 1.0, 40) //--- // .addStack("Green", 10.0, 1) // .addStack("Yellow", 10.0, 1) // .addStack("Orange", 10.0, 1) .build(); System.out.println("\nu1 : "+u1 +"\nOffer1 = "+offer1); System.out.println("=> "+u1.getUtility(offer1)); System.out.println("\nu1 : "+u1 +"\nOffer2 = "+offer2); System.out.println("=> "+u1.getUtility(offer2)); System.out.println("\nu1 : "+u1 +"\nOffer3 = "+offer3); System.out.println("=> "+u1.getUtility(offer3)); System.out.println("\nu2 : "+u2 +"\nOffer1 = "+offer1); System.out.println("=> "+u2.getUtility(offer1)); System.out.println("\nu2 : "+u2 +"\nOffer2 = "+offer2); System.out.println("=> "+u2.getUtility(offer2)); System.out.println("\nu2 : "+u2 +"\nOffer3 = "+offer3); System.out.println("=> "+u2.getUtility(offer3)); System.out.println("\nu3 : "+u3 +"\nOffer1 = "+offer1); System.out.println("=> "+u3.getUtility(offer1)); System.out.println("\nu3 : "+u3 +"\nOffer2 = "+offer2); System.out.println("=> "+u3.getUtility(offer2)); System.out.println("\nu3 : "+u3 +"\nOffer3 = "+offer3); System.out.println("=> "+u3.getUtility(offer3)); } }