Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ValueConfigurations/ValueConfiguration.cs @ 5087

Last change on this file since 5087 was 5087, checked in by cneumuel, 13 years ago

#1215

  • enabled multiple problems
  • enabled n repetitions
  • improved results output
  • reduced memory footprint significantly
  • removed viewhost icons for less screen space waste
File size: 11.7 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;
10
11namespace 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            if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);
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 virtual ConstrainedValue ActualValue {
56      get { return actualValue; }
57      set {
58        if (this.actualValue != value) {
59          ClearParameterConfigurations();
60          this.actualValue = value;
61          if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);
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(IParameterizedNamedItem parameterizedItem) {
115      foreach (var childParameter in parameterizedItem.Parameters) {
116        var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
117        if (pc != null) this.parameterConfigurations.Add(pc);
118      }
119    }
120    protected virtual void ClearParameterConfigurations() {
121      parameterConfigurations.Clear();
122    }
123
124    private void RegisterEvents() {
125      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
126      if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
127    }
128    private void DeregisterEvents() {
129      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
130      if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
131    }
132
133    void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
134      OnToStringChanged();
135    }
136    void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
137      OnToStringChanged();
138    }
139
140    #region IItem Members
141    public override string ItemDescription {
142      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
143    }
144
145    public override Image ItemImage {
146      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
147    }
148
149    public override string ItemName {
150      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
151    }
152    #endregion
153
154    #region Event Handlers
155    public virtual event EventHandler ValueChanged;
156    protected virtual void OnValueChanged() {
157      var handler = ValueChanged;
158      if (handler != null) handler(this, EventArgs.Empty);
159    }
160
161    public virtual event EventHandler IsOptimizableChanged;
162    private void OnIsOptimizableChanged() {
163      var handler = IsOptimizableChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166
167    public virtual event EventHandler OptimizeChanged;
168    protected virtual void OnOptimizeChanged() {
169      var handler = OptimizeChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172    #endregion
173
174    public override string ToString() {
175      if (ActualValue != null && ActualValue.Value != null) {
176        if (ActualValue.Value is IParameterizedItem) {
177          if (Optimize) {
178            return string.Format("{0} (Optimize)", ActualValue.Value.ItemName);
179          } else {
180            return string.Format("{0}", ActualValue.Value.ItemName);
181          }
182        } else {
183          if (Optimize) {
184            return string.Format("{0} (Optimize: {1})", ActualValue.Value.ItemName, RangeConstraint);
185          } else {
186            return string.Format("{0}: {1}", ActualValue.Value.ItemName, ActualValue.Value);
187          }
188        }
189      } else {
190        return base.ToString();
191      }
192    }
193
194    public virtual void Parameterize(IParameterizedItem item) {
195      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
196        pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
197      }
198    }
199
200    public void Randomize(IRandom random) {
201      if (Optimize) {
202        if (rangeConstraint != null) {
203          this.actualValue.Value = rangeConstraint.GetRandomValue(random);
204        } else {
205          foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
206            pc.Randomize(random);
207          }
208        }
209      }
210    }
211
212    public void Mutate(IRandom random) {
213      if (Optimize) {
214        if (rangeConstraint != null) {
215          if (random.NextDouble() > 0.5)
216            this.actualValue.Value = rangeConstraint.GetRandomValue(random);
217        } else {
218          foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
219            pc.Mutate(random);
220          }
221        }
222      }
223    }
224
225    public void Cross(IOptimizable other, IRandom random) {
226      if (Optimize) {
227        IValueConfiguration otherVc = (IValueConfiguration)other;
228        if (rangeConstraint != null) {
229          if (this.actualValue.ValueDataType == typeof(IntValue)) {
230            //this.actualValue.Value = new IntValue((((IntValue)this.actualValue.Value).Value + ((IntValue)other.ActualValue.Value).Value) / 2);
231
232            IntegerVector[] parents = new IntegerVector[2];
233            parents[0] = new IntegerVector(new int[] { ((IntValue)this.actualValue.Value).Value });
234            parents[1] = new IntegerVector(new int[] { ((IntValue)other.ActualValue.Value).Value });
235
236            this.actualValue.Value = new IntValue(HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(random, parents[0], parents[1]).First());
237
238          } else if (this.actualValue.ValueDataType == typeof(DoubleValue)) {
239            //this.actualValue.Value = new DoubleValue((((DoubleValue)this.actualValue.Value).Value + ((DoubleValue)other.ActualValue.Value).Value) / 2);
240            RealVector[] parents = new RealVector[2];
241            parents[0] = new RealVector( new double[] {((DoubleValue)this.actualValue.Value).Value} );
242            parents[1] = new RealVector( new double[] {((DoubleValue)other.ActualValue.Value).Value} );
243
244            if (random.NextDouble() < 0.5) {
245              this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
246            } else {
247              this.actualValue.Value = new DoubleValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
248            }
249            //this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
250           
251          } else if (this.actualValue.ValueDataType == typeof(PercentValue)) {
252            //this.actualValue.Value = new PercentValue((((PercentValue)this.actualValue.Value).Value + ((PercentValue)other.ActualValue.Value).Value) / 2);
253
254            RealVector[] parents = new RealVector[2];
255            parents[0] = new RealVector(new double[] { ((PercentValue)this.actualValue.Value).Value });
256            parents[1] = new RealVector(new double[] { ((PercentValue)other.ActualValue.Value).Value });
257
258            if (random.NextDouble() < 0.5) {
259              this.actualValue.Value = new PercentValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
260            } else {
261              this.actualValue.Value = new PercentValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First());
262            }
263
264          } else if (this.actualValue.ValueDataType == typeof(BoolValue)) {
265            if (random.NextDouble() > 0.5)
266              this.actualValue.Value = this.actualValue.Value;
267            else
268              this.actualValue.Value = other.ActualValue.Value;
269          } else {
270            throw new NotImplementedException();
271          }
272        } else {
273          for (int i = 0; i < this.ParameterConfigurations.Count; i++) {
274            this.ParameterConfigurations.ElementAt(i).Cross(otherVc.ParameterConfigurations.ElementAt(i), random);
275          }
276        }
277      }
278    }
279  }
280}
Note: See TracBrowser for help on using the repository browser.