1 | package genius.core.boaframework;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.HashSet;
|
---|
5 |
|
---|
6 | import genius.core.misc.Pair;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * This stores the parameter specification of a BOA component: the name
|
---|
10 | * description and range of valid values. Used in in {@link Tournament}.
|
---|
11 | *
|
---|
12 | * If lower and higher bound is used, it also requires a step size and all
|
---|
13 | * in-between values in the range are being generated immediately. Basically,
|
---|
14 | * what is stored is [Lowerbound:Stepsize:Upperbound]. [1:5:20] = {1, 6, 11,
|
---|
15 | * 16}.
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class BOAparameter implements Serializable {
|
---|
19 |
|
---|
20 | private static final long serialVersionUID = 2555736049221913613L;
|
---|
21 | /** Name of the parameter. */
|
---|
22 | private String name;
|
---|
23 | /** Lowerbound of the specified range. */
|
---|
24 | private Double low;
|
---|
25 | /** Upperbound of the specified range. */
|
---|
26 | private Double high;
|
---|
27 | /** Step size of the specified range. */
|
---|
28 | private Double step;
|
---|
29 | /** set of separate values which the specified variable should attain */
|
---|
30 | private HashSet<Pair<String, Double>> valuePairs;
|
---|
31 | /** description of the parameter */
|
---|
32 | private String description;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Describes a parameter for a BOA component. A parameter consists of a
|
---|
36 | * name, and the possible values for the parameter.
|
---|
37 | *
|
---|
38 | * @param name
|
---|
39 | * of the parameter.
|
---|
40 | * @param low
|
---|
41 | * value of the range.
|
---|
42 | * @param high
|
---|
43 | * value of the range.
|
---|
44 | * @param step
|
---|
45 | * of the range.
|
---|
46 | */
|
---|
47 | public BOAparameter(String name, Double low, Double high, Double step) {
|
---|
48 | this.name = name;
|
---|
49 | this.low = low;
|
---|
50 | this.high = high;
|
---|
51 | this.step = step;
|
---|
52 | description = "";
|
---|
53 | generatePairs();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Describes a parameter for a BOA component with a fixed single value.
|
---|
58 | *
|
---|
59 | * @param name
|
---|
60 | * @param defaultValue
|
---|
61 | * @param description
|
---|
62 | */
|
---|
63 | public BOAparameter(String name, Double defaultValue, String description) {
|
---|
64 | this.name = name;
|
---|
65 | this.description = description;
|
---|
66 | this.low = defaultValue;
|
---|
67 | this.high = defaultValue;
|
---|
68 | this.step = 1D;
|
---|
69 | generatePairs();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Describes a parameter for a decoupled component. A parameter consists of
|
---|
74 | * a name, a description, and the possible values for the parameter.
|
---|
75 | *
|
---|
76 | * @param name
|
---|
77 | * of the parameter.
|
---|
78 | * @param low
|
---|
79 | * value of the range.
|
---|
80 | * @param high
|
---|
81 | * value of the range.
|
---|
82 | * @param step
|
---|
83 | * of the range.
|
---|
84 | * @param description
|
---|
85 | * of the parameter.
|
---|
86 | */
|
---|
87 | public BOAparameter(String name, Double low, Double high, Double step, String description) {
|
---|
88 | this.name = name;
|
---|
89 | this.low = low;
|
---|
90 | this.high = high;
|
---|
91 | this.step = step;
|
---|
92 | this.description = description;
|
---|
93 | generatePairs();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Generates the set of all possible configurations for the parameter given
|
---|
98 | * the range and step size of the component.
|
---|
99 | */
|
---|
100 | private void generatePairs() {
|
---|
101 | valuePairs = new HashSet<Pair<String, Double>>();
|
---|
102 | for (Double value = low; value <= high; value += step) {
|
---|
103 | valuePairs.add(new Pair<String, Double>(name, value));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns all values of the parameters which satisfy
|
---|
109 | * [Lowerbound:Stepsize:Upperbound].
|
---|
110 | *
|
---|
111 | * @return possible values for the parameter specified.
|
---|
112 | */
|
---|
113 | public HashSet<Pair<String, Double>> getValuePairs() {
|
---|
114 | return valuePairs;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * @return name of the parameter.
|
---|
119 | */
|
---|
120 | public String getName() {
|
---|
121 | return name;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * @return value for the lowerbound.
|
---|
126 | */
|
---|
127 | public Double getLow() {
|
---|
128 | return low;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * @return upperbound of the range.
|
---|
133 | */
|
---|
134 | public Double getHigh() {
|
---|
135 | return high;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * @return stepsize of the range.
|
---|
140 | */
|
---|
141 | public Double getStep() {
|
---|
142 | return step;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public String toString() {
|
---|
146 | if (!name.equals("null")) {
|
---|
147 | if (low.compareTo(high) == 0) {
|
---|
148 | /*
|
---|
149 | * without doubleValue we get a crazy number of digits
|
---|
150 | */
|
---|
151 | return name + ": " + low.doubleValue();
|
---|
152 | }
|
---|
153 | return name + ": [" + low + " : " + step + " : " + high + "]";
|
---|
154 | } else {
|
---|
155 | return "";
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * @return description of the parameter.
|
---|
161 | */
|
---|
162 | public String getDescription() {
|
---|
163 | return description;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public String toXML() {
|
---|
167 | return "<parameter name=\"" + name + "\" default=\"" + high + "\" description=\"" + description + "\"/>";
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | *
|
---|
172 | * @param newDescr
|
---|
173 | * the new description for this parameter
|
---|
174 | * @return new {@link BOAparameter} with same settings as this but new
|
---|
175 | * description as given
|
---|
176 | */
|
---|
177 | public BOAparameter withDescription(String newDescr) {
|
---|
178 | return new BOAparameter(name, low, high, step, newDescr);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * @param newLow
|
---|
183 | * the new low value for this parameter.
|
---|
184 | * @return new BOAparameter with same settings as this but with new low
|
---|
185 | * value. If the high value is < newLow, it is also set to newLow.
|
---|
186 | */
|
---|
187 | public BOAparameter withLow(Double newLow) {
|
---|
188 | return new BOAparameter(name, newLow, Math.max(newLow, high), step, description);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | *
|
---|
193 | * @param newHigh
|
---|
194 | * the new high value for this parameter
|
---|
195 | * @return new {@link BOAparameter} with same settings as this but with the
|
---|
196 | * new high value. If the high value is < low, then low is also set
|
---|
197 | * to newHigh.
|
---|
198 | */
|
---|
199 | public BOAparameter withHigh(Double newHigh) {
|
---|
200 | return new BOAparameter(name, Math.min(low, newHigh), newHigh, step, description);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | *
|
---|
205 | * @param step
|
---|
206 | * the new step value
|
---|
207 | * @return new {@link BOAparameter} with same settings as this but with step
|
---|
208 | * value as given
|
---|
209 | */
|
---|
210 | public BOAparameter withStep(Double step) {
|
---|
211 | return new BOAparameter(name, low, high, step, description);
|
---|
212 | }
|
---|
213 |
|
---|
214 | } |
---|