source: src/main/java/agents/anac/y2010/Yushu/Utility.java@ 84

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

Initial import : Genius 9.0.0

File size: 4.8 KB
Line 
1package agents.anac.y2010.Yushu;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Agent;
8import genius.core.Bid;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.Offer;
12import genius.core.issue.Issue;
13import genius.core.issue.IssueDiscrete;
14import genius.core.issue.IssueInteger;
15import genius.core.issue.IssueReal;
16import genius.core.issue.Value;
17import genius.core.issue.ValueInteger;
18import genius.core.issue.ValueReal;
19import genius.core.utility.AdditiveUtilitySpace;
20
21public class Utility {
22 static double MINIMUM_BID_UTILITY = 0.95;
23
24 /**
25 * Wrapper for getRandomBid, for convenience.
26 *
27 * @param lastOppBid
28 * the last opponent bid that was received.
29 * @return new Action(Bid(..)), with bid utility > MINIMUM_BID_UTIL. If a
30 * problem occurs, it returns an Accept() action.
31 */
32 public static Action chooseRandomBidAction(Agent agent, Bid lastOppBid) {
33 Bid nextBid = null;
34 try {
35 nextBid = Utility
36 .getRandomBid((AdditiveUtilitySpace) agent.utilitySpace);
37 } catch (Exception e) {
38 e.printStackTrace();
39 }
40 if (nextBid == null)
41 return (new Accept(agent.getAgentID(), lastOppBid));
42 return (new Offer(agent.getAgentID(), nextBid));
43 }
44
45 /**
46 * @return a random bid with high enough utility value.
47 * @throws Exception
48 * if we can't compute the utility (eg no evaluators have been
49 * set) or when other evaluators than a DiscreteEvaluator are
50 * present in the util space.
51 */
52 public static Bid getRandomBid(AdditiveUtilitySpace utilityspace)
53 throws Exception {
54 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
55 // <issuenumber,chosen
56 // value
57 // string>
58 List<Issue> issues = utilityspace.getDomain().getIssues();
59 Random randomnr = new Random();
60 // createFrom a random bid with utility>MINIMUM_BID_UTIL.
61 // note that this may never succeed if you set MINIMUM too high!!!
62 // in that case we will search for a bid till the time is up (2 minutes)
63 // but this is just a simple agent.
64 Bid bid = null;
65 do {
66
67 for (Issue lIssue : issues) {
68 switch (lIssue.getType()) {
69 case DISCRETE:
70 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
71 int optionIndex = randomnr.nextInt(lIssueDiscrete
72 .getNumberOfValues());
73 values.put(lIssue.getNumber(),
74 lIssueDiscrete.getValue(optionIndex));
75 break;
76 case REAL:
77 IssueReal lIssueReal = (IssueReal) lIssue;
78 int optionInd = randomnr.nextInt(lIssueReal
79 .getNumberOfDiscretizationSteps() - 1);
80 values.put(
81 lIssueReal.getNumber(),
82 new ValueReal(lIssueReal.getLowerBound()
83 + (lIssueReal.getUpperBound() - lIssueReal
84 .getLowerBound())
85 * (double) (optionInd)
86 / (double) (lIssueReal
87 .getNumberOfDiscretizationSteps())));
88 break;
89 case INTEGER:
90 IssueInteger lIssueInteger = (IssueInteger) lIssue;
91 int optionIndex2 = lIssueInteger.getLowerBound()
92 + randomnr.nextInt(lIssueInteger.getUpperBound()
93 - lIssueInteger.getLowerBound());
94 values.put(lIssueInteger.getNumber(), new ValueInteger(
95 optionIndex2));
96 break;
97 default:
98 throw new Exception("issue type " + lIssue.getType()
99 + " not supported by SimpleAgent2");
100 }
101 }
102 bid = new Bid(utilityspace.getDomain(), values);
103 } while (utilityspace.getUtility(bid) < MINIMUM_BID_UTILITY);
104
105 return bid;
106 }
107
108 /**
109 * This function determines the accept probability for an offer. At t=0 it
110 * will prefer high-utility offers. As t gets closer to 1, it will accept
111 * lower utility offers with increasing probability. it will never accept
112 * offers with utility 0.
113 *
114 * @param u
115 * is the utility
116 * @param t
117 * is the time as fraction of the total available time (t=0 at
118 * start, and t=1 at end time)
119 * @return the probability of an accept at time t
120 * @throws Exception
121 * if you use wrong values for u or t.
122 *
123 */
124 public static double Paccept(double u, double t1) throws Exception {
125 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
126 if (u < 0 || u > 1.05)
127 throw new Exception("utility " + u + " outside [0,1]");
128 // normalization may be slightly off, therefore we have a broad boundary
129 // up to 1.05
130 if (t < 0 || t > 1)
131 throw new Exception("time " + t + " outside [0,1]");
132 if (u > 1.)
133 u = 1;
134 if (t == 0.5)
135 return u;
136 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u
137 * (-1. + 2 * t))))
138 / (-1. + 2 * t);
139 }
140
141 public static double sq(double x) {
142 return x * x;
143 }
144
145}
Note: See TracBrowser for help on using the repository browser.