[1] | 1 | package geniusweb.exampleparties.simpleshaop;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collections;
|
---|
| 6 | import java.util.HashMap;
|
---|
| 7 | import java.util.Iterator;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 | import java.util.Map.Entry;
|
---|
| 11 | import java.util.Set;
|
---|
| 12 | import java.util.TreeMap;
|
---|
| 13 |
|
---|
| 14 | import geniusweb.issuevalue.Bid;
|
---|
| 15 | import geniusweb.issuevalue.Value;
|
---|
| 16 | import geniusweb.issuevalue.ValueSet;
|
---|
| 17 |
|
---|
| 18 | public class NegotiationInfo {
|
---|
| 19 | private CompRegress compRegress;
|
---|
| 20 | private List<String> issues;
|
---|
| 21 | private ArrayList<Bid> myBidHistory = null;
|
---|
| 22 | private ArrayList<Bid> bestOfferedBidHistory = null;
|
---|
| 23 | private ArrayList<Bid> opponentBidHistory = null;
|
---|
| 24 | private BigDecimal opponentSum;
|
---|
| 25 | private BigDecimal opponentPowSum;
|
---|
| 26 | private BigDecimal opponentAvg;
|
---|
| 27 | private BigDecimal opponentVar;
|
---|
| 28 | private HashMap<String, HashMap<Value, BigDecimal>> valueRelativeUtil = null;
|
---|
| 29 | private HashMap<String, HashMap<Value, Integer>> opponentValueFreq = null;
|
---|
| 30 | private BigDecimal bestOfferedUtil = BigDecimal.ZERO;
|
---|
| 31 |
|
---|
| 32 | private HashMap<String, Double> opponentTheta;
|
---|
| 33 | private HashMap<String, HashMap<Value, Double>> estOpponentUtil;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * NegotiationInfo - constructor
|
---|
| 37 | *
|
---|
| 38 | * @param utilitySpace
|
---|
| 39 | * @param isPrinting
|
---|
| 40 | */
|
---|
| 41 | public NegotiationInfo(CompRegress compRegress) {
|
---|
| 42 | this.compRegress = compRegress;
|
---|
| 43 | this.issues = compRegress.getIssues();
|
---|
| 44 |
|
---|
| 45 | this.myBidHistory = new ArrayList<>();
|
---|
| 46 | this.bestOfferedBidHistory = new ArrayList<>();
|
---|
| 47 |
|
---|
| 48 | this.valueRelativeUtil = new HashMap<String, HashMap<Value, BigDecimal>>();
|
---|
| 49 |
|
---|
| 50 | this.opponentBidHistory = new ArrayList<Bid>();
|
---|
| 51 | this.opponentSum = BigDecimal.ZERO;
|
---|
| 52 | this.opponentPowSum = BigDecimal.ZERO;
|
---|
| 53 | this.opponentAvg = BigDecimal.ZERO;
|
---|
| 54 | this.opponentVar = BigDecimal.ZERO;
|
---|
| 55 |
|
---|
| 56 | this.opponentValueFreq = new HashMap<String, HashMap<Value, Integer>>();
|
---|
| 57 |
|
---|
| 58 | this.opponentTheta = new HashMap<String, Double>();
|
---|
| 59 | this.estOpponentUtil = new HashMap<String, HashMap<Value, Double>>();
|
---|
| 60 |
|
---|
| 61 | try {
|
---|
| 62 | initValueRelativeUtil();
|
---|
| 63 | } catch (Exception e) {
|
---|
| 64 | System.out.println("initValueRelativeUtil failed");
|
---|
| 65 | e.printStackTrace();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * initValueRelativeUtil - initialize the relative utilities of all values
|
---|
| 71 | *
|
---|
| 72 | * @throws Exception
|
---|
| 73 | */
|
---|
| 74 | private void initValueRelativeUtil() throws Exception {
|
---|
| 75 | ArrayList<Value> values = null;
|
---|
| 76 | for (String issue : issues) {
|
---|
| 77 | valueRelativeUtil.put(issue, new HashMap<Value, BigDecimal>());
|
---|
| 78 | values = getValues(issue);
|
---|
| 79 | for (Value value : values) {
|
---|
| 80 | valueRelativeUtil.get(issue).put(value, BigDecimal.ZERO);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 86 | // method initOpponent and its helper functions used in ShaopParty
|
---|
| 87 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * initOpponent - initialize the opponent related parameters
|
---|
| 91 | * calls private methods initOpponentInfo, initOpponentsValueFreq
|
---|
| 92 | *
|
---|
| 93 | * @param sender - the opponent agent to initialize
|
---|
| 94 | */
|
---|
| 95 | public void initOpponent() {
|
---|
| 96 | initOpponentValueFreq();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * initOpponetsValueFreq - initialize opponent's frequency of all values
|
---|
| 101 | *
|
---|
| 102 | * @param sender - the opponent agent to initialize
|
---|
| 103 | * @throws Exception
|
---|
| 104 | */
|
---|
| 105 | private void initOpponentValueFreq() {
|
---|
| 106 | for (String issue : issues) {
|
---|
| 107 | opponentValueFreq.put(issue, new HashMap<Value, Integer>());
|
---|
| 108 | ArrayList<Value> values = getValues(issue);
|
---|
| 109 | for (Value value : values) {
|
---|
| 110 | opponentValueFreq.get(issue).put(value, 0);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 116 | // method updateInfo and its helper functions used in ShaopParty
|
---|
| 117 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * updateInfo - updates negotiation information
|
---|
| 121 | * calls updateNegotiatingInfo, updateFreqLists
|
---|
| 122 | *
|
---|
| 123 | * @param sender - the opponent agent to update
|
---|
| 124 | * @param offeredBid - Bid; the offered Bid
|
---|
| 125 | */
|
---|
| 126 | public void updateInfo(Bid offeredBid) {
|
---|
| 127 | try {
|
---|
| 128 | updateNegotiatingInfo(offeredBid);
|
---|
| 129 | } catch (Exception e) {
|
---|
| 130 | System.out.println("updateNegotiatingInfo failed");
|
---|
| 131 | e.printStackTrace();
|
---|
| 132 | }
|
---|
| 133 | try {
|
---|
| 134 | updateFreqLists(offeredBid);
|
---|
| 135 | } catch (Exception e) {
|
---|
| 136 | System.out.println("updateFreqLists failed");
|
---|
| 137 | e.printStackTrace();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * updateNegotiatingInfo - updates negotiating info of opponent and offered bid
|
---|
| 143 | *
|
---|
| 144 | * @param sender - the opponent agent to update
|
---|
| 145 | * @param offeredBid - Bid; the offered Bid
|
---|
| 146 | * @throws Exception
|
---|
| 147 | */
|
---|
| 148 | private void updateNegotiatingInfo(Bid offeredBid) throws Exception {
|
---|
| 149 | BigDecimal util = compRegress.getUtil(offeredBid);
|
---|
| 150 |
|
---|
| 151 | opponentBidHistory.add(offeredBid);
|
---|
| 152 | opponentSum = opponentSum.add(util);
|
---|
| 153 | opponentPowSum = opponentPowSum.add(util.pow(2));
|
---|
| 154 |
|
---|
| 155 | BigDecimal roundNum = BigDecimal.valueOf(opponentBidHistory.size());
|
---|
| 156 |
|
---|
| 157 | opponentAvg = opponentSum.divide(roundNum);
|
---|
| 158 | opponentVar = (opponentPowSum.divide(roundNum)).subtract(opponentAvg.pow(2));
|
---|
| 159 | if (opponentVar.compareTo(BigDecimal.ZERO) < 0) {
|
---|
| 160 | opponentVar = BigDecimal.ZERO;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if (util.compareTo(bestOfferedUtil) > 0) {
|
---|
| 164 | bestOfferedBidHistory.add(offeredBid);
|
---|
| 165 | bestOfferedUtil = util;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * updateFreqLists - updates the two frequency HashMaps
|
---|
| 171 | *
|
---|
| 172 | * @param sender - the opponent agent to update
|
---|
| 173 | * @param offeredBid - Bid; the offered Bid
|
---|
| 174 | * @throws Exception
|
---|
| 175 | */
|
---|
| 176 | private void updateFreqLists(Bid offeredBid) throws Exception {
|
---|
| 177 | for (String issue : issues) {
|
---|
| 178 | Value value = offeredBid.getValue(issue);
|
---|
| 179 | opponentValueFreq.get(issue).put(value, opponentValueFreq.
|
---|
| 180 | get(issue).get(value) + 1);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 185 | // method updateMyBidHistory used in ShaopParty
|
---|
| 186 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * updateMyBidHistory
|
---|
| 190 | *
|
---|
| 191 | * @param offerBid - the bid to be added to history list
|
---|
| 192 | */
|
---|
| 193 | public void updateMyBidHistory(Bid offerBid) {
|
---|
| 194 | myBidHistory.add(offerBid);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * getBestOfferedBid
|
---|
| 199 | * @return the offered bid with the highest estimated utility
|
---|
| 200 | */
|
---|
| 201 | public Bid getBestOfferedBid() {
|
---|
| 202 | if (bestOfferedBidHistory.isEmpty()) return null;
|
---|
| 203 | else return bestOfferedBidHistory.get(bestOfferedBidHistory.size()-1);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 208 | // public methods used in NegotiationStrategy
|
---|
| 209 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * getBestOfferedUtil - gets the best offered utility
|
---|
| 213 | *
|
---|
| 214 | * @return bestOfferedUtil
|
---|
| 215 | */
|
---|
| 216 | public BigDecimal getBestOfferedUtil() {
|
---|
| 217 | return bestOfferedUtil;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * getValues - returns an ArrayList of all the values of an issue
|
---|
| 223 | *
|
---|
| 224 | * @param issue - String; the issue name
|
---|
| 225 | * @return a list of all the values associated with the input issue
|
---|
| 226 | */
|
---|
| 227 | private ArrayList<Value> getValues(String issue) {
|
---|
| 228 | ArrayList<Value> values = new ArrayList<Value>();
|
---|
| 229 | ValueSet valueSet = compRegress.getValues(issue);
|
---|
| 230 | for (Value value : valueSet) {
|
---|
| 231 | values.add(value);
|
---|
| 232 | }
|
---|
| 233 | return values;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 237 | // public methods used to model opponent
|
---|
| 238 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 239 |
|
---|
| 240 | /**
|
---|
| 241 | * estimates the theta values and acceptance probabilities of opponent
|
---|
| 242 | */
|
---|
| 243 | public void initOpponentProbs() {
|
---|
| 244 | TreeMap<Double, List<String>> importanceMap =
|
---|
| 245 | new TreeMap<Double, List<String>>();
|
---|
| 246 | for (String issue : issues) {
|
---|
| 247 | int totalFreq = 0;
|
---|
| 248 | int squaredSum = 0;
|
---|
| 249 | int maxFreq = 0;
|
---|
| 250 | Value maxFreqValue = null;
|
---|
| 251 | ValueSet values = compRegress.getValues(issue);
|
---|
| 252 | estOpponentUtil.put(issue, new HashMap<Value, Double>());
|
---|
| 253 | for (Value value : values) {
|
---|
| 254 | int freq = opponentValueFreq.get(issue).get(value);
|
---|
| 255 | totalFreq += freq;
|
---|
| 256 | squaredSum += freq * freq;
|
---|
| 257 | if (freq > maxFreq) {
|
---|
| 258 | maxFreq = freq;
|
---|
| 259 | maxFreqValue = value;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | for (Value value : values) {
|
---|
| 264 | int freq = opponentValueFreq.get(issue).get(value);
|
---|
| 265 | double estUtil;
|
---|
| 266 | if (value == maxFreqValue) estUtil = 1.0;
|
---|
| 267 | else {
|
---|
| 268 | if (totalFreq-maxFreq == 0) estUtil = 0.0;
|
---|
| 269 | else estUtil = freq*1.0/(totalFreq-maxFreq);
|
---|
| 270 | }
|
---|
| 271 | estOpponentUtil.get(issue).put(value, estUtil);
|
---|
| 272 | }
|
---|
| 273 | double importance;
|
---|
| 274 | if (totalFreq - maxFreq == 0) importance = 1.0;
|
---|
| 275 | else importance = (((squaredSum - maxFreq * maxFreq) * 1.0 /
|
---|
| 276 | (totalFreq - maxFreq)) + maxFreq) / totalFreq;
|
---|
| 277 | List<String> issueList;
|
---|
| 278 | if (importanceMap.get(importance) == null) issueList =
|
---|
| 279 | new ArrayList<String>();
|
---|
| 280 | else issueList = importanceMap.get(importance);
|
---|
| 281 | issueList.add(issue);
|
---|
| 282 | importanceMap.put(importance, issueList);
|
---|
| 283 |
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | initOpponentTheta(importanceMap);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * creates the opponent theta values for corresponding issues
|
---|
| 291 | * @param importanceMap - map of importance and issues in descending order
|
---|
| 292 | */
|
---|
| 293 | private void initOpponentTheta(TreeMap<Double, List<String>> importanceMap) {
|
---|
| 294 | double multInverse = 1.0 / issues.size();
|
---|
| 295 | double minTheta = multInverse / 2.0;
|
---|
| 296 | double incr = multInverse / (issues.size() - 1);
|
---|
| 297 |
|
---|
| 298 | int i = 0;
|
---|
| 299 | for (Map.Entry<Double, List<String>> mapElement : importanceMap.entrySet()) {
|
---|
| 300 | List<String> issueList = mapElement.getValue();
|
---|
| 301 | int listSize = issueList.size();
|
---|
| 302 | double theta = minTheta + incr * (i + 0.5*(listSize - 1));
|
---|
| 303 | for (int j = 0; j < listSize; j++) {
|
---|
| 304 | String issue = issueList.get(j);
|
---|
| 305 | opponentTheta.put(issue, theta);
|
---|
| 306 | }
|
---|
| 307 | i += listSize;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /**
|
---|
| 312 | * gets a list of bids in descending order of preference
|
---|
| 313 | * @param myPrefBids - a list of bids to order
|
---|
| 314 | * @param time - current time
|
---|
| 315 | * @return an ordered list of bids based on joint utility
|
---|
| 316 | */
|
---|
| 317 | public List<Bid> getJointPref(List<Bid> myPrefBids, double time) {
|
---|
| 318 | BigDecimal biasFactor = BigDecimal.valueOf(1.8 - time * time * 0.3);
|
---|
| 319 | TreeMap<BigDecimal, Bid> jointUtil = new TreeMap<BigDecimal,
|
---|
| 320 | Bid>(Collections.reverseOrder());
|
---|
| 321 | for (Bid bid : myPrefBids) {
|
---|
| 322 | BigDecimal opponentUtil = BigDecimal.valueOf(opponentAcceptProb(bid));
|
---|
| 323 | BigDecimal util = compRegress.getUtil(bid).multiply(biasFactor).
|
---|
| 324 | add(opponentUtil);
|
---|
| 325 | jointUtil.put(util, bid);
|
---|
| 326 | }
|
---|
| 327 | List<Bid> bidOrder = new ArrayList<Bid>();
|
---|
| 328 | Set<Entry<BigDecimal, Bid>> set = jointUtil.entrySet();
|
---|
| 329 | Iterator<Entry<BigDecimal, Bid>> i = set.iterator();
|
---|
| 330 | while (i.hasNext()) {
|
---|
| 331 | Map.Entry<BigDecimal, Bid> map = (Map.Entry<BigDecimal, Bid>) i.next();
|
---|
| 332 | bidOrder.add(map.getValue());
|
---|
| 333 | }
|
---|
| 334 | return bidOrder;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /**
|
---|
| 338 | * gets the estimated acceptance probability of a bid
|
---|
| 339 | * @param bid - the bid we want to know the acceptance probability
|
---|
| 340 | * @return the estimate acceptance probability of the input bid
|
---|
| 341 | */
|
---|
| 342 | private double opponentAcceptProb(Bid bid) {
|
---|
| 343 | double prob = 0.0;
|
---|
| 344 | for (String issue : issues) {
|
---|
| 345 | Value value = bid.getValue(issue);
|
---|
| 346 | prob += opponentTheta.get(issue) * estOpponentUtil.get(issue).get(value);
|
---|
| 347 | }
|
---|
| 348 | return prob;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
| 352 | * updates the compRegress
|
---|
| 353 | * @param newCompRegress - the new compRegress
|
---|
| 354 | */
|
---|
| 355 | public void updateCompRegress(CompRegress newCompRegress) {
|
---|
| 356 | this.compRegress = newCompRegress;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | }
|
---|