source: src/main/java/agents/FuzzyAgent.java@ 1

Last change on this file since 1 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 31.3 KB
Line 
1package agents;
2
3import java.util.HashMap;
4import java.util.Random;
5
6import genius.core.Agent;
7import genius.core.Bid;
8import genius.core.SupportedNegotiationSetting;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.Offer;
12import genius.core.issue.Value;
13import genius.core.issue.ValueReal;
14
15public class FuzzyAgent extends Agent {
16
17 private static int deadline = 100;
18 private static int deadlineB = 100;
19 private static int deadlineS = 100;
20 private static double[][] BuyOffer;
21 private static double[][] SellOffer;
22 private static int tacticB;
23 private static int tacticS;
24
25 // private static int tacticBStretch,tacticSStretch;
26 private static double K = 0.1;
27 private static double MaxB = 40;
28 private static double MinB = 10;
29 // private static double MinB2=10;
30 private static double MaxS = 40;
31 // private static double MaxS2=40;
32 private static double MinS = 10;
33
34 private static boolean deal = false;
35 private static double Utl = 0;
36 private static double BBuy, BSell;
37 // parameters for stretch 260307
38 private static double BBuyStretch, BSellStretch;
39 private static int LBuy, LSell;
40 // private static int LBuyStretch,LSellStretch;
41 private static int iteration = 1;
42 // private static double constant=0.05;
43 private static int MBuy, MSell;
44 // private static int MBuyStretch,MSellStretch;
45 // private static boolean dealB=false;
46 // private static boolean dealS=false;
47 private static double AcceptedValue = 0;
48 // for counting rounds in resource dependat
49 private static int itercount = 0;
50 // The stretch constant
51 private static double Scons, Bcons;
52 private static boolean CBuy = false;
53 private static boolean CSell = false;
54 // private static double[][] Distance;
55 // private static double DST;
56 private static double[] dist;
57 private static double percentageB1, percentageB2, percentageS1,
58 percentageS2;
59 // A threshhold parameter which determines when we should and should not
60 // accept or propose crisp 230407
61 private static double threshholdB, threshholdS;
62
63 private static final int BUYER = 1;
64 private static final int SELLER = 2;
65 private int fPlayingFor;
66 private int fRound;
67 private Bid lastReceivedOffer;
68
69 @Override
70 public Action chooseAction() {
71 double lNextBidValue = 0;
72 Action lAction = null;
73
74 switch (fPlayingFor) {
75 case BUYER:
76 deal = Buyer(fRound);
77 lNextBidValue = BuyOffer[fRound][0];
78 break;
79
80 case SELLER:
81 deal = Seller(fRound);
82 lNextBidValue = SellOffer[fRound][0];
83 break;
84 }
85 if (deal)
86 lAction = new Accept(getAgentID(), lastReceivedOffer);
87
88 else {
89 HashMap<Integer, Value> lValues = new HashMap<Integer, Value>();
90 ValueReal lValue = new ValueReal(lNextBidValue);
91 lValues.put(utilitySpace.getDomain().getIssues().get(0).getNumber(),
92 lValue);
93 try {
94 Bid lBid = new Bid(utilitySpace.getDomain(), lValues);
95 lAction = new Offer(getAgentID(), lBid);
96 } catch (Exception e) {
97 e.printStackTrace();
98 }
99
100 }
101 return lAction;
102 }
103
104 @Override
105 public String getVersion() {
106 return "1.0";
107 }
108
109 @Override
110 public void init() {
111
112 fRound = 0;
113 dist = new double[deadline + 1];
114 BuyOffer = new double[deadlineB + 1][2];
115 SellOffer = new double[deadlineS + 1][2];
116 BuyOffer[0][0] = MinB;
117 SellOffer[0][0] = MaxS;
118 BuyOffer[0][1] = BuyOffer[0][0] + (percentageB1 * Bcons);
119 SellOffer[0][1] = SellOffer[0][0] - (percentageS1 * Scons);
120 if (getName().equals("Seller"))
121 fPlayingFor = BUYER;
122 else
123 fPlayingFor = SELLER;
124 tacticB = 1;
125 tacticS = 1;
126 BBuy = 0.01;
127 LBuy = 2;
128 MBuy = 2;
129 BSell = 50;
130 LSell = 2;
131 MSell = 2;
132 Bcons = 1;
133 Scons = 1;
134 BBuyStretch = 0.1;
135 BSellStretch = 0.1;
136 // Initializing the value of the threshold for the first offers
137 threshholdB = ThreshFind(deadlineB, 0, BuyOffer[0][0], BuyOffer[0][1]);
138 threshholdS = ThreshFind(deadlineS, 0, SellOffer[0][0],
139 SellOffer[0][1]);
140
141 }
142
143 @Override
144 public void ReceiveMessage(Action opponentAction) {
145 fRound++;
146 if (opponentAction instanceof Offer) {
147 Offer lOffer = (Offer) opponentAction;
148 lastReceivedOffer = lOffer.getBid();
149 switch (fPlayingFor) {
150 case BUYER:
151 try {
152 SellOffer[fRound][0] = ((ValueReal) (lastReceivedOffer
153 .getValue(utilitySpace.getDomain().getIssues()
154 .get(0).getNumber()))).getValue();
155 SellOffer[fRound][1] = SellOffer[fRound][0]
156 + (percentageB1 * Bcons);
157 } catch (Exception e) {
158 e.printStackTrace();
159 }
160 break;
161 case SELLER:
162 try {
163 BuyOffer[fRound][0] = ((ValueReal) (lastReceivedOffer
164 .getValue(utilitySpace.getDomain().getIssues()
165 .get(0).getNumber()))).getValue();
166 BuyOffer[fRound][1] = BuyOffer[fRound][0]
167 - (percentageS1 * Scons);
168 } catch (Exception e) {
169 e.printStackTrace();
170 }
171
172 break;
173 }// switch
174 } // if
175 }
176
177 public static double CalculateCost(double constant, int NoIteration) {
178 double cost;
179 cost = (java.lang.Math.exp(NoIteration * constant)
180 - java.lang.Math.exp(-NoIteration * constant))
181 / (java.lang.Math.pow(java.lang.Math.E, NoIteration * constant)
182 + java.lang.Math.pow(java.lang.Math.E,
183 -NoIteration * constant));
184 return cost;
185 }
186
187 protected static boolean Buyer(int i) {
188 double A = 0;
189 itercount++;
190 double[] inter = {};
191 switch (tacticB) {
192 case 1:
193 // If it has reached the deadline terminate negotiaiton
194 if (i > deadlineB) {
195 // System.out.println("Finished");
196 return true;
197 }
198 // if it has not reached the deadline continue negotiation
199 else {
200 Utl = (MaxB - SellOffer[i - 1][0]) / (MaxB - MinB);
201 if (i > 0) {
202 Utl = (MaxB - SellOffer[i - 1][0]) / (MaxB - MinB);
203 if (CSell == false) {
204 /*
205 * if
206 * (((double)SellOffer[i-1][0]<=(double)BuyOffer[i-1][
207 * 1]) && (i<deadlineB)) {
208 * System.out.println("Accepted");
209 * AcceptedValue=SellOffer[i-1][0]; return true; }
210 */
211 if ((SellOffer[i - 1][1] <= BuyOffer[i - 1][1])
212 && (i <= deadlineB)) {
213 double[] xParams = { SellOffer[i - 1][1],
214 SellOffer[i - 1][0], BuyOffer[i - 1][0],
215 BuyOffer[i - 1][1] };
216 if (BuyOffer[i - 1][1] == BuyOffer[i - 1][0]) {
217 CBuy = false;
218 } else {
219 double[] x = { SellOffer[i - 1][1],
220 SellOffer[i - 1][0], BuyOffer[i - 1][0],
221 BuyOffer[i - 1][1] };
222 xParams = x;
223 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
224 inter = Intersection(xParams, yParams);
225 // Checks if the the intersection pointis lower
226 // than the threshhold, if so agent will
227 // propose the crisp value
228 if (inter[0] <= threshholdB) {
229 CBuy = true;
230 } else {
231 CBuy = false;
232 }
233 }
234
235 }
236 }
237 }
238 if (BBuy == 0.01 || BBuy == 0.2) {
239 A = java.lang.Math.exp(java.lang.Math.pow(
240 1 - ((double) java.lang.Math.min(i + 1, deadlineB)
241 / (double) deadlineB),
242 (BBuy)) * java.lang.Math.log(K));
243 } else {
244 A = K + ((1 - K) * (java.lang.Math
245 .pow(((double) java.lang.Math.min(i + 1, deadlineB)
246 / (double) deadlineB), (1 / BBuy))));
247 }
248 double tmp = MinB + ((MaxB - MinB) * A);
249 double tmpUtl = (MaxB - tmp) / (MaxB - MinB);
250 if (CSell == true && i >= 2) {
251 if (SellOffer[i - 1][0] <= threshholdB) {
252 AcceptedValue = SellOffer[i - 1][0];
253 return true;
254 } else {
255 CSell = false;
256 }
257 }
258 if ((float) tmpUtl < (float) Utl && (CBuy == false)) {
259 // System.out.println("Accepted"+SellOffer[i-1][0]);
260 AcceptedValue = SellOffer[i - 1][0];
261 return true;
262 } else {
263
264 if (CBuy == true) {
265 BuyOffer[i][0] = inter[0];
266 BuyOffer[i][1] = BuyOffer[i][0];
267
268 } else {
269 BuyOffer[i][0] = tmp;
270 // code added to calculate the stretch reduction
271 if (BBuyStretch == 0.01 || BBuyStretch == 0.2) {
272 A = java.lang.Math.exp(java.lang.Math.pow(
273 1 - ((double) java.lang.Math.min(i + 1,
274 deadlineB) / (double) deadlineB),
275 (BBuyStretch)) * java.lang.Math.log(K));
276
277 } else {
278 A = K + ((1 - K) * (java.lang.Math.pow(
279 ((double) java.lang.Math.min(i + 1,
280 deadlineB) / (double) deadlineB),
281 (1 / BBuyStretch))));
282 }
283 double tmpBcons = ((percentageB2 * (Bcons))
284 + ((percentageB1 - percentageB2) * (Bcons)
285 * (1 - A)));
286
287 if (BuyOffer[i][0] + tmpBcons >= MaxB) {
288 BuyOffer[i][1] = MaxB;
289
290 } else {
291 BuyOffer[i][1] = BuyOffer[i][0] + tmpBcons;
292 }
293 }
294 threshholdB = ThreshFind(deadlineB, i + 1, BuyOffer[i][0],
295 BuyOffer[i][1]);
296 return false;
297 }
298
299 }
300
301 case 2:
302 if (i > deadlineB) {
303 // System.out.println("Finished");
304 return true;
305 } else {
306 if (i > 0) {
307 Utl = (MaxB - SellOffer[i - 1][0]) / (MaxB - MinB);
308 if (CSell == false) {
309 /*
310 * if
311 * (((double)SellOffer[i-1][0]<=(double)BuyOffer[i-1][
312 * 1]) && (i<deadlineB)) {
313 * System.out.println("Accepted");
314 * AcceptedValue=SellOffer[i-1][0]; return true; }
315 */
316
317 if ((SellOffer[i - 1][1] <= BuyOffer[i - 1][1])
318 && (i <= deadlineB)) {
319 double[] xParams = new double[4];
320 if (BuyOffer[i - 1][0] == BuyOffer[i - 1][1]) {
321 CBuy = false;
322 } else {
323 double[] x = { SellOffer[i - 1][1],
324 SellOffer[i - 1][0], BuyOffer[i - 1][0],
325 BuyOffer[i - 1][1] };
326 xParams = x;
327 CBuy = true;
328 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
329 inter = Intersection(xParams, yParams);
330 double DifUtlOurs = (MaxB - BuyOffer[i - 1][0])
331 / (MaxB - MinB);
332 double MidUtl = (MaxB - inter[0])
333 / (MaxB - MinB);
334 DifUtlOurs -= MidUtl;
335 double DifUtlOps = (MaxB - SellOffer[i - 1][0])
336 / (MaxB - MinB);
337 DifUtlOps = MidUtl - DifUtlOps;
338 // if ((float)DifUtlOurs<=(float)DifUtlOps){
339 CBuy = true;
340 MinB = inter[0];
341 // }
342 // else
343 // {
344 // CBuy=false;
345 // }
346
347 }
348 /*
349 * double[] yParams={0.0,1.0,1.0,0.0}; double[]
350 * inter={}; inter=Intersection(xParams, yParams);
351 * CBuy=true; MinB=BuyOffer[i-1][0];
352 * MaxB=SellOffer[i-1][0]; MinS=BuyOffer[i-1][0];
353 * MaxS=SellOffer[i-1][0]; BBuy=40;
354 * BuyOffer[i][0]=inter[0]; BuyOffer[i][1]=0; return
355 * false;
356 */
357 }
358 }
359 }
360 if (CSell == true && i >= 2) {
361 double DifUtlOurs = (MaxB - BuyOffer[i - 1][0])
362 / (MaxB - MinB);
363 DifUtlOurs -= Utl;
364 double DifUtlOps = (MaxB - SellOffer[i - 1][0])
365 / (MaxB - MinB);
366 DifUtlOps = Utl - DifUtlOps;
367 if ((float) DifUtlOurs <= (float) DifUtlOps) {
368 // System.out.println("Accepted the middle value");
369 AcceptedValue = SellOffer[i - 1][0];
370 return true;
371 } else {
372 CSell = false;
373 }
374 }
375 if (LBuy == 3) {
376 Random generator = new Random();
377 if (i > LBuy) {
378 // double
379 // tmp3=java.lang.Math.min(java.lang.Math.max((BuyOffer[i-1][0]+(double)SellOffer[i-1-LBuy][0]-(double)SellOffer[i-1][0]+(generator.nextInt(4))),MinB),MaxB);
380 double tmp3 = java.lang.Math.min(java.lang.Math.max(
381 (BuyOffer[i - 1][0] + SellOffer[i - 1 - LBuy][0]
382 - SellOffer[i - 1][0] + 2),
383 MinB), MaxB);
384 double tmpUtl3 = (MaxB - tmp3) / (MaxB - MinB);
385 if (((float) tmpUtl3 <= (float) Utl)
386 && (CBuy == false)) {
387 // System.out.println("Accepted");
388 AcceptedValue = SellOffer[i - 1][0];
389 return true;
390 } else {
391
392 if (CBuy == true) {
393 BuyOffer[i][0] = inter[0];
394 BuyOffer[i][1] = BuyOffer[i][0];
395 } else {
396 BuyOffer[i][0] = tmp3;
397 if (BuyOffer[i][0] + Bcons >= MaxB) {
398 BuyOffer[i][1] = MaxB;
399 } else {
400 BuyOffer[i][1] = BuyOffer[i][0] + Bcons;
401 }
402 }
403 return false;
404 }
405 } else {
406 BuyOffer[i][0] = MinB + (i * 0.5);
407 BuyOffer[i][1] = MinB + (i * 0.5) + Bcons;
408 return false;
409 }
410 } else {
411 if (i > LBuy) {
412 double tmp2 = java.lang.Math.min(
413 java.lang.Math.max((SellOffer[i - 1 - LBuy][0]
414 / SellOffer[i - 1][0])
415 * BuyOffer[i - 1][0], MinB),
416 MaxB);
417 // double
418 // tmp3=java.lang.Math.min(java.lang.Math.max((BuyOffer[i-1][0]+(double)SellOffer[i-1-LBuy][0]-(double)SellOffer[i-1][0]+2),MinB),MaxB);
419 // Utl=(double)(MaxB-SellOffer[i-1])/(double)(MaxB-MinB);
420 double tmpUtl2 = (MaxB - tmp2) / (MaxB - MinB);
421 if (((float) tmpUtl2 <= (float) Utl)
422 && (CBuy == false)) {
423 // System.out.println("Accepted");
424 AcceptedValue = SellOffer[i - 1][0];
425 return true;
426
427 } else {
428
429 if (CBuy == true) {
430 BuyOffer[i][0] = inter[0];
431 BuyOffer[i][1] = BuyOffer[i][0];
432 } else {
433 BuyOffer[i][0] = tmp2;
434 if (BuyOffer[i][0] + Bcons >= MaxB) {
435 BuyOffer[i][1] = MaxB;
436 } else {
437 BuyOffer[i][1] = BuyOffer[i][0] + Bcons;
438 }
439 }
440 return false;
441 }
442 } else {
443 BuyOffer[i][0] = MinB + ((i) * 0.5);
444 BuyOffer[i][1] = MinB + ((i) * 0.5) + Bcons;
445 return false;
446 }
447 }
448 }
449 case 3:
450 // If it has reached the termiantion condition stop negotiating
451 if (i > deadlineB) {
452 // System.out.println("Finished");
453 return true;
454
455 }
456 // if it has not reached the deadline continue negotiation
457 else {
458 if (i > 0) {
459
460 Utl = (MaxB - SellOffer[i - 1][0]) / (MaxB - MinB);
461 if (CSell == false) {
462 /*
463 * if
464 * (((double)SellOffer[i-1][0]<=(double)BuyOffer[i-1][
465 * 1]) && (i<deadlineB)) {
466 * System.out.println("Accepted");
467 * AcceptedValue=SellOffer[i-1][0]; return true; }
468 */
469
470 if ((SellOffer[i - 1][1] <= BuyOffer[i - 1][1])
471 && (i <= deadlineB)) {
472 double[] xParams = new double[4];
473 if (BuyOffer[i - 1][0] == BuyOffer[i - 1][1]) {
474 CBuy = false;
475 } else {
476 double[] x = { SellOffer[i - 1][1],
477 SellOffer[i - 1][0], BuyOffer[i - 1][0],
478 BuyOffer[i - 1][1] };
479 xParams = x;
480 CBuy = true;
481 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
482 inter = Intersection(xParams, yParams);
483 double DifUtlOurs = (MaxB - BuyOffer[i - 1][0])
484 / (MaxB - MinB);
485 double MidUtl = (MaxB - inter[0])
486 / (MaxB - MinB);
487 DifUtlOurs -= MidUtl;
488 // System.out.println(inter[0]);
489 double DifUtlOps = (MaxB - SellOffer[i - 1][0])
490 / (MaxB - MinB);
491 DifUtlOps = MidUtl - DifUtlOps;
492 // System.out.println((float)DifUtlOps);
493 // System.out.println(DifUtlOurs);
494 // if ((float)DifUtlOurs<=(float)DifUtlOps){
495 CBuy = true;
496 MinB = inter[0];
497 // }
498 // else
499 // {
500 // CBuy=false;
501 // }
502
503 }
504 /*
505 * double[]
506 * xParams={SellOffer[i-1][1],SellOffer[i-1]
507 * [0],BuyOffer[i-1][0],BuyOffer[i-1][1]}; double[]
508 * yParams={0.0,1.0,1.0,0.0}; //double[] inter={};
509 * inter=Intersection(xParams, yParams); CBuy=true;
510 * MinB=BuyOffer[i-1][0]; MaxB=SellOffer[i-1][0];
511 * MinS=BuyOffer[i-1][0]; MaxS=SellOffer[i-1][0];
512 * BBuy=40; BuyOffer[i][0]=inter[0];
513 * BuyOffer[i][1]=0; return false;
514 */
515 }
516 }
517 }
518 if (CSell == true && i >= 2) {
519 double DifUtlOurs = (MaxB - BuyOffer[i - 1][0])
520 / (MaxB - MinB);
521 DifUtlOurs -= Utl;
522 double DifUtlOps = (MaxB - SellOffer[i - 1][0])
523 / (MaxB - MinB);
524 DifUtlOps = Utl - DifUtlOps;
525 if ((float) DifUtlOurs <= (float) DifUtlOps) {
526 // System.out.println("Accepted the middle value");
527 AcceptedValue = SellOffer[i - 1][0];
528 return true;
529 } else {
530 CSell = false;
531 }
532 }
533 double resource = new Double(
534 MBuy * ((double) 1 / (double) (i + 1)));
535 A = K + ((1 - K) * (1 / (java.lang.Math.exp(resource))));
536 double tmp4 = MinB + ((MaxB - MinB) * A);
537 double tmpUtl4 = (MaxB - tmp4) / (MaxB - MinB);
538 if (((float) tmpUtl4 <= (float) Utl) && (CBuy == false)) {
539 // System.out.println("Accepted");
540 AcceptedValue = SellOffer[i - 1][0];
541 return true;
542
543 } else {
544
545 if (CBuy == true) {
546 BuyOffer[i][0] = inter[0];
547 BuyOffer[i][1] = BuyOffer[i][0];
548 } else {
549 BuyOffer[i][0] = tmp4;
550 if (BuyOffer[i][0] + Bcons >= MaxB) {
551 BuyOffer[i][1] = MaxB;
552 } else {
553 BuyOffer[i][1] = BuyOffer[i][0] + Bcons;
554 }
555 }
556 return false;
557 }
558
559 }
560 }
561 return false;
562 }
563
564 protected static boolean Seller(int i) {
565 double A = 0;
566 itercount++;
567 double[] inter = {};
568 switch (tacticS) {
569 case 1:
570 // If it has reached the deadline terminate negotiaiton
571 if (i > deadlineS) {
572 // System.out.println("Finished");
573 return true;
574 }
575 // if it has not reached the deadline continue negotiation
576 else {
577 Utl = (BuyOffer[i][0] - MinS) / (MaxS - MinS);
578 if (i > 0) {
579 if (CBuy == false) {
580 /*
581 * if
582 * (((double)SellOffer[i-1][1]<=(double)BuyOffer[i][0]
583 * )&& (i<deadlineS)) { System.out.println("Accepted");
584 * AcceptedValue=BuyOffer[i-1][0]; return true; }
585 */
586 if ((SellOffer[i - 1][1] <= BuyOffer[i][1])
587 && (i <= deadlineS)) {
588 double[] xParams = new double[4];
589 if (SellOffer[i - 1][0] == SellOffer[i - 1][1]) {
590 CSell = false;
591 } else {
592 double[] x = { SellOffer[i - 1][1],
593 SellOffer[i - 1][0], BuyOffer[i][0],
594 BuyOffer[i][1] };
595 xParams = x;
596 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
597 inter = Intersection(xParams, yParams);
598 if (inter[0] >= threshholdS) {
599 CSell = true;
600 } else {
601 CSell = false;
602 }
603 }
604
605 }
606 }
607 }
608 if (BSell == 0.01 || BSell == 0.2) {
609 A = java.lang.Math.exp(java.lang.Math.pow(
610 (1 - ((double) java.lang.Math.min(i + 1, deadlineS)
611 / (double) deadlineS)),
612 (BSell)) * java.lang.Math.log(K));
613 } else {
614 A = K + ((1 - K) * (java.lang.Math
615 .pow(((double) java.lang.Math.min(i + 1, deadlineS)
616 / (double) deadlineS), (1 / BSell))));
617 }
618 double tmp = MinS + ((MaxS - MinS) * (1 - A));
619 double tmpUtl = (tmp - MinS) / (MaxS - MinS);
620 if (CBuy == true) {
621 if (BuyOffer[i][0] >= threshholdS) {
622 AcceptedValue = BuyOffer[i][0];
623 return true;
624 } else {
625 CBuy = false;
626 }
627 }
628 if (((float) tmpUtl <= (float) Utl) && (CSell == false)) {
629 // System.out.println("Accepted");
630 AcceptedValue = BuyOffer[i][0];
631 return true;
632 } else {
633
634 if (CSell == true) {
635 SellOffer[i][0] = inter[0];
636 SellOffer[i][1] = SellOffer[i][0];
637 } else {
638 SellOffer[i][0] = tmp;
639 // code added to calculate the stretch for fuzzy offers
640 if (BSellStretch == 0.01 || BSellStretch == 0.2) {
641 A = java.lang.Math.exp(java.lang.Math.pow(
642 (1 - ((double) java.lang.Math.min(i + 1,
643 deadlineS) / (double) deadlineS)),
644 (BSellStretch)) * java.lang.Math.log(K));
645 } else {
646 A = K + ((1 - K) * (java.lang.Math.pow(
647 ((double) java.lang.Math.min(i + 1,
648 deadlineS) / (double) deadlineS),
649 (1 / BSellStretch))));
650 }
651 double tmpScons = ((percentageS2 * (Scons))
652 + ((percentageS1 - percentageS2) * (Scons)
653 * (1 - A)));
654 if ((SellOffer[i][0] - tmpScons) <= MinS) {
655 SellOffer[i][1] = MinS;
656 } else {
657 SellOffer[i][1] = SellOffer[i][0] - tmpScons;
658 }
659 }
660 threshholdS = ThreshFind(deadlineS, i + 1, SellOffer[i][0],
661 SellOffer[i][1]);
662 return false;
663 }
664 }
665 case 2:
666 if (i > deadlineS) {
667 // System.out.println("Finished");
668 return true;
669 } else {
670 Utl = (BuyOffer[i][0] - MinS) / (MaxS - MinS);
671 if (i > 0) {
672 if (CBuy == false) {
673 /*
674 * if
675 * (((double)SellOffer[i-1][1]<=(double)BuyOffer[i][0]
676 * )&& (i<deadlineS)) { System.out.println("Accepted");
677 * AcceptedValue=BuyOffer[i-1][0]; return true; }
678 */
679 if ((SellOffer[i - 1][1] <= BuyOffer[i][1])
680 && (i <= deadlineS)) {
681 double[] xParams = new double[4];
682 if (SellOffer[i - 1][0] == SellOffer[i - 1][1]) {
683 CSell = false;
684 } else {
685 double[] x = { SellOffer[i - 1][1],
686 SellOffer[i - 1][0], BuyOffer[i][0],
687 BuyOffer[i][1] };
688 xParams = x;
689 CSell = true;
690 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
691 inter = Intersection(xParams, yParams);
692 double DifUtlOurs = (SellOffer[i - 1][0] - MinS)
693 / (MaxS - MinS);
694 double MinUtl = (inter[0] - MinS)
695 / (MaxS - MinS);
696 DifUtlOurs -= MinUtl;
697 double DifUtlOps = (BuyOffer[i - 1][0] - MinS)
698 / (MaxS - MinS);
699 DifUtlOps = Utl - DifUtlOps;
700 // if ((float)DifUtlOurs<=(float)DifUtlOps){
701 MaxS = inter[0];
702 CSell = true;
703 // }
704 // else
705 // {
706 // CSell=false;
707 // }
708 }
709 /*
710 * double[]
711 * xParams={SellOffer[i-1][1],SellOffer[i-1]
712 * [0],BuyOffer[i][0],BuyOffer[i][1]}; double[]
713 * yParams={0.0,1.0,1.0,0.0}; //double[] inter={};
714 * inter=Intersection(xParams, yParams); CSell=true;
715 * //MinB=BuyOffer[i-1][0];
716 * //MaxB=SellOffer[i-1][0];
717 * //MinS=BuyOffer[i-1][0];
718 * //MaxS=SellOffer[i-1][0]; //BSell=40;
719 * //SellOffer[i][0]=inter[0]; //SellOffer[i][1]=0;
720 * //return false;
721 */
722 }
723 }
724 if (CBuy == true) {
725 double DifUtlOurs = (SellOffer[i - 1][0] - MinS)
726 / (MaxS - MinS);
727 DifUtlOurs -= Utl;
728 double DifUtlOps = (BuyOffer[i - 1][0] - MinS)
729 / (MaxS - MinS);
730 DifUtlOps = Utl - DifUtlOps;
731 if ((float) DifUtlOurs <= (float) DifUtlOps) {
732 // System.out.println("Accepted");
733 AcceptedValue = BuyOffer[i][0];
734 return true;
735 } else {
736 CBuy = false;
737 }
738 }
739 }
740 if (LSell == 3) {
741 Random generator = new Random();
742 int rand = generator.nextInt(4);
743 rand = 2;
744 if (i >= LSell) {
745 double tmp3 = java.lang.Math.min(
746 java.lang.Math.max((SellOffer[i - 1][0]
747 + BuyOffer[i - LSell][0]
748 - BuyOffer[i][0] - rand), MinS),
749 MaxS);
750 double tmpUtl3 = (tmp3 - MinS) / (MaxS - MinS);
751 if (((float) tmpUtl3 <= (float) Utl)
752 && (CSell == false)) {
753 // System.out.println("Accepted");
754 AcceptedValue = BuyOffer[i][0];
755 return true;
756 } else {
757
758 if (CSell == true) {
759 SellOffer[i][0] = inter[0];
760 SellOffer[i][1] = SellOffer[i][0];
761 } else {
762 SellOffer[i][0] = tmp3;
763 if ((SellOffer[i][0] - Scons) <= MinS) {
764 SellOffer[i][1] = MinS;
765 } else {
766 SellOffer[i][1] = SellOffer[i][0] - Scons;
767 }
768 }
769 return false;
770 }
771 } else {
772 SellOffer[i][0] = MaxS - ((i) * 0.5);
773 SellOffer[i][1] = MaxS - ((i) * 0.5) - Scons;
774 return false;
775 }
776 } else {
777 if (i >= LSell) {
778
779 double tmp2 = java.lang.Math.min(java.lang.Math
780 .max((BuyOffer[i - LSell][0] / BuyOffer[i][0])
781 * SellOffer[i - 1][0], MinS),
782 MaxS);
783 Utl = (BuyOffer[i][0] - MinS) / (MaxS - MinS);
784 double tmpUtl2 = (tmp2 - MinS) / (MaxS - MinS);
785 if (((float) tmpUtl2 <= (float) Utl)
786 && (CSell == false)) {
787 // System.out.println("Accepted");
788 AcceptedValue = BuyOffer[i][0];
789 return true;
790 } else {
791
792 if (CSell == true) {
793 SellOffer[i][0] = inter[0];
794 SellOffer[i][1] = SellOffer[i][0];
795 } else {
796 SellOffer[i][0] = tmp2;
797 if ((SellOffer[i][0] - Scons) <= MinS) {
798 SellOffer[i][1] = MinS;
799 } else {
800 SellOffer[i][1] = SellOffer[i][0] - Scons;
801 }
802 }
803 return false;
804 }
805 } else {
806 SellOffer[i][0] = MaxS - ((i) * 0.5);
807 SellOffer[i][1] = MaxS - ((i) * 0.5) - Scons;
808 return false;
809 }
810
811 }
812 }
813 case 3:
814 // If it has reached the deadline terminate negotiaiton
815 if (i > deadlineS) {
816 // System.out.println("Finished");
817 return true;
818 }
819 // if it has not reached the deadline continue negotiation
820 else {
821 Utl = (BuyOffer[i][0] - MinS) / (MaxS - MinS);
822 if (i > 0) {
823 if (CBuy == false) {
824 /*
825 * if
826 * (((double)SellOffer[i-1][1]<=(double)BuyOffer[i][0]
827 * )&& (i<deadlineS)) { System.out.println("Accepted");
828 * AcceptedValue=BuyOffer[i-1][0]; return true; }
829 */
830 if ((SellOffer[i - 1][1] <= BuyOffer[i][1])
831 && (i <= deadlineS)) {
832 double[] xParams = new double[4];
833 if (SellOffer[i - 1][0] == SellOffer[i - 1][1]) {
834 CSell = false;
835 } else {
836 double[] x = { SellOffer[i - 1][1],
837 SellOffer[i - 1][0], BuyOffer[i][0],
838 BuyOffer[i][1] };
839 xParams = x;
840 CSell = true;
841 double[] yParams = { 0.0, 1.0, 1.0, 0.0 };
842 inter = Intersection(xParams, yParams);
843 double DifUtlOurs = (SellOffer[i - 1][0] - MinS)
844 / (MaxS - MinS);
845 double MinUtl = (inter[0] - MinS)
846 / (MaxS - MinS);
847 DifUtlOurs -= MinUtl;
848 double DifUtlOps = (BuyOffer[i - 1][0] - MinS)
849 / (MaxS - MinS);
850 DifUtlOps = Utl - DifUtlOps;
851 // if ((float)DifUtlOurs<=(float)DifUtlOps){
852 MaxS = inter[0];
853 CSell = true;
854 // }
855 // else
856 // {
857 // CSell=false;
858 // }
859 }
860 /*
861 * double[]
862 * xParams={SellOffer[i-1][1],SellOffer[i-1]
863 * [0],BuyOffer[i][0],BuyOffer[i][1]}; double[]
864 * yParams={0.0,1.0,1.0,0.0}; //double[] inter={};
865 * inter=Intersection(xParams, yParams); CSell=true;
866 * //MinB=BuyOffer[i-1][0];
867 * //MaxB=SellOffer[i-1][0];
868 * //MinS=BuyOffer[i-1][0];
869 * //MaxS=SellOffer[i-1][0]; //BSell=40;
870 * //SellOffer[i][0]=inter[0]; //SellOffer[i][1]=0;
871 * //return false;
872 */
873 }
874 } else if (CBuy == true) {
875 double DifUtlOurs = (SellOffer[i - 1][0] - MinS)
876 / (MaxS - MinS);
877 DifUtlOurs -= Utl;
878 double DifUtlOps = (BuyOffer[i - 1][0] - MinS)
879 / (MaxS - MinS);
880 DifUtlOps = Utl - DifUtlOps;
881 if ((float) DifUtlOurs <= (float) DifUtlOps) {
882 // System.out.println("Accepted");
883 AcceptedValue = BuyOffer[i][0];
884 return true;
885 } else {
886 CBuy = false;
887 }
888 }
889 }
890 double resource = new Double(
891 MSell * ((double) 1 / (double) (i + 1)));
892 A = K + ((1 - K) * (1 / (java.lang.Math.exp(resource))));
893 double tmp4 = MinS + ((MaxS - MinS) * (1 - A));
894 double tmpUtl4 = (tmp4 - MinS) / (MaxS - MinS);
895 if (((float) tmpUtl4 < (float) Utl) && (CSell == false)) {
896 // System.out.println("Accepted");
897 AcceptedValue = BuyOffer[i][0];
898 return true;
899 } else {
900
901 if (CSell == true) {
902 SellOffer[i][0] = inter[0];
903 SellOffer[i][1] = SellOffer[i][0];
904 } else {
905 SellOffer[i][0] = tmp4;
906 if ((SellOffer[i][0] - Scons) <= MinS) {
907 SellOffer[i][1] = MinS;
908 } else {
909 SellOffer[i][1] = SellOffer[i][0] - Scons;
910 }
911 }
912 return false;
913 }
914
915 }
916 }
917 return false;
918 }
919
920 /**
921 * Function threshfind() which receives the current time of negotiation,
922 * pick value and stretch value of the offers and generates the threshhold
923 * for each offer
924 */
925 public static double ThreshFind(int deadline, int time, double pick,
926 double stretch) {
927 double weightPick, weightStretch;
928 weightStretch = (double) time / (double) deadline;
929 weightPick = (double) (deadline - time) / (double) deadline;
930 double thresh = (weightPick * pick) + (weightStretch * stretch);
931 return thresh;
932 }
933
934 /**
935 * Function Intersection returns intersection of two lines
936 */
937 public static double[] Intersection(double[] xPars, double[] yPars) {
938 // Calculating the intersection point of the two fuzzy offers
939 double ua = 0;
940 ua = (((xPars[3] - xPars[2]) * (yPars[0] - yPars[2]))
941 - ((yPars[3] - yPars[2]) * (xPars[0] - xPars[2])))
942 / (((yPars[3] - yPars[2]) * (xPars[1] - xPars[0]))
943 - ((xPars[3] - xPars[2]) * (yPars[1] - yPars[0])));
944 double[] interPoint = new double[2];
945 interPoint[0] = xPars[0] + (ua * (xPars[1] - xPars[0]));
946 interPoint[1] = yPars[0] + (ua * (yPars[1] - yPars[0]));
947 return interPoint;
948 }
949
950 public static void FuzzyDist() {
951 double midPointXB = 0;
952 double midPointYB = 0;
953 double midPointXS = 0;
954 double midPointYS = 0;
955 // midPointXB=((double)(java.lang.Math.pow(BuyOffer[iteration-1][0],2)-java.lang.Math.pow(MinB,2))/(double)(2));
956 if (BuyOffer[iteration - 1][0] != BuyOffer[iteration - 1][1]) {
957 midPointXB += (((3
958 * (java.lang.Math.pow(BuyOffer[iteration - 1][0], 2)
959 * BuyOffer[iteration - 1][1]))
960 - java.lang.Math.pow(BuyOffer[iteration - 1][1], 3)
961 - (2 * java.lang.Math.pow(BuyOffer[iteration - 1][0], 3)))
962 / (6 * (BuyOffer[iteration - 1][0]
963 - BuyOffer[iteration - 1][1])));
964 midPointXB = midPointXB / (((2 * BuyOffer[iteration - 1][1]
965 * BuyOffer[iteration - 1][0])
966 - java.lang.Math.pow(BuyOffer[iteration - 1][1], 2)
967 - java.lang.Math.pow(BuyOffer[iteration - 1][0], 2))
968 / (2 * (BuyOffer[iteration - 1][0]
969 - BuyOffer[iteration - 1][1])));
970 midPointYB = 0.5;
971 } else {
972 midPointXB = BuyOffer[iteration - 1][0];
973 }
974 if (SellOffer[iteration - 1][0] != SellOffer[iteration - 1][1]) {
975 // midPointXS=((double)(java.lang.Math.pow(MaxS,2)-java.lang.Math.pow(SellOffer[iteration-1][0],2))/(double)(2));
976 midPointXS += (((3
977 * (java.lang.Math.pow(SellOffer[iteration - 1][1], 2)
978 * SellOffer[iteration - 1][0]))
979 - java.lang.Math.pow(SellOffer[iteration - 1][0], 3)
980 - (2 * java.lang.Math.pow(SellOffer[iteration - 1][1], 3)))
981 / (6 * (SellOffer[iteration - 1][0]
982 - SellOffer[iteration - 1][1])));
983 midPointXS = midPointXS / (((2 * SellOffer[iteration - 1][1]
984 * SellOffer[iteration - 1][0])
985 - java.lang.Math.pow(SellOffer[iteration - 1][1], 2)
986 - java.lang.Math.pow(SellOffer[iteration - 1][0], 2))
987 / (2 * (SellOffer[iteration - 1][0]
988 - SellOffer[iteration - 1][1])));
989 midPointYS = 0.5;
990 } else {
991 midPointXS = SellOffer[iteration - 1][0];
992 }
993 double last = java.lang.Math
994 .sqrt(java.lang.Math.pow((midPointXS - midPointXB), 2)
995 + java.lang.Math.pow((midPointYS - midPointYB), 2));
996 dist[iteration - 1] = last;
997 }
998
999 public static double FinalDistance(double[] dist) {
1000 double last = 0;
1001 for (int i = 0; i < iteration; i++) {
1002 last += dist[i];
1003 }
1004 return last;
1005 }
1006
1007 @Override
1008 public SupportedNegotiationSetting getSupportedNegotiationSetting() {
1009 return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
1010 }
1011
1012 @Override
1013 public String getDescription() {
1014 return "Fuzzy negotiator";
1015 }
1016
1017}
Note: See TracBrowser for help on using the repository browser.