source: src/main/java/genius/core/utility/EvaluatorInteger.java

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 6.0 KB
Line 
1package genius.core.utility;
2
3import java.text.DecimalFormat;
4
5import genius.core.Bid;
6import genius.core.issue.Objective;
7import genius.core.issue.ValueInteger;
8import genius.core.xml.SimpleElement;
9
10/**
11 * This class is used to convert the value of an integer issue to a utility.
12 * This object stores the range of the issue and a linear function mapping each
13 * value to a utility. Note that this utility is not yet normalized by the issue
14 * weight and is therefore in the range [0,1].
15 *
16 */
17@SuppressWarnings("serial")
18public class EvaluatorInteger implements Evaluator {
19
20 // Class fields
21 private double fweight; // the weight of the evaluated Objective or Issue.
22 private boolean fweightLock;
23 private int lowerBound;
24 private int upperBound;
25 private double slope = 0.0;
26 private double offset = 0.0;
27 private DecimalFormat f = new DecimalFormat("0.0000");
28
29 /**
30 * Creates a new integer evaluator with weight 0 and no values.
31 */
32 public EvaluatorInteger() {
33 fweight = 0;
34 }
35
36 @Override
37 public double getWeight() {
38 return fweight;
39 }
40
41 @Override
42 public void setWeight(double wt) {
43 fweight = wt;
44 }
45
46 @Override
47 public void lockWeight() {
48 fweightLock = true;
49 }
50
51 @Override
52 public void unlockWeight() {
53 fweightLock = false;
54 }
55
56 @Override
57 public boolean weightLocked() {
58 return fweightLock;
59 }
60
61 @Override
62 public Double getEvaluation(AdditiveUtilitySpace uspace, Bid bid,
63 int index) {
64 Integer lTmp = null;
65 try {
66 lTmp = ((ValueInteger) bid.getValue(index)).getValue();
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 return getEvaluation(lTmp);
71 }
72
73 @Override
74 public EVALUATORTYPE getType() {
75 return EVALUATORTYPE.INTEGER;
76 }
77
78 @Override
79 public void loadFromXML(SimpleElement pRoot) {
80
81 this.lowerBound = Integer.valueOf(pRoot.getAttribute("lowerbound"));
82 this.upperBound = Integer.valueOf(pRoot.getAttribute("upperbound"));
83
84 Object[] xml_items = pRoot.getChildByTagName("evaluator");
85 if (xml_items.length != 0) {
86 this.slope = Double.valueOf(
87 ((SimpleElement) xml_items[0]).getAttribute("slope"));
88 this.offset = Double.valueOf(
89 ((SimpleElement) xml_items[0]).getAttribute("offset"));
90 }
91 }
92
93 @Override
94 public String toString() {
95 return "{Integer: offset=" + f.format(offset) + " slope="
96 + f.format(slope) + " range=[" + lowerBound + ":" + upperBound
97 + "]}";
98 }
99
100 @Override
101 public String isComplete(Objective whichobj) {
102 return null;
103 }
104
105 /************** specific for EvaluationInteger ****************/
106 /**
107 * @param value
108 * of an issue.
109 * @return utility of the given value (range: [0,1]).
110 */
111 public Double getEvaluation(int value) {
112 double utility;
113
114 utility = EVALFUNCTYPE.evalLinear(value - lowerBound, slope, offset);
115 if (utility < 0)
116 utility = 0;
117 else if (utility > 1)
118 utility = 1;
119 return utility;
120 }
121
122 /**
123 * @return evaluation function type.
124 */
125 public EVALFUNCTYPE getFuncType() {
126 return null;
127 }
128
129 /**
130 * @return lowerbound of the integer issue.
131 */
132 public int getLowerBound() {
133 return lowerBound;
134 }
135
136 /**
137 * @return higherbound of the integer issue.
138 */
139 public int getUpperBound() {
140 return upperBound;
141 }
142
143 /**
144 * @return lowest possible utility value.
145 */
146 public double getUtilLowestValue() {
147 return offset;
148 }
149
150 /**
151 * @return highest possible utility value.
152 */
153 public double getUtilHighestValue() {
154 return (offset + slope * (upperBound - lowerBound));
155 }
156
157 /**
158 * Sets the lower bound of this evaluator.
159 *
160 * @param lb
161 * The new lower bound
162 */
163 public void setLowerBound(int lb) {
164 lowerBound = lb;
165 }
166
167 /**
168 * Sets the upper bound of this evaluator.
169 *
170 * @param ub
171 * The new upper bound
172 */
173 public void setUpperBound(int ub) {
174 upperBound = ub;
175 }
176
177 /**
178 * Specifies the linear utility function of the issue by giving the utility
179 * of the lowest value and the highest value.
180 *
181 * @param utilLowInt
182 * utility of the lowest vale.
183 * @param utilHighInt
184 * utility of the highest value.
185 */
186 public void setLinearFunction(double utilLowInt, double utilHighInt) {
187 slope = (utilHighInt - utilLowInt) / (-lowerBound + upperBound);
188 offset = utilLowInt;
189 }
190
191 /**
192 * Sets weights and evaluator properties for the object in SimpleElement
193 * representation that is passed to it.
194 *
195 * @param evalObj
196 * The object of which to set the evaluation properties.
197 * @return The modified simpleElement with all evaluator properties set.
198 */
199 public SimpleElement setXML(SimpleElement evalObj) {
200 return evalObj;
201 }
202
203 /**
204 * @return slope of the linear utility function.
205 */
206 public double getSlope() {
207 return slope;
208 }
209
210 /**
211 * Sets the slope of the linear utility function.
212 *
213 * @param slope
214 * of the linear utility function.
215 */
216 public void setSlope(double slope) {
217 this.slope = slope;
218 }
219
220 /**
221 * Sets the slope of the linear utility function.
222 *
223 * @param slope
224 * of the linear utility function.
225 */
226 @Deprecated
227 public void setLinearParam(double slope) {
228 setSlope(slope);
229 }
230
231 /**
232 * @return offset of the linear utility function.
233 */
234 public double getOffset() {
235 return offset;
236 }
237
238 /**
239 * Sets the offset of the linear utility function.
240 *
241 * @param offset
242 * of the linear utility function.
243 */
244 public void setOffset(double offset) {
245 this.offset = offset;
246 }
247
248 /**
249 * Sets the offset of the linear utility function.
250 *
251 * @param offset
252 * of the linear utility function.
253 */
254 @Deprecated
255 public void setConstantParam(double offset) {
256 setOffset(offset);
257 }
258
259 @Override
260 public EvaluatorInteger clone() {
261 EvaluatorInteger ed = new EvaluatorInteger();
262 ed.setWeight(fweight);
263 ed.setUpperBound(upperBound);
264 ed.setLowerBound(lowerBound);
265 ed.slope = slope;
266 ed.offset = offset;
267 return ed;
268 }
269}
Note: See TracBrowser for help on using the repository browser.