1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using System.Drawing;
|
---|
8 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
9 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
12 | // TODO: ItemName/Descr, storability
|
---|
13 | [StorableClass]
|
---|
14 | public class ValueConfiguration : Item, IValueConfiguration {
|
---|
15 | [Storable]
|
---|
16 | protected bool isOptimizable;
|
---|
17 | public bool IsOptimizable {
|
---|
18 | get { return isOptimizable; }
|
---|
19 | set {
|
---|
20 | if (this.isOptimizable != value) {
|
---|
21 | this.isOptimizable = value;
|
---|
22 | OnIsOptimizableChanged();
|
---|
23 | }
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | [Storable]
|
---|
28 | protected bool optimize;
|
---|
29 | public bool Optimize {
|
---|
30 | get { return optimize; }
|
---|
31 | set {
|
---|
32 | if (optimize != value) {
|
---|
33 | optimize = value;
|
---|
34 | if (optimize) {
|
---|
35 | ClearParameterConfigurations();
|
---|
36 | PopulateParameterConfigurations();
|
---|
37 | } else {
|
---|
38 | ClearParameterConfigurations();
|
---|
39 | }
|
---|
40 | OnOptimizeChanged();
|
---|
41 | OnToStringChanged();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [Storable]
|
---|
47 | protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
|
---|
48 | public IItemCollection<IParameterConfiguration> ParameterConfigurations {
|
---|
49 | get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
|
---|
50 | protected set { this.parameterConfigurations = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | protected ConstrainedValue actualValue;
|
---|
55 | public ConstrainedValue ActualValue {
|
---|
56 | get { return actualValue; }
|
---|
57 | set {
|
---|
58 | if (this.actualValue != value) {
|
---|
59 | ClearParameterConfigurations();
|
---|
60 | this.actualValue = value;
|
---|
61 | PopulateParameterConfigurations();
|
---|
62 | OnValueChanged();
|
---|
63 | OnToStringChanged();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [Storable]
|
---|
69 | protected IRange rangeConstraint;
|
---|
70 | public IRange RangeConstraint {
|
---|
71 | get { return rangeConstraint; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | #region Constructors and Cloning
|
---|
75 | public ValueConfiguration(IItem value, Type valueDataType) {
|
---|
76 | this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
|
---|
77 | this.ActualValue = new ConstrainedValue(value, valueDataType);
|
---|
78 | this.IsOptimizable = true;
|
---|
79 | if (actualValue.ValueDataType == typeof(IntValue)) {
|
---|
80 | rangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value, new IntValue(1));
|
---|
81 | } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
|
---|
82 | rangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.01));
|
---|
83 | } else if (actualValue.ValueDataType == typeof(PercentValue)) {
|
---|
84 | rangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
|
---|
85 | } else if (actualValue.ValueDataType == typeof(BoolValue)) {
|
---|
86 | this.IsOptimizable = false; // there is nothing to configure for bools
|
---|
87 | } else {
|
---|
88 | rangeConstraint = null;
|
---|
89 | }
|
---|
90 | RegisterEvents();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public ValueConfiguration() { }
|
---|
94 | [StorableConstructor]
|
---|
95 | protected ValueConfiguration(bool deserializing) { }
|
---|
96 | protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
|
---|
97 | : base(original, cloner) {
|
---|
98 | this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);
|
---|
99 | this.actualValue = cloner.Clone(original.ActualValue);
|
---|
100 | this.rangeConstraint = cloner.Clone(original.RangeConstraint);
|
---|
101 | this.isOptimizable = original.isOptimizable;
|
---|
102 | this.optimize = original.optimize;
|
---|
103 | RegisterEvents();
|
---|
104 | }
|
---|
105 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
106 | return new ValueConfiguration(this, cloner);
|
---|
107 | }
|
---|
108 | [StorableHook(HookType.AfterDeserialization)]
|
---|
109 | private void AfterDeserialization() {
|
---|
110 | RegisterEvents();
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | protected virtual void PopulateParameterConfigurations() {
|
---|
115 | if (this.actualValue.Value is IParameterizedNamedItem) {
|
---|
116 | var parameterizedItem = this.actualValue.Value as IParameterizedNamedItem;
|
---|
117 | foreach (var childParameter in parameterizedItem.Parameters) {
|
---|
118 | var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
|
---|
119 | if (pc != null) this.parameterConfigurations.Add(pc);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | protected virtual void ClearParameterConfigurations() {
|
---|
124 | parameterConfigurations.Clear();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void RegisterEvents() {
|
---|
128 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
129 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
130 | }
|
---|
131 | private void DeregisterEvents() {
|
---|
132 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
133 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
134 | }
|
---|
135 |
|
---|
136 | void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
|
---|
137 | OnToStringChanged();
|
---|
138 | }
|
---|
139 | void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
|
---|
140 | OnToStringChanged();
|
---|
141 | }
|
---|
142 |
|
---|
143 | #region IItem Members
|
---|
144 | public override string ItemDescription {
|
---|
145 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public override Image ItemImage {
|
---|
149 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public override string ItemName {
|
---|
153 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
|
---|
154 | }
|
---|
155 | #endregion
|
---|
156 |
|
---|
157 | #region Event Handlers
|
---|
158 | public virtual event EventHandler ValueChanged;
|
---|
159 | protected virtual void OnValueChanged() {
|
---|
160 | var handler = ValueChanged;
|
---|
161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public virtual event EventHandler IsOptimizableChanged;
|
---|
165 | private void OnIsOptimizableChanged() {
|
---|
166 | var handler = IsOptimizableChanged;
|
---|
167 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
168 | }
|
---|
169 |
|
---|
170 | public virtual event EventHandler OptimizeChanged;
|
---|
171 | protected virtual void OnOptimizeChanged() {
|
---|
172 | var handler = OptimizeChanged;
|
---|
173 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
174 | }
|
---|
175 | #endregion
|
---|
176 |
|
---|
177 | public override string ToString() {
|
---|
178 | if (ActualValue != null && ActualValue.Value != null) {
|
---|
179 | if (ActualValue.Value is IParameterizedItem) {
|
---|
180 | if (Optimize) {
|
---|
181 | return string.Format("{0} (Optimize)", ActualValue.Value.ItemName);
|
---|
182 | } else {
|
---|
183 | return string.Format("{0}", ActualValue.Value.ItemName);
|
---|
184 | }
|
---|
185 | } else {
|
---|
186 | if (Optimize) {
|
---|
187 | return string.Format("{0} (Optimize: {1})", ActualValue.Value.ItemName, RangeConstraint);
|
---|
188 | } else {
|
---|
189 | return string.Format("{0}: {1}", ActualValue.Value.ItemName, ActualValue.Value);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | } else {
|
---|
193 | return base.ToString();
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void Parameterize(IParameterizedItem item) {
|
---|
198 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
199 | pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void Randomize(IRandom random) {
|
---|
204 | if (Optimize) {
|
---|
205 | if (rangeConstraint != null) {
|
---|
206 | this.actualValue.Value = rangeConstraint.GetRandomValue(random);
|
---|
207 | } else {
|
---|
208 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
209 | pc.Randomize(random);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void Mutate(IRandom random) {
|
---|
216 | if (Optimize) {
|
---|
217 | if (rangeConstraint != null) {
|
---|
218 | if (random.NextDouble() > 0.5)
|
---|
219 | this.actualValue.Value = rangeConstraint.GetRandomValue(random);
|
---|
220 | } else {
|
---|
221 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
222 | pc.Mutate(random);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void Cross(IOptimizable other, IRandom random) {
|
---|
229 | if (Optimize) {
|
---|
230 | IValueConfiguration otherVc = (IValueConfiguration)other;
|
---|
231 | if (rangeConstraint != null) {
|
---|
232 | if (this.actualValue.ValueDataType == typeof(IntValue)) {
|
---|
233 | //this.actualValue.Value = new IntValue((((IntValue)this.actualValue.Value).Value + ((IntValue)other.ActualValue.Value).Value) / 2);
|
---|
234 |
|
---|
235 | IntegerVector[] parents = new IntegerVector[2];
|
---|
236 | parents[0] = new IntegerVector(new int[] { ((IntValue)this.actualValue.Value).Value });
|
---|
237 | parents[1] = new IntegerVector(new int[] { ((IntValue)other.ActualValue.Value).Value });
|
---|
238 |
|
---|
239 | this.actualValue.Value = new IntValue(HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(random, parents[0], parents[1]).First());
|
---|
240 |
|
---|
241 | } else if (this.actualValue.ValueDataType == typeof(DoubleValue)) {
|
---|
242 | //this.actualValue.Value = new DoubleValue((((DoubleValue)this.actualValue.Value).Value + ((DoubleValue)other.ActualValue.Value).Value) / 2);
|
---|
243 | RealVector[] parents = new RealVector[2];
|
---|
244 | parents[0] = new RealVector( new double[] {((DoubleValue)this.actualValue.Value).Value} );
|
---|
245 | parents[1] = new RealVector( new double[] {((DoubleValue)other.ActualValue.Value).Value} );
|
---|
246 |
|
---|
247 | if (random.NextDouble() < 0.5) {
|
---|
248 | this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
|
---|
249 | } else {
|
---|
250 | this.actualValue.Value = new DoubleValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
|
---|
251 | }
|
---|
252 | //this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
|
---|
253 |
|
---|
254 | } else if (this.actualValue.ValueDataType == typeof(PercentValue)) {
|
---|
255 | //this.actualValue.Value = new PercentValue((((PercentValue)this.actualValue.Value).Value + ((PercentValue)other.ActualValue.Value).Value) / 2);
|
---|
256 |
|
---|
257 | RealVector[] parents = new RealVector[2];
|
---|
258 | parents[0] = new RealVector(new double[] { ((PercentValue)this.actualValue.Value).Value });
|
---|
259 | parents[1] = new RealVector(new double[] { ((PercentValue)other.ActualValue.Value).Value });
|
---|
260 |
|
---|
261 | if (random.NextDouble() < 0.5) {
|
---|
262 | this.actualValue.Value = new PercentValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
|
---|
263 | } else {
|
---|
264 | this.actualValue.Value = new PercentValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
|
---|
265 | }
|
---|
266 |
|
---|
267 | } else if (this.actualValue.ValueDataType == typeof(BoolValue)) {
|
---|
268 | if (random.NextDouble() > 0.5)
|
---|
269 | this.actualValue.Value = this.actualValue.Value;
|
---|
270 | else
|
---|
271 | this.actualValue.Value = other.ActualValue.Value;
|
---|
272 | } else {
|
---|
273 | throw new NotImplementedException();
|
---|
274 | }
|
---|
275 | } else {
|
---|
276 | for (int i = 0; i < this.ParameterConfigurations.Count; i++) {
|
---|
277 | this.ParameterConfigurations.ElementAt(i).Cross(otherVc.ParameterConfigurations.ElementAt(i), random);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|