Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215 worked on MetaOptimization

  • split configurations into ValueConfigurations and ParameterConfigurations
File size: 7.8 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 : DeepCloneable, IParameterConfiguration, IStorableContent {
12    [Storable]
13    public string Filename { get; set; }
14
15    [Storable]
16    protected bool optimizationEnabled;
17    public bool OptimizationEnabled {
18      get { return optimizationEnabled; }
19      set {
20        if (optimizationEnabled != value) {
21          optimizationEnabled = value;
22          if (optimizationEnabled) {
23            PopulateValueConfigurations();
24          } else {
25            this.ValueConfigurations.Clear();
26          }
27          OnOptimizationEnabledChanged();
28        }
29      }
30    }
31
32    [Storable]
33    protected string parameterName;
34    public string ParameterName {
35      get { return parameterName; }
36      set {
37        if (parameterName != value) {
38          parameterName = value;
39        }
40      }
41    }
42
43    protected Type parameterDataType;
44    public Type ParameterDataType {
45      get { return this.parameterDataType; }
46    }
47
48    protected IEnumerable<IItem> validValues;
49    public IEnumerable<IItem> ValidValues {
50      get { return validValues; }
51      protected set {
52        if (this.validValues != value) {
53          this.validValues = value;
54        }
55      }
56    }
57
58    protected Type valueDataType;
59    public Type ValueDataType {
60      get { return valueDataType; }
61      protected set { this.valueDataType = value; }
62    }
63
64    protected ICheckedItemList<IValueConfiguration> valueConfigurations;
65    public ICheckedItemList<IValueConfiguration> ValueConfigurations {
66      get { return this.valueConfigurations; }
67      protected set { this.valueConfigurations = value; }
68    }
69
70    // todo: chooses one of the parameter configurations as actual value
71    protected IValueConfiguration actualValueConfiguration;
72    public IValueConfiguration ActualValueConfiguration {
73      get { return this.actualValueConfiguration; }
74      set { this.actualValueConfiguration = value; }
75    }
76
77    // store parameter reference only for name/description/image/...
78    private IValueParameter parameter;
79
80    public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
81      this.ParameterName = parameterName;
82      this.parameterDataType = valueParameter.GetType();
83      this.parameter = valueParameter;
84      this.valueDataType = valueParameter.DataType;
85      this.validValues = GetValidValues();
86      this.ValueConfigurations = new CheckedItemList<IValueConfiguration>();
87      this.ActualValueConfiguration = new ValueConfiguration(valueParameter.Value, valueParameter.DataType);
88      if (OptimizationEnabled) {
89        PopulateValueConfigurations();
90      }
91    }
92
93    public ParameterConfiguration() { }
94    [StorableConstructor]
95    protected ParameterConfiguration(bool deserializing) { }
96    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) {
97      this.ParameterName = original.ParameterName;
98      this.parameterDataType = original.parameterDataType;
99      this.parameter = cloner.Clone(original.parameter);
100      this.ValueDataType = original.ValueDataType;
101      this.ValidValues = original.ValidValues.Select(x => cloner.Clone(x));
102      this.ValueConfigurations = cloner.Clone(original.ValueConfigurations);
103      this.ActualValueConfiguration = cloner.Clone(original.ActualValueConfiguration);
104      this.OptimizationEnabled = original.optimizationEnabled;
105    }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new ParameterConfiguration(this, cloner);
108    }
109
110    private void PopulateValueConfigurations() {
111      foreach (IItem item in this.validValues) {
112        this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()), false);
113      }
114    }
115
116    private IEnumerable<IItem> GetValidValues() {
117      return ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName).ToArray();
118    }
119
120    void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) {
121      var oldItems = e.OldItems.Select(x => x.Value);
122      var newItems = e.Items.Select(x => x.Value);
123      var intersection = oldItems.Intersect(newItems);
124      var removedItems = oldItems.Where(x => !intersection.Contains(x));
125      var addedItems = newItems.Where(x => !intersection.Contains(x));
126
127      foreach (var item in removedItems) {
128        RemoveValueConfiguration(item);
129      }
130      foreach (var item in addedItems) {
131        this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()));
132      }
133    }
134
135    private void RemoveValueConfiguration(IItem item) {
136      IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType());
137      this.ValueConfigurations.Remove(vc);
138    }
139
140    #region INamedItem Properties
141    public virtual string Name {
142      get { return ParameterName; }
143      set { throw new NotSupportedException(); }
144    }
145    public virtual string Description {
146      get { return parameter.Description; }
147      set { throw new NotSupportedException(); }
148    }
149    public virtual bool CanChangeDescription {
150      get { return false; }
151    }
152    public virtual bool CanChangeName {
153      get { return false; }
154    }
155    public virtual string ItemDescription {
156      get { return parameter.Description; }
157    }
158    public virtual System.Drawing.Image ItemImage {
159      get { return parameter.ItemImage; }
160    }
161    public virtual string ItemName {
162      get { return parameter.ItemName; }
163    }
164    public virtual Version ItemVersion {
165      get { return parameter.ItemVersion; }
166    }
167    #endregion
168
169    #region Events
170    public virtual event EventHandler NameChanged;
171    protected virtual void OnNameChanged(object sender, EventArgs e) {
172      var handler = NameChanged;
173      if (handler != null) handler(sender, e);
174    }
175
176    public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
177    protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
178      var handler = NameChanging;
179      if (handler != null) handler(sender, e);
180    }
181
182    public virtual event EventHandler DescriptionChanged;
183    protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
184      var handler = DescriptionChanged;
185      if (handler != null) handler(sender, e);
186    }
187
188    public virtual event EventHandler ItemImageChanged;
189    protected virtual void OnItemImageChanged(object sender, EventArgs e) {
190      var handler = ItemImageChanged;
191      if (handler != null) handler(sender, e);
192    }
193
194    public virtual event EventHandler ToStringChanged;
195    protected virtual void OnStringChanged(object sender, EventArgs e) {
196      var handler = ToStringChanged;
197      if (handler != null) handler(this, e); // important to set 'this' as sender
198    }
199
200    public virtual event EventHandler OptimizationEnabledChanged;
201    protected virtual void OnOptimizationEnabledChanged() {
202      var handler = OptimizationEnabledChanged;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    #endregion
206
207    public override string ToString() {
208      return string.Format("{0}", ParameterName);
209    }
210
211    public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter) {
212      if (parameter is IValueParameter) {
213        IValueParameter valueParameter = parameter as IValueParameter;
214
215        return new ParameterConfiguration(parameter.Name, valueParameter);
216      }
217      return null;
218    }
219
220  }
221}
Note: See TracBrowser for help on using the repository browser.