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.
|
---|
14 | * Becomes lower with higher quantity deviations.
|
---|
15 | */
|
---|
16 | public class UF_CloseToQuantity implements UtilityFunction
|
---|
17 | {
|
---|
18 | private WishList wishlist;
|
---|
19 |
|
---|
20 |
|
---|
21 | public UF_CloseToQuantity(WishList w)
|
---|
22 | {
|
---|
23 | this.wishlist = w;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Double getUtility(Bundle b)
|
---|
29 | {
|
---|
30 | int totalDeviation = 0;
|
---|
31 | for (Chip c : wishlist)
|
---|
32 | {
|
---|
33 | int desired = wishlist.getQuantity(c);
|
---|
34 | Integer offered = b.getQuantity(c);
|
---|
35 | if (offered == null)
|
---|
36 | offered = 0;
|
---|
37 |
|
---|
38 | int deviationOffered = Math.abs(desired - offered);
|
---|
39 | totalDeviation += deviationOffered;
|
---|
40 | }
|
---|
41 |
|
---|
42 | return 1.0 / (1 + totalDeviation);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public String toString()
|
---|
47 | {
|
---|
48 | return this.getClass().getSimpleName() + ": " + wishlist.toString();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static void main(String[] args)
|
---|
52 | {
|
---|
53 | WishList wishlist = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
|
---|
54 | UF_CloseToQuantity u = new UF_CloseToQuantity(wishlist);
|
---|
55 | System.out.println(u);
|
---|
56 |
|
---|
57 | Bundle offer = new BundleBuilder()
|
---|
58 | .addStack("Red", 1.0, 7)
|
---|
59 | .addStack("Green", 3.0, 5)
|
---|
60 | .addStack("Purple", 0.10, 10)
|
---|
61 | .build();
|
---|
62 | System.out.println(u.getUtility(offer));
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|