1 | /**
|
---|
2 | * UtilityFunctionAllOrNothing class
|
---|
3 | */
|
---|
4 | package onetomany.bargainingchipsgame.players.utilityfunction;
|
---|
5 |
|
---|
6 | import onetomany.bargainingchipsgame.Bundle;
|
---|
7 | import onetomany.bargainingchipsgame.BundleBuilder;
|
---|
8 | import onetomany.bargainingchipsgame.Chip;
|
---|
9 | import onetomany.bargainingchipsgame.WishList;
|
---|
10 | import onetomany.bargainingchipsgame.WishListBuilder;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Returns 1 when the wish list is exactly satisfied and price <= max.
|
---|
14 | * Becomes lower with higher quantity deviations.
|
---|
15 | */
|
---|
16 | public class UF_CloseToQuantityMaxPrice implements UtilityFunction
|
---|
17 | {
|
---|
18 | private WishList wishlist;
|
---|
19 | private double maxprice;
|
---|
20 |
|
---|
21 |
|
---|
22 | public UF_CloseToQuantityMaxPrice(WishList w, double max)
|
---|
23 | {
|
---|
24 | this.wishlist = w;
|
---|
25 | this.maxprice = max;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public Double getUtility(Bundle b)
|
---|
31 | {
|
---|
32 | if (b.getTotalPrice() > maxprice)
|
---|
33 | return 0.0;
|
---|
34 |
|
---|
35 | int totalDeviation = 0;
|
---|
36 | for (Chip c : wishlist)
|
---|
37 | {
|
---|
38 | int desired = wishlist.getQuantity(c);
|
---|
39 | Integer offered = b.getQuantity(c);
|
---|
40 | if (offered == null)
|
---|
41 | offered = 0;
|
---|
42 |
|
---|
43 | int deviationOffered = Math.abs(desired - offered);
|
---|
44 | totalDeviation+=deviationOffered;
|
---|
45 | }
|
---|
46 |
|
---|
47 | return 1.0 / (1 + totalDeviation);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public String toString()
|
---|
52 | {
|
---|
53 | return this.getClass().getSimpleName() + ": " + wishlist.toString() + " for <= $" + maxprice;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static void main(String[] args)
|
---|
57 | {
|
---|
58 | WishList wishlist = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
|
---|
59 | UF_CloseToQuantityMaxPrice u = new UF_CloseToQuantityMaxPrice(wishlist, 100.0);
|
---|
60 | System.out.println(u);
|
---|
61 |
|
---|
62 | Bundle offer = new BundleBuilder()
|
---|
63 | .addStack("Red", 1.0, 6)
|
---|
64 | .addStack("Green", 3.0, 5)
|
---|
65 | .addStack("Purple", 0.10, 10)
|
---|
66 | .build();
|
---|
67 | System.out.println(u.getUtility(offer));
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|