[1] | 1 | package geniusweb.exampleparties.simpleshaop;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.HashMap;
|
---|
| 5 | import java.util.List;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 | import java.util.Set;
|
---|
| 8 | import geniusweb.issuevalue.Bid;
|
---|
| 9 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 10 | import geniusweb.issuevalue.Domain;
|
---|
| 11 | import geniusweb.issuevalue.Value;
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | public class GravityEs {
|
---|
| 16 |
|
---|
| 17 | private static final double HALF_OF_TIME = 0.5;
|
---|
| 18 | Bid sampleBid;
|
---|
| 19 | private List<Bid> orderedBidListInAscendingOrder;
|
---|
| 20 | private List<String> listOfIssues;
|
---|
| 21 | private int numberOfIssues;
|
---|
| 22 |
|
---|
| 23 | Map<Integer, double[][]> eachIssueAnItsIndexingValuesDoubleArrayMap;
|
---|
| 24 | Map<Integer, double[]> eachIssueAndItsValuesUtilityMap;
|
---|
| 25 |
|
---|
| 26 | double[] sumOfSquaredErrorsOfIssueValues;
|
---|
| 27 | Map<Integer, Double> eachIssueAndItsUtilityMap;
|
---|
| 28 |
|
---|
| 29 | // opponent modeling
|
---|
| 30 | Bid lastReceivedBid;
|
---|
| 31 | Map<Integer, double[]> eachIssueAndItsValuesFrequencyArrayMap;
|
---|
| 32 | Map<Integer, double[]> opponentEachIssueAndItsValuesUtilityMap;
|
---|
| 33 | double[] opponentSumOfSquaredErrosOfEachIssueMatrix;
|
---|
| 34 | Map<Integer, Double> opponentEachIssueAndItsUtilityMap;
|
---|
| 35 |
|
---|
| 36 | double lastOfferingTime;
|
---|
| 37 | Bid lastOfferedBid;
|
---|
| 38 | Bid nextBid;
|
---|
| 39 |
|
---|
| 40 | private List<Bid> bidOrder;
|
---|
| 41 | private Set<String> issList;
|
---|
| 42 | private Domain domain;
|
---|
| 43 |
|
---|
| 44 | HashMap<String,List<Value>> issueValindex = new HashMap<String, List<Value>>();
|
---|
| 45 |
|
---|
| 46 | public GravityEs(Domain domain, List<Bid> bidOrder) {
|
---|
| 47 |
|
---|
| 48 | this.bidOrder = bidOrder;
|
---|
| 49 | this.domain = domain;
|
---|
| 50 | issList = domain.getIssues();
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | for ( String iss : issList ) {
|
---|
| 54 | List<Value> vaList = new ArrayList<Value>();
|
---|
| 55 | for ( Value val : domain.getValues(iss) ) {
|
---|
| 56 | vaList.add(val);
|
---|
| 57 | }
|
---|
| 58 | issueValindex.put(iss, vaList);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | initAgentVariables();
|
---|
| 62 | initOpponentVars();
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | // uncertain preferences
|
---|
| 71 | private void initAgentVariables() {
|
---|
| 72 | initUncertainPrefVariables();
|
---|
| 73 | createCopelandMatrices();
|
---|
| 74 | fillAgentVars();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void fillAgentVars() {
|
---|
| 78 | fillCopelandMatrices();
|
---|
| 79 | setValueUtilities();
|
---|
| 80 | fillMatriceOfNoChangeOfEachIssue();
|
---|
| 81 | setIssueUtilitiesWithNormalization();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // opponent modeling
|
---|
| 85 | private void initOpponentVars() {
|
---|
| 86 | initOpponentModelingVars();
|
---|
| 87 | createFrequencyArrays();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void initUncertainPrefVariables() {
|
---|
| 91 |
|
---|
| 92 | //this.outcomeSpace = new SortedOutcomeSpace(utilitySpace);
|
---|
| 93 |
|
---|
| 94 | this.orderedBidListInAscendingOrder = bidOrder;
|
---|
| 95 | this.sampleBid = orderedBidListInAscendingOrder.get(0);
|
---|
| 96 | //this.listOfIssues = space.getDomain().getIssues();
|
---|
| 97 | this.numberOfIssues = issList.size();
|
---|
| 98 |
|
---|
| 99 | this.eachIssueAnItsIndexingValuesDoubleArrayMap = new HashMap<Integer, double[][]>();
|
---|
| 100 | this.eachIssueAndItsValuesUtilityMap = new HashMap<Integer, double[]>();
|
---|
| 101 |
|
---|
| 102 | this.sumOfSquaredErrorsOfIssueValues = new double[numberOfIssues];
|
---|
| 103 | this.eachIssueAndItsUtilityMap = new HashMap<Integer, Double>();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | private void initOpponentModelingVars() {
|
---|
| 107 | this.eachIssueAndItsValuesFrequencyArrayMap = new HashMap<Integer, double[]>();
|
---|
| 108 | this.opponentEachIssueAndItsValuesUtilityMap = new HashMap<Integer, double[]>();
|
---|
| 109 | this.opponentSumOfSquaredErrosOfEachIssueMatrix = new double[numberOfIssues];
|
---|
| 110 | this.opponentEachIssueAndItsUtilityMap = new HashMap<Integer, Double>();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private void createCopelandMatrices() {
|
---|
| 114 |
|
---|
| 115 | int i = 0;
|
---|
| 116 | for ( String iss : issList ) {
|
---|
| 117 | int valueSize = domain.getValues(iss).size().intValue() ;
|
---|
| 118 | eachIssueAnItsIndexingValuesDoubleArrayMap.put(i, new double[valueSize][valueSize]);
|
---|
| 119 | i++;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | private void createFrequencyArrays() {
|
---|
| 125 |
|
---|
| 126 | int i = 0;
|
---|
| 127 | for ( String issue : issList ) {
|
---|
| 128 | int valueSize = domain.getValues(issue).size().intValue();
|
---|
| 129 | eachIssueAndItsValuesFrequencyArrayMap.put(i, new double[valueSize]);
|
---|
| 130 | i++;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | * Gets the bigger bid from the sorted list . Gets the smaller bids from the
|
---|
| 138 | * sorted list. Does pairwise Copeland comparison with one bigger bid and
|
---|
| 139 | * smaller bids. depending on the bigger bid > all smaller bids
|
---|
| 140 | */
|
---|
| 141 | private void fillCopelandMatrices() {
|
---|
| 142 | for (int i = orderedBidListInAscendingOrder.size() - 1; i > 0; i--) {
|
---|
| 143 | Bid biggerBid = orderedBidListInAscendingOrder.get(i);
|
---|
| 144 | for (int j = i - 1; j >= 0; j--) {
|
---|
| 145 | Bid smallerBid = orderedBidListInAscendingOrder.get(j);
|
---|
| 146 | fillCopelandMatriceWithBiggerAndSmaller(biggerBid, smallerBid, i, j);
|
---|
| 147 | }
|
---|
| 148 | //System.out.println(biggerBid);
|
---|
| 149 | }
|
---|
| 150 | //printIntDoubleDoubleArrMap(eachIssueAnItsIndexingValuesDoubleArrayMap);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /*
|
---|
| 154 | * pairwise Copeland comparison in each issue value, update the frequency
|
---|
| 155 | * matrices for different issue values depending on the similarity.
|
---|
| 156 | */
|
---|
| 157 | private void fillCopelandMatriceWithBiggerAndSmaller(Bid biggerBid, Bid smallerBid, int biggerIndex,
|
---|
| 158 | int smallerIndex) {
|
---|
| 159 | int i = 0;
|
---|
| 160 | for (String issue : domain.getIssues()) {
|
---|
| 161 | DiscreteValue biggerBidValue = (DiscreteValue) ( biggerBid.getValue(issue) );
|
---|
| 162 | int biggerBidValueIndex = getIndexOfValueInIssue(biggerBid, issue, biggerBidValue);
|
---|
| 163 | DiscreteValue smallerValue = (DiscreteValue) (smallerBid.getValue(issue) );
|
---|
| 164 | int smallerBidValueIndex = getIndexOfValueInIssue(smallerBid, issue, smallerValue);
|
---|
| 165 | int numberOfSimilarities = countEqualValues(smallerBid, biggerBid);
|
---|
| 166 | if (numberOfSimilarities > 0) {
|
---|
| 167 | eachIssueAnItsIndexingValuesDoubleArrayMap.get(i)[biggerBidValueIndex][smallerBidValueIndex] += (1d
|
---|
| 168 | / (biggerIndex - smallerIndex)) * numberOfSimilarities;
|
---|
| 169 | }
|
---|
| 170 | i++;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private int getIndexOfValueInIssue(Bid bid, String issue, Value value) {
|
---|
| 175 |
|
---|
| 176 | List<Value> valList = issueValindex.get(issue);
|
---|
| 177 |
|
---|
| 178 | for ( int i = 0; i < valList.size(); i++ )
|
---|
| 179 | if ( valList.get(i).equals(value) )
|
---|
| 180 | return i;
|
---|
| 181 |
|
---|
| 182 | return -1;
|
---|
| 183 |
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | private void setValueUtilities() {
|
---|
| 187 |
|
---|
| 188 | int i = 0;
|
---|
| 189 | for ( String issueDiscrete : issList ) {
|
---|
| 190 |
|
---|
| 191 | int valueSize = domain.getValues(issueDiscrete).size().intValue();
|
---|
| 192 | double[] valuesBeingBigInfoArray = new double[valueSize];
|
---|
| 193 | double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i);
|
---|
| 194 | for (int j = 0; j < valueSize; j++) {
|
---|
| 195 | double sumOfRowInMatrix = getSumOfRowInMatrix(matrix, j);
|
---|
| 196 | double sumOfColInMatrix = getSumOfColInMatrix(matrix, j);
|
---|
| 197 | double total = sumOfColInMatrix + sumOfRowInMatrix;
|
---|
| 198 | if (total == 0) {
|
---|
| 199 | valuesBeingBigInfoArray[j] = 0;
|
---|
| 200 | } else {
|
---|
| 201 | double beingBigPercentage = (sumOfRowInMatrix) / total;
|
---|
| 202 | valuesBeingBigInfoArray[j] = beingBigPercentage;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | normalize(i, valueSize, valuesBeingBigInfoArray);
|
---|
| 206 | i++;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | //System.out.println("------------AGENT------------");
|
---|
| 210 | //printIntDoubleArrMap(eachIssueAndItsValuesUtilityMap);
|
---|
| 211 |
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | private void normalize(int i, int valueSize, double[] valuesBeingBigInfoArray) {
|
---|
| 215 | double[] utilityArr = new double[valueSize];
|
---|
| 216 | double totalSum = getSumOfRowInOneDimensionalMatrix(valuesBeingBigInfoArray);
|
---|
| 217 | for (int j = 0; j < valueSize; j++) {
|
---|
| 218 | if (totalSum == 0) {
|
---|
| 219 | utilityArr[j] = 0;
|
---|
| 220 | } else {
|
---|
| 221 | utilityArr[j] = valuesBeingBigInfoArray[j] / totalSum;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | eachIssueAndItsValuesUtilityMap.put(i, utilityArr);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | private void fillMatriceOfNoChangeOfEachIssue() {
|
---|
| 228 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
| 229 | double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i);
|
---|
| 230 | double sumOfMatrix = 0;
|
---|
| 231 | for (int j = 0; j < matrix.length; j++) {
|
---|
| 232 | sumOfMatrix += getSumOfRowInMatrix(matrix, j);
|
---|
| 233 | }
|
---|
| 234 | //System.out.println("Sum of matrix: " + sumOfMatrix);
|
---|
| 235 | double average = sumOfMatrix / (matrix.length * matrix.length);
|
---|
| 236 | //System.out.println("average of matrix: " + average);
|
---|
| 237 | double sumOfSquaredErrors = 0;
|
---|
| 238 | for (int j = 0; j < matrix.length; j++) {
|
---|
| 239 | for (int k = 0; k < matrix.length; k++) {
|
---|
| 240 | sumOfSquaredErrors += Math.pow(matrix[j][k] - average, 2);
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | sumOfSquaredErrorsOfIssueValues[i] = Math.sqrt(sumOfSquaredErrors / (matrix.length * matrix.length));
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | private int getSumOfRowInMatrix(double[][] matrix, int row) {
|
---|
| 248 | int rowSum = 0;
|
---|
| 249 | for (int col = 0; col < matrix[row].length; col++) {
|
---|
| 250 | rowSum += matrix[row][col];
|
---|
| 251 | }
|
---|
| 252 | return rowSum;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | private int getSumOfColInMatrix(double[][] matrix, int col) {
|
---|
| 256 | int colSum = 0;
|
---|
| 257 | for (int row = 0; row < matrix.length; row++) {
|
---|
| 258 | colSum += matrix[row][col];
|
---|
| 259 | }
|
---|
| 260 | return colSum;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | private void setIssueUtilitiesWithNormalization() {
|
---|
| 264 | double totalOfsumOfSquares = 0;
|
---|
| 265 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
| 266 | totalOfsumOfSquares += sumOfSquaredErrorsOfIssueValues[i];
|
---|
| 267 | }
|
---|
| 268 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
| 269 | eachIssueAndItsUtilityMap.put(i, sumOfSquaredErrorsOfIssueValues[i] / totalOfsumOfSquares);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | //System.out.println("----------------------AGENT------------------");
|
---|
| 273 | //printIntDoubleMap(eachIssueAndItsUtilityMap);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 | /*
|
---|
| 279 | private void printIntDoubleDoubleArrMap(Map<Integer, double[][]> eachIssueAnItsValuesDoubleArrayMap) {
|
---|
| 280 | System.out.println("EACH ISSUE AND ITS VALUES");
|
---|
| 281 | for (Entry<Integer, double[][]> entry : eachIssueAnItsValuesDoubleArrayMap.entrySet()) {
|
---|
| 282 | System.out.println(entry.getKey() + " ");
|
---|
| 283 | double[][] values = entry.getValue();
|
---|
| 284 | for (int j = 0; j < values.length; j++) {
|
---|
| 285 | for (int k = 0; k < values.length; k++) {
|
---|
| 286 | System.out.print(values[j][k] + " ");
|
---|
| 287 | }
|
---|
| 288 | System.out.println();
|
---|
| 289 | }
|
---|
| 290 | System.out.println();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | private void printIntDoubleArrMap(Map<Integer, double[]> map) {
|
---|
| 295 | System.out.println("EACH ISSUE AND ITS VALUES UTILITIES");
|
---|
| 296 | for (Entry<Integer, double[]> entry : map.entrySet()) {
|
---|
| 297 | System.out.print(entry.getKey() + " ");
|
---|
| 298 | double[] values = entry.getValue();
|
---|
| 299 | for (int j = 0; j < values.length; j++) {
|
---|
| 300 | System.out.print(values[j] + " ");
|
---|
| 301 | }
|
---|
| 302 | System.out.println();
|
---|
| 303 | }
|
---|
| 304 | System.out.println();
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | private void printIntDoubleMap(Map<Integer, Double> map) {
|
---|
| 308 | System.out.println("----------------------EACH ISSUE AND ITS UTILITIES------------------");
|
---|
| 309 | for (Entry<Integer, Double> entry : map.entrySet()) {
|
---|
| 310 | System.out.println(entry.getKey() + " " + entry.getValue());
|
---|
| 311 | }
|
---|
| 312 | System.out.println();
|
---|
| 313 | }
|
---|
| 314 | */
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | private double getSumOfRowInOneDimensionalMatrix(double[] matrix) {
|
---|
| 320 | double rowSum = 0;
|
---|
| 321 | for (int i = 0; i < matrix.length; i++) {
|
---|
| 322 | rowSum += matrix[i];
|
---|
| 323 | }
|
---|
| 324 | return rowSum;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 | /**
|
---|
| 330 | * Counts the number of equal values with another bid (assuming they are defined on the same domain)
|
---|
| 331 | */
|
---|
| 332 | public int countEqualValues(Bid b,Bid bOwn)
|
---|
| 333 | {
|
---|
| 334 | int count = 0;
|
---|
| 335 | for ( String iss : bOwn.getIssueValues().keySet() )
|
---|
| 336 | if ( bOwn.getValue(iss).equals(b.getValue(iss)) )
|
---|
| 337 | count++;
|
---|
| 338 | return count;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | public double getUtilityForBid(Bid bid) {
|
---|
| 346 |
|
---|
| 347 | double totalUtility = 0;
|
---|
| 348 | if (bid != null) {
|
---|
| 349 | int i = 0;
|
---|
| 350 | for (String issue : issList) {
|
---|
| 351 | Double utilityOfIssue = eachIssueAndItsUtilityMap.get(i);
|
---|
| 352 | DiscreteValue biggerBidValue = (DiscreteValue) (bid.getValue(issue));
|
---|
| 353 | int indexOfValue = getIndexOfValueInIssue(bid, issue, biggerBidValue );
|
---|
| 354 | double[] valueUtilities = eachIssueAndItsValuesUtilityMap.get(i);
|
---|
| 355 | double utilityOfValue = valueUtilities[indexOfValue];
|
---|
| 356 |
|
---|
| 357 | totalUtility += utilityOfIssue * utilityOfValue;
|
---|
| 358 | i++;
|
---|
| 359 | }
|
---|
| 360 | return totalUtility;
|
---|
| 361 | }
|
---|
| 362 | return totalUtility;
|
---|
| 363 |
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 |
|
---|
| 367 |
|
---|
| 368 | public GravityEs updateGravityModel( List<Bid> bidOrder2 ) {
|
---|
| 369 | return new GravityEs(this.domain, bidOrder2);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 |
|
---|
| 373 | }
|
---|