1 | package negotiator.boaframework.offeringstrategy.anac2010.IAMhaggler2010;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | public class ConcessionUtils {
|
---|
6 |
|
---|
7 | public static double getBeta(ArrayList<Pair<Double, Double>> bestOpponentBidUtilityHistory, double time, double discounting, double utility0,
|
---|
8 | double utility1) {
|
---|
9 | return getBeta(bestOpponentBidUtilityHistory, time, discounting, utility0, utility1, 0.1, 0.01, 2, 1, 1, 1);
|
---|
10 | }
|
---|
11 |
|
---|
12 | public static double getBeta(ArrayList<Pair<Double, Double>> bestOpponentBidUtilityHistory, double time, double discounting, double utility0,
|
---|
13 | double utility1, double minDiscounting, double minBeta, double maxBeta, double defaultBeta, double ourTime, double opponentTime) {
|
---|
14 | discounting = Math.max(discounting, minDiscounting);
|
---|
15 | try {
|
---|
16 | // Estimate the alpha value.
|
---|
17 | //double alpha = getAlpha(bestOpponentBidUtilityHistory);
|
---|
18 | Pair<Double, Double> params = getRegressionParameters(bestOpponentBidUtilityHistory, utility0);
|
---|
19 | double a = params.fst;
|
---|
20 | double b = params.snd;
|
---|
21 | // Find the maxima.
|
---|
22 | double maxima = getMaxima(a, b, time, discounting, utility0, opponentTime);
|
---|
23 | // Find the current utility of our current concession strategy.
|
---|
24 | //double util = utility0 + (utility1 - utility0) * (1 - Math.exp(-alpha * maxima));
|
---|
25 | double util = utility0 + (Math.exp(a) * Math.pow(maxima, b));
|
---|
26 | util = Math.max(0, Math.min(1, util));
|
---|
27 | // Calculate the beta value.
|
---|
28 | double beta = Math.max(0, Math.log(maxima) / Math.log((1 - util) / 0.5));
|
---|
29 |
|
---|
30 | /*
|
---|
31 | if (time < 0.5) {
|
---|
32 | double weightBeta = time / 0.5;
|
---|
33 | //beta = (beta * weightBeta) + (defaultBeta * (1 - weightBeta));
|
---|
34 | beta = Math.exp(Math.log(beta) * weightBeta) + Math.exp(Math.log(defaultBeta) * (1 - weightBeta));
|
---|
35 | }
|
---|
36 | */
|
---|
37 |
|
---|
38 | return Math.min(maxBeta, Math.max(minBeta, beta));
|
---|
39 | } catch (Exception ex) {
|
---|
40 | return defaultBeta;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static Pair<Double, Double> getRegressionParameters(ArrayList<Pair<Double, Double>> bestOpponentBidUtilityHistory, double utility0) {
|
---|
45 | double n = 1;
|
---|
46 | double x = 0;//Math.log(1);
|
---|
47 | double y = Math.log(1 - utility0);
|
---|
48 | double sumlnxlny = 0;//x * y;
|
---|
49 | double sumlnx = 0;//x;
|
---|
50 | double sumlny = y;
|
---|
51 | double sumlnxlnx = 0;//x * y;
|
---|
52 | for (Pair<Double, Double> d : bestOpponentBidUtilityHistory) {
|
---|
53 | x = Math.log(d.snd);
|
---|
54 | y = Math.log(d.fst - utility0);
|
---|
55 |
|
---|
56 | if(Double.isNaN(x))
|
---|
57 | throw new RuntimeException("Unable to perform regression using provided points (x).");
|
---|
58 | if(Double.isNaN(y))
|
---|
59 | throw new RuntimeException("Unable to perform regression using provided points (y).");
|
---|
60 | if(Double.isInfinite(x) || Double.isInfinite(y))
|
---|
61 | continue;
|
---|
62 |
|
---|
63 | sumlnxlny += x * y;
|
---|
64 | sumlnx += x;
|
---|
65 | sumlny += y;
|
---|
66 | sumlnxlnx += x * x;
|
---|
67 | n++;
|
---|
68 | }
|
---|
69 |
|
---|
70 | double b = ((n * sumlnxlny) - (sumlnx * sumlny)) / ((n * sumlnxlnx) - (sumlnx * sumlnx));
|
---|
71 | if(Double.isNaN(b))
|
---|
72 | throw new RuntimeException("Unable to perform regression using provided points (b)." + (sumlnxlny) + ", " + (n * sumlnxlnx) + ", " + (sumlnx * sumlnx));
|
---|
73 | double a = (sumlny - (b * sumlnx)) / n;
|
---|
74 |
|
---|
75 | if(Double.isNaN(a))
|
---|
76 | throw new RuntimeException("Unable to perform regression using provided points (a).");
|
---|
77 | return new Pair<Double, Double>(a, b);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | private static double getMaxima(double alpha, double time, double discounting, double utility0, double utility1) {
|
---|
82 | double alpharoot = Math.log((discounting * utility1) / ((alpha + discounting) * (utility1 - utility0))) / -alpha;
|
---|
83 | ArrayList<Double> maxima = new ArrayList<Double>();
|
---|
84 | if (alpharoot >= 0 && alpharoot <= 1 && alpharoot > time) {
|
---|
85 | maxima.add(alpharoot);
|
---|
86 | }
|
---|
87 | maxima.add(time);
|
---|
88 | maxima.add(1.0);
|
---|
89 | double maxUtil = 0;
|
---|
90 | double result = 0;
|
---|
91 | for (double maximum : maxima) {
|
---|
92 | double util = (utility0 + ((utility1 - utility0) * (1 - Math.exp(-alpha * maximum)))) * Math.exp(-discounting * maximum);
|
---|
93 | if (util > maxUtil) {
|
---|
94 | result = maximum;
|
---|
95 | maxUtil = util;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 | */
|
---|
101 |
|
---|
102 | private static double getMaxima(double a, double b, double time, double discounting, double utility0, double opponentTime) {
|
---|
103 | //double root = b / discounting;
|
---|
104 | ArrayList<Double> maxima = new ArrayList<Double>();
|
---|
105 | maxima.add(time);
|
---|
106 | for(int i=(int) Math.floor(time*1000); i<=1000; i++) {
|
---|
107 | double root = (double)i / 1000.0;
|
---|
108 | //if (root >= 0 && root <= 1 && root > time) {
|
---|
109 | maxima.add(root);
|
---|
110 | //}
|
---|
111 | }
|
---|
112 | maxima.add(1.0);
|
---|
113 | double maxUtil = 0;
|
---|
114 | double result = 0;
|
---|
115 |
|
---|
116 | double timeScaledDiscounting = -discounting * getTimeScaleFactor(time, opponentTime);
|
---|
117 | double expA = Math.exp(a);
|
---|
118 |
|
---|
119 | for (double maximum : maxima) {
|
---|
120 | double util = (utility0 + (expA * Math.pow(maximum, b))) * Math.exp(timeScaledDiscounting * maximum);
|
---|
121 | if (util > maxUtil) {
|
---|
122 | result = maximum;
|
---|
123 | maxUtil = util;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | return result;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private static double getTimeScaleFactor(double ourTime, double opponentTime) {
|
---|
130 | double sf = (ourTime + opponentTime)/(ourTime * 2.0);
|
---|
131 | return sf;
|
---|
132 | }
|
---|
133 |
|
---|
134 | }
|
---|