source: src/main/java/agents/anac/y2018/yeela/Individual.java

Last change on this file was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 6.2 KB
RevLine 
[341]1package agents.anac.y2018.yeela;
2
3import java.util.List;
4
[343]5import java.util.HashMap;
[341]6import java.util.Random;
7
[343]8import genius.core.Bid;
9import genius.core.issue.Issue;
10import genius.core.issue.IssueDiscrete;
11import genius.core.issue.Value;
12import genius.core.issue.ValueDiscrete;
13import genius.core.parties.NegotiationInfo;
14import genius.core.utility.AdditiveUtilitySpace;
15import genius.core.utility.EvaluatorDiscrete;
16
[341]17public class Individual implements Comparable<Individual> {
18 private Double ALPHA = 0.9; // TODO
19
20 private HashMap<Integer, Value> m_gene;
21 private Double m_util;
22 private NegotiationInfo m_info;
23 private Random m_rand;
24 private Bid m_opponent;
25 private Double m_best;
26 private Double m_maxDist;
27
28 public Individual(Bid b, NegotiationInfo info)
29 {
30 m_gene = b.getValues();
31 m_info = info;
32 m_util = CalcUtility();
33 m_rand = new Random();
34
35 try
36 {
37 Bid best = m_info.getUtilitySpace().getMaxUtilityBid();
38 m_best = m_info.getUtilitySpace().getUtility(best);
39 m_maxDist = Dist(
40 best.getValues(),
41 m_info.getUtilitySpace().getMinUtilityBid().getValues());
42 }
43 catch (Exception e)
44 {
45 m_maxDist = Double.MIN_VALUE;
46 e.printStackTrace();
47 }
48 }
49
50 @Override
51 public int compareTo(Individual other)
52 {
53 try
54 {
55 return (int) java.lang.Math.signum(this.GetFitness() - other.GetFitness());
56 }
57 catch (Exception e)
58 {
59 e.printStackTrace();
60 return 0;
61 }
62 }
63
64 public Value GetValue(Integer key)
65 {
66 return m_gene.get(key);
67 }
68
69 public void SetValue(Integer key, Value value)
70 {
71 m_gene.put(key, value);
72 }
73
74 private Double TP()
75 {
76 // TODO
77 return 1.0;
78 }
79
80 private Double Dist(HashMap<Integer, Value> v1, HashMap<Integer, Value> v2)
81 {
82 Double d = 0.0;
83
84 AdditiveUtilitySpace additiveUtilitySpace = (AdditiveUtilitySpace) m_info.getUtilitySpace();
85 List<Issue> issues = additiveUtilitySpace.getDomain().getIssues();
86
87 for (Issue issue : issues)
88 {
89 int issueNumber = issue.getNumber();
90 Double weight = additiveUtilitySpace.getWeight(issueNumber);
91
92 // Assuming that issues are discrete only
93 EvaluatorDiscrete evaluatorDiscrete = (EvaluatorDiscrete) additiveUtilitySpace.getEvaluator(issueNumber);
94
95 try
96 {
97 Double evaluation1 = evaluatorDiscrete.getEvaluation((ValueDiscrete)v1.get(issueNumber));
98 Double evaluation2 = evaluatorDiscrete.getEvaluation((ValueDiscrete)v2.get(issueNumber));
99
100 d += (weight * Math.pow(evaluation1 - evaluation2, 2));
101 }
102 catch (Exception e)
103 {
104 d = Double.POSITIVE_INFINITY;
105 e.printStackTrace();
106 }
107 }
108
109 return Math.sqrt(d);
110 }
111
112 public void UpdateOpponent(Bid opponent)
113 {
114 m_opponent = opponent;
115 }
116
117 public Double GetFitness()
118 {
119 Double otherSide = (1 - ALPHA) * TP() * (1 - (Dist(m_gene, m_opponent.getValues()) / m_maxDist));
120 Double ourSide = ALPHA * TP() * (this.GetUtility() / m_best);
121 return ourSide + otherSide;
122 }
123
124 public Double GetUtility()
125 {
126 return m_util;
127 }
128
129 private Double CalcUtility()
130 {
131 // TODO maybe simply call utilitySpace.getUtility(randomBid); see https://github.com/tdgunes/ExampleAgent/wiki/Generating-a-random-bid-with-a-utility-threshold
132 Double util = 0.0;
133
134 AdditiveUtilitySpace additiveUtilitySpace = (AdditiveUtilitySpace) m_info.getUtilitySpace();
135 List<Issue> issues = additiveUtilitySpace.getDomain().getIssues();
136
137 for (Issue issue : issues)
138 {
139 int issueNumber = issue.getNumber();
140 Double weight = additiveUtilitySpace.getWeight(issueNumber);
141
142 // Assuming that issues are discrete only
143 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
144 EvaluatorDiscrete evaluatorDiscrete = (EvaluatorDiscrete) additiveUtilitySpace.getEvaluator(issueNumber);
145 Double evaluation = 0.0;
146
147 for (ValueDiscrete valueDiscrete : issueDiscrete.getValues())
148 {
149 if (0 == valueDiscrete.getValue().compareTo(m_gene.get(issueNumber).toString()))
150 {
151 try
152 {
153 evaluation = evaluatorDiscrete.getEvaluation(valueDiscrete);
154 util += (weight * evaluation);
155 }
156 catch (Exception e)
157 {
158 e.printStackTrace();
159 }
160 }
161 }
162 }
163 return util;
164 }
165
166 public Individual Clone()
167 {
168 Bid randomBid = m_info.getUtilitySpace().getDomain().getRandomBid(m_rand);
169
170 try
171 {
172 for (Integer key : m_gene.keySet())
173 {
174 randomBid.putValue(key, m_gene.get(key));
175 }
176 }
177 catch (Exception e)
178 {
179 e.printStackTrace();
180 }
181
182 Individual ind = new Individual(randomBid, m_info);
183 ind.UpdateOpponent(m_opponent);
184 return ind;
185 }
186
187 public void Mutate()
188 {
189 try
190 {
191 AdditiveUtilitySpace additiveUtilitySpace = (AdditiveUtilitySpace) m_info.getUtilitySpace();
192 List<Issue> issues = additiveUtilitySpace.getDomain().getIssues();
193
194 int randomNum = m_rand.nextInt(issues.size());
195 Issue issue = issues.get(randomNum);
196
197 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
198 int issueNumber = issue.getNumber();
199 randomNum = issueNumber;
200 while (randomNum == issueNumber)
201 {
202 randomNum = m_rand.nextInt(issueDiscrete.getValues().size());
203 }
204
205 Value newValue = issueDiscrete.getValues().get(randomNum);
206 m_gene.put(issueNumber, newValue);
207 }
208 catch (Exception e)
209 {
210 e.printStackTrace();
211 }
212
213 }
214
215 public void Crossover(Individual other)
216 {
217 try
218 {
219 AdditiveUtilitySpace additiveUtilitySpace = (AdditiveUtilitySpace) m_info.getUtilitySpace();
220 List<Issue> issues = additiveUtilitySpace.getDomain().getIssues();
221
222 if (1 < issues.size()) // otherwise no need to crossover
223 {
224 int randomNum = m_rand.nextInt(issues.size() - 1) + 1; // minus one since crossover location is in gap between alleles
225
226 for (Integer key : m_gene.keySet())
227 {
228 if (key > randomNum)
229 {
230 break;
231 }
232
233 Value temp = m_gene.get(key);
234 m_gene.put(key, other.GetValue(key));
235 other.SetValue(key, temp);
236 }
237 }
238 }
239 catch (Exception e)
240 {
241 e.printStackTrace();
242 }
243 }
244}
Note: See TracBrowser for help on using the repository browser.