1 | /**
|
---|
2 | *
|
---|
3 | */
|
---|
4 | package bargainingchips.players;
|
---|
5 |
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.concurrent.BlockingQueue;
|
---|
10 | import java.util.concurrent.LinkedBlockingQueue;
|
---|
11 |
|
---|
12 | import bargainingchips.ChipIssueValue;
|
---|
13 | import bargainingchips.ChipIssueValueBuilder;
|
---|
14 | import bargainingchips.NegotiationContext;
|
---|
15 | import bargainingchips.actions.Offer;
|
---|
16 | import bargainingchips.actions.OfferBy;
|
---|
17 | import bargainingchips.messaging.coordination.CoordinationMessage;
|
---|
18 | import bargainingchips.messaging.status.StatusMessage;
|
---|
19 | import bargainingchips.protocol.AsynchronousOffersProtocol;
|
---|
20 | import bargainingchips.utilityfunctions.UtilityFunction;
|
---|
21 | import bargainingchips.wishlist.WishList;
|
---|
22 | import bargainingchips.wishlist.WishListBuilder;
|
---|
23 | //import bargainingchips.utilityfunctions.UF;
|
---|
24 | import bargainingchips.utilityfunctions.UF_BezierPriceBezierQuantity;
|
---|
25 | import bargainingchips.utilityfunctions.UF_BezierPriceGaussianQuantity;
|
---|
26 | import bargainingchips.utilityfunctions.UF_CloseToQuantity;
|
---|
27 | import bargainingchips.utilityfunctions.UF_IntensifiedLessPriceCloseToQuantity;
|
---|
28 | import bargainingchips.utilityfunctions.UF_LessPrice;
|
---|
29 | import bargainingchips.utilityfunctions.UF_LessPriceCloseToQuantity;
|
---|
30 | import bargainingchips.utilityfunctions.UF_PeakedFlatCurvePriceGaussianQuantity;
|
---|
31 | import bargainingchips.utilityfunctions.UF_PeakedFlatPricePeakedFlatQuantity;
|
---|
32 | import bargainingchips.utilityfunctions.UF_PeakedPricePeakedQuantity;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * The {@link Buyer} class is extended in a way that the buyer negotiators could be created by using different random utility functions among
|
---|
36 | * UF_CloseToQuantity, UF_LessPrice, UF_LessPriceCloseToQuantity, UF_IntensifiedLessPriceCloseToQuantity, UF_PeakedPricePeakedQuantity,
|
---|
37 | * UF_PeakedFlatPricePeakedFlatQuantity, UF_PeakedFlatCurvePriceGaussianQuantity, UF_BezierPriceBezierQuantity, and
|
---|
38 | * UF_BezierPriceGaussianQuantity, where the first function takes only quantity into account,
|
---|
39 | * the second function considers only price, but all the others deal with both price and quantity, receive different numbers of parameters,
|
---|
40 | * and consider the importance of issues (i.e., for price, quantity, etc.) as well as the importance of Chips (i.e., colors) with respect to each other.
|
---|
41 | * It also created buyer negotiators using either Boulware or Bargaining (based on NiceTitForTat) negotiation strategies.
|
---|
42 | *
|
---|
43 | *
|
---|
44 | * @author Faria Nassiri-Mofakham
|
---|
45 | *
|
---|
46 | */
|
---|
47 |
|
---|
48 |
|
---|
49 | public class BargainingBuyer {
|
---|
50 |
|
---|
51 | private Coordinator c;
|
---|
52 | private BlockingQueue<CoordinationMessage> cin;
|
---|
53 | private BlockingQueue<StatusMessage> cout;
|
---|
54 |
|
---|
55 | private List<NegotiationThread> threads;
|
---|
56 |
|
---|
57 | public BargainingBuyer(WishList overallWishlist)//, ChipIssueValue<Double> breakEvenPrices, boolean priceVSQuantity)
|
---|
58 | {
|
---|
59 | cin = new LinkedBlockingQueue<CoordinationMessage>();
|
---|
60 | cout = new LinkedBlockingQueue<StatusMessage>();
|
---|
61 |
|
---|
62 | // Coordinator
|
---|
63 | c = new Coordinator(overallWishlist, cout, cin);
|
---|
64 |
|
---|
65 | threads = new ArrayList<NegotiationThread>();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void connectSeller(String sellerName, WishList wishlistSam)
|
---|
69 | {
|
---|
70 | // Set up the protocol
|
---|
71 | BlockingQueue<OfferBy> from = new LinkedBlockingQueue<OfferBy>();
|
---|
72 | BlockingQueue<Offer> toBuyer = new LinkedBlockingQueue<Offer>();
|
---|
73 | BlockingQueue<Offer> toSeller = new LinkedBlockingQueue<Offer>();
|
---|
74 |
|
---|
75 | String bobiName = "Bob" + " " + getThreadNumber();
|
---|
76 |
|
---|
77 | AsynchronousOffersProtocol aop = new AsynchronousOffersProtocol(from, bobiName, toBuyer, sellerName, toSeller);
|
---|
78 |
|
---|
79 | NegotiationContext context = new NegotiationContext();
|
---|
80 |
|
---|
81 | // Make a new subnegotiator
|
---|
82 | WishList wishlist = new WishListBuilder().addWish("Green", 4).addWish("Yellow", 6).addWish("Orange", 40).build();
|
---|
83 | ChipIssueValue<Double> breakEvenPrices = new ChipIssueValueBuilder<Double>().addIssue("Green", 3.0).addIssue("Yellow", 5.0).addIssue("Orange", 1.0).build();
|
---|
84 | ChipIssueValue<Double[]> prices1 = new ChipIssueValueBuilder<Double[]>().addIssue("Green", new Double[] {3.0, 4.8}).addIssue("Yellow", new Double[] {5.0, 8.0}).addIssue("Orange", new Double[] {1.0, 2.5}).build();
|
---|
85 | ChipIssueValue<Double> uPrices1 = new ChipIssueValueBuilder<Double>().addIssue("Green", 1.0).addIssue("Yellow", 0.8).addIssue("Orange", 0.75).build();
|
---|
86 | ChipIssueValue<Integer> deviations1 = new ChipIssueValueBuilder<Integer>().addIssue("Green", 1).addIssue("Yellow", 1).addIssue("Orange", 10).build();
|
---|
87 | ChipIssueValue<Double[]> prices2 = new ChipIssueValueBuilder<Double[]>().addIssue("Green", new Double[] {3.0, 3.5, 4.8, 5.0}).addIssue("Yellow", new Double[] {5.0, 5.2, 6.2, 8.0}).addIssue("Orange", new Double[] {1.0, 1.8, 2.5, 3.0}).build();
|
---|
88 | ChipIssueValue<Double[]> uPrices2 = new ChipIssueValueBuilder<Double[]>().addIssue("Green", new Double[] {0.7, 0.2}).addIssue("Yellow", new Double[] {0.8, 0.1}).addIssue("Orange", new Double[] {0.9, 0.3}).build();
|
---|
89 | ChipIssueValue<Double[]> deviations2 = new ChipIssueValueBuilder<Double[]>().addIssue("Green", new Double[] {100.0, 20.0, 50.0, 500.0}).addIssue("Yellow", new Double[] {50.0, 30.0, 30.0, 50.0}).addIssue("Orange", new Double[] {200.0, 40.0, 60.0, 1000.0}).build();
|
---|
90 | ChipIssueValue<Double[]> uDev2 = new ChipIssueValueBuilder<Double[]>().addIssue("Green", new Double[] {0.25, 0.8, 0.9, 0.1}).addIssue("Yellow", new Double[] {0.3, 0.7, 0.7, 0.3}).addIssue("Orange", new Double[] {0.15, 0.95, 0.85, 0.3}).build();
|
---|
91 | ChipIssueValue<Integer[]> deviations3 = new ChipIssueValueBuilder<Integer[]>().addIssue("Green", new Integer[] {2,1,2,3}).addIssue("Yellow", new Integer[] {3,2,2,3}).addIssue("Orange", new Integer[] {10,2,5,10}).build();
|
---|
92 | ChipIssueValue<Double> wC = new ChipIssueValueBuilder<Double>().addIssue("Green", 0.5).addIssue("Yellow", 0.3).addIssue("Orange", 0.2).build();
|
---|
93 | double wP = 0.6;
|
---|
94 | double wQ = 0.4;
|
---|
95 | boolean pq = ( (wP > wQ) ? true : false); // true, if less price is more important; false, if close to quantity is important.
|
---|
96 | // boolean pq = ((new Random().nextInt(2)+1 == 1) ? true : false);
|
---|
97 | int m=4; // input Bezier points
|
---|
98 |
|
---|
99 | //One utility function is assigned to bobi, in random
|
---|
100 | // UtilityFunction u = new UF(wishlist, breakEvenPrices, prices1, uPrices1, deviations1, uPrices2, prices2, deviations2, uDev2, deviations3, wC, wP, wQ, pq, m );
|
---|
101 | UtilityFunction u = null;
|
---|
102 | switch(new Random().nextInt(9)+1)
|
---|
103 | {
|
---|
104 | case 1: u = new UF_CloseToQuantity(wishlist); break;
|
---|
105 | case 2: u = new UF_LessPrice(wishlist, breakEvenPrices); break;
|
---|
106 | case 3: u = new UF_LessPriceCloseToQuantity(wishlist, breakEvenPrices); break;
|
---|
107 | case 4: u = new UF_IntensifiedLessPriceCloseToQuantity(wishlist, breakEvenPrices, pq); break;
|
---|
108 | case 5: u = new UF_PeakedPricePeakedQuantity(wishlist, breakEvenPrices, deviations1, wC, wP, wQ); break;
|
---|
109 | case 6: u = new UF_PeakedFlatPricePeakedFlatQuantity(wishlist, deviations2, uDev2, prices2, uPrices2, wC, wP, wQ); break;
|
---|
110 | case 7: u = new UF_PeakedFlatCurvePriceGaussianQuantity<Double>(wishlist, prices1, uPrices1, deviations1, wC, wP, wQ); break;
|
---|
111 | case 8: u = new UF_BezierPriceBezierQuantity<Double> (wishlist, m, prices2, deviations3, wC, wP, wQ); break;
|
---|
112 | case 9: u = new UF_BezierPriceGaussianQuantity<Double> (wishlist, m, prices2, deviations1, wC, wP, wQ); break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Agent bobi = ((new Random().nextInt(2)+1 == 1) ?
|
---|
116 | new BoulwareAgent(bobiName, u, context, toBuyer, from, cin, cout) :
|
---|
117 | new BargainingAgent(bobiName, u, context, toBuyer, from, cin, cout));
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 | // Seller
|
---|
122 | UtilityFunction uSam = new UF_CloseToQuantity(wishlistSam);
|
---|
123 | Agent sam = new BasicAgent(sellerName, uSam, context, toSeller, from);
|
---|
124 |
|
---|
125 | // The thread
|
---|
126 | NegotiationThread thread = new NegotiationThread();
|
---|
127 | thread.protocol = aop;
|
---|
128 | thread.subbuyer = bobi;
|
---|
129 | thread.seller = sam;
|
---|
130 |
|
---|
131 | threads.add(thread);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void startThreads()
|
---|
135 | {
|
---|
136 | // Start the coordinator once
|
---|
137 | Thread threadCoordinator = new Thread(c);
|
---|
138 | threadCoordinator.start();
|
---|
139 |
|
---|
140 | // Start all threads: subnegotiator + protocol + seller
|
---|
141 | for (NegotiationThread t : threads)
|
---|
142 | {
|
---|
143 | System.out.println(t.subbuyer.toDescription());
|
---|
144 | System.out.println("playing vs");
|
---|
145 | System.out.println(t.seller.toDescription());
|
---|
146 |
|
---|
147 | Thread aopThread = new Thread(t.protocol);
|
---|
148 | aopThread.start();
|
---|
149 |
|
---|
150 | Thread threadBuyer = new Thread(t.subbuyer);
|
---|
151 | threadBuyer.start();
|
---|
152 |
|
---|
153 | Thread threadSeller = new Thread(t.seller);
|
---|
154 | threadSeller.start();
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | private int getThreadNumber()
|
---|
160 | {
|
---|
161 | return threads.size();
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|