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