Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterCombinationsEnumerator.cs @ 5357

Last change on this file since 5357 was 5357, checked in by cneumuel, 12 years ago

#1215

  • changed ordering of parameter combinations to make them better readable
  • changed handling of invalid parameter settings from penalty approach to repair approach
File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Collections;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.Problems.MetaOptimization {
9  public class ParameterCombinationsEnumerator : IEnumerator<IOptimizable> {
10    private IOptimizable node;
11    private List<IEnumerator> enumerators;
12    private EnumeratorCollectionEnumerator<IItem> valueEnumerator;
13    private bool initialized = false;
14
15    public ParameterCombinationsEnumerator(IOptimizable node) {
16      this.node = node;
17      this.enumerators = new List<IEnumerator>();
18    }
19
20    public IOptimizable Current {
21      get { return node; }
22    }
23    object IEnumerator.Current {
24      get {
25        if (!initialized)
26          throw new SystemException("Enumeration not started. Call MoveNext!");
27        return Current;
28      }
29    }
30
31    public void Dispose() { }
32
33    public bool MoveNext() {
34      if (!initialized) {
35        foreach (var enu in enumerators) {
36          enu.Reset();
37          if (!enu.MoveNext())
38            return false;
39        }
40        initialized = true;
41      } else {
42        int i = 0;
43        bool ok = false;
44        while (!ok && i < enumerators.Count) {
45          if (enumerators[i].MoveNext()) {
46            ok = true;
47          } else {
48            i++;
49          }
50        }
51
52        if (ok) {
53          for (int k = i - 1; k >= 0; k--) {
54            enumerators[k].Reset();
55            enumerators[k].MoveNext();
56          }
57        } else {
58          return false;
59        }
60      }
61
62      var pc = node as IParameterConfiguration;
63      if (pc != null && valueEnumerator != null) {
64        pc.ActualValue = ((IValueConfiguration)valueEnumerator.Current).ActualValue;
65        pc.UpdateActualValueIndexToItem((IValueConfiguration)valueEnumerator.Current);
66      }
67      var vc = node as IValueConfiguration;
68      if (vc != null && valueEnumerator != null) {
69        vc.ActualValue.Value = (IItem)valueEnumerator.Current;
70      }
71      return true;
72    }
73
74    public void Reset() {
75      enumerators.Clear();
76      valueEnumerator = null;
77      initialized = false;
78
79      var pc = node as IParameterConfiguration;
80      if (pc != null) {
81        valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
82
83        foreach (var valueConfiguration in pc.ValueConfigurations.CheckedItems) {
84          if (valueConfiguration.Value.Optimize) {
85            var enumerator = new ParameterCombinationsEnumerator(valueConfiguration.Value);
86            enumerator.Reset();
87            valueEnumerator.AddEnumerator(enumerator);
88          } else {
89            valueEnumerator.AddEnumerator(new List<IItem> { valueConfiguration.Value }.GetEnumerator());
90          }
91        }
92        valueEnumerator.Reset();
93        enumerators.Add(valueEnumerator);
94      }
95
96      var vc = node as IValueConfiguration;
97      if (vc != null) {
98        if (vc.RangeConstraint != null) {
99          valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
100          valueEnumerator.AddEnumerator(vc.RangeConstraint.GetCombinations().GetEnumerator());
101          valueEnumerator.Reset();
102          enumerators.Add(valueEnumerator);
103        } else {
104          foreach (var parameterConfiguration in vc.ParameterConfigurations) {
105            if (parameterConfiguration.Optimize) {
106              var enumerator = new ParameterCombinationsEnumerator(parameterConfiguration);
107              enumerator.Reset();
108              enumerators.Add(enumerator);
109            }
110          }
111          enumerators.Reverse(); // this makes the list of combinations better readable
112        }
113      }
114    }
115  }
116
117  /// <summary>
118  /// Enumerator which can enumerate all elements of a list of enumerators
119  /// </summary>
120  /// <typeparam name="T"></typeparam>
121  public class EnumeratorCollectionEnumerator<T> : IEnumerator<T> {
122    private List<IEnumerator<T>> enumerators = new List<IEnumerator<T>>();
123    private IEnumerator<IEnumerator<T>> currentEnumerator;
124
125    public EnumeratorCollectionEnumerator() { }
126
127    public void AddEnumerator(IEnumerator<T> enumerator) {
128      enumerators.Add(enumerator);
129    }
130
131    public void Dispose() {  }
132
133    public T Current {
134      get { return currentEnumerator.Current.Current; }
135    }
136
137    object IEnumerator.Current {
138      get { return this.Current; }
139    }
140
141    public bool MoveNext() {
142      bool ok = currentEnumerator.Current.MoveNext();
143      if (!ok) {
144        ok = currentEnumerator.MoveNext();
145        if (!ok)
146          return false;
147        else
148          return this.MoveNext();
149      }
150      return true;
151    }
152
153    public void Reset() {
154      foreach (var enu in enumerators) {
155        enu.Reset();
156      }
157      currentEnumerator = enumerators.GetEnumerator();
158      currentEnumerator.Reset();
159      currentEnumerator.MoveNext();
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.