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