source: src/main/java/bargainingchips/utilityfunctions/UF_IntensifiedLessPriceCloseToQuantity.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: 7.7 KB
RevLine 
[338]1/**
2 *
3 */
4package bargainingchips.utilityfunctions;
5
6import bargainingchips.Bundle;
7import bargainingchips.BundleBuilder;
8import bargainingchips.Chip;
9import bargainingchips.ChipIssueValue;
10import bargainingchips.ChipIssueValueBuilder;
[340]11import bargainingchips.wishlist.WishList;
12import bargainingchips.wishlist.WishListBuilder;
[338]13
14/**
15 * This utility function evaluates the utility of an offer based on its separate utilities regarding the ratio of its total price {@link UF_LessPrice}
16 * with respect to the break even prices as well as the total deviation of the chips quantities {@link UF_CloseToQuantity} by considering the importance of price vs. quantity.
17 *
18 * To this end, this function adapts the `Intensification' concept in Fuzzy logic [1] depending the value (below or above 0.5) of the above mentioned separate utilities.
[339]19 * The values below 0.5 are more decreased (using 2*value^2); however, the values above 0.5 are more increased (using 1-2*(1-value)^2).
[338]20 *
21 * And, if price is more important than quantity, but the price utility has been below 0.5, then the respected total utility would also be decreased; otherwise, it would be increased.
22 * The similar scenario would hold for the quantity.
23 *
24 *
25 * [1] Lotfi-Ali Asgar-Zadeh, Fuzzy sets, Information and control 8 (3), 338-353 (1965).
26 *
27 *
28 * @author Faria Nassiri-Mofakham
29 *
30 */
31public class UF_IntensifiedLessPriceCloseToQuantity implements UtilityFunction {
32
33 private WishList wishlist;
34 private ChipIssueValue<Double> breakEvenPrices;
[339]35 private boolean priceVSquantity; // true, if `less price' is more important;
36 // false, if `close to quantity' is important.
[338]37
38
39 public UF_IntensifiedLessPriceCloseToQuantity(WishList w, ChipIssueValue<Double> bEP, boolean pVSq)
40 {
41 this.wishlist = w;
42 this.breakEvenPrices = bEP;
43 this.priceVSquantity = pVSq;
44 }
45
46
47 @Override
48 public Double getUtility(Bundle b)
49 {
50 int totalDeviationQ = 0;
51 double desiredP=0.0;
52 for (Chip c : wishlist)
53 {
54 int desiredQ = wishlist.getQuantity(c);
55 Integer offeredQ = b.getQuantity(c);
56 if (offeredQ == null)
57 offeredQ = 0;
58
59 int deviationOfferedQ = Math.abs(desiredQ - offeredQ);
60 totalDeviationQ += deviationOfferedQ;
61
62 desiredP += ((c!=null) ? breakEvenPrices.getUnitValue(c) : 0.0);
63 }
64 double offeredP = ((b!=null) ? b.getTotalPrice() : 0.0);
65 if (offeredP > desiredP)
66 {
67 //System.out.println("offeredPrice "+ offeredP + " > desiredPrice "+ desiredP);
68 return 0.0;
69 }
70 double uP = 1.0 - offeredP/desiredP;
71 double uQ = 1.0 / (1 + totalDeviationQ);
72
73 //System.out.println("uP "+uP+"\n"+"uQ "+uQ);
74
75 double u = (
76 priceVSquantity ?
77 ( (uP < 0.5) ?
78 ( (uQ < 0.5) ?
79 Math.pow(2*Math.pow(uP, 2), 1-uQ)
80 :
81 Math.pow(1-2*Math.pow(1-uP, 2), uQ)
82 )
83 :
84 Math.pow(1-2*Math.pow(1-uP, 2), 1-uQ)
85 )
86 :
87 ( (uQ < 0.5) ?
88 ( (uP < 0.5) ?
89 Math.pow(2*Math.pow(uQ, 2), 1-uP)
90 :
91 Math.pow(1-2*Math.pow(1-uQ, 2), uP)
92 )
93 :
94 Math.pow(1-2*Math.pow(1-uQ, 2), 1-uP)
95 )
96 );
97 return (Double.isNaN(u) ? 0.0 : u);
98 }
99
100 @Override
101 public String toString()
102 {
103 return this.getClass().getSimpleName() + ": WishList " + wishlist.toString() + ": BreakEvenPrices "+ breakEvenPrices.toString()+ ": Price Vs. Quantity "+ (priceVSquantity? " `Price'" : " `Quantity'");
104 }
105
106 public static void main(String[] args)
107 {
108 //boolean pq=true;
109
110 WishList wishlist1 = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
111 ChipIssueValue<Double> prices1 = new ChipIssueValueBuilder<Double>().addIssue("Red", 100.0).addIssue("Green", 200.0).addIssue("Blue", 700.0).build();
112
113 UF_IntensifiedLessPriceCloseToQuantity u11 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist1, prices1, true);
114 UF_IntensifiedLessPriceCloseToQuantity u12 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist1, prices1, false);
115
116
117
118 WishList wishlist2 = new WishListBuilder().addWish("Red", 7).addWish("Green", 5).build();
119 ChipIssueValue<Double> prices2 = new ChipIssueValueBuilder<Double>().addIssue("Red", 10.0).addIssue("Green", 20.0).addIssue("Blue", 70.0).build();
120
121 UF_IntensifiedLessPriceCloseToQuantity u21 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist2, prices2, true);
122 UF_IntensifiedLessPriceCloseToQuantity u22 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist2, prices2, false);
123
124
125
126 WishList wishlist3 = new WishListBuilder().addWish("Green", 4).addWish("Yellow", 6).addWish("Orange", 40).build();
127 ChipIssueValue<Double> prices3 = new ChipIssueValueBuilder<Double>().addIssue("Green", 3.0).addIssue("Yellow", 5.0).addIssue("Orange", 1.0).build();
128
129 UF_IntensifiedLessPriceCloseToQuantity u31 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist3, prices3, true);
130 UF_IntensifiedLessPriceCloseToQuantity u32 = new UF_IntensifiedLessPriceCloseToQuantity(wishlist3, prices3, false);
131
132
133
134 Bundle offer1 = new BundleBuilder()
135 .addStack("Red", 1.0, 6)
136 .addStack("Green", 3.0, 15)
137 .addStack("Purple", 0.10, 10)
138 .build();
139
140 Bundle offer2 = new BundleBuilder()
141 .addStack("Red", 1.0, 1)
142 .addStack("Green", 3.0, 1)
143 .addStack("Purple", 0.10, 1)
144 .build();
145
146 Bundle offer3 = new BundleBuilder()
147 // .addStack("Green", 2.0, 3)
148 // .addStack("Yellow", 5.0, 4)
149 // .addStack("Orange", 1.0, 17)
150 //---
151 .addStack("Green", 3.0, 4)
152 .addStack("Yellow", 5.0, 6)
153 .addStack("Orange", 1.0, 40)
154 //---
155 // .addStack("Green", 10.0, 1)
156 // .addStack("Yellow", 10.0, 1)
157 // .addStack("Orange", 10.0, 1)
158 .build();
159
160 System.out.println("\nu11 : "+u11 +"\nOffer1 = "+offer1);
161 System.out.println("=> "+u11.getUtility(offer1));
162 System.out.println("\nu11 : "+u11 +"\nOffer2 = "+offer2);
163 System.out.println("=> "+u11.getUtility(offer2));
164 System.out.println("\nu11 : "+u11 +"\nOffer3 = "+offer3);
165 System.out.println("=> "+u11.getUtility(offer3));
166 System.out.println("\nu12 : "+u12 +"\nOffer1 = "+offer1);
167 System.out.println("=> "+u12.getUtility(offer1));
168 System.out.println("\nu12 : "+u12 +"\nOffer2 = "+offer2);
169 System.out.println("=> "+u12.getUtility(offer2));
170 System.out.println("\nu12 : "+u12 +"\nOffer3 = "+offer3);
171 System.out.println("=> "+u12.getUtility(offer3));
172
173 System.out.println("\nu21 : "+u21 +"\nOffer1 = "+offer1);
174 System.out.println("=> "+u21.getUtility(offer1));
175 System.out.println("\nu21 : "+u21 +"\nOffer2 = "+offer2);
176 System.out.println("=> "+u21.getUtility(offer2));
177 System.out.println("\nu21 : "+u21 +"\nOffer3 = "+offer3);
178 System.out.println("=> "+u21.getUtility(offer3));
179 System.out.println("\nu22 : "+u22 +"\nOffer1 = "+offer1);
180 System.out.println("=> "+u22.getUtility(offer1));
181 System.out.println("\nu22 : "+u22 +"\nOffer2 = "+offer2);
182 System.out.println("=> "+u22.getUtility(offer2));
183 System.out.println("\nu22 : "+u22 +"\nOffer3 = "+offer3);
184 System.out.println("=> "+u22.getUtility(offer3));
185
186 System.out.println("\nu31 : "+u31 +"\nOffer1 = "+offer1);
187 System.out.println("=> "+u31.getUtility(offer1));
188 System.out.println("\nu31 : "+u31 +"\nOffer2 = "+offer2);
189 System.out.println("=> "+u31.getUtility(offer2));
190 System.out.println("\nu31 : "+u31 +"\nOffer3 = "+offer3);
191 System.out.println("=> "+u31.getUtility(offer3));
192 System.out.println("\nu32 : "+u32 +"\nOffer1 = "+offer1);
193 System.out.println("=> "+u32.getUtility(offer1));
194 System.out.println("\nu32 : "+u32 +"\nOffer2 = "+offer2);
195 System.out.println("=> "+u32.getUtility(offer2));
196 System.out.println("\nu32 : "+u32 +"\nOffer3 = "+offer3);
197 System.out.println("=> "+u32.getUtility(offer3));
198
199 }
200
201}
Note: See TracBrowser for help on using the repository browser.