source: src/main/java/onetomany/bargainingchipsgame/players/utilityfunction/UF_CloseToQuantityMaxPrice.java@ 301

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

Boulware works with UF_CloseToQuantityMaxPrice

File size: 1.7 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 and price <= max.
14 * Becomes lower with higher quantity deviations.
15 */
16public 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
Note: See TracBrowser for help on using the repository browser.