1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
12 | [StorableClass]
|
---|
13 | public class ParameterConfiguration : DeepCloneable, IParameterConfiguration, IStorableContent {
|
---|
14 | protected ParameterConfiguration parentConfiguration;
|
---|
15 |
|
---|
16 | [Storable]
|
---|
17 | public string Filename { get; set; }
|
---|
18 |
|
---|
19 | [Storable]
|
---|
20 | protected bool optimizationEnabled;
|
---|
21 | public bool OptimizationEnabled {
|
---|
22 | get { return optimizationEnabled; }
|
---|
23 | set {
|
---|
24 | if (optimizationEnabled != value) {
|
---|
25 | optimizationEnabled = value;
|
---|
26 | OnOptimizationEnabledChanged();
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | [Storable]
|
---|
32 | protected string parameterName;
|
---|
33 | public string ParameterName {
|
---|
34 | get { return parameterName; }
|
---|
35 | set {
|
---|
36 | if (parameterName != value) {
|
---|
37 | parameterName = value;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | protected IItemList<IParameterConfiguration> childParameterConfigurations = new ItemList<IParameterConfiguration>();
|
---|
44 | public IItemList<IParameterConfiguration> ChildParameterConfigurations {
|
---|
45 | get { return childParameterConfigurations; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | //public virtual IParameter Parameter {
|
---|
49 | // get {
|
---|
50 | // return parentConfiguration.GetChildParameter(this.parameterName);
|
---|
51 | // }
|
---|
52 | //}
|
---|
53 |
|
---|
54 |
|
---|
55 | protected Type parameterType;
|
---|
56 | public Type ParameterType {
|
---|
57 | get { return this.parameterType; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected Type valueType;
|
---|
61 | public Type ValueType {
|
---|
62 | get { return this.valueType; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected IItem value;
|
---|
66 | public IItem Value {
|
---|
67 | get { return value; }
|
---|
68 | set {
|
---|
69 | if (this.value != value) {
|
---|
70 | ClearChildParameterConfigurations();
|
---|
71 | OnValueChanging();
|
---|
72 | this.value = value;
|
---|
73 | if(this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem);
|
---|
74 | OnValueChanged();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | // store parameter reference only for name/description/image/...
|
---|
80 | private IValueParameter parameter;
|
---|
81 |
|
---|
82 | public ParameterConfiguration(ParameterConfiguration parentConfiguration, string parameterName, IValueParameter valueParameter) {
|
---|
83 | this.parentConfiguration = parentConfiguration;
|
---|
84 | this.ParameterName = parameterName;
|
---|
85 | this.Value = valueParameter.Value;
|
---|
86 | this.parameterType = valueParameter.GetType();
|
---|
87 | this.valueType = valueParameter.DataType;
|
---|
88 |
|
---|
89 | this.parameter = valueParameter;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public ParameterConfiguration() { }
|
---|
93 | [StorableConstructor]
|
---|
94 | protected ParameterConfiguration(bool deserializing) { }
|
---|
95 | protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
|
---|
96 | : base(original, cloner) {
|
---|
97 | this.OptimizationEnabled = original.optimizationEnabled;
|
---|
98 | this.ParameterName = original.parameterName;
|
---|
99 | this.Value = cloner.Clone(original.Value);
|
---|
100 | this.childParameterConfigurations = cloner.Clone(original.childParameterConfigurations);
|
---|
101 | }
|
---|
102 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
103 | return new ParameterConfiguration(this, cloner);
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected virtual IValueParameter GetChildParameter(string childParameterName) {
|
---|
107 | return (IValueParameter)((IParameterizedItem)Value).Parameters[childParameterName];
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) {
|
---|
111 | foreach (var childParameter in parameterizedItem.Parameters) {
|
---|
112 | var pc = ParameterConfiguration.Create(this, parameterizedItem, childParameter);
|
---|
113 | if (pc != null) this.childParameterConfigurations.Add(pc);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | protected virtual void ClearChildParameterConfigurations() {
|
---|
117 | childParameterConfigurations.Clear();
|
---|
118 | }
|
---|
119 |
|
---|
120 | #region INamedItem Properties
|
---|
121 | public virtual string Name {
|
---|
122 | get { return ParameterName; }
|
---|
123 | set { throw new NotSupportedException(); }
|
---|
124 | }
|
---|
125 | public virtual string Description {
|
---|
126 | get { return parameter.Description; }
|
---|
127 | set { throw new NotSupportedException(); }
|
---|
128 | }
|
---|
129 | public virtual bool CanChangeDescription {
|
---|
130 | get { return false; }
|
---|
131 | }
|
---|
132 | public virtual bool CanChangeName {
|
---|
133 | get { return false; }
|
---|
134 | }
|
---|
135 | public virtual string ItemDescription {
|
---|
136 | get { return parameter.Description; }
|
---|
137 | }
|
---|
138 | public virtual System.Drawing.Image ItemImage {
|
---|
139 | get { return parameter.ItemImage; }
|
---|
140 | }
|
---|
141 | public virtual string ItemName {
|
---|
142 | get { return parameter.ItemName; }
|
---|
143 | }
|
---|
144 | public virtual Version ItemVersion {
|
---|
145 | get { return parameter.ItemVersion; }
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Events
|
---|
150 | public virtual event EventHandler NameChanged;
|
---|
151 | protected virtual void OnNameChanged(object sender, EventArgs e) {
|
---|
152 | var handler = NameChanged;
|
---|
153 | if (handler != null) handler(sender, e);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
157 | protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
|
---|
158 | var handler = NameChanging;
|
---|
159 | if (handler != null) handler(sender, e);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public virtual event EventHandler DescriptionChanged;
|
---|
163 | protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
|
---|
164 | var handler = DescriptionChanged;
|
---|
165 | if (handler != null) handler(sender, e);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public virtual event EventHandler ItemImageChanged;
|
---|
169 | protected virtual void OnItemImageChanged(object sender, EventArgs e) {
|
---|
170 | var handler = ItemImageChanged;
|
---|
171 | if (handler != null) handler(sender, e);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public virtual event EventHandler ToStringChanged;
|
---|
175 | protected virtual void OnStringChanged(object sender, EventArgs e) {
|
---|
176 | var handler = ToStringChanged;
|
---|
177 | if (handler != null) handler(this, e); // important to set 'this' as sender
|
---|
178 | }
|
---|
179 |
|
---|
180 | public virtual event EventHandler OptimizationEnabledChanged;
|
---|
181 | protected virtual void OnOptimizationEnabledChanged() {
|
---|
182 | var handler = OptimizationEnabledChanged;
|
---|
183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public virtual event EventHandler ValueChanging;
|
---|
187 | protected virtual void OnValueChanging() {
|
---|
188 | var handler = ValueChanging;
|
---|
189 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public virtual event EventHandler ValueChanged;
|
---|
193 | protected virtual void OnValueChanged() {
|
---|
194 | var handler = ValueChanged;
|
---|
195 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
196 | }
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | public override string ToString() {
|
---|
200 | return string.Format("{0} ({1})", ParameterName, Value);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public static IParameterConfiguration Create(IParameterizedNamedItem parent) {
|
---|
204 | return new RootParameterConfiguration(parent);
|
---|
205 | }
|
---|
206 |
|
---|
207 | public static IParameterConfiguration Create(ParameterConfiguration parentConfiguration, IParameterizedNamedItem parent, IParameter parameter) {
|
---|
208 | if (parameter is IValueParameter) {
|
---|
209 | IValueParameter valueParameter = parameter as IValueParameter;
|
---|
210 |
|
---|
211 | if (typeof(IntValue).IsAssignableFrom(parameter.DataType)) {
|
---|
212 | return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter);
|
---|
213 | } else if (typeof(IParameterizedItem).IsAssignableFrom(parameter.DataType)) {
|
---|
214 | return new ParameterConfiguration(parentConfiguration, parameter.Name, valueParameter);
|
---|
215 | } else {
|
---|
216 | // todo
|
---|
217 | return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return null;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public void Parameterize(IParameterizedItem parameterizedItem) {
|
---|
224 | throw new NotImplementedException();
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|
228 | }
|
---|