Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • minor changes
File size: 24.9 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
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 (parameter is IFixedValueParameter)
488        return;
489
490      if (Optimize) {
491        if (this.ActualValue.Value is IParameterizedItem) {
492          ((ParameterizedValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((IParameterizedItem)this.ActualValue.Value);
493        }
494        if (this.ActualValue.Value is ISymbolicExpressionGrammar) {
495          ((SymbolicExpressionGrammarValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((ISymbolicExpressionGrammar)this.ActualValue.Value);
496        }
497      }
498      var clonedValue = this.ActualValue.Value != null ? (IItem)this.ActualValue.Value.Clone() : null;
499      if (clonedValue != null) AdaptValidValues(parameter, clonedValue);
500      parameter.Value = clonedValue;
501    }
502
503    /// <summary>
504    /// Adds value to the ValidValues of the parameter if they don't contain the value
505    /// </summary>
506    private void AdaptValidValues(IValueParameter parameter, IItem value) {
507      // this requires some tricky reflection, because the type is unknown at runtime so parameter.ValidValues cannot be casted to Collection<?>
508      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
509        var validValues = parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
510        Type validValuesType = validValues.GetType();
511
512        var containsMethod = validValuesType.GetMethod("Contains");
513        if (!(bool)containsMethod.Invoke(validValues, new object[] { value })) {
514          var addMethod = validValuesType.GetMethod("Add");
515          addMethod.Invoke(validValues, new object[] { value });
516        }
517      }
518    }
519
520    public void Randomize(IRandom random) {
521      if (Optimize) {
522        foreach (var vc in this.ValueConfigurations) {
523          if (this.ValueConfigurations.ItemChecked(vc)) {
524            if (vc.Optimize) vc.Randomize(random);
525          }
526        }
527        do {
528          actualValueConfigurationIndex = random.Next(ValueConfigurations.Count());
529        } while (!this.ValueConfigurations.ItemChecked(this.ValueConfigurations[actualValueConfigurationIndex]));
530        this.ActualValue = this.ValueConfigurations[actualValueConfigurationIndex].ActualValue;
531      }
532    }
533
534    public void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
535      if (Optimize) {
536        foreach (var vc in this.ValueConfigurations) {
537          if (this.ValueConfigurations.ItemChecked(vc)) {
538            if (vc.Optimize) vc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
539          }
540        }
541        mutate(random, this, intValueManipulator, doubleValueManipulator);
542      }
543    }
544
545    public void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
546      if (Optimize) {
547        IParameterConfiguration otherPc = (IParameterConfiguration)other;
548        for (int i = 0; i < this.ValueConfigurations.Count; i++) {
549          if (this.ValueConfigurations.ItemChecked(this.ValueConfigurations[i])) {
550            if (this.ValueConfigurations[i].Optimize) this.ValueConfigurations[i].Cross(random, otherPc.ValueConfigurations[i], cross, intValueCrossover, doubleValueCrossover);
551          }
552        }
553        cross(random, this, other, intValueCrossover, doubleValueCrossover);
554      }
555    }
556
557    public void UpdateActualValueIndexToItem(IValueConfiguration vc) {
558      for (int i = 0; i < this.ValueConfigurations.Count(); i++) {
559        if (this.ValueConfigurations.ElementAt(i) == vc) {
560          this.actualValueConfigurationIndex = i;
561          return;
562        }
563      }
564    }
565
566    public List<IOptimizable> GetAllOptimizables() {
567      var list = new List<IOptimizable>();
568      foreach (var vc in ValueConfigurations) {
569        if (vc.Optimize) {
570          list.Add(vc);
571          list.AddRange(vc.GetAllOptimizables());
572        }
573      }
574      return list;
575    }
576
577    public void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
578      foreach (var vc in ValueConfigurations) {
579        if (vc.Optimize) {
580          vc.CollectOptimizedParameterNames(parameterNames, prefix);
581        }
582      }
583    }
584
585    public double CalculateSimilarity(IOptimizable optimizable) {
586      var other = (IParameterConfiguration)optimizable;
587      if (this.ActualValueConfigurationIndex == other.ActualValueConfigurationIndex) {
588        return this.ValueConfigurations[this.ActualValueConfigurationIndex].CalculateSimilarity(other.ValueConfigurations[other.ActualValueConfigurationIndex]);
589      } else {
590        return 0.0;
591      }
592    }
593
594    /// <summary>
595    /// Ensures that the ActualValueConfigurationIndex has a valid value
596    /// Only indices of items which are checked are allowed
597    /// </summary>
598    private void KeepActualValueConfigurationIndexConsistent() {
599      if (this.valueConfigurations != null && this.valueConfigurations.CheckedItems.Count() > 0) {
600        if(this.valueConfigurations.Count <= this.actualValueConfigurationIndex &&
601           this.valueConfigurations.CheckedItems.Count(x => x.Index == this.actualValueConfigurationIndex) == 1) {
602          // everything is ok; do nothing
603        } else {
604          // current index is invalid, set new one
605          this.ActualValueConfigurationIndex = this.valueConfigurations.CheckedItems.First().Index;
606        }
607      } else {
608        // no checked valueConfiguration is available; cannot be used
609        this.ActualValueConfigurationIndex = -1;
610      }
611    }
612  }
613}
Note: See TracBrowser for help on using the repository browser.