[316] | 1 | package bargainingchips.utilityfunctions;
|
---|
[315] | 2 |
|
---|
[316] | 3 | import bargainingchips.Bundle;
|
---|
| 4 | import bargainingchips.BundleBuilder;
|
---|
| 5 | import bargainingchips.Chip;
|
---|
[340] | 6 | import bargainingchips.wishlist.WishList;
|
---|
| 7 | import bargainingchips.wishlist.WishListBuilder;
|
---|
[315] | 8 |
|
---|
| 9 | /**
|
---|
[338] | 10 | * Utility function for testing that acts on quantity alone.
|
---|
[315] | 11 | * Returns 1 when the wish list is exactly satisfied. Becomes lower with higher quantity deviations.
|
---|
[338] | 12 | *
|
---|
| 13 | * @author Tim Baarslag
|
---|
| 14 | *
|
---|
[315] | 15 | */
|
---|
| 16 | public class UF_CloseToQuantity implements UtilityFunction
|
---|
| 17 | {
|
---|
| 18 | private WishList wishlist;
|
---|
| 19 |
|
---|
| 20 | public UF_CloseToQuantity(WishList w)
|
---|
| 21 | {
|
---|
| 22 | this.wishlist = w;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | @Override
|
---|
| 27 | public Double getUtility(Bundle b)
|
---|
| 28 | {
|
---|
| 29 | int totalDeviation = 0;
|
---|
| 30 | for (Chip c : wishlist)
|
---|
| 31 | {
|
---|
| 32 | int desired = wishlist.getQuantity(c);
|
---|
| 33 | Integer offered = b.getQuantity(c);
|
---|
| 34 | if (offered == null)
|
---|
| 35 | offered = 0;
|
---|
| 36 |
|
---|
| 37 | int deviationOffered = Math.abs(desired - offered);
|
---|
| 38 | totalDeviation += deviationOffered;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return 1.0 / (1 + totalDeviation);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | @Override
|
---|
| 45 | public String toString()
|
---|
| 46 | {
|
---|
| 47 | return this.getClass().getSimpleName() + ": " + wishlist.toString();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public static void main(String[] args)
|
---|
| 51 | {
|
---|
| 52 | WishList wishlist = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
|
---|
| 53 | UF_CloseToQuantity u = new UF_CloseToQuantity(wishlist);
|
---|
| 54 | System.out.println(u);
|
---|
| 55 |
|
---|
| 56 | Bundle offer = new BundleBuilder()
|
---|
| 57 | .addStack("Red", 1.0, 6)
|
---|
| 58 | .addStack("Green", 3.0, 15)
|
---|
| 59 | .addStack("Purple", 0.10, 10)
|
---|
[338] | 60 | //---
|
---|
| 61 | // .addStack("Green", 2.0, 3)
|
---|
| 62 | // .addStack("Yellow", 5.0, 4)
|
---|
| 63 | // .addStack("Orange", 1.0, 17)
|
---|
| 64 | //---
|
---|
[339] | 65 | // .addStack("Green", 3.0, 4) //should give utility 1.0 on additive utility
|
---|
[338] | 66 | // .addStack("Yellow", 5.0, 6)
|
---|
| 67 | // .addStack("Orange", 1.0, 40)
|
---|
| 68 | //---
|
---|
[339] | 69 | // .addStack("Green", 10.0, 1) //should give utility 0.0 on additive utility
|
---|
[338] | 70 | // .addStack("Yellow", 10.0, 1)
|
---|
| 71 | // .addStack("Orange", 10.0, 1)
|
---|
| 72 | //---
|
---|
[315] | 73 | .build();
|
---|
| 74 | System.out.println(u.getUtility(offer));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | }
|
---|
| 78 |
|
---|