source: src/main/java/bargainingchips/utilityfunctions/UF_AllOrNothing.java@ 340

Last change on this file since 340 was 340, checked in by Tim Baarslag, 4 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: 1.5 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 * If all requested quantities at the total requested price (or less) would be satisfied,
11 * then the bundle is evaluated as 1.0, otherwise as 0.0;
12 *
13 *
14 * @author Faria Nassiri-Mofakham
15 *
16 */
17public class UF_AllOrNothing implements UtilityFunction
18{
19 private WishList wishlist;
20 private double maxprice;
21
22
23 public UF_AllOrNothing(WishList w, double max)
24 {
25 this.wishlist = w;
26 this.maxprice = max;
27 }
28
29
30 @Override
31 public Double getUtility(Bundle b)
32 {
33 if (b.getTotalPrice() > maxprice)
34 return 0.0;
35
36 for (Chip c : wishlist)
37 {
38 int desired = wishlist.getQuantity(c);
39 Integer offered = b.getQuantity(c);
40
41 if (offered == null || desired != offered)
42 return 0.0;
43 }
44
45 return 1.0;
46 }
47
48 @Override
49 public String toString()
50 {
51 return this.getClass().getSimpleName() + ": " + wishlist.toString() + " for <= $" + maxprice;
52 }
53
54 public static void main(String[] args)
55 {
56 WishList wishlist = new WishListBuilder().addWish("Red", 3).addWish("Green", 2).build();
57 UF_AllOrNothing u = new UF_AllOrNothing(wishlist, 10.0);
58 System.out.println(u);
59
60 Bundle offer = new BundleBuilder()
61 .addStack("Red", 1.0, 3)
62 .addStack("Green", 3.0, 2)
63 .addStack("Purple", 0.10, 10)
64 .build();
65 System.out.println(u.getUtility(offer));
66 }
67
68}
69
Note: See TracBrowser for help on using the repository browser.