Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • implemented crossover and manipulator operators for int and double values
File size: 4.8 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        }
112      }
113    }
114  }
115
116  /// <summary>
117  /// Enumerator which can enumerate all elements of a list of enumerators
118  /// </summary>
119  /// <typeparam name="T"></typeparam>
120  public class EnumeratorCollectionEnumerator<T> : IEnumerator<T> {
121    private List<IEnumerator<T>> enumerators = new List<IEnumerator<T>>();
122    private IEnumerator<IEnumerator<T>> currentEnumerator;
123
124    public EnumeratorCollectionEnumerator() { }
125
126    public void AddEnumerator(IEnumerator<T> enumerator) {
127      enumerators.Add(enumerator);
128    }
129
130    public void Dispose() {  }
131
132    public T Current {
133      get { return currentEnumerator.Current.Current; }
134    }
135
136    object IEnumerator.Current {
137      get { return this.Current; }
138    }
139
140    public bool MoveNext() {
141      bool ok = currentEnumerator.Current.MoveNext();
142      if (!ok) {
143        ok = currentEnumerator.MoveNext();
144        if (!ok)
145          return false;
146        else
147          return this.MoveNext();
148      }
149      return true;
150    }
151
152    public void Reset() {
153      foreach (var enu in enumerators) {
154        enu.Reset();
155      }
156      currentEnumerator = enumerators.GetEnumerator();
157      currentEnumerator.Reset();
158      currentEnumerator.MoveNext();
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.