source: src/main/java/onetomany/bargainingchipsgame/players/utilityfunction/UF_AllOrNothing.java@ 290

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

Logging

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 * If all requested quantities at the total requested price (or less) would be satisfied,
14 * then the bundle is evaluated as 1.0, otherwise as 0.0;
15 */
16public class UF_AllOrNothing implements UtilityFunction
17{
18 private WishList wishlist;
19 private double maxprice;
20
21
22 public UF_AllOrNothing(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 for (Chip c : wishlist)
36 {
37 int desired = wishlist.getQuantity(c);
38 Integer offered = b.getQuantity(c);
39
40 if (offered == null || desired != offered)
41 return 0.0;
42 }
43
44 return 1.0;
45 }
46
47 @Override
48 public String toString()
49 {
50 return this.getClass().getSimpleName() + ": " + wishlist.toString() + " for <= $" + maxprice;
51 }
52
53 public static void main(String[] args)
54 {
55 WishList wishlist = new WishListBuilder().addWish("Red", 3).addWish("Green", 2).build();
56 UF_AllOrNothing u = new UF_AllOrNothing(wishlist, 10.0);
57 System.out.println(u);
58
59 Bundle offer = new BundleBuilder()
60 .addStack("Red", 1.0, 3)
61 .addStack("Green", 3.0, 2)
62 .addStack("Purple", 0.10, 10)
63 .build();
64 System.out.println(u.getUtility(offer));
65 }
66
67}
68
Note: See TracBrowser for help on using the repository browser.