1 | package parties.in4010.q12015.group10;
|
---|
2 |
|
---|
3 | import static java.lang.Math.pow;
|
---|
4 |
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.Deadline;
|
---|
10 | import genius.core.DeadlineType;
|
---|
11 | import genius.core.bidding.BidDetails;
|
---|
12 | import genius.core.boaframework.OutcomeSpace;
|
---|
13 | import genius.core.misc.Range;
|
---|
14 | import genius.core.timeline.TimeLineInfo;
|
---|
15 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
16 |
|
---|
17 | public class OpponentStrategyEstimator {
|
---|
18 |
|
---|
19 | static double updateAllModels(Opponent[] opponents, Deadline deadLine,
|
---|
20 | TimeLineInfo timeLine, AdditiveUtilitySpace myUtilitySpace) {
|
---|
21 | boolean acceptableQuality = false;
|
---|
22 | if (deadLine.getType() == DeadlineType.TIME) {
|
---|
23 | return 0.0;
|
---|
24 | }
|
---|
25 |
|
---|
26 | int maxNumberOfBids = deadLine.getValue();
|
---|
27 | double nextBidNumberDouble = timeLine.getCurrentTime()
|
---|
28 | / timeLine.getTotalTime();
|
---|
29 | int nextBidNumber = ((int) nextBidNumberDouble);
|
---|
30 |
|
---|
31 | // generate time array
|
---|
32 | double[] totalTimeArray = new double[maxNumberOfBids];
|
---|
33 | for (int bidNumber = 0; bidNumber < maxNumberOfBids; bidNumber++) {
|
---|
34 | totalTimeArray[bidNumber] = ((double) nextBidNumber)
|
---|
35 | / ((double) maxNumberOfBids);
|
---|
36 | }
|
---|
37 |
|
---|
38 | // for each opponent
|
---|
39 | // u = 1+a*t+b*t^c
|
---|
40 | // see report
|
---|
41 | double[] a = new double[opponents.length]; // estimated boulware
|
---|
42 | // parameter
|
---|
43 | double[] b = new double[opponents.length]; // estimated boulware
|
---|
44 | // parameter
|
---|
45 | double[] c = new double[opponents.length]; // estimated boulware
|
---|
46 | // parameter
|
---|
47 | double[] d = new double[opponents.length]; // estimated boulware
|
---|
48 | // parameter
|
---|
49 | double[] errorVariance = new double[opponents.length]; // estimated
|
---|
50 | // variance of
|
---|
51 | // error for the
|
---|
52 | // found fit
|
---|
53 |
|
---|
54 | // estimate fitting parameters
|
---|
55 | double minimumPassedTime = 0.75; // this is the minimum time that must
|
---|
56 | // be passed before we consider
|
---|
57 | // getting an estimate
|
---|
58 | double powerIncrement = 0.2;
|
---|
59 | int maxLoops = 100;
|
---|
60 | int spacing = 5;
|
---|
61 | int beginOffset = 1;
|
---|
62 |
|
---|
63 | // System.out.println("minimumPassedTimeboolean"+minimumPassedTime);
|
---|
64 | if (timeLine.getTime() > minimumPassedTime) {
|
---|
65 | acceptableQuality = true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | for (int opponentNumber = 0; opponentNumber < opponents.length; opponentNumber++) {
|
---|
69 | if (opponents[opponentNumber].getHistorySize() < 10 * spacing) {
|
---|
70 | return 0.0;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (acceptableQuality) {
|
---|
75 | for (int opponentNumber = 0; opponentNumber < opponents.length; opponentNumber++) {
|
---|
76 | double[] bidEval = new double[opponents[opponentNumber]
|
---|
77 | .getHistorySize()];
|
---|
78 | double[] time = new double[opponents[opponentNumber]
|
---|
79 | .getHistorySize()];
|
---|
80 | // for each bid in the history
|
---|
81 | Bid bid;
|
---|
82 | for (int bidNumber = 0; bidNumber < opponents[opponentNumber]
|
---|
83 | .getHistorySize(); bidNumber++) {
|
---|
84 | bid = opponents[opponentNumber]
|
---|
85 | .getBidfromHistory(bidNumber);
|
---|
86 | try {
|
---|
87 | bidEval[bidNumber] = opponents[opponentNumber]
|
---|
88 | .getEstimatedUtilitySpace().getUtility(bid);
|
---|
89 | } catch (Exception e) {
|
---|
90 | e.printStackTrace();
|
---|
91 | }
|
---|
92 | time[bidNumber] = ((double) bidNumber)
|
---|
93 | / ((double) deadLine.getValue());
|
---|
94 | }
|
---|
95 |
|
---|
96 | // begin offset not implemented
|
---|
97 | double[] minTime = boulwareParameterEstimator.getMinTime(
|
---|
98 | bidEval, time, spacing, beginOffset);
|
---|
99 | double[] minBidEval = boulwareParameterEstimator.getMinEval(
|
---|
100 | bidEval, time, spacing, beginOffset);
|
---|
101 |
|
---|
102 | double[] parABCE = boulwareParameterEstimator.leastSquaresFit(
|
---|
103 | minBidEval, minTime, maxLoops, powerIncrement);
|
---|
104 |
|
---|
105 | a[opponentNumber] = parABCE[0];
|
---|
106 | b[opponentNumber] = parABCE[1];
|
---|
107 | c[opponentNumber] = parABCE[2];
|
---|
108 | d[opponentNumber] = parABCE[3];
|
---|
109 | errorVariance[opponentNumber] = parABCE[4];
|
---|
110 | System.out.println("errorVariance"
|
---|
111 | + errorVariance[opponentNumber]);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | // if the minimum variance is higher than the threshold the model is
|
---|
116 | // deemed unusable
|
---|
117 | double modelQualityThreshold = 0.02;
|
---|
118 | for (int n = 0; n < errorVariance.length; n++) {
|
---|
119 | if (errorVariance[n] > modelQualityThreshold) {
|
---|
120 | acceptableQuality = true;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | // get the full outcome space
|
---|
125 | Range UtilRange = new Range(0, 1);
|
---|
126 | OutcomeSpace fullOutComeSpace = new OutcomeSpace(myUtilitySpace);
|
---|
127 | List<BidDetails> allPossibleBids = fullOutComeSpace
|
---|
128 | .getBidsinRange(UtilRange);
|
---|
129 | double[] maxUtilityMySpaceOfCommonBids = new double[maxNumberOfBids];
|
---|
130 | // for each opponent:
|
---|
131 | // get the estimated lowerbound on the bidding utility that they will
|
---|
132 | // bid unitl N bids before the deadline
|
---|
133 | double t; // non array time variable
|
---|
134 | int offsetBidsFromDeadLine = 4;
|
---|
135 | double[] lowerBoundOfferedUtility = new double[opponents.length];
|
---|
136 | double totalMaxUtilityMySpaceOfCommonBids = 0;
|
---|
137 | if (acceptableQuality) {
|
---|
138 | // for each future bidnumber
|
---|
139 | for (int bidNumber = nextBidNumber; bidNumber < maxNumberOfBids
|
---|
140 | - offsetBidsFromDeadLine; bidNumber++) {
|
---|
141 | // for each opponent get the lower bound on the expected utility
|
---|
142 | for (int opponentNumber = 1; opponentNumber < opponents.length; opponentNumber++) {
|
---|
143 | t = totalTimeArray[bidNumber];
|
---|
144 | lowerBoundOfferedUtility[opponentNumber] = a[opponentNumber]
|
---|
145 | + b[opponentNumber]
|
---|
146 | * t
|
---|
147 | + c[opponentNumber]
|
---|
148 | * pow(t, d[opponentNumber]);
|
---|
149 | }
|
---|
150 | // ////// check the max attainable common utility
|
---|
151 | maxUtilityMySpaceOfCommonBids[bidNumber] = 0;
|
---|
152 | for (Iterator<BidDetails> BidDetailsIterator = allPossibleBids
|
---|
153 | .iterator(); BidDetailsIterator.hasNext();) {
|
---|
154 | // Iterates over all possible bids
|
---|
155 | BidDetails bidDetails = BidDetailsIterator.next();
|
---|
156 | Bid bid = bidDetails.getBid();
|
---|
157 | boolean bidInCommonBidRange = true;
|
---|
158 | double utilityOfBid;
|
---|
159 | for (int opponentNumber = 1; opponentNumber < opponents.length; opponentNumber++) {
|
---|
160 | utilityOfBid = 0;
|
---|
161 | try {
|
---|
162 | utilityOfBid = opponents[opponentNumber]
|
---|
163 | .getEstimatedUtilitySpace().getUtility(bid);
|
---|
164 | } catch (Exception e1) {
|
---|
165 | e1.printStackTrace();
|
---|
166 | } // Throws Exception when bid is invalid
|
---|
167 | if (utilityOfBid < lowerBoundOfferedUtility[opponentNumber]) {
|
---|
168 | bidInCommonBidRange = false;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | if (bidInCommonBidRange) {
|
---|
172 | utilityOfBid = 0;
|
---|
173 | try {
|
---|
174 | utilityOfBid = myUtilitySpace.getUtility(bid);
|
---|
175 | } catch (Exception e1) {
|
---|
176 | e1.printStackTrace();
|
---|
177 | } // Throws Exception when bid is invalid
|
---|
178 | if (maxUtilityMySpaceOfCommonBids[bidNumber] < utilityOfBid) {
|
---|
179 | maxUtilityMySpaceOfCommonBids[bidNumber] = utilityOfBid;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | }
|
---|
184 |
|
---|
185 | // //////
|
---|
186 | }
|
---|
187 | for (int bidNumber = nextBidNumber; bidNumber < maxNumberOfBids
|
---|
188 | - offsetBidsFromDeadLine; bidNumber++) {
|
---|
189 | if (totalMaxUtilityMySpaceOfCommonBids < maxUtilityMySpaceOfCommonBids[bidNumber]) {
|
---|
190 | totalMaxUtilityMySpaceOfCommonBids = maxUtilityMySpaceOfCommonBids[bidNumber];
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | double uncertaintyBandwidth = 0.05;
|
---|
196 | return totalMaxUtilityMySpaceOfCommonBids - uncertaintyBandwidth;
|
---|
197 |
|
---|
198 | }
|
---|
199 | }
|
---|