source: src/main/java/negotiator/boaframework/opponentmodel/IAMHagglerOpponentConcessionModel.java@ 316

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 6.0 KB
Line 
1package negotiator.boaframework.opponentmodel;
2
3import java.util.ArrayList;
4
5import agents.Jama.Matrix;
6import agents.uk.ac.soton.ecs.gp4j.bmc.BasicPrior;
7import agents.uk.ac.soton.ecs.gp4j.bmc.GaussianProcessMixture;
8import agents.uk.ac.soton.ecs.gp4j.bmc.GaussianProcessMixturePrediction;
9import agents.uk.ac.soton.ecs.gp4j.bmc.GaussianProcessRegressionBMC;
10import agents.uk.ac.soton.ecs.gp4j.gp.covariancefunctions.CovarianceFunction;
11import agents.uk.ac.soton.ecs.gp4j.gp.covariancefunctions.Matern3CovarianceFunction;
12import agents.uk.ac.soton.ecs.gp4j.gp.covariancefunctions.NoiseCovarianceFunction;
13import agents.uk.ac.soton.ecs.gp4j.gp.covariancefunctions.SumCovarianceFunction;
14import genius.core.utility.AdditiveUtilitySpace;
15
16/**
17 * This class is a component of the ANAC 2011 agent IAMHaggler where it estimates
18 * the concession rate of the opponent. For more information on how it works see
19 * "Using Gaussian Processes to Optimise Concession in Complex Negotiations against Unknown Opponents"
20 * by Colins et al.
21 * @author Alex Dirkzwager
22 *
23 */
24
25public class IAMHagglerOpponentConcessionModel {
26
27 private Matrix timeSamples;
28 private Matrix means;
29 private Matrix variances;
30 private GaussianProcessRegressionBMC regression;
31 private int lastTimeSlot = -1;
32 private ArrayList<Double> opponentTimes = new ArrayList<Double>();
33 private ArrayList<Double> opponentUtilities = new ArrayList<Double>();
34 private double intercept;
35 private double maxUtilityInTimeSlot;
36 private Matrix matrixTimeSamplesAdjust;
37 private int slots;
38
39
40
41 /**
42 *
43 * @param numberOfSlots (within the total negotiation
44 * @param utilitySpace
45 */
46 public IAMHagglerOpponentConcessionModel(int numberOfSlots, AdditiveUtilitySpace utilitySpace, int amountOfSamples){
47 double discountingFactor = 0.5;
48 try
49 {
50 discountingFactor = utilitySpace.getDiscountFactor();
51 }
52 catch(Exception ex)
53 {
54 ex.printStackTrace();
55 }
56 if(discountingFactor == 0)
57 discountingFactor = 1;
58 makeTimeSamples(amountOfSamples);
59 this.slots = numberOfSlots;
60 BasicPrior[] bps = { new BasicPrior(11, 0.252, 0.5),
61 new BasicPrior(11, 0.166, 0.5), new BasicPrior(1, .01, 1.0) };
62 CovarianceFunction cf = new SumCovarianceFunction(
63 Matern3CovarianceFunction.getInstance(),
64 NoiseCovarianceFunction.getInstance());
65
66 regression = new GaussianProcessRegressionBMC();
67 regression.setCovarianceFunction(cf);
68 regression.setPriors(bps);
69 }
70
71 /**
72 * updates the model with the most recent opponent bid
73 * @param opponentUtility
74 * @param time
75 */
76 public void updateModel(double opponentUtility, double time) {
77 // Calculate the current time slot
78 int timeSlot = (int) Math.floor(time * slots);
79
80 boolean regressionUpdateRequired = false;
81 if (lastTimeSlot == -1) {
82 regressionUpdateRequired = true;
83 }
84
85 // If the time slot has changed
86 if (timeSlot != lastTimeSlot) {
87 if (lastTimeSlot != -1) {
88 // Store the data from the time slot
89 opponentTimes.add((lastTimeSlot + 0.5) / slots);
90 if(opponentUtilities.size() == 0)
91 {
92 intercept = Math.max(0.5, maxUtilityInTimeSlot);
93 double[] timeSamplesAdjust = new double[timeSamples.getColumnDimension()];
94 System.out.println("timeSampleAdjusted[15]: " + timeSamplesAdjust.length);
95
96
97 int i = 0;
98 double gradient = 0.9 - intercept;
99 for (double d : timeSamples.getRowPackedCopy()) {
100 timeSamplesAdjust[i++] = intercept + (gradient * d);
101 }
102 matrixTimeSamplesAdjust = new Matrix(timeSamplesAdjust, timeSamplesAdjust.length);
103 }
104 opponentUtilities.add(maxUtilityInTimeSlot);
105 // Flag regression receiveMessage required
106 regressionUpdateRequired = true;
107 }
108 // Update the time slot
109 lastTimeSlot = timeSlot;
110 // Reset the max utility
111 maxUtilityInTimeSlot = 0;
112 }
113
114 // Calculate the maximum utility observed in the current time slot
115 maxUtilityInTimeSlot = Math.max(maxUtilityInTimeSlot, opponentUtility);
116
117 if (timeSlot == 0) {
118 return;
119 }
120
121 if (regressionUpdateRequired && opponentTimes.size() > 0) {
122 double gradient = 0.9 - intercept;
123
124 GaussianProcessMixture predictor;
125
126 if(lastTimeSlot == -1)
127 {
128 predictor = regression.calculateRegression(new double[] {}, new double[] {});
129 }
130 else
131 {
132 double x;
133 double y;
134 try {
135 x = opponentTimes.get(opponentTimes.size() - 1);
136 y = opponentUtilities.get(opponentUtilities.size() - 1);
137
138 } catch(Exception ex) {
139 System.out.println("Error getting x or y");
140 throw new Error(ex);
141 }
142
143 predictor = regression.updateRegression(
144 new Matrix(new double[] {x}, 1),
145 new Matrix(new double[] {y - intercept - (gradient * x)}, 1));
146 }
147
148 GaussianProcessMixturePrediction prediction = predictor
149 .calculatePrediction(timeSamples.transpose());
150
151 // Store the means and variances
152 means = prediction.getMean().plus(matrixTimeSamplesAdjust);
153
154 variances = prediction.getVariance();
155 }
156 }
157
158 /**
159 * Gets all means in a n-by-1 Matrix
160 * @return
161 */
162 public Matrix getMeans(){
163 return means;
164 }
165
166 /**
167 * Gets a specific mean point corresponding to the timeSlot
168 * @param timeSlot
169 * @return
170 */
171 public double getMeanAt(int timeSlot){
172 return means.get(timeSlot, 0);
173 }
174
175 /**
176 * Gets all Variances in a n-by-1 Matrix
177 * @return
178 */
179 public Matrix getVariance(){
180 return variances;
181 }
182
183 /**
184 * Gets a specific variance point corresponding to the timeSlot
185 * @param timeSlot
186 * @return
187 */
188 public double getVarianceAt(int timeSlot){
189 return variances.get(timeSlot, 0);
190 }
191
192 /**
193 * Create a 1-by-n matrix of time samples.
194 *
195 * @param n
196 * The sample size.
197 */
198 private void makeTimeSamples(int n) {
199 double[] timeSamplesArray = new double[n + 1];
200 {
201 for (int i = 0; i < timeSamplesArray.length; i++) {
202 timeSamplesArray[i] = ((double) i) / ((double) n);
203 }
204 }
205 System.out.println("timeSampleArray[15]: " + timeSamplesArray.length);
206 timeSamples = new Matrix(timeSamplesArray, 1);
207 }
208}
Note: See TracBrowser for help on using the repository browser.