[1] | 1 | package agents.anac.y2013.AgentKF;
|
---|
| 2 |
|
---|
| 3 | import java.io.Serializable;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.List;
|
---|
| 7 | import java.util.Random;
|
---|
| 8 |
|
---|
| 9 | import genius.core.Agent;
|
---|
| 10 | import genius.core.Bid;
|
---|
| 11 | import genius.core.BidHistory;
|
---|
| 12 | import genius.core.actions.Accept;
|
---|
| 13 | import genius.core.actions.Action;
|
---|
| 14 | import genius.core.actions.ActionWithBid;
|
---|
| 15 | import genius.core.actions.EndNegotiation;
|
---|
| 16 | import genius.core.actions.Offer;
|
---|
| 17 | import genius.core.bidding.BidDetails;
|
---|
| 18 | import genius.core.issue.Issue;
|
---|
| 19 | import genius.core.issue.IssueDiscrete;
|
---|
| 20 | import genius.core.issue.IssueInteger;
|
---|
| 21 | import genius.core.issue.IssueReal;
|
---|
| 22 | import genius.core.issue.Value;
|
---|
| 23 | import genius.core.issue.ValueInteger;
|
---|
| 24 | import genius.core.issue.ValueReal;
|
---|
| 25 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
| 26 |
|
---|
| 27 | public class AgentKF extends Agent {
|
---|
| 28 |
|
---|
| 29 | // I want to print "state" when I print a message about saving data.
|
---|
| 30 | private String state;
|
---|
| 31 | private Action partner;
|
---|
| 32 | private HashMap<Bid, Double> offeredBidMap;
|
---|
| 33 | private double target;
|
---|
| 34 | private double bidTarget;
|
---|
| 35 | private double bidReduction;
|
---|
| 36 | private double sum;
|
---|
| 37 | private double sum2;
|
---|
| 38 | private int rounds;
|
---|
| 39 | private double tremor;
|
---|
| 40 | private int MaxLoopNum;
|
---|
| 41 |
|
---|
| 42 | private BidHistory currSessOppBidHistory = new BidHistory();
|
---|
| 43 | private BidHistory prevSessOppBidHistory = new BidHistory();
|
---|
| 44 | private double MINIMUM_BID_UTILITY;
|
---|
| 45 | private double MinAutoAcceptUtil;
|
---|
| 46 | private boolean FinalPhase;
|
---|
| 47 | private double PrevMean;
|
---|
| 48 |
|
---|
| 49 | @Override
|
---|
| 50 | public void init() {
|
---|
| 51 |
|
---|
| 52 | offeredBidMap = new HashMap<Bid, Double>();
|
---|
| 53 | target = 1.0;
|
---|
| 54 | bidTarget = 1.0;
|
---|
| 55 | bidReduction = 0.01;
|
---|
| 56 | sum = 0.0;
|
---|
| 57 | sum2 = 0.0;
|
---|
| 58 | rounds = 0;
|
---|
| 59 | tremor = 2.0;
|
---|
| 60 | MinAutoAcceptUtil = 0.8;
|
---|
| 61 | MaxLoopNum = 1000;
|
---|
| 62 | PrevMean = 0;
|
---|
| 63 | FinalPhase = false;
|
---|
| 64 |
|
---|
| 65 | MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
|
---|
| 66 |
|
---|
| 67 | myBeginSession();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public void myBeginSession() {
|
---|
| 71 |
|
---|
| 72 | // ---- Code for trying save and load functionality
|
---|
| 73 | // First try to load saved data
|
---|
| 74 | // ---- Loading from agent's function "loadSessionData"
|
---|
| 75 | Serializable prev = this.loadSessionData();
|
---|
| 76 | if (prev != null) {
|
---|
| 77 | prevSessOppBidHistory = (BidHistory) prev;
|
---|
| 78 | currSessOppBidHistory = prevSessOppBidHistory;
|
---|
| 79 | PrevMean = prevSessOppBidHistory
|
---|
| 80 | .getAverageDiscountedUtility(utilitySpace);
|
---|
| 81 | } else {
|
---|
| 82 | // If didn't succeed, it means there is no data for this preference
|
---|
| 83 | // profile
|
---|
| 84 | // in this domain.
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public String getVersion() {
|
---|
| 90 | return "1.1";
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
| 94 | public String getName() {
|
---|
| 95 | return "AgentKF";
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public void ReceiveMessage(Action opponentAction) {
|
---|
| 100 | partner = opponentAction;
|
---|
| 101 | if (opponentAction instanceof Offer) {
|
---|
| 102 | Bid bid = ((Offer) opponentAction).getBid();
|
---|
| 103 | // 2. store the opponent's trace
|
---|
| 104 | try {
|
---|
| 105 | BidDetails opponentBid = new BidDetails(bid,
|
---|
| 106 | utilitySpace.getUtility(bid), timeline.getTime());
|
---|
| 107 | currSessOppBidHistory.add(opponentBid);
|
---|
| 108 | } catch (Exception e) {
|
---|
| 109 | e.printStackTrace();
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | @Override
|
---|
| 115 | public Action chooseAction() {
|
---|
| 116 | Action action = null;
|
---|
| 117 | try {
|
---|
| 118 | if (partner == null) {
|
---|
| 119 | action = selectBid();
|
---|
| 120 | }
|
---|
| 121 | if (partner instanceof Offer) {
|
---|
| 122 | Bid offeredBid = ((Offer) partner).getBid();
|
---|
| 123 |
|
---|
| 124 | double p = acceptProbability(offeredBid);
|
---|
| 125 |
|
---|
| 126 | if (utilitySpace.getUtility(offeredBid) > MinAutoAcceptUtil) {
|
---|
| 127 | p = 1.0;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | if (rounds % 500 == 0) {
|
---|
| 131 | tremor += adjustTremor(timeline.getCurrentTime());
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | if (timeline.getCurrentTime() > 0.85) {
|
---|
| 135 | BidHistory FinalBidHistory = currSessOppBidHistory
|
---|
| 136 | .filterBetweenTime(timeline.getCurrentTime(), 1.0);
|
---|
| 137 | double FinalAvg = FinalBidHistory.getAverageUtility();
|
---|
| 138 | if (FinalAvg < sum / rounds) {
|
---|
| 139 | FinalPhase = true;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | if (p > Math.random()) {
|
---|
| 144 | action = new Accept(getAgentID(), offeredBid);
|
---|
| 145 | } else {
|
---|
| 146 | action = selectBid();
|
---|
| 147 | }
|
---|
| 148 | // ---- Code for trying save and load functionality
|
---|
| 149 | // /////////////////////////////////
|
---|
| 150 | state = "Opponet Send the Bid ";
|
---|
| 151 | tryToSaveAndPrintState();
|
---|
| 152 | // /////////////////////////////////
|
---|
| 153 | }
|
---|
| 154 | if (partner instanceof EndNegotiation) {
|
---|
| 155 | // ---- Code for trying save and load
|
---|
| 156 | // functionality///////////////////////////////////
|
---|
| 157 | state = "Got EndNegotiation from opponent. ";
|
---|
| 158 | tryToSaveAndPrintState();
|
---|
| 159 | // /////////////////////////////////
|
---|
| 160 | }
|
---|
| 161 | } catch (Exception e) {
|
---|
| 162 | // ---- Code for trying save and load functionality
|
---|
| 163 | // /////////////////////////////////
|
---|
| 164 | state = "Got Exception. ";
|
---|
| 165 | tryToSaveAndPrintState();
|
---|
| 166 | // /////////////////////////////////
|
---|
| 167 | action = new Accept(getAgentID(),
|
---|
| 168 | ((ActionWithBid) partner).getBid());
|
---|
| 169 | }
|
---|
| 170 | return action;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | // ---- Code for trying save and load functionality
|
---|
| 174 | private void tryToSaveAndPrintState() {
|
---|
| 175 |
|
---|
| 176 | // ---- Saving from agent's function "saveSessionData"
|
---|
| 177 | if (currSessOppBidHistory.size() < Math.pow(10, 5)) {
|
---|
| 178 | this.saveSessionData(currSessOppBidHistory);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | private Action selectBid() {
|
---|
| 183 | Bid nextBid = null;
|
---|
| 184 | double time = timeline.getTime();
|
---|
| 185 |
|
---|
| 186 | ArrayList<Bid> bidTemp = new ArrayList<Bid>();
|
---|
| 187 |
|
---|
| 188 | for (Bid bid : offeredBidMap.keySet()) {
|
---|
| 189 | if (offeredBidMap.get(bid) > target) {
|
---|
| 190 | bidTemp.add(bid);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | int size = bidTemp.size();
|
---|
| 195 | if (size > 0) {
|
---|
| 196 | int sindex = (int) Math.floor(Math.random() * size);
|
---|
| 197 | nextBid = bidTemp.get(sindex);
|
---|
| 198 | } else {
|
---|
| 199 | double searchUtil = 0.0;
|
---|
| 200 | try {
|
---|
| 201 | int loop = 0;
|
---|
| 202 | boolean NotFind = true;
|
---|
| 203 | ArrayList<Bid> AltNextBid = new ArrayList<Bid>();
|
---|
| 204 | while (loop < MaxLoopNum) {/* searchUtil < bidTarget */
|
---|
| 205 | if (loop == MaxLoopNum - 1 & NotFind) {
|
---|
| 206 | bidTarget -= bidReduction;
|
---|
| 207 | loop = 0;
|
---|
| 208 | }
|
---|
| 209 | Bid altNextBid = searchBid();
|
---|
| 210 | searchUtil = utilitySpace.getUtilityWithDiscount(altNextBid,
|
---|
| 211 | time);
|
---|
| 212 | if (searchUtil >= bidTarget) {
|
---|
| 213 | NotFind = false;
|
---|
| 214 | AltNextBid.add(altNextBid);
|
---|
| 215 | }
|
---|
| 216 | loop++;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | double minUtil = Double.MAX_VALUE;
|
---|
| 220 | Bid minBid = null;
|
---|
| 221 | for (int i = 0; i < AltNextBid.size(); i++) {
|
---|
| 222 | Bid bufBid = AltNextBid.get(i);
|
---|
| 223 | Double bufUtil = utilitySpace.getUtilityWithDiscount(bufBid,
|
---|
| 224 | time);
|
---|
| 225 | if (minUtil > bufUtil) {
|
---|
| 226 | minUtil = bufUtil;
|
---|
| 227 | minBid = bufBid;
|
---|
| 228 | } else if (minUtil == bufUtil) {
|
---|
| 229 | BidHistory simHistory = currSessOppBidHistory
|
---|
| 230 | .filterBetweenUtility(MINIMUM_BID_UTILITY, 1.0);
|
---|
| 231 | if (this.similarBid(simHistory, bufBid) < this
|
---|
| 232 | .similarBid(simHistory, minBid)) {
|
---|
| 233 | minBid = bufBid;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | nextBid = minBid;
|
---|
| 237 | }
|
---|
| 238 | } catch (Exception e) {
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | if (nextBid == null) {
|
---|
| 243 | return (new Accept(getAgentID(),
|
---|
| 244 | ((ActionWithBid) partner).getBid()));
|
---|
| 245 | }
|
---|
| 246 | return (new Offer(getAgentID(), nextBid));
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | private int similarBid(BidHistory theHistory, Bid theBid) throws Exception {
|
---|
| 250 | int Value = Integer.MAX_VALUE;
|
---|
| 251 | ArrayList<BidDetails> AltList = (ArrayList<BidDetails>) theHistory
|
---|
| 252 | .getNBestBids(theHistory.size() - 1);
|
---|
| 253 | for (int i = 0; i < AltList.size(); i++) {
|
---|
| 254 | Bid targetBid = AltList.get(i).getBid();
|
---|
| 255 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
| 256 | for (Issue lIssue : issues) {
|
---|
| 257 | switch (lIssue.getType()) {
|
---|
| 258 | case DISCRETE:
|
---|
| 259 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
| 260 | double weight_d = ((AdditiveUtilitySpace) utilitySpace)
|
---|
| 261 | .getWeight(lIssueDiscrete.getNumber());
|
---|
| 262 | if (theBid.getValue(lIssueDiscrete.getNumber()) == targetBid
|
---|
| 263 | .getValue(lIssueDiscrete.getNumber()))
|
---|
| 264 | Value += 1.0 * weight_d;
|
---|
| 265 | break;
|
---|
| 266 | case REAL:
|
---|
| 267 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
| 268 | double weight_r = ((AdditiveUtilitySpace) utilitySpace)
|
---|
| 269 | .getWeight(lIssueReal.getNumber());
|
---|
| 270 | if (theBid.getValue(lIssueReal.getNumber()) == targetBid
|
---|
| 271 | .getValue(lIssueReal.getNumber()))
|
---|
| 272 | Value += 1.0 * weight_r;
|
---|
| 273 | break;
|
---|
| 274 | case INTEGER:
|
---|
| 275 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
| 276 | double weight_i = ((AdditiveUtilitySpace) utilitySpace)
|
---|
| 277 | .getWeight(lIssueInteger.getNumber());
|
---|
| 278 | if (theBid.getValue(lIssueInteger.getNumber()) == targetBid
|
---|
| 279 | .getValue(lIssueInteger.getNumber()))
|
---|
| 280 | Value += 1.0 * weight_i;
|
---|
| 281 | break;
|
---|
| 282 | default:
|
---|
| 283 | throw new Exception("issue type " + lIssue.getType()
|
---|
| 284 | + " not supported by SimpleAgent2");
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 | return Value;
|
---|
| 289 |
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | private Bid searchBid() throws Exception {
|
---|
| 293 | HashMap<Integer, Value> values = new HashMap<Integer, Value>();
|
---|
| 294 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
| 295 | Random randomnr = new Random();
|
---|
| 296 |
|
---|
| 297 | Bid bid = null;
|
---|
| 298 |
|
---|
| 299 | for (Issue lIssue : issues) {
|
---|
| 300 | switch (lIssue.getType()) {
|
---|
| 301 | case DISCRETE:
|
---|
| 302 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
| 303 | int optionIndex = randomnr
|
---|
| 304 | .nextInt(lIssueDiscrete.getNumberOfValues());
|
---|
| 305 | values.put(lIssue.getNumber(),
|
---|
| 306 | lIssueDiscrete.getValue(optionIndex));
|
---|
| 307 | break;
|
---|
| 308 | case REAL:
|
---|
| 309 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
| 310 | int optionInd = randomnr.nextInt(
|
---|
| 311 | lIssueReal.getNumberOfDiscretizationSteps() - 1);
|
---|
| 312 | values.put(lIssueReal.getNumber(),
|
---|
| 313 | new ValueReal(lIssueReal.getLowerBound() + (lIssueReal
|
---|
| 314 | .getUpperBound() - lIssueReal.getLowerBound())
|
---|
| 315 | * (optionInd) / (lIssueReal
|
---|
| 316 | .getNumberOfDiscretizationSteps())));
|
---|
| 317 | break;
|
---|
| 318 | case INTEGER:
|
---|
| 319 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
| 320 | int optionIndex2 = lIssueInteger.getLowerBound()
|
---|
| 321 | + randomnr.nextInt(lIssueInteger.getUpperBound()
|
---|
| 322 | - lIssueInteger.getLowerBound());
|
---|
| 323 | values.put(lIssueInteger.getNumber(),
|
---|
| 324 | new ValueInteger(optionIndex2));
|
---|
| 325 | break;
|
---|
| 326 | default:
|
---|
| 327 | throw new Exception("issue type " + lIssue.getType()
|
---|
| 328 | + " not supported by SimpleAgent2");
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | bid = new Bid(utilitySpace.getDomain(), values);
|
---|
| 332 | return bid;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | private double adjustTremor(double time) {
|
---|
| 336 | if (currSessOppBidHistory.isEmpty()) {
|
---|
| 337 | return 0.0;
|
---|
| 338 | } else {
|
---|
| 339 | double avg = sum / rounds;
|
---|
| 340 | // double histry_avg =
|
---|
| 341 | // currSessOppBidHistory.getAverageDiscountedUtility(utilitySpace);
|
---|
| 342 | double histry_avg = currSessOppBidHistory
|
---|
| 343 | .filterBetweenTime(0.0, timeline.getCurrentTime())
|
---|
| 344 | .getAverageUtility();
|
---|
| 345 | if (avg > histry_avg) {
|
---|
| 346 | return 0.3;
|
---|
| 347 | } else {
|
---|
| 348 | return -0.3;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | double acceptProbability(Bid offeredBid) throws Exception {
|
---|
| 354 | double time = timeline.getTime();
|
---|
| 355 | double offeredUtility = utilitySpace.getUtilityWithDiscount(offeredBid,
|
---|
| 356 | time);
|
---|
| 357 | offeredBidMap.put(offeredBid, offeredUtility);
|
---|
| 358 |
|
---|
| 359 | sum += offeredUtility;
|
---|
| 360 | sum2 += offeredUtility * offeredUtility;
|
---|
| 361 | rounds++;
|
---|
| 362 |
|
---|
| 363 | double mean = sum / rounds;
|
---|
| 364 | mean = 0.7 * mean + 0.3 * PrevMean;
|
---|
| 365 |
|
---|
| 366 | double variance = (sum2 / rounds) - (mean * mean);
|
---|
| 367 |
|
---|
| 368 | double deviation = Math.sqrt(variance * 12);
|
---|
| 369 | if (Double.isNaN(deviation)) {
|
---|
| 370 | deviation = 0.0;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | double t = time * time * time;
|
---|
| 374 |
|
---|
| 375 | if (offeredUtility < 0 || offeredUtility > 1.05) {
|
---|
| 376 | throw new Exception("utility " + offeredUtility + " outside [0,1]");
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | if (t < 0 || t > 1) {
|
---|
| 380 | throw new Exception("time " + t + " outside [0,1]");
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | if (offeredUtility > 1.) {
|
---|
| 384 | offeredUtility = 1;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | double estimateMax = mean + ((1 - mean) * deviation);
|
---|
| 388 |
|
---|
| 389 | double alpha = 1 + tremor + (10 * mean) - (2 * tremor * mean);
|
---|
| 390 | double beta = alpha + (Math.random() * tremor) - (tremor / 2);
|
---|
| 391 |
|
---|
| 392 | double preTarget = 1 - (Math.pow(time, alpha) * (1 - estimateMax));
|
---|
| 393 | double preTarget2 = 1 - (Math.pow(time, beta) * (1 - estimateMax));
|
---|
| 394 |
|
---|
| 395 | double ratio = (deviation + 0.1) / (1 - preTarget);
|
---|
| 396 | if (Double.isNaN(ratio) || ratio > 2.0) {
|
---|
| 397 | ratio = 2.0;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | double ratio2 = (deviation + 0.1) / (1 - preTarget2);
|
---|
| 401 | if (Double.isNaN(ratio2) || ratio2 > 2.0) {
|
---|
| 402 | ratio2 = 2.0;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | target = ratio * preTarget + 1 - ratio;
|
---|
| 406 | bidTarget = ratio2 * preTarget2 + 1 - ratio2;
|
---|
| 407 |
|
---|
| 408 | double m = t * (-300) + 400;
|
---|
| 409 | if (target > estimateMax) {
|
---|
| 410 | double r = target - estimateMax;
|
---|
| 411 | double f = 1 / (r * r);
|
---|
| 412 | if (f > m || Double.isNaN(f))
|
---|
| 413 | f = m;
|
---|
| 414 | double app = r * f / m;
|
---|
| 415 | target = target - app;
|
---|
| 416 | } else {
|
---|
| 417 | target = estimateMax;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | if (bidTarget > estimateMax) {
|
---|
| 421 | double r = bidTarget - estimateMax;
|
---|
| 422 | double f = 1 / (r * r);
|
---|
| 423 | if (f > m || Double.isNaN(f))
|
---|
| 424 | f = m;
|
---|
| 425 | double app = r * f / m;
|
---|
| 426 | bidTarget = bidTarget - app;
|
---|
| 427 | } else {
|
---|
| 428 | bidTarget = estimateMax;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | // test code for Discount Factor
|
---|
| 432 | if (FinalPhase) {
|
---|
| 433 | double discount_utility = utilitySpace
|
---|
| 434 | .getUtilityWithDiscount(offeredBid, time);
|
---|
| 435 | double discount_ratio = discount_utility / offeredUtility;
|
---|
| 436 | if (!Double.isNaN(discount_utility)) {
|
---|
| 437 | target *= discount_ratio;
|
---|
| 438 | bidTarget *= discount_ratio;
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | // test code for Discount Factor
|
---|
| 442 |
|
---|
| 443 | double utilityEvaluation = offeredUtility - estimateMax;
|
---|
| 444 | double satisfy = offeredUtility - target;
|
---|
| 445 |
|
---|
| 446 | double p = (Math.pow(time, alpha) / 5) + utilityEvaluation + satisfy;
|
---|
| 447 | if (p < 0.1) {
|
---|
| 448 | p = 0.0;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | return p;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | @Override
|
---|
| 455 | public String getDescription() {
|
---|
| 456 | return "ANAC2013 compatible with non-linear utility spaces";
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | } |
---|