1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HEAL.Attic;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
10 | // TODO: ItemName/Descr, storability
|
---|
11 | [StorableType("A5710490-7B38-40B2-943B-5AA55181C816")]
|
---|
12 | public abstract class ValueConfiguration : NamedItem, IValueConfiguration {
|
---|
13 | [Storable]
|
---|
14 | protected bool isOptimizable;
|
---|
15 | public virtual bool IsOptimizable {
|
---|
16 | get { return isOptimizable; }
|
---|
17 | set {
|
---|
18 | if (this.isOptimizable != value) {
|
---|
19 | this.isOptimizable = value;
|
---|
20 | OnIsOptimizableChanged();
|
---|
21 | }
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | [Storable]
|
---|
26 | protected bool optimize;
|
---|
27 | public virtual bool Optimize {
|
---|
28 | get { return optimize; }
|
---|
29 | set {
|
---|
30 | if (optimize != value) {
|
---|
31 | if (value == true && !this.IsOptimizable)
|
---|
32 | throw new NotSupportedException("This value is not optimizable.");
|
---|
33 |
|
---|
34 | optimize = value;
|
---|
35 | OnOptimizeChanged();
|
---|
36 | OnToStringChanged();
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | protected ConstrainedValue actualValue;
|
---|
43 | public virtual ConstrainedValue ActualValue {
|
---|
44 | get { return actualValue; }
|
---|
45 | set {
|
---|
46 | if (this.actualValue != value) {
|
---|
47 | DeregisterActualValueEvents();
|
---|
48 | this.actualValue = value;
|
---|
49 | OnValueChanged();
|
---|
50 | OnToStringChanged();
|
---|
51 | RegisterActualValueEvents();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | [Storable]
|
---|
57 | protected int number = 0;
|
---|
58 | public int Number {
|
---|
59 | get { return number; }
|
---|
60 | set {
|
---|
61 | if (value != number) {
|
---|
62 | number = value;
|
---|
63 | OnToStringChanged();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [Storable]
|
---|
69 | protected bool valuesReadOnly = false;
|
---|
70 | public virtual bool ValuesReadOnly {
|
---|
71 | get { return valuesReadOnly; }
|
---|
72 | set {
|
---|
73 | if (value != this.valuesReadOnly) {
|
---|
74 | this.valuesReadOnly = value;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | #region Constructors and Cloning
|
---|
80 | protected ValueConfiguration(IItem value, Type valueDataType) {
|
---|
81 | this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
|
---|
82 | this.IsOptimizable = true;
|
---|
83 | this.Name = value == null ? "ValueConfiguration" : value.ItemName;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected ValueConfiguration() { }
|
---|
87 | [StorableConstructor]
|
---|
88 | protected ValueConfiguration(StorableConstructorFlag _) { }
|
---|
89 | protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | this.actualValue = cloner.Clone(original.ActualValue);
|
---|
92 | this.isOptimizable = original.isOptimizable;
|
---|
93 | this.optimize = original.optimize;
|
---|
94 | this.number = original.number;
|
---|
95 | this.valuesReadOnly = original.valuesReadOnly;
|
---|
96 | RegisterActualValueEvents();
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | private void RegisterActualValueEvents() {
|
---|
101 | if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
|
---|
102 | }
|
---|
103 | private void DeregisterActualValueEvents() {
|
---|
104 | if (this.actualValue != null) this.actualValue.ToStringChanged -= new EventHandler(actualValue_ToStringChanged);
|
---|
105 | }
|
---|
106 |
|
---|
107 | #region Events
|
---|
108 | private void actualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
109 | OnToStringChanged();
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region IItem Members
|
---|
114 | public override string ItemDescription {
|
---|
115 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override Image ItemImage {
|
---|
119 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override string ItemName {
|
---|
123 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
|
---|
124 | }
|
---|
125 | #endregion
|
---|
126 |
|
---|
127 | #region Event Handlers
|
---|
128 | public virtual event EventHandler ValueChanged;
|
---|
129 | protected virtual void OnValueChanged() {
|
---|
130 | var handler = ValueChanged;
|
---|
131 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public virtual event EventHandler IsOptimizableChanged;
|
---|
135 | private void OnIsOptimizableChanged() {
|
---|
136 | var handler = IsOptimizableChanged;
|
---|
137 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public virtual event EventHandler OptimizeChanged;
|
---|
141 | protected virtual void OnOptimizeChanged() {
|
---|
142 | var handler = OptimizeChanged;
|
---|
143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | public string NumberedName {
|
---|
148 | get {
|
---|
149 | if (this.number == 0) {
|
---|
150 | return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
|
---|
151 | } else {
|
---|
152 | return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public abstract void Randomize(IRandom random);
|
---|
158 | public abstract void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator);
|
---|
159 | public abstract void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover);
|
---|
160 | public abstract double CalculateSimilarity(IOptimizable optimizable);
|
---|
161 | public abstract string ParameterInfoString { get; }
|
---|
162 | public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
|
---|
163 | public abstract List<IOptimizable> GetAllOptimizables();
|
---|
164 | }
|
---|
165 | }
|
---|