Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfigurations/ParameterConfiguration.cs @ 4997

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

#1215 worked on metaoptimization

File size: 10.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.PluginInfrastructure;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [StorableClass]
11  public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent {
12    public bool IsOptimizable {
13      get { return true; }
14    }
15
16    [Storable]
17    public string Filename { get; set; }
18
19    [Storable]
20    protected bool optimize;
21    public bool Optimize {
22      get { return optimize; }
23      set {
24        if (optimize != value) {
25          optimize = value;
26          if (optimize) {
27            PopulateValueConfigurations();
28          } else {
29            this.ValueConfigurations.Clear();
30          }
31          OnOptimizeChanged();
32          OnToStringChanged();
33        }
34      }
35    }
36
37    [Storable]
38    protected string parameterName;
39    public string ParameterName {
40      get { return parameterName; }
41      set {
42        if (parameterName != value) {
43          parameterName = value;
44          OnToStringChanged();
45        }
46      }
47    }
48
49    protected Type parameterDataType;
50    public Type ParameterDataType {
51      get { return this.parameterDataType; }
52    }
53
54    protected IEnumerable<IItem> validValues;
55    public IEnumerable<IItem> ValidValues {
56      get { return validValues; }
57      protected set {
58        if (this.validValues != value) {
59          this.validValues = value;
60        }
61      }
62    }
63
64    protected Type valueDataType;
65    public Type ValueDataType {
66      get { return valueDataType; }
67      protected set { this.valueDataType = value; }
68    }
69
70    protected ICheckedValueConfigurationCollection valueConfigurations;
71    public ICheckedValueConfigurationCollection ValueConfigurations {
72      get { return this.valueConfigurations; }
73      protected set {
74        if (this.valueConfigurations != value) {
75          if (this.valueConfigurations != null) DeregisterValueConfigurationEvents();
76          this.valueConfigurations = value;
77          if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
78        }
79      }
80    }
81
82    protected int actualValueConfigurationIndex = 0;
83
84    protected ConstrainedValue actualValue;
85    public ConstrainedValue ActualValue {
86      get { return actualValue; }
87      set { actualValue = value; }
88    }
89   
90    // store parameter reference only for name/description/image/...
91    private IValueParameter parameter;
92
93    #region Constructors and Cloning
94    public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
95      this.ParameterName = parameterName;
96      this.parameterDataType = valueParameter.GetType();
97      this.parameter = valueParameter;
98      this.valueDataType = valueParameter.DataType;
99      this.validValues = GetValidValues();
100      this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType);
101      this.ActualValue = new ConstrainedValue(valueParameter.Value, valueParameter.DataType);
102      if (Optimize) {
103        PopulateValueConfigurations();
104      }
105    }
106
107    public ParameterConfiguration() { }
108    [StorableConstructor]
109    protected ParameterConfiguration(bool deserializing) { }
110    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) {
111      this.parameterName = original.ParameterName;
112      this.parameterDataType = original.parameterDataType;
113      this.parameter = cloner.Clone(original.parameter);
114      this.valueDataType = original.ValueDataType;
115      this.validValues = original.ValidValues.Select(x => cloner.Clone(x));
116      this.ValueConfigurations = cloner.Clone(original.ValueConfigurations);
117      this.ActualValue = cloner.Clone(original.ActualValue);
118      this.optimize = original.optimize;
119    }
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new ParameterConfiguration(this, cloner);
122    }
123    #endregion
124
125    private void RegisterValueConfigurationEvents() {
126      this.ValueConfigurations.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
127      this.ValueConfigurations.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
128      this.ValueConfigurations.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
129    }
130
131    private void DeregisterValueConfigurationEvents() {
132      this.ValueConfigurations.CheckedItemsChanged -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
133      this.ValueConfigurations.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
134      this.ValueConfigurations.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
135    }
136
137    private void PopulateValueConfigurations() {
138      foreach (IItem validValue in this.validValues) {
139        IItem val;
140        if (ActualValue.Value != null && ActualValue.ValueDataType == validValue.GetType()) {
141          val = ActualValue.Value; // use existing value for that type (if available)
142        } else {
143          val = validValue;
144        }
145        this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
146      }
147    }
148
149    private IEnumerable<IItem> GetValidValues() {
150      return ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName).ToArray();
151    }
152
153    void ValueConfigurations_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
154      OnToStringChanged();
155    }
156    void ValueConfigurations_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
157      OnToStringChanged();
158    }
159    void ValueConfigurations_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
160      OnToStringChanged();
161    }
162
163    #region INamedItem Properties
164    public virtual string Name {
165      get { return ParameterName; }
166      set { throw new NotSupportedException(); }
167    }
168    public virtual string Description {
169      get { return parameter.Description; }
170      set { throw new NotSupportedException(); }
171    }
172    public virtual bool CanChangeDescription {
173      get { return false; }
174    }
175    public virtual bool CanChangeName {
176      get { return false; }
177    }
178    public override string ItemDescription {
179      get { return parameter != null ? parameter.Description : base.ItemDescription; }
180    }
181    public override System.Drawing.Image ItemImage {
182      get { return parameter != null ? parameter.ItemImage : base.ItemImage; }
183    }
184    public override string ItemName {
185      get { return parameter != null ?parameter.ItemName : base.ItemName; }
186    }
187    #endregion
188
189    #region Events
190    public virtual event EventHandler NameChanged;
191    protected virtual void OnNameChanged(object sender, EventArgs e) {
192      var handler = NameChanged;
193      if (handler != null) handler(sender, e);
194    }
195
196    public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
197    protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
198      var handler = NameChanging;
199      if (handler != null) handler(sender, e);
200    }
201
202    public virtual event EventHandler DescriptionChanged;
203    protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
204      var handler = DescriptionChanged;
205      if (handler != null) handler(sender, e);
206    }
207   
208    public virtual event EventHandler IsOptimizableChanged;
209    private void OnIsOptimizableChanged() {
210      var handler = IsOptimizableChanged;
211      if (handler != null) handler(this, EventArgs.Empty);
212    }
213
214    public virtual event EventHandler OptimizeChanged;
215    protected virtual void OnOptimizeChanged() {
216      var handler = OptimizeChanged;
217      if (handler != null) handler(this, EventArgs.Empty);
218    }
219    #endregion
220
221    public override string ToString() {
222      if (Optimize) {
223        return string.Format("{0}: (Optimize: {1})", ParameterName, ValueConfigurations.CheckedItems.Count());
224      } else {
225        return string.Format("{0}: {1}", ParameterName, ActualValue.Value);
226      }
227    }
228
229    public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter) {
230      if (parameter is IValueParameter) {
231        IValueParameter valueParameter = parameter as IValueParameter;
232        return new ParameterConfiguration(parameter.Name, valueParameter);
233      }
234      return null;
235    }
236
237    public void Parameterize() {
238      this.parameter.ActualValue = this.ActualValue.Value;
239    }
240
241    public void Randomize() {
242      if (Optimize) {
243        Random rand = new Random(); // todo: use global random
244        actualValueConfigurationIndex = rand.Next(ValueConfigurations.CheckedItems.Count());
245        this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).Randomize();
246        this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
247      }
248    }
249
250    public void Mutate() {
251      if (Optimize) {
252        foreach (IValueConfiguration vc in this.ValueConfigurations.CheckedItems) {
253          vc.Mutate();
254        }
255        this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
256      }
257    }
258
259    public void Cross(IOptimizable other) {
260      if (Optimize) {
261        IParameterConfiguration otherPc = (IParameterConfiguration)other;
262        for (int i = 0; i < this.ValueConfigurations.Count; i++) {
263          this.ValueConfigurations.ElementAt(i).Cross(otherPc.ValueConfigurations.ElementAt(i));
264        }
265        this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
266      }
267    }
268  }
269}
Note: See TracBrowser for help on using the repository browser.