source: src/main/java/agents/anac/y2011/IAMhaggler2011/RandomBidCreator.java

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

Initial import : Genius 9.0.0

File size: 4.0 KB
Line 
1package agents.anac.y2011.IAMhaggler2011;
2
3import java.util.HashMap;
4import java.util.Iterator;
5import java.util.List;
6import java.util.Random;
7
8import genius.core.Bid;
9import genius.core.Domain;
10import genius.core.issue.Issue;
11import genius.core.issue.IssueDiscrete;
12import genius.core.issue.IssueInteger;
13import genius.core.issue.IssueReal;
14import genius.core.issue.Value;
15import genius.core.issue.ValueInteger;
16import genius.core.issue.ValueReal;
17import genius.core.utility.AdditiveUtilitySpace;
18
19public class RandomBidCreator implements BidCreator {
20
21 protected Random random;
22 private boolean TEST_EQUIVALENCE = false;
23
24 public RandomBidCreator() {
25 if (TEST_EQUIVALENCE) {
26 random = new Random(100);
27 } else {
28 random = new Random();
29 }
30 }
31
32 /**
33 * Get a random bid.
34 *
35 * @param utilitySpace
36 * The utility space to generate the random bid from.
37 * @return a random bid.
38 */
39 private Bid getRandomBid(AdditiveUtilitySpace utilitySpace) {
40 Domain domain = utilitySpace.getDomain();
41 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
42 List<Issue> issues = domain.getIssues();
43 Bid bid = null;
44 for (Iterator<Issue> iterator = issues.iterator(); iterator.hasNext();) {
45 Issue issue = (Issue) iterator.next();
46 switch (issue.getType()) {
47 case DISCRETE:
48 generateValue(values, (IssueDiscrete) issue);
49 break;
50
51 case REAL:
52 generateValue(values, (IssueReal) issue);
53 break;
54
55 case INTEGER:
56 generateValue(values, (IssueInteger) issue);
57 break;
58 }
59 }
60
61 try {
62 bid = new Bid(domain, values);
63 } catch (Exception e) {
64 }
65 return bid;
66 }
67
68 protected void generateValue(HashMap<Integer, Value> values,
69 IssueDiscrete issue) {
70 int randomDiscrete = random.nextInt(issue.getNumberOfValues());
71 values.put(Integer.valueOf(issue.getNumber()),
72 issue.getValue(randomDiscrete));
73 }
74
75 protected void generateValue(HashMap<Integer, Value> values, IssueReal issue) {
76 double randomReal = issue.getLowerBound() + random.nextDouble()
77 * (issue.getUpperBound() - issue.getLowerBound());
78 values.put(Integer.valueOf(issue.getNumber()),
79 new ValueReal(randomReal));
80 }
81
82 protected void generateValue(HashMap<Integer, Value> values,
83 IssueInteger issue) {
84 int randomInteger = issue.getLowerBound()
85 + random.nextInt(issue.getUpperBound() - issue.getLowerBound()
86 + 1);
87 values.put(Integer.valueOf(issue.getNumber()), new ValueInteger(
88 randomInteger));
89 }
90
91 /**
92 * Get a random bid (above a minimum utility value if possible).
93 *
94 * @param utilitySpace
95 * The utility space to generate the random bid from.
96 * @param min
97 * The minimum utility value.
98 * @return a random bid (above a minimum utility value if possible).
99 */
100 private Bid getRandomBid(AdditiveUtilitySpace utilitySpace, double min) {
101 int i = 0;
102 while (true) {
103 Bid b = getRandomBid(utilitySpace);
104 try {
105 double util = utilitySpace.getUtility(b);
106 if (util >= min) {
107 // printVal(util);
108 return b;
109 }
110 } catch (Exception e) {
111 }
112 i++;
113 if (i == 500) {
114 min -= 0.01;
115 i = 0;
116 }
117 }
118 }
119
120 /**
121 * Get a random bid (within a utility range if possible).
122 *
123 * @param utilitySpace
124 * The utility space to generate the random bid from.
125 * @param min
126 * The minimum utility value.
127 * @param max
128 * The maximum utility value.
129 * @return a random bid (within a utility range if possible).
130 */
131 public Bid getRandomBid(AdditiveUtilitySpace utilitySpace, double min,
132 double max) {
133 int i = 0;
134 while (true) {
135 if (max >= 1) {
136 return getRandomBid(utilitySpace, min);
137 }
138 Bid b = getRandomBid(utilitySpace);
139 try {
140 double util = utilitySpace.getUtility(b);
141 if (util >= min && util <= max) {
142 // printVal(util);
143 return b;
144 }
145 } catch (Exception e) {
146 }
147 i++;
148 if (i == 500) {
149 max += 0.01;
150 i = 0;
151 }
152 }
153 }
154
155 @Override
156 public Bid getBid(AdditiveUtilitySpace utilitySpace, double min, double max) {
157 return getRandomBid(utilitySpace, min, max);
158 }
159}
Note: See TracBrowser for help on using the repository browser.