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

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

Change BilateralProtocol loop to avoid busy wait; note that this fixes only a part of the busy wait problem.

Package structure now in line with latest Acumex discussions.

Pinpointed error avoidance in Agent.

File size: 2.0 KB
Line 
1package bargainingchips.utilityfunctions;
2
3import bargainingchips.Bundle;
4import bargainingchips.BundleBuilder;
5import bargainingchips.Chip;
6import bargainingchips.wishlist.WishList;
7import bargainingchips.wishlist.WishListBuilder;
8
9/**
10 * Utility function for testing that acts on quantity alone.
11 * Returns 1 when the wish list is exactly satisfied. Becomes lower with higher quantity deviations.
12 *
13 * @author Tim Baarslag
14 *
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//---
61// .addStack("Green", 2.0, 3)
62// .addStack("Yellow", 5.0, 4)
63// .addStack("Orange", 1.0, 17)
64//---
65// .addStack("Green", 3.0, 4) //should give utility 1.0 on additive utility
66// .addStack("Yellow", 5.0, 6)
67// .addStack("Orange", 1.0, 40)
68//---
69// .addStack("Green", 10.0, 1) //should give utility 0.0 on additive utility
70// .addStack("Yellow", 10.0, 1)
71// .addStack("Orange", 10.0, 1)
72//---
73 .build();
74 System.out.println(u.getUtility(offer));
75 }
76
77}
78
Note: See TracBrowser for help on using the repository browser.