package geniusweb.exampleparties.simpleshaop; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.issuevalue.Domain; import geniusweb.issuevalue.Value; public class GravityEs { private static final double HALF_OF_TIME = 0.5; Bid sampleBid; private List orderedBidListInAscendingOrder; private List listOfIssues; private int numberOfIssues; Map eachIssueAnItsIndexingValuesDoubleArrayMap; Map eachIssueAndItsValuesUtilityMap; double[] sumOfSquaredErrorsOfIssueValues; Map eachIssueAndItsUtilityMap; // opponent modeling Bid lastReceivedBid; Map eachIssueAndItsValuesFrequencyArrayMap; Map opponentEachIssueAndItsValuesUtilityMap; double[] opponentSumOfSquaredErrosOfEachIssueMatrix; Map opponentEachIssueAndItsUtilityMap; double lastOfferingTime; Bid lastOfferedBid; Bid nextBid; private List bidOrder; private Set issList; private Domain domain; HashMap> issueValindex = new HashMap>(); public GravityEs(Domain domain, List bidOrder) { this.bidOrder = bidOrder; this.domain = domain; issList = domain.getIssues(); for ( String iss : issList ) { List vaList = new ArrayList(); for ( Value val : domain.getValues(iss) ) { vaList.add(val); } issueValindex.put(iss, vaList); } initAgentVariables(); initOpponentVars(); } // uncertain preferences private void initAgentVariables() { initUncertainPrefVariables(); createCopelandMatrices(); fillAgentVars(); } private void fillAgentVars() { fillCopelandMatrices(); setValueUtilities(); fillMatriceOfNoChangeOfEachIssue(); setIssueUtilitiesWithNormalization(); } // opponent modeling private void initOpponentVars() { initOpponentModelingVars(); createFrequencyArrays(); } private void initUncertainPrefVariables() { //this.outcomeSpace = new SortedOutcomeSpace(utilitySpace); this.orderedBidListInAscendingOrder = bidOrder; this.sampleBid = orderedBidListInAscendingOrder.get(0); //this.listOfIssues = space.getDomain().getIssues(); this.numberOfIssues = issList.size(); this.eachIssueAnItsIndexingValuesDoubleArrayMap = new HashMap(); this.eachIssueAndItsValuesUtilityMap = new HashMap(); this.sumOfSquaredErrorsOfIssueValues = new double[numberOfIssues]; this.eachIssueAndItsUtilityMap = new HashMap(); } private void initOpponentModelingVars() { this.eachIssueAndItsValuesFrequencyArrayMap = new HashMap(); this.opponentEachIssueAndItsValuesUtilityMap = new HashMap(); this.opponentSumOfSquaredErrosOfEachIssueMatrix = new double[numberOfIssues]; this.opponentEachIssueAndItsUtilityMap = new HashMap(); } private void createCopelandMatrices() { int i = 0; for ( String iss : issList ) { int valueSize = domain.getValues(iss).size().intValue() ; eachIssueAnItsIndexingValuesDoubleArrayMap.put(i, new double[valueSize][valueSize]); i++; } } private void createFrequencyArrays() { int i = 0; for ( String issue : issList ) { int valueSize = domain.getValues(issue).size().intValue(); eachIssueAndItsValuesFrequencyArrayMap.put(i, new double[valueSize]); i++; } } /* * Gets the bigger bid from the sorted list . Gets the smaller bids from the * sorted list. Does pairwise Copeland comparison with one bigger bid and * smaller bids. depending on the bigger bid > all smaller bids */ private void fillCopelandMatrices() { for (int i = orderedBidListInAscendingOrder.size() - 1; i > 0; i--) { Bid biggerBid = orderedBidListInAscendingOrder.get(i); for (int j = i - 1; j >= 0; j--) { Bid smallerBid = orderedBidListInAscendingOrder.get(j); fillCopelandMatriceWithBiggerAndSmaller(biggerBid, smallerBid, i, j); } //System.out.println(biggerBid); } //printIntDoubleDoubleArrMap(eachIssueAnItsIndexingValuesDoubleArrayMap); } /* * pairwise Copeland comparison in each issue value, update the frequency * matrices for different issue values depending on the similarity. */ private void fillCopelandMatriceWithBiggerAndSmaller(Bid biggerBid, Bid smallerBid, int biggerIndex, int smallerIndex) { int i = 0; for (String issue : domain.getIssues()) { DiscreteValue biggerBidValue = (DiscreteValue) ( biggerBid.getValue(issue) ); int biggerBidValueIndex = getIndexOfValueInIssue(biggerBid, issue, biggerBidValue); DiscreteValue smallerValue = (DiscreteValue) (smallerBid.getValue(issue) ); int smallerBidValueIndex = getIndexOfValueInIssue(smallerBid, issue, smallerValue); int numberOfSimilarities = countEqualValues(smallerBid, biggerBid); if (numberOfSimilarities > 0) { eachIssueAnItsIndexingValuesDoubleArrayMap.get(i)[biggerBidValueIndex][smallerBidValueIndex] += (1d / (biggerIndex - smallerIndex)) * numberOfSimilarities; } i++; } } private int getIndexOfValueInIssue(Bid bid, String issue, Value value) { List valList = issueValindex.get(issue); for ( int i = 0; i < valList.size(); i++ ) if ( valList.get(i).equals(value) ) return i; return -1; } private void setValueUtilities() { int i = 0; for ( String issueDiscrete : issList ) { int valueSize = domain.getValues(issueDiscrete).size().intValue(); double[] valuesBeingBigInfoArray = new double[valueSize]; double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i); for (int j = 0; j < valueSize; j++) { double sumOfRowInMatrix = getSumOfRowInMatrix(matrix, j); double sumOfColInMatrix = getSumOfColInMatrix(matrix, j); double total = sumOfColInMatrix + sumOfRowInMatrix; if (total == 0) { valuesBeingBigInfoArray[j] = 0; } else { double beingBigPercentage = (sumOfRowInMatrix) / total; valuesBeingBigInfoArray[j] = beingBigPercentage; } } normalize(i, valueSize, valuesBeingBigInfoArray); i++; } //System.out.println("------------AGENT------------"); //printIntDoubleArrMap(eachIssueAndItsValuesUtilityMap); } private void normalize(int i, int valueSize, double[] valuesBeingBigInfoArray) { double[] utilityArr = new double[valueSize]; double totalSum = getSumOfRowInOneDimensionalMatrix(valuesBeingBigInfoArray); for (int j = 0; j < valueSize; j++) { if (totalSum == 0) { utilityArr[j] = 0; } else { utilityArr[j] = valuesBeingBigInfoArray[j] / totalSum; } } eachIssueAndItsValuesUtilityMap.put(i, utilityArr); } private void fillMatriceOfNoChangeOfEachIssue() { for (int i = 0; i < numberOfIssues; i++) { double[][] matrix = eachIssueAnItsIndexingValuesDoubleArrayMap.get(i); double sumOfMatrix = 0; for (int j = 0; j < matrix.length; j++) { sumOfMatrix += getSumOfRowInMatrix(matrix, j); } //System.out.println("Sum of matrix: " + sumOfMatrix); double average = sumOfMatrix / (matrix.length * matrix.length); //System.out.println("average of matrix: " + average); double sumOfSquaredErrors = 0; for (int j = 0; j < matrix.length; j++) { for (int k = 0; k < matrix.length; k++) { sumOfSquaredErrors += Math.pow(matrix[j][k] - average, 2); } } sumOfSquaredErrorsOfIssueValues[i] = Math.sqrt(sumOfSquaredErrors / (matrix.length * matrix.length)); } } private int getSumOfRowInMatrix(double[][] matrix, int row) { int rowSum = 0; for (int col = 0; col < matrix[row].length; col++) { rowSum += matrix[row][col]; } return rowSum; } private int getSumOfColInMatrix(double[][] matrix, int col) { int colSum = 0; for (int row = 0; row < matrix.length; row++) { colSum += matrix[row][col]; } return colSum; } private void setIssueUtilitiesWithNormalization() { double totalOfsumOfSquares = 0; for (int i = 0; i < numberOfIssues; i++) { totalOfsumOfSquares += sumOfSquaredErrorsOfIssueValues[i]; } for (int i = 0; i < numberOfIssues; i++) { eachIssueAndItsUtilityMap.put(i, sumOfSquaredErrorsOfIssueValues[i] / totalOfsumOfSquares); } //System.out.println("----------------------AGENT------------------"); //printIntDoubleMap(eachIssueAndItsUtilityMap); } /* private void printIntDoubleDoubleArrMap(Map eachIssueAnItsValuesDoubleArrayMap) { System.out.println("EACH ISSUE AND ITS VALUES"); for (Entry entry : eachIssueAnItsValuesDoubleArrayMap.entrySet()) { System.out.println(entry.getKey() + " "); double[][] values = entry.getValue(); for (int j = 0; j < values.length; j++) { for (int k = 0; k < values.length; k++) { System.out.print(values[j][k] + " "); } System.out.println(); } System.out.println(); } } private void printIntDoubleArrMap(Map map) { System.out.println("EACH ISSUE AND ITS VALUES UTILITIES"); for (Entry entry : map.entrySet()) { System.out.print(entry.getKey() + " "); double[] values = entry.getValue(); for (int j = 0; j < values.length; j++) { System.out.print(values[j] + " "); } System.out.println(); } System.out.println(); } private void printIntDoubleMap(Map map) { System.out.println("----------------------EACH ISSUE AND ITS UTILITIES------------------"); for (Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } System.out.println(); } */ private double getSumOfRowInOneDimensionalMatrix(double[] matrix) { double rowSum = 0; for (int i = 0; i < matrix.length; i++) { rowSum += matrix[i]; } return rowSum; } /** * Counts the number of equal values with another bid (assuming they are defined on the same domain) */ public int countEqualValues(Bid b,Bid bOwn) { int count = 0; for ( String iss : bOwn.getIssueValues().keySet() ) if ( bOwn.getValue(iss).equals(b.getValue(iss)) ) count++; return count; } public double getUtilityForBid(Bid bid) { double totalUtility = 0; if (bid != null) { int i = 0; for (String issue : issList) { Double utilityOfIssue = eachIssueAndItsUtilityMap.get(i); DiscreteValue biggerBidValue = (DiscreteValue) (bid.getValue(issue)); int indexOfValue = getIndexOfValueInIssue(bid, issue, biggerBidValue ); double[] valueUtilities = eachIssueAndItsValuesUtilityMap.get(i); double utilityOfValue = valueUtilities[indexOfValue]; totalUtility += utilityOfIssue * utilityOfValue; i++; } return totalUtility; } return totalUtility; } public GravityEs updateGravityModel( List bidOrder2 ) { return new GravityEs(this.domain, bidOrder2); } }