source: src/main/java/bargainingchips/utilityfunctions/UF_CloseToQuantity.java@ 330

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

new packages complete

File size: 1.5 KB
Line 
1package bargainingchips.utilityfunctions;
2
3import bargainingchips.Bundle;
4import bargainingchips.BundleBuilder;
5import bargainingchips.Chip;
6import bargainingchips.WishList;
7import bargainingchips.WishListBuilder;
8
9/**
10 * Utility function for testing that acts on queantity alone.
11 * Returns 1 when the wish list is exactly satisfied. Becomes lower with higher quantity deviations.
12 */
13public class UF_CloseToQuantity implements UtilityFunction
14{
15 private WishList wishlist;
16
17 public UF_CloseToQuantity(WishList w)
18 {
19 this.wishlist = w;
20 }
21
22
23 @Override
24 public Double getUtility(Bundle b)
25 {
26 int totalDeviation = 0;
27 for (Chip c : wishlist)
28 {
29 int desired = wishlist.getQuantity(c);
30 Integer offered = b.getQuantity(c);
31 if (offered == null)
32 offered = 0;
33
34 int deviationOffered = Math.abs(desired - offered);
35 totalDeviation += deviationOffered;
36 }
37
38 return 1.0 / (1 + totalDeviation);
39 }
40
41 @Override
42 public String toString()
43 {
44 return this.getClass().getSimpleName() + ": " + wishlist.toString();
45 }
46
47 public static void main(String[] args)
48 {
49 WishList wishlist = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
50 UF_CloseToQuantity u = new UF_CloseToQuantity(wishlist);
51 System.out.println(u);
52
53 Bundle offer = new BundleBuilder()
54 .addStack("Red", 1.0, 6)
55 .addStack("Green", 3.0, 15)
56 .addStack("Purple", 0.10, 10)
57 .build();
58 System.out.println(u.getUtility(offer));
59 }
60
61}
62
Note: See TracBrowser for help on using the repository browser.