Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13365 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: 4.9 KB
RevLine 
[5144]1using System;
[5653]2using System.Collections;
[5144]3using System.Collections.Generic;
4using HeuristicLab.Core;
5
6namespace HeuristicLab.Problems.MetaOptimization {
7  public class ParameterCombinationsEnumerator : IEnumerator<IOptimizable> {
8    private IOptimizable node;
9    private List<IEnumerator> enumerators;
[5184]10    private EnumeratorCollectionEnumerator<IItem> valueEnumerator;
11    private bool initialized = false;
[5144]12
13    public ParameterCombinationsEnumerator(IOptimizable node) {
14      this.node = node;
15      this.enumerators = new List<IEnumerator>();
16    }
17
18    public IOptimizable Current {
19      get { return node; }
20    }
21    object IEnumerator.Current {
[5184]22      get {
23        if (!initialized)
[6489]24          throw new InvalidOperationException("Enumeration not started. Call MoveNext!");
[5653]25        return Current;
[5184]26      }
[5144]27    }
28
29    public void Dispose() { }
30
31    public bool MoveNext() {
[5184]32      if (!initialized) {
33        foreach (var enu in enumerators) {
34          enu.Reset();
35          if (!enu.MoveNext())
36            return false;
[5144]37        }
[5184]38        initialized = true;
39      } else {
40        int i = 0;
41        bool ok = false;
42        while (!ok && i < enumerators.Count) {
43          if (enumerators[i].MoveNext()) {
44            ok = true;
45          } else {
46            i++;
47          }
48        }
[5144]49
[5184]50        if (ok) {
51          for (int k = i - 1; k >= 0; k--) {
52            enumerators[k].Reset();
53            enumerators[k].MoveNext();
54          }
55        } else {
56          return false;
[5144]57        }
58      }
59
60      var pc = node as IParameterConfiguration;
61      if (pc != null && valueEnumerator != null) {
62        pc.ActualValue = ((IValueConfiguration)valueEnumerator.Current).ActualValue;
63        pc.UpdateActualValueIndexToItem((IValueConfiguration)valueEnumerator.Current);
64      }
65      var vc = node as IValueConfiguration;
66      if (vc != null && valueEnumerator != null) {
67        vc.ActualValue.Value = (IItem)valueEnumerator.Current;
68      }
69      return true;
70    }
71
72    public void Reset() {
73      enumerators.Clear();
74      valueEnumerator = null;
[5184]75      initialized = false;
[5144]76
77      var pc = node as IParameterConfiguration;
78      if (pc != null) {
[5184]79        valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
[5144]80
81        foreach (var valueConfiguration in pc.ValueConfigurations.CheckedItems) {
[5277]82          if (valueConfiguration.Value.Optimize) {
83            var enumerator = new ParameterCombinationsEnumerator(valueConfiguration.Value);
[5144]84            enumerator.Reset();
[5184]85            valueEnumerator.AddEnumerator(enumerator);
86          } else {
[5277]87            valueEnumerator.AddEnumerator(new List<IItem> { valueConfiguration.Value }.GetEnumerator());
[5144]88          }
89        }
[5184]90        valueEnumerator.Reset();
91        enumerators.Add(valueEnumerator);
[5144]92      }
93
[5653]94      var rangeVc = node as RangeValueConfiguration;
95      if (rangeVc != null) {
96        valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
97        valueEnumerator.AddEnumerator(rangeVc.RangeConstraint.GetCombinations().GetEnumerator());
98        valueEnumerator.Reset();
99        enumerators.Add(valueEnumerator);
100      }
101
102      var parameterizedVc = node as ParameterizedValueConfiguration;
103      if (parameterizedVc != null) {
104        foreach (var parameterConfiguration in parameterizedVc.ParameterConfigurations) {
105          if (parameterConfiguration.Optimize) {
106            var enumerator = new ParameterCombinationsEnumerator(parameterConfiguration);
107            enumerator.Reset();
108            enumerators.Add(enumerator);
[5144]109          }
110        }
[5653]111        enumerators.Reverse(); // this makes the list of combinations better readable
[5144]112      }
113    }
[5184]114  }
[5144]115
[5184]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
[5653]130    public void Dispose() { }
[5184]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    }
[5144]160  }
161}
Note: See TracBrowser for help on using the repository browser.