1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Text;
|
---|
7 | using HeuristicLab.Collections;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
12 | using HeuristicLab.Parameters;
|
---|
13 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
14 | using HeuristicLab.PluginInfrastructure;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
17 | [StorableClass]
|
---|
18 | public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent {
|
---|
19 | public bool IsOptimizable {
|
---|
20 | get { return true; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | [Storable]
|
---|
24 | public string Filename { get; set; }
|
---|
25 |
|
---|
26 | [Storable]
|
---|
27 | protected bool optimize;
|
---|
28 | public virtual bool Optimize {
|
---|
29 | get { return optimize; }
|
---|
30 | set {
|
---|
31 | if (optimize != value) {
|
---|
32 | optimize = value;
|
---|
33 | if (optimize) {
|
---|
34 | PopulateValueConfigurations();
|
---|
35 | } else {
|
---|
36 | ClearValueConfigurations();
|
---|
37 | }
|
---|
38 | OnOptimizeChanged();
|
---|
39 | OnToStringChanged();
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | protected Image itemImage;
|
---|
46 | public override Image ItemImage {
|
---|
47 | get { return itemImage ?? base.ItemImage; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | protected string parameterName;
|
---|
52 | public string ParameterName {
|
---|
53 | get { return parameterName; }
|
---|
54 | set {
|
---|
55 | if (parameterName != value) {
|
---|
56 | parameterName = value;
|
---|
57 | OnToStringChanged();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [Storable]
|
---|
63 | protected Type parameterDataType;
|
---|
64 | public Type ParameterDataType {
|
---|
65 | get { return this.parameterDataType; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [Storable]
|
---|
69 | protected Type[] validTypes;
|
---|
70 | public Type[] ValidTypes {
|
---|
71 | get { return validTypes; }
|
---|
72 | protected set {
|
---|
73 | if (this.validTypes != value) {
|
---|
74 | this.validTypes = value;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | protected Type valueDataType;
|
---|
81 | public Type ValueDataType {
|
---|
82 | get { return valueDataType; }
|
---|
83 | protected set { this.valueDataType = value; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | [Storable]
|
---|
87 | protected ICheckedValueConfigurationList valueConfigurations;
|
---|
88 | public ICheckedValueConfigurationList ValueConfigurations {
|
---|
89 | get { return this.valueConfigurations; }
|
---|
90 | protected set {
|
---|
91 | if (this.valueConfigurations != value) {
|
---|
92 | if (this.valueConfigurations != null) DeregisterValueConfigurationEvents();
|
---|
93 | this.valueConfigurations = value;
|
---|
94 | KeepActualValueConfigurationIndexConsistent();
|
---|
95 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | [Storable]
|
---|
101 | private int actualValueConfigurationIndex = 0;
|
---|
102 | public int ActualValueConfigurationIndex {
|
---|
103 | get { return actualValueConfigurationIndex; }
|
---|
104 | set { actualValueConfigurationIndex = value; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | [Storable]
|
---|
108 | protected ConstrainedValue actualValue;
|
---|
109 | public ConstrainedValue ActualValue {
|
---|
110 | get { return actualValue; }
|
---|
111 | set {
|
---|
112 | if (actualValue != value) {
|
---|
113 | DeregisterActualValueEvents();
|
---|
114 | actualValue = value;
|
---|
115 | RegisterActualValueEvents();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | [Storable]
|
---|
121 | protected bool isNullable;
|
---|
122 | public bool IsNullable {
|
---|
123 | get { return isNullable; }
|
---|
124 | protected set {
|
---|
125 | if (this.isNullable != value) {
|
---|
126 | this.isNullable = value;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | [Storable]
|
---|
132 | protected bool discoverValidValues;
|
---|
133 | public bool DiscoverValidValues {
|
---|
134 | get { return discoverValidValues; }
|
---|
135 | set { discoverValidValues = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 | [Storable]
|
---|
139 | protected IItemSet<IItem> validValues;
|
---|
140 |
|
---|
141 | [Storable]
|
---|
142 | private bool autoPopulateValueConfigurations = true;
|
---|
143 | public bool AutoPopulateValueConfigurations {
|
---|
144 | get { return autoPopulateValueConfigurations; }
|
---|
145 | set { autoPopulateValueConfigurations = value; }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | #region Constructors and Cloning
|
---|
150 | public ParameterConfiguration(string parameterName, IValueParameter valueParameter, bool discoverValidValues) {
|
---|
151 | this.AutoPopulateValueConfigurations = true;
|
---|
152 | this.ParameterName = parameterName;
|
---|
153 | this.parameterDataType = valueParameter.GetType();
|
---|
154 | this.valueDataType = valueParameter.DataType;
|
---|
155 | this.discoverValidValues = discoverValidValues;
|
---|
156 | this.validValues = discoverValidValues ? GetValidValues(valueParameter) : new ItemSet<IItem> { valueParameter.Value };
|
---|
157 | this.validTypes = GetValidTypes(valueParameter).ToArray();
|
---|
158 | this.IsNullable = valueParameter.ItemName.StartsWith("Optional");
|
---|
159 | this.itemImage = valueParameter.ItemImage;
|
---|
160 | if (IsNullable) {
|
---|
161 | validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray();
|
---|
162 | }
|
---|
163 | this.ValueConfigurations = new CheckedValueConfigurationList(this.validValues ?? CreateValidValues());
|
---|
164 | this.ActualValue = new ConstrainedValue(
|
---|
165 | valueParameter.Value != null ? valueParameter.Value : null, // don't clone here; otherwise settings of a non-optimized ValueParameter will not be synchronized with the ConstrainedValue
|
---|
166 | valueParameter.DataType,
|
---|
167 | this.validValues != null ? new ItemSet<IItem>(this.validValues) : CreateValidValues(),
|
---|
168 | this.IsNullable);
|
---|
169 | if (Optimize) {
|
---|
170 | PopulateValueConfigurations();
|
---|
171 | }
|
---|
172 | }
|
---|
173 | public ParameterConfiguration(string parameterName, Type type, IItem actualValue, IEnumerable<IValueConfiguration> valueConfigurations) {
|
---|
174 | this.AutoPopulateValueConfigurations = false;
|
---|
175 | this.ParameterName = parameterName;
|
---|
176 | this.parameterDataType = type;
|
---|
177 | this.valueDataType = type;
|
---|
178 | this.discoverValidValues = false;
|
---|
179 | this.validValues = null; // maybe use values from valueConfigurations
|
---|
180 | this.validTypes = new Type[] { type };
|
---|
181 | this.IsNullable = false;
|
---|
182 | this.itemImage = valueConfigurations.Count() > 0 ? valueConfigurations.FirstOrDefault().ItemImage : null;
|
---|
183 | this.ValueConfigurations = new CheckedValueConfigurationList(valueConfigurations);
|
---|
184 | this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable);
|
---|
185 | }
|
---|
186 | public ParameterConfiguration(string parameterName, Type type, IItem actualValue) {
|
---|
187 | this.AutoPopulateValueConfigurations = true;
|
---|
188 | this.ParameterName = parameterName;
|
---|
189 | this.parameterDataType = type;
|
---|
190 | this.valueDataType = type;
|
---|
191 | this.discoverValidValues = false;
|
---|
192 | this.validValues = null;
|
---|
193 | this.validTypes = new Type[] { type };
|
---|
194 | this.IsNullable = false;
|
---|
195 | this.itemImage = actualValue.ItemImage;
|
---|
196 | this.ValueConfigurations = new CheckedValueConfigurationList();
|
---|
197 | this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable);
|
---|
198 | if (Optimize) {
|
---|
199 | PopulateValueConfigurations();
|
---|
200 | }
|
---|
201 | }
|
---|
202 | public ParameterConfiguration() { }
|
---|
203 | [StorableConstructor]
|
---|
204 | protected ParameterConfiguration(bool deserializing) { }
|
---|
205 | protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
|
---|
206 | : base(original, cloner) {
|
---|
207 | this.parameterName = original.ParameterName;
|
---|
208 | this.parameterDataType = original.parameterDataType;
|
---|
209 | this.valueDataType = original.ValueDataType;
|
---|
210 | this.validValues = cloner.Clone(original.validValues);
|
---|
211 | this.validTypes = original.validTypes.ToArray();
|
---|
212 | this.valueConfigurations = cloner.Clone(original.ValueConfigurations);
|
---|
213 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
214 | this.actualValue = cloner.Clone(original.actualValue);
|
---|
215 | if (this.actualValue != null) RegisterActualValueEvents();
|
---|
216 | this.optimize = original.optimize;
|
---|
217 | this.actualValueConfigurationIndex = original.actualValueConfigurationIndex;
|
---|
218 | this.isNullable = original.isNullable;
|
---|
219 | this.itemImage = original.itemImage;
|
---|
220 | this.discoverValidValues = original.discoverValidValues;
|
---|
221 | this.AutoPopulateValueConfigurations = original.AutoPopulateValueConfigurations;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
226 | return new ParameterConfiguration(this, cloner);
|
---|
227 | }
|
---|
228 | [StorableHook(HookType.AfterDeserialization)]
|
---|
229 | protected virtual void AfterDeserialization() {
|
---|
230 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
231 | if (this.actualValue != null) RegisterActualValueEvents();
|
---|
232 | }
|
---|
233 | #endregion
|
---|
234 |
|
---|
235 | private void RegisterValueConfigurationEvents() {
|
---|
236 | this.ValueConfigurations.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_CheckedItemsChanged);
|
---|
237 | this.ValueConfigurations.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsAdded);
|
---|
238 | this.ValueConfigurations.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsRemoved);
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void DeregisterValueConfigurationEvents() {
|
---|
242 | this.ValueConfigurations.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_CheckedItemsChanged);
|
---|
243 | this.ValueConfigurations.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsAdded);
|
---|
244 | this.ValueConfigurations.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(ValueConfigurations_ItemsRemoved);
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void RegisterActualValueEvents() {
|
---|
248 | if (this.ActualValue != null) this.ActualValue.ToStringChanged += new EventHandler(ActualValue_ToStringChanged);
|
---|
249 | }
|
---|
250 | private void DeregisterActualValueEvents() {
|
---|
251 | if (this.ActualValue != null) this.ActualValue.ToStringChanged -= new EventHandler(ActualValue_ToStringChanged);
|
---|
252 | }
|
---|
253 |
|
---|
254 | protected virtual void PopulateValueConfigurations() {
|
---|
255 | if (!this.AutoPopulateValueConfigurations)
|
---|
256 | return;
|
---|
257 |
|
---|
258 | foreach (Type t in this.validTypes) {
|
---|
259 | if (t == typeof(NullValue)) {
|
---|
260 | this.ValueConfigurations.Add(new NullValueConfiguration());
|
---|
261 | } else {
|
---|
262 | IItem val;
|
---|
263 | if (ActualValue.Value != null && ActualValue.Value.GetType() == t) {
|
---|
264 | val = (IItem)ActualValue.Value.Clone(); // use existing value for that type (if available)
|
---|
265 | } else {
|
---|
266 | val = CreateItem(t);
|
---|
267 | }
|
---|
268 | if (val != null) { // val can be null when ValidValues does not contain the type (this can happen when discoverValidValues=false)
|
---|
269 | IValueConfiguration valueConfiguration;
|
---|
270 | if (val is IParameterizedItem) {
|
---|
271 | valueConfiguration = new ParameterizedValueConfiguration(val, val.GetType(), true);
|
---|
272 | } else {
|
---|
273 | if (val is ISymbolicExpressionGrammar) {
|
---|
274 | valueConfiguration = new SymbolicExpressionGrammarValueConfiguration((ISymbolicExpressionGrammar)val);
|
---|
275 | } else {
|
---|
276 | valueConfiguration = new RangeValueConfiguration(val, val.GetType());
|
---|
277 | }
|
---|
278 | }
|
---|
279 | this.ValueConfigurations.Add(valueConfiguration, true);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | protected virtual void ClearValueConfigurations() {
|
---|
286 | if (!this.AutoPopulateValueConfigurations)
|
---|
287 | return;
|
---|
288 |
|
---|
289 | this.ValueConfigurations.Clear();
|
---|
290 | }
|
---|
291 |
|
---|
292 | private static IEnumerable<Type> GetValidTypes(IValueParameter parameter) {
|
---|
293 | // in case of IOperator return empty list, otherwise hundreds of types would be returned. this mostly occurs for Successor which will never be optimized
|
---|
294 | if (parameter.DataType == typeof(IOperator))
|
---|
295 | return new List<Type>();
|
---|
296 |
|
---|
297 | // return only one type for ValueTypeValues<> (Int, Double, Percent, Bool). Otherwise 2 types would be returned for DoubleValue (PercentValue which is derived)
|
---|
298 | if (IsSubclassOfRawGeneric(typeof(ValueTypeValue<>), parameter.DataType))
|
---|
299 | return new List<Type> { parameter.DataType };
|
---|
300 |
|
---|
301 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
302 | // use existing validvalues if known
|
---|
303 | var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
304 | return parameterValidValues.Cast<object>().Select(x => x.GetType());
|
---|
305 | } else {
|
---|
306 | // otherwise use all assignable types
|
---|
307 | return ApplicationManager.Manager.GetTypes(parameter.DataType, true);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | private IItemSet<IItem> GetValidValues(IValueParameter parameter) {
|
---|
312 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
313 | var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
314 | return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone())); // cloning actually saves memory, because references to event-subscribers are removed
|
---|
315 | } else {
|
---|
316 | return null;
|
---|
317 | }
|
---|
318 |
|
---|
319 | }
|
---|
320 |
|
---|
321 | public IItem CreateItem(Type type) {
|
---|
322 | // no valid values; just instantiate
|
---|
323 | try {
|
---|
324 | if (validValues == null)
|
---|
325 | return (IItem)Activator.CreateInstance(type);
|
---|
326 | }
|
---|
327 | catch (MissingMemberException) {
|
---|
328 | return null; // can happen, when ApplicationManager.Manager.GetTypes(type, OnlyInstantiable=true) returns objects which have no default constructor
|
---|
329 | }
|
---|
330 |
|
---|
331 | if (type == typeof(NullValue))
|
---|
332 | return new NullValue();
|
---|
333 |
|
---|
334 | // copy value from ValidValues; this ensures that previously set ActualNames for a type are kept
|
---|
335 | IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault();
|
---|
336 | if (value != null)
|
---|
337 | return value;
|
---|
338 |
|
---|
339 | return null;
|
---|
340 | }
|
---|
341 |
|
---|
342 | private IItemSet<IItem> CreateValidValues() {
|
---|
343 | var validValues = new ItemSet<IItem>();
|
---|
344 | foreach (Type t in this.validTypes) {
|
---|
345 | try {
|
---|
346 | var val = CreateItem(t);
|
---|
347 | if (val != null) validValues.Add(val);
|
---|
348 | }
|
---|
349 | catch (MissingMethodException) { /* Constructor is missing, don't use those types */ }
|
---|
350 | }
|
---|
351 | return validValues;
|
---|
352 | }
|
---|
353 |
|
---|
354 | public static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
|
---|
355 | while (toCheck != typeof(object)) {
|
---|
356 | var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
|
---|
357 | if (generic == cur) {
|
---|
358 | return true;
|
---|
359 | }
|
---|
360 | toCheck = toCheck.BaseType; // baseType is null when toCheck is an Interface
|
---|
361 | if (toCheck == null)
|
---|
362 | return false;
|
---|
363 | }
|
---|
364 | return false;
|
---|
365 | }
|
---|
366 |
|
---|
367 | private void ValueConfigurations_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
368 | OnToStringChanged();
|
---|
369 | KeepActualValueConfigurationIndexConsistent();
|
---|
370 | }
|
---|
371 |
|
---|
372 | private void ValueConfigurations_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
373 | OnToStringChanged();
|
---|
374 | KeepActualValueConfigurationIndexConsistent();
|
---|
375 | }
|
---|
376 | private void ValueConfigurations_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
377 | OnToStringChanged();
|
---|
378 | KeepActualValueConfigurationIndexConsistent();
|
---|
379 | }
|
---|
380 |
|
---|
381 | #region INamedItem Properties
|
---|
382 | public virtual string Name {
|
---|
383 | get { return ParameterName; }
|
---|
384 | set { throw new NotSupportedException(); }
|
---|
385 | }
|
---|
386 | public virtual string Description {
|
---|
387 | get { return base.ItemDescription; }
|
---|
388 | set { throw new NotSupportedException(); }
|
---|
389 | }
|
---|
390 | public virtual bool CanChangeDescription {
|
---|
391 | get { return false; }
|
---|
392 | }
|
---|
393 | public virtual bool CanChangeName {
|
---|
394 | get { return false; }
|
---|
395 | }
|
---|
396 | public override string ItemDescription {
|
---|
397 | get { return base.ItemDescription; }
|
---|
398 | }
|
---|
399 | public override string ItemName {
|
---|
400 | get { return base.ItemName; }
|
---|
401 | }
|
---|
402 | #endregion
|
---|
403 |
|
---|
404 | #region Events
|
---|
405 | protected virtual void ActualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
406 | OnToStringChanged();
|
---|
407 | }
|
---|
408 | #endregion
|
---|
409 |
|
---|
410 | #region Event Handler
|
---|
411 | public virtual event EventHandler NameChanged;
|
---|
412 | protected virtual void OnNameChanged(object sender, EventArgs e) {
|
---|
413 | var handler = NameChanged;
|
---|
414 | if (handler != null) handler(sender, e);
|
---|
415 | }
|
---|
416 |
|
---|
417 | public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
418 | protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
|
---|
419 | var handler = NameChanging;
|
---|
420 | if (handler != null) handler(sender, e);
|
---|
421 | }
|
---|
422 |
|
---|
423 | public virtual event EventHandler DescriptionChanged;
|
---|
424 | protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
|
---|
425 | var handler = DescriptionChanged;
|
---|
426 | if (handler != null) handler(sender, e);
|
---|
427 | }
|
---|
428 |
|
---|
429 | public virtual event EventHandler IsOptimizableChanged;
|
---|
430 | private void OnIsOptimizableChanged() {
|
---|
431 | var handler = IsOptimizableChanged;
|
---|
432 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
433 | }
|
---|
434 |
|
---|
435 | public virtual event EventHandler OptimizeChanged;
|
---|
436 | protected virtual void OnOptimizeChanged() {
|
---|
437 | var handler = OptimizeChanged;
|
---|
438 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
439 | }
|
---|
440 | #endregion
|
---|
441 |
|
---|
442 | public override string ToString() {
|
---|
443 | if (Optimize) {
|
---|
444 | return string.Format("{0}: (Optimize: {1})", ParameterName, ValueConfigurations.CheckedItems.Count());
|
---|
445 | } else {
|
---|
446 | return string.Format("{0}: {1}", ParameterName, ActualValue.Value);
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | public virtual string ParameterInfoString {
|
---|
451 | get {
|
---|
452 | StringBuilder sb = new StringBuilder();
|
---|
453 | if (this.Optimize) {
|
---|
454 | var vc = this.ValueConfigurations[actualValueConfigurationIndex];
|
---|
455 | if (vc.ActualValue == null || vc.ActualValue.Value == null) {
|
---|
456 | sb.Append(string.Format("{0}: null", parameterName));
|
---|
457 | } else if (IsSubclassOfRawGeneric(typeof(ValueTypeValue<>), vc.ActualValue.Value.GetType())) {
|
---|
458 | // for int, double, bool use the value directly
|
---|
459 | sb.Append(string.Format("{0}: {1}", parameterName, this.ActualValue.Value.ToString()));
|
---|
460 | } else {
|
---|
461 | // for other types use NumberedName (this also uses the Number-Property for otherwise ambiguous ValueConfigurations)
|
---|
462 | sb.Append(string.Format("{0}: {1}", parameterName, vc.NumberedName));
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (this.ValueConfigurations[actualValueConfigurationIndex] is ParameterizedValueConfiguration) {
|
---|
466 | string subParams = this.ValueConfigurations[actualValueConfigurationIndex].ParameterInfoString;
|
---|
467 | if (!string.IsNullOrEmpty(subParams)) {
|
---|
468 | sb.Append(" (");
|
---|
469 | sb.Append(subParams);
|
---|
470 | sb.Append(")");
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }
|
---|
474 | return sb.ToString();
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter, bool discoverValidValues) {
|
---|
479 | if (parameter is IValueParameter) {
|
---|
480 | IValueParameter valueParameter = parameter as IValueParameter;
|
---|
481 | return new ParameterConfiguration(parameter.Name, valueParameter, discoverValidValues);
|
---|
482 | }
|
---|
483 | return null;
|
---|
484 | }
|
---|
485 |
|
---|
486 | public void Parameterize(IValueParameter parameter) {
|
---|
487 | if (Optimize) {
|
---|
488 | if (this.ActualValue.Value is IParameterizedItem) {
|
---|
489 | ((ParameterizedValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((IParameterizedItem)this.ActualValue.Value);
|
---|
490 | }
|
---|
491 | if (this.ActualValue.Value is ISymbolicExpressionGrammar) {
|
---|
492 | ((SymbolicExpressionGrammarValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((ISymbolicExpressionGrammar)this.ActualValue.Value);
|
---|
493 | }
|
---|
494 | }
|
---|
495 | var clonedValue = this.ActualValue.Value != null ? (IItem)this.ActualValue.Value.Clone() : null;
|
---|
496 | if (clonedValue != null) AdaptValidValues(parameter, clonedValue);
|
---|
497 | parameter.Value = clonedValue;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /// <summary>
|
---|
501 | /// Adds value to the ValidValues of the parameter if they don't contain the value
|
---|
502 | /// </summary>
|
---|
503 | private void AdaptValidValues(IValueParameter parameter, IItem value) {
|
---|
504 | // this requires some tricky reflection, because the type is unknown at runtime so parameter.ValidValues cannot be casted to Collection<?>
|
---|
505 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
506 | var validValues = parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
507 | Type validValuesType = validValues.GetType();
|
---|
508 |
|
---|
509 | var containsMethod = validValuesType.GetMethod("Contains");
|
---|
510 | if (!(bool)containsMethod.Invoke(validValues, new object[] { value })) {
|
---|
511 | var addMethod = validValuesType.GetMethod("Add");
|
---|
512 | addMethod.Invoke(validValues, new object[] { value });
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | public void Randomize(IRandom random) {
|
---|
518 | if (Optimize) {
|
---|
519 | foreach (var vc in this.ValueConfigurations) {
|
---|
520 | if (this.ValueConfigurations.ItemChecked(vc)) {
|
---|
521 | if (vc.Optimize) vc.Randomize(random);
|
---|
522 | }
|
---|
523 | }
|
---|
524 | do {
|
---|
525 | actualValueConfigurationIndex = random.Next(ValueConfigurations.Count());
|
---|
526 | } while (!this.ValueConfigurations.ItemChecked(this.ValueConfigurations[actualValueConfigurationIndex]));
|
---|
527 | this.ActualValue = this.ValueConfigurations[actualValueConfigurationIndex].ActualValue;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | public void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
|
---|
532 | if (Optimize) {
|
---|
533 | foreach (var vc in this.ValueConfigurations) {
|
---|
534 | if (this.ValueConfigurations.ItemChecked(vc)) {
|
---|
535 | if (vc.Optimize) vc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
|
---|
536 | }
|
---|
537 | }
|
---|
538 | mutate(random, this, intValueManipulator, doubleValueManipulator);
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | public void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
543 | if (Optimize) {
|
---|
544 | IParameterConfiguration otherPc = (IParameterConfiguration)other;
|
---|
545 | for (int i = 0; i < this.ValueConfigurations.Count; i++) {
|
---|
546 | if (this.ValueConfigurations.ItemChecked(this.ValueConfigurations[i])) {
|
---|
547 | if (this.ValueConfigurations[i].Optimize) this.ValueConfigurations[i].Cross(random, otherPc.ValueConfigurations[i], cross, intValueCrossover, doubleValueCrossover);
|
---|
548 | }
|
---|
549 | }
|
---|
550 | cross(random, this, other, intValueCrossover, doubleValueCrossover);
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | public void UpdateActualValueIndexToItem(IValueConfiguration vc) {
|
---|
555 | for (int i = 0; i < this.ValueConfigurations.Count(); i++) {
|
---|
556 | if (this.ValueConfigurations.ElementAt(i) == vc) {
|
---|
557 | this.actualValueConfigurationIndex = i;
|
---|
558 | return;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | public List<IOptimizable> GetAllOptimizables() {
|
---|
564 | var list = new List<IOptimizable>();
|
---|
565 | foreach (var vc in ValueConfigurations) {
|
---|
566 | if (vc.Optimize) {
|
---|
567 | list.Add(vc);
|
---|
568 | list.AddRange(vc.GetAllOptimizables());
|
---|
569 | }
|
---|
570 | }
|
---|
571 | return list;
|
---|
572 | }
|
---|
573 |
|
---|
574 | public void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
|
---|
575 | foreach (var vc in ValueConfigurations) {
|
---|
576 | if (vc.Optimize) {
|
---|
577 | vc.CollectOptimizedParameterNames(parameterNames, prefix);
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | public double CalculateSimilarity(IOptimizable optimizable) {
|
---|
583 | var other = (IParameterConfiguration)optimizable;
|
---|
584 | if (this.ActualValueConfigurationIndex == other.ActualValueConfigurationIndex) {
|
---|
585 | return this.ValueConfigurations[this.ActualValueConfigurationIndex].CalculateSimilarity(other.ValueConfigurations[other.ActualValueConfigurationIndex]);
|
---|
586 | } else {
|
---|
587 | return 0.0;
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | /// <summary>
|
---|
592 | /// Ensures that the ActualValueConfigurationIndex has a valid value
|
---|
593 | /// Only indices of items which are checked are allowed
|
---|
594 | /// </summary>
|
---|
595 | private void KeepActualValueConfigurationIndexConsistent() {
|
---|
596 | if (this.valueConfigurations != null && this.valueConfigurations.CheckedItems.Count() > 0) {
|
---|
597 | if(this.valueConfigurations.Count <= this.actualValueConfigurationIndex &&
|
---|
598 | this.valueConfigurations.CheckedItems.Count(x => x.Index == this.actualValueConfigurationIndex) == 1) {
|
---|
599 | // everything is ok; do nothing
|
---|
600 | } else {
|
---|
601 | // current index is invalid, set new one
|
---|
602 | this.ActualValueConfigurationIndex = this.valueConfigurations.CheckedItems.First().Index;
|
---|
603 | }
|
---|
604 | } else {
|
---|
605 | // no checked valueConfiguration is available; cannot be used
|
---|
606 | this.ActualValueConfigurationIndex = -1;
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|
610 | }
|
---|