1 | package genius.core;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
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 |
|
---|
13 | import javax.xml.bind.annotation.XmlAttribute;
|
---|
14 | import javax.xml.bind.annotation.XmlElement;
|
---|
15 | import javax.xml.bind.annotation.XmlElementRef;
|
---|
16 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
17 | import javax.xml.bind.annotation.adapters.XmlAdapter;
|
---|
18 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
---|
19 |
|
---|
20 | import genius.core.analysis.pareto.IssueValue;
|
---|
21 | import genius.core.issue.Issue;
|
---|
22 | import genius.core.issue.Value;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A bid is a set of tuples [idnumber,value], where idnumber is the unique
|
---|
26 | * number of the issue, and value is the picked alternative.
|
---|
27 | * <p>
|
---|
28 | * Bid is a immutable. But you can create modified copies using
|
---|
29 | * {@link #putValue(int, Value)}.
|
---|
30 | *
|
---|
31 | * Bid should be considered final so do not extend this.
|
---|
32 | *
|
---|
33 | * @author Dmytro Tykhonov, Koen Hindriks
|
---|
34 | */
|
---|
35 | @XmlRootElement
|
---|
36 | public class Bid implements Serializable {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | *
|
---|
40 | */
|
---|
41 | private static final long serialVersionUID = -7723017380013100614L;
|
---|
42 |
|
---|
43 | private final Domain fDomain;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * the bid values for each IssueID
|
---|
47 | */
|
---|
48 | @XmlElement(name = "values")
|
---|
49 | @XmlJavaTypeAdapter(MyMapAdapter.class)
|
---|
50 | private HashMap<Integer, Value> fValues;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Only for (de)serialization
|
---|
54 | */
|
---|
55 | private Bid() {
|
---|
56 | fDomain = null; // keep Java happy. Serializer shall overwrite anyway
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Create a new empty bid of which the values still must be set.
|
---|
61 | *
|
---|
62 | * @param domain
|
---|
63 | * the domain for this bid
|
---|
64 | */
|
---|
65 | public Bid(Domain domain) {
|
---|
66 | fDomain = domain;
|
---|
67 | fValues = new HashMap<Integer, Value>();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * createFrom a new bid in a domain. Partially checks the validity of the
|
---|
72 | * bid as well There is only this constructor because we require that ALL
|
---|
73 | * values in the domain get assigned a value.
|
---|
74 | *
|
---|
75 | * @param domainP
|
---|
76 | * the domain in which the bid is done
|
---|
77 | * @param bidP
|
---|
78 | * HashMap, which is a set of pairs [issueID,value]
|
---|
79 | */
|
---|
80 | public Bid(Domain domainP, HashMap<Integer, Value> bidP) {
|
---|
81 | this.fDomain = domainP; // THIS NEEDS A CHECK!
|
---|
82 |
|
---|
83 | // Check if indexes are ok
|
---|
84 | // Discussion with Dmytro 16oct 1200: it is possible to do only a
|
---|
85 | // partial bid, leaving
|
---|
86 | // part of the issues un-set. But each issue being bidded on has to
|
---|
87 | // exist in the domain,
|
---|
88 | // and this is what we check here.
|
---|
89 | // Discussion 16oct 16:03: No, ALL values have to be set.
|
---|
90 | // Discussion 19oct: probably there only is this particular constructor
|
---|
91 | // because
|
---|
92 | // that enables to enforce this.
|
---|
93 | // ArrayList<Issue> issues=domainP.getIssues();
|
---|
94 | /*
|
---|
95 | * for (Issue issue:issues) if (bidP.get(new
|
---|
96 | * Integer(issue.getNumber()))==null) throw new
|
---|
97 | * BidDoesNotExistInDomainException(
|
---|
98 | * "bid for issue '"+issue.getName()+"' (issue #"
|
---|
99 | * +issue.getNumber()+") lacks");
|
---|
100 | */
|
---|
101 | fValues = bidP;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * create bid from set of {@link IssueValue}s
|
---|
106 | *
|
---|
107 | * @param domain
|
---|
108 | * the {@link Domain}
|
---|
109 | * @param values
|
---|
110 | * a {@link Collection} of {@link IssueValue}s
|
---|
111 | */
|
---|
112 | public Bid(Domain domain, Collection<IssueValue> values) {
|
---|
113 | if (domain == null)
|
---|
114 | throw new NullPointerException("null domain");
|
---|
115 | if (values == null)
|
---|
116 | throw new NullPointerException("null values");
|
---|
117 |
|
---|
118 | this.fDomain = domain;
|
---|
119 | fValues = new HashMap<>();
|
---|
120 | for (IssueValue iv : values) {
|
---|
121 | fValues.put(iv.getIssue().getNumber(), iv.getValue());
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * This method clones the given bid.
|
---|
127 | *
|
---|
128 | * @param bid
|
---|
129 | * the bid to clone
|
---|
130 | */
|
---|
131 | public Bid(Bid bid) {
|
---|
132 |
|
---|
133 | fDomain = bid.fDomain;
|
---|
134 | fValues = (HashMap<Integer, Value>) bid.fValues.clone();
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @param issueNr
|
---|
139 | * number of an issue.
|
---|
140 | * @return the picked value for given issue idnumber
|
---|
141 | * @throws IllegalArgumentException
|
---|
142 | * if there exist no issue with the given number.
|
---|
143 | */
|
---|
144 | public Value getValue(int issueNr) {
|
---|
145 | Value v = fValues.get(issueNr);
|
---|
146 | if (v == null) {
|
---|
147 | if (fDomain.getIssues().get(issueNr) == null)
|
---|
148 | throw new IllegalArgumentException("Bid.getValue: issue " + issueNr + " does not exist at all");
|
---|
149 | throw new IllegalStateException("There is no evaluator for issue " + issueNr);
|
---|
150 | }
|
---|
151 | return v;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * @param issueId
|
---|
156 | * unique ID of an issue.
|
---|
157 | * @param pValue
|
---|
158 | * value of the issue.
|
---|
159 | * @return new Bid as the current bid but with the value of the issue with
|
---|
160 | * the given issueID to the given value
|
---|
161 | * @throws IllegalArgumentException
|
---|
162 | * if there exist no issue with the given number.
|
---|
163 | */
|
---|
164 | public Bid putValue(int issueId, Value pValue) {
|
---|
165 | if (fValues.get(issueId).getType() != pValue.getType()) {
|
---|
166 | // FIXME
|
---|
167 | // if (fDomain.getIssue(issueId).getType() != pValue.getType()) {
|
---|
168 | throw new IllegalArgumentException("expected value of type " + fDomain.getIssues().get(issueId).getType()
|
---|
169 | + " but got " + pValue + " of type " + pValue.getType());
|
---|
170 | }
|
---|
171 | HashMap<Integer, Value> newValues = new HashMap<Integer, Value>(fValues);
|
---|
172 | newValues.put(issueId, pValue);
|
---|
173 | return new Bid(fDomain, newValues);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public String toString() {
|
---|
177 | String s = "Bid[";
|
---|
178 | Set<Entry<Integer, Value>> value_set = fValues.entrySet();
|
---|
179 | Iterator<Entry<Integer, Value>> value_it = value_set.iterator();
|
---|
180 | while (value_it.hasNext()) {
|
---|
181 | int ind = ((Entry<Integer, Value>) value_it.next()).getKey();
|
---|
182 | Object tmpobj = fDomain.getObjectivesRoot().getObjective(ind); // Objective
|
---|
183 | // isn't
|
---|
184 | // recognized here, GKW.
|
---|
185 | // hdv
|
---|
186 | if (tmpobj != null) {
|
---|
187 | String nm = fDomain.getObjectivesRoot().getObjective(ind).getName();
|
---|
188 | s += nm + ": " + fValues.get(ind) + ", ";
|
---|
189 | } else {
|
---|
190 | System.out.println("objective with index " + ind + " does not exist");
|
---|
191 | }
|
---|
192 |
|
---|
193 | }
|
---|
194 | s = s + "]";
|
---|
195 | return s;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * @param pBid
|
---|
200 | * to which this bid must be compared.
|
---|
201 | * @return true if the values of this and the given bid are equal.
|
---|
202 | */
|
---|
203 | public boolean equals(Bid pBid) {
|
---|
204 | if (pBid == null)
|
---|
205 | return false;
|
---|
206 | return fValues.equals(pBid.fValues);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * (non-Javadoc)
|
---|
211 | *
|
---|
212 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
213 | */
|
---|
214 | @Override
|
---|
215 | public boolean equals(Object obj) {
|
---|
216 | if (obj instanceof Bid)
|
---|
217 | return equals((Bid) obj);
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * @return a (copy of ) the list of all values in this bid. FIXME we really
|
---|
223 | * should return an immutable {@link Map} here but that may break
|
---|
224 | * many agents.
|
---|
225 | */
|
---|
226 |
|
---|
227 | public HashMap<Integer, Value> getValues() {
|
---|
228 | return new HashMap<Integer, Value>(fValues);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // Reyhan: add this method
|
---|
232 | public List<Issue> getIssues() {
|
---|
233 | return fDomain.getIssues();
|
---|
234 | }
|
---|
235 |
|
---|
236 | public Domain getDomain() {
|
---|
237 | return fDomain;
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public int hashCode() {
|
---|
242 | int code = 0;
|
---|
243 | for (Entry<Integer, Value> lEntry : fValues.entrySet()) {
|
---|
244 | code = code + lEntry.getValue().hashCode();
|
---|
245 | }
|
---|
246 | return code;// fValues.hashCode();
|
---|
247 | }
|
---|
248 |
|
---|
249 | }
|
---|
250 |
|
---|
251 | class MyMapAdapter extends XmlAdapter<Temp, Map<Integer, Value>> {
|
---|
252 |
|
---|
253 | @Override
|
---|
254 | public Temp marshal(Map<Integer, Value> arg0) throws Exception {
|
---|
255 | Temp temp = new Temp();
|
---|
256 | for (Entry<Integer, Value> entry : arg0.entrySet()) {
|
---|
257 | temp.entry.add(new Item(entry.getKey(), entry.getValue()));
|
---|
258 | }
|
---|
259 | return temp;
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public Map<Integer, Value> unmarshal(Temp arg0) throws Exception {
|
---|
264 | Map<Integer, Value> map = new HashMap<Integer, Value>();
|
---|
265 | for (Item item : arg0.entry) {
|
---|
266 | map.put(item.key, item.value);
|
---|
267 | }
|
---|
268 | return map;
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | class Temp {
|
---|
274 | @XmlElement(name = "issue")
|
---|
275 | public List<Item> entry;
|
---|
276 |
|
---|
277 | public Temp() {
|
---|
278 | entry = new ArrayList<Item>();
|
---|
279 | }
|
---|
280 |
|
---|
281 | }
|
---|
282 |
|
---|
283 | @XmlRootElement
|
---|
284 | class Item {
|
---|
285 | @XmlAttribute(name = "index")
|
---|
286 | public Integer key;
|
---|
287 |
|
---|
288 | @XmlElementRef
|
---|
289 | public Value value;
|
---|
290 |
|
---|
291 | public Item() {
|
---|
292 | }
|
---|
293 |
|
---|
294 | public Item(Integer key, Value val) {
|
---|
295 | this.key = key;
|
---|
296 | this.value = val;
|
---|
297 | }
|
---|
298 | }
|
---|