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