Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215 worked on metaoptimization

File size: 9.3 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    // todo: chooses one of the parameter configurations as actual value
83    protected IValueConfiguration actualValueConfiguration;
84    public IValueConfiguration ActualValueConfiguration {
85      get { return this.actualValueConfiguration; }
86      set { this.actualValueConfiguration = value; }
87    }
88
89    public ConstrainedValue ConstrainedValue {
90      get { return actualValueConfiguration.ConstrainedValue; }
91      set { actualValueConfiguration.ConstrainedValue = value; }
92    }
93   
94    // store parameter reference only for name/description/image/...
95    private IValueParameter parameter;
96
97    #region Constructors and Cloning
98    public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
99      this.ParameterName = parameterName;
100      this.parameterDataType = valueParameter.GetType();
101      this.parameter = valueParameter;
102      this.valueDataType = valueParameter.DataType;
103      this.validValues = GetValidValues();
104      this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType);
105      this.ActualValueConfiguration = new ValueConfiguration(valueParameter.Value, valueParameter.DataType);
106      if (Optimize) {
107        PopulateValueConfigurations();
108      }
109    }
110
111    public ParameterConfiguration() { }
112    [StorableConstructor]
113    protected ParameterConfiguration(bool deserializing) { }
114    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
115      : base(original, cloner) {
116      this.ParameterName = original.ParameterName;
117      this.parameterDataType = original.parameterDataType;
118      this.parameter = cloner.Clone(original.parameter);
119      this.ValueDataType = original.ValueDataType;
120      this.ValidValues = original.ValidValues.Select(x => cloner.Clone(x));
121      this.ValueConfigurations = cloner.Clone(original.ValueConfigurations);
122      this.ActualValueConfiguration = cloner.Clone(original.ActualValueConfiguration);
123      this.Optimize = original.optimize;
124    }
125    public override IDeepCloneable Clone(Cloner cloner) {
126      return new ParameterConfiguration(this, cloner);
127    }
128    #endregion
129
130    private void RegisterValueConfigurationEvents() {
131      this.ValueConfigurations.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
132      this.ValueConfigurations.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
133      this.ValueConfigurations.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
134    }
135
136    private void DeregisterValueConfigurationEvents() {
137      this.ValueConfigurations.CheckedItemsChanged -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
138      this.ValueConfigurations.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
139      this.ValueConfigurations.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
140    }
141
142    private void PopulateValueConfigurations() {
143      foreach (IItem validValue in this.validValues) {
144        IItem val;
145        if (actualValueConfiguration.ConstrainedValue.Value != null && actualValueConfiguration.ConstrainedValue.ValueDataType == validValue.GetType()) {
146          val = actualValueConfiguration.ConstrainedValue.Value; // use existing value for that type (if available)
147        } else {
148          val = validValue;
149        }
150        this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
151      }
152    }
153
154    private IEnumerable<IItem> GetValidValues() {
155      return ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName).ToArray();
156    }
157
158    void ValueConfigurations_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
159      OnToStringChanged();
160    }
161    void ValueConfigurations_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
162      OnToStringChanged();
163    }
164    void ValueConfigurations_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
165      OnToStringChanged();
166    }
167
168    #region INamedItem Properties
169    public virtual string Name {
170      get { return ParameterName; }
171      set { throw new NotSupportedException(); }
172    }
173    public virtual string Description {
174      get { return parameter.Description; }
175      set { throw new NotSupportedException(); }
176    }
177    public virtual bool CanChangeDescription {
178      get { return false; }
179    }
180    public virtual bool CanChangeName {
181      get { return false; }
182    }
183    public override string ItemDescription {
184      get { return parameter != null ? parameter.Description : base.ItemDescription; }
185    }
186    public override System.Drawing.Image ItemImage {
187      get { return parameter != null ? parameter.ItemImage : base.ItemImage; }
188    }
189    public override string ItemName {
190      get { return parameter != null ?parameter.ItemName : base.ItemName; }
191    }
192    #endregion
193
194    #region Events
195    public virtual event EventHandler NameChanged;
196    protected virtual void OnNameChanged(object sender, EventArgs e) {
197      var handler = NameChanged;
198      if (handler != null) handler(sender, e);
199    }
200
201    public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
202    protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
203      var handler = NameChanging;
204      if (handler != null) handler(sender, e);
205    }
206
207    public virtual event EventHandler DescriptionChanged;
208    protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
209      var handler = DescriptionChanged;
210      if (handler != null) handler(sender, e);
211    }
212   
213    public virtual event EventHandler IsOptimizableChanged;
214    private void OnIsOptimizableChanged() {
215      var handler = IsOptimizableChanged;
216      if (handler != null) handler(this, EventArgs.Empty);
217    }
218
219    public virtual event EventHandler OptimizeChanged;
220    protected virtual void OnOptimizeChanged() {
221      var handler = OptimizeChanged;
222      if (handler != null) handler(this, EventArgs.Empty);
223    }
224    #endregion
225
226    public override string ToString() {
227      if (Optimize) {
228        return string.Format("{0}: (Optimize: {1})", ParameterName, ValueConfigurations.CheckedItems.Count());
229      } else {
230        return string.Format("{0}: {1}", ParameterName, ConstrainedValue.Value);
231      }
232    }
233
234    public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter) {
235      if (parameter is IValueParameter) {
236        IValueParameter valueParameter = parameter as IValueParameter;
237        return new ParameterConfiguration(parameter.Name, valueParameter);
238      }
239      return null;
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.