Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs @ 6489

Last change on this file since 6489 was 6489, checked in by cneumuel, 13 years ago

#1215

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