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

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

UF_CloseToQuantity

Accept(Offer agreement)

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
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
Note: See TracBrowser for help on using the repository browser.