Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs @ 5207

Last change on this file since 5207 was 5207, checked in by cneumuel, 14 years ago

#1215

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