Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215 worked on metaoptimization

File size: 7.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;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  // TODO: ItemName/Descr, storability
11  public class ValueConfiguration : Item, IValueConfiguration {
12    protected bool isOptimizable;
13    public bool IsOptimizable {
14      get { return isOptimizable; }
15      set {
16        if (this.isOptimizable != value) {
17          this.isOptimizable = value;
18          OnIsOptimizableChanged();
19        }
20      }
21    }
22
23    protected bool optimize;
24    public bool Optimize {
25      get { return optimize; }
26      set {
27        if (optimize != value) {
28          optimize = value;
29          if (optimize) {
30            ClearParameterConfigurations();
31            PopulateParameterConfigurations();
32          } else {
33            ClearParameterConfigurations();
34          }
35          OnOptimizeChanged();
36          OnToStringChanged();
37        }
38      }
39    }
40
41    protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
42    public IItemCollection<IParameterConfiguration> ParameterConfigurations {
43      get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
44      protected set { this.parameterConfigurations = value; }
45    }
46
47    protected ConstrainedValue constrainedValue;
48    public ConstrainedValue ConstrainedValue {
49      get { return constrainedValue; }
50      set {
51        if (this.constrainedValue != value) {
52          ClearParameterConfigurations();
53          this.constrainedValue = value;
54          PopulateParameterConfigurations();
55          OnValueChanged();
56          OnToStringChanged();
57        }
58      }
59    }
60
61    protected IRange rangeConstraint;
62    public IRange RangeConstraint {
63      get { return rangeConstraint; }
64    }
65
66    #region Constructors and Cloning
67    public ValueConfiguration(IItem value, Type valueDataType) {
68      this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
69      this.ConstrainedValue = new ConstrainedValue(value, valueDataType);
70      this.IsOptimizable = true;
71      if (constrainedValue.ValueDataType == typeof(IntValue)) {
72        rangeConstraint = new Range<IntValue>(new IntValue(0), (IntValue)value, new IntValue(1));
73      } else if (constrainedValue.ValueDataType == typeof(DoubleValue)) {
74        rangeConstraint = new Range<DoubleValue>(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.01));
75      } else if (constrainedValue.ValueDataType == typeof(PercentValue)) {
76        rangeConstraint = new Range<PercentValue>(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
77      } else if (constrainedValue.ValueDataType == typeof(BoolValue)) {
78        this.IsOptimizable = false; // there is nothing to configure for bools
79      } else {
80        rangeConstraint = null;
81      }
82      RegisterEvents();
83    }
84
85    public ValueConfiguration() { }
86    [StorableConstructor]
87    protected ValueConfiguration(bool deserializing) { }
88    protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) {
89      this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations);
90      this.ConstrainedValue = cloner.Clone(original.ConstrainedValue);
91      this.rangeConstraint = cloner.Clone(original.RangeConstraint);
92      this.IsOptimizable = original.IsOptimizable;
93      RegisterEvents();
94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new ValueConfiguration(this, cloner);
97    }
98    #endregion
99
100    protected virtual void PopulateParameterConfigurations() {
101      if (this.constrainedValue.Value is IParameterizedNamedItem) {
102        var parameterizedItem = this.constrainedValue.Value as IParameterizedNamedItem;
103        foreach (var childParameter in parameterizedItem.Parameters) {
104          var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
105          if (pc != null) this.parameterConfigurations.Add(pc);
106        }
107      }
108    }
109    protected virtual void ClearParameterConfigurations() {
110      parameterConfigurations.Clear();
111    }
112
113    private void RegisterEvents() {
114      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
115      if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
116    }
117    private void DeregisterEvents() {
118      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
119      if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
120    }
121
122    void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
123      OnToStringChanged();
124    }
125    void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
126      OnToStringChanged();
127    }
128
129    #region IItem Members
130    public override string ItemDescription {
131      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; }
132    }
133
134    public override Image ItemImage {
135      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemImage : base.ItemImage; }
136    }
137
138    public override string ItemName {
139      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemName; }
140    }
141    #endregion
142
143    #region Event Handlers
144    public virtual event EventHandler ValueChanged;
145    protected virtual void OnValueChanged() {
146      var handler = ValueChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149
150    public virtual event EventHandler IsOptimizableChanged;
151    private void OnIsOptimizableChanged() {
152      var handler = IsOptimizableChanged;
153      if (handler != null) handler(this, EventArgs.Empty);
154    }
155
156    public virtual event EventHandler OptimizeChanged;
157    protected virtual void OnOptimizeChanged() {
158      var handler = OptimizeChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161    #endregion
162
163    public override string ToString() {
164      if (ConstrainedValue != null && ConstrainedValue.Value != null) {
165        if (ConstrainedValue.Value is IParameterizedItem) {
166          if (Optimize) {
167            return string.Format("{0} (Optimize)", ConstrainedValue.Value.ItemName);
168          } else {
169            return string.Format("{0}", ConstrainedValue.Value.ItemName);
170          }
171        } else {
172          if (Optimize) {
173            return string.Format("{0} (Optimize: {1})", ConstrainedValue.Value.ItemName, RangeConstraint);
174          } else {
175            return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, ConstrainedValue.Value);
176          }
177        }
178      } else {
179        return base.ToString();
180      }
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.