1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using System.Drawing;
|
---|
8 |
|
---|
9 | namespace 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 actualValue;
|
---|
48 | public ConstrainedValue ActualValue {
|
---|
49 | get { return actualValue; }
|
---|
50 | set {
|
---|
51 | if (this.actualValue != value) {
|
---|
52 | ClearParameterConfigurations();
|
---|
53 | this.actualValue = 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.ActualValue = new ConstrainedValue(value, valueDataType);
|
---|
70 | this.IsOptimizable = true;
|
---|
71 | if (actualValue.ValueDataType == typeof(IntValue)) {
|
---|
72 | rangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value, new IntValue(1));
|
---|
73 | } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
|
---|
74 | rangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.01));
|
---|
75 | } else if (actualValue.ValueDataType == typeof(PercentValue)) {
|
---|
76 | rangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
|
---|
77 | } else if (actualValue.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.actualValue = cloner.Clone(original.ActualValue);
|
---|
91 | this.rangeConstraint = cloner.Clone(original.RangeConstraint);
|
---|
92 | this.isOptimizable = original.isOptimizable;
|
---|
93 | this.optimize = original.optimize;
|
---|
94 | RegisterEvents();
|
---|
95 | }
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new ValueConfiguration(this, cloner);
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | protected virtual void PopulateParameterConfigurations() {
|
---|
102 | if (this.actualValue.Value is IParameterizedNamedItem) {
|
---|
103 | var parameterizedItem = this.actualValue.Value as IParameterizedNamedItem;
|
---|
104 | foreach (var childParameter in parameterizedItem.Parameters) {
|
---|
105 | var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
|
---|
106 | if (pc != null) this.parameterConfigurations.Add(pc);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | protected virtual void ClearParameterConfigurations() {
|
---|
111 | parameterConfigurations.Clear();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void RegisterEvents() {
|
---|
115 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
116 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
117 | }
|
---|
118 | private void DeregisterEvents() {
|
---|
119 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
120 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
|
---|
124 | OnToStringChanged();
|
---|
125 | }
|
---|
126 | void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
|
---|
127 | OnToStringChanged();
|
---|
128 | }
|
---|
129 |
|
---|
130 | #region IItem Members
|
---|
131 | public override string ItemDescription {
|
---|
132 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override Image ItemImage {
|
---|
136 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override string ItemName {
|
---|
140 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
|
---|
141 | }
|
---|
142 | #endregion
|
---|
143 |
|
---|
144 | #region Event Handlers
|
---|
145 | public virtual event EventHandler ValueChanged;
|
---|
146 | protected virtual void OnValueChanged() {
|
---|
147 | var handler = ValueChanged;
|
---|
148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public virtual event EventHandler IsOptimizableChanged;
|
---|
152 | private void OnIsOptimizableChanged() {
|
---|
153 | var handler = IsOptimizableChanged;
|
---|
154 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public virtual event EventHandler OptimizeChanged;
|
---|
158 | protected virtual void OnOptimizeChanged() {
|
---|
159 | var handler = OptimizeChanged;
|
---|
160 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | public override string ToString() {
|
---|
165 | if (ActualValue != null && ActualValue.Value != null) {
|
---|
166 | if (ActualValue.Value is IParameterizedItem) {
|
---|
167 | if (Optimize) {
|
---|
168 | return string.Format("{0} (Optimize)", ActualValue.Value.ItemName);
|
---|
169 | } else {
|
---|
170 | return string.Format("{0}", ActualValue.Value.ItemName);
|
---|
171 | }
|
---|
172 | } else {
|
---|
173 | if (Optimize) {
|
---|
174 | return string.Format("{0} (Optimize: {1})", ActualValue.Value.ItemName, RangeConstraint);
|
---|
175 | } else {
|
---|
176 | return string.Format("{0}: {1}", ActualValue.Value.ItemName, ActualValue.Value);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | } else {
|
---|
180 | return base.ToString();
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void Parameterize() {
|
---|
185 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
186 | pc.Parameterize();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | public void Randomize() {
|
---|
191 | if (Optimize) {
|
---|
192 | if (rangeConstraint != null) {
|
---|
193 | this.actualValue.Value = rangeConstraint.GetRandomValue();
|
---|
194 | } else {
|
---|
195 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
196 | pc.Randomize();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void Mutate() {
|
---|
203 | if (Optimize) {
|
---|
204 | if (rangeConstraint != null) {
|
---|
205 | Random rand = new Random(); // todo: use common random
|
---|
206 | // mutate every other value. todo: use some kind of mutation operator which is exchangable
|
---|
207 | if(rand.NextDouble() > 0.5)
|
---|
208 | this.actualValue.Value = rangeConstraint.GetRandomValue();
|
---|
209 | } else {
|
---|
210 | foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
|
---|
211 | pc.Mutate();
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | public void Cross(IOptimizable other) {
|
---|
218 | if (Optimize) {
|
---|
219 | IValueConfiguration otherVc = (IValueConfiguration)other;
|
---|
220 | if (rangeConstraint != null) {
|
---|
221 | if (this.actualValue.ValueDataType == typeof(IntValue)) {
|
---|
222 | this.actualValue.Value = new IntValue((((IntValue)this.actualValue.Value).Value + ((IntValue)other.ActualValue.Value).Value) / 2);
|
---|
223 | } else if (this.actualValue.ValueDataType == typeof(DoubleValue)) {
|
---|
224 | this.actualValue.Value = new DoubleValue((((DoubleValue)this.actualValue.Value).Value + ((DoubleValue)other.ActualValue.Value).Value) / 2);
|
---|
225 | } else if (this.actualValue.ValueDataType == typeof(PercentValue)) {
|
---|
226 | this.actualValue.Value = new PercentValue((((PercentValue)this.actualValue.Value).Value + ((PercentValue)other.ActualValue.Value).Value) / 2);
|
---|
227 | } else if (this.actualValue.ValueDataType == typeof(BoolValue)) {
|
---|
228 | bool first = ((BoolValue)this.actualValue.Value).Value;
|
---|
229 | bool second = ((BoolValue)other.ActualValue.Value).Value;
|
---|
230 | if (first && second) {
|
---|
231 | this.actualValue.Value = new BoolValue(true);
|
---|
232 | } else if (!first && !second) {
|
---|
233 | this.actualValue.Value = new BoolValue(false);
|
---|
234 | } else {
|
---|
235 | Random rand = new Random(); // todo: use common random
|
---|
236 | if(rand.NextDouble() > 0.5)
|
---|
237 | this.actualValue.Value = new BoolValue(true);
|
---|
238 | else
|
---|
239 | this.actualValue.Value = new BoolValue(false);
|
---|
240 | }
|
---|
241 | } else {
|
---|
242 | throw new NotImplementedException();
|
---|
243 | }
|
---|
244 | } else {
|
---|
245 | for (int i = 0; i < this.ParameterConfigurations.Count; i++) {
|
---|
246 | this.ParameterConfigurations.ElementAt(i).Cross(otherVc.ParameterConfigurations.ElementAt(i));
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|