source: src/main/java/onetomany/bargainingchipsgame/players/utilityfunction/UF_CloseToQuantity.java@ 313

Last change on this file since 313 was 304, checked in by Tim Baarslag, 5 years ago

UFs

File size: 1.6 KB
Line 
1/**
2 * UtilityFunctionAllOrNothing class
3 */
4package onetomany.bargainingchipsgame.players.utilityfunction;
5
6import onetomany.bargainingchipsgame.Bundle;
7import onetomany.bargainingchipsgame.BundleBuilder;
8import onetomany.bargainingchipsgame.Chip;
9import onetomany.bargainingchipsgame.WishList;
10import onetomany.bargainingchipsgame.WishListBuilder;
11
12/**
13 * Returns 1 when the wish list is exactly satisfied.
14 * Becomes lower with higher quantity deviations.
15 */
16public 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)
60 .build();
61 System.out.println(u.getUtility(offer));
62 }
63
64}
65
Note: See TracBrowser for help on using the repository browser.