Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ParameterCombinationsEnumerator.cs @ 10194

Last change on this file since 10194 was 7840, checked in by spimming, 12 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 5.7 KB
RevLine 
[7840]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using HeuristicLab.Core;
26
27namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
28  public class ParameterCombinationsEnumerator : IEnumerator<IOptimizable> {
29    private IOptimizable node;
30    private List<IEnumerator> enumerators;
31    private EnumeratorCollectionEnumerator<IItem> valueEnumerator;
32    private bool initialized = false;
33
34    public ParameterCombinationsEnumerator(IOptimizable node) {
35      this.node = node;
36      this.enumerators = new List<IEnumerator>();
37    }
38
39    public IOptimizable Current {
40      get { return node; }
41    }
42    object IEnumerator.Current {
43      get {
44        if (!initialized)
45          throw new InvalidOperationException("Enumeration not started. Call MoveNext!");
46        return Current;
47      }
48    }
49
50    public void Dispose() { }
51
52    public bool MoveNext() {
53      if (!initialized) {
54        foreach (var enu in enumerators) {
55          enu.Reset();
56          if (!enu.MoveNext())
57            return false;
58        }
59        initialized = true;
60      } else {
61        int i = 0;
62        bool ok = false;
63        while (!ok && i < enumerators.Count) {
64          if (enumerators[i].MoveNext()) {
65            ok = true;
66          } else {
67            i++;
68          }
69        }
70
71        if (ok) {
72          for (int k = i - 1; k >= 0; k--) {
73            enumerators[k].Reset();
74            enumerators[k].MoveNext();
75          }
76        } else {
77          return false;
78        }
79      }
80
81      var pc = node as IParameterConfiguration;
82      if (pc != null && valueEnumerator != null) {
83        pc.ActualValue = ((IValueConfiguration)valueEnumerator.Current).ActualValue;
84        pc.UpdateActualValueIndexToItem((IValueConfiguration)valueEnumerator.Current);
85      }
86      var vc = node as IValueConfiguration;
87      if (vc != null && valueEnumerator != null) {
88        vc.ActualValue.Value = (IItem)valueEnumerator.Current;
89      }
90      return true;
91    }
92
93    public void Reset() {
94      enumerators.Clear();
95      valueEnumerator = null;
96      initialized = false;
97
98      var pc = node as IParameterConfiguration;
99      if (pc != null) {
100        valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
101
102        foreach (var valueConfiguration in pc.ValueConfigurations.CheckedItems) {
103          if (valueConfiguration.Value.Optimize) {
104            var enumerator = new ParameterCombinationsEnumerator(valueConfiguration.Value);
105            enumerator.Reset();
106            valueEnumerator.AddEnumerator(enumerator);
107          } else {
108            valueEnumerator.AddEnumerator(new List<IItem> { valueConfiguration.Value }.GetEnumerator());
109          }
110        }
111        valueEnumerator.Reset();
112        enumerators.Add(valueEnumerator);
113      }
114
115      var rangeVc = node as RangeValueConfiguration;
116      if (rangeVc != null) {
117        valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
118        valueEnumerator.AddEnumerator(rangeVc.RangeConstraint.GetCombinations().GetEnumerator());
119        valueEnumerator.Reset();
120        enumerators.Add(valueEnumerator);
121      }
122
123      var parameterizedVc = node as ParameterizedValueConfiguration;
124      if (parameterizedVc != null) {
125        foreach (var parameterConfiguration in parameterizedVc.ParameterConfigurations) {
126          if (parameterConfiguration.Optimize) {
127            var enumerator = new ParameterCombinationsEnumerator(parameterConfiguration);
128            enumerator.Reset();
129            enumerators.Add(enumerator);
130          }
131        }
132        enumerators.Reverse(); // this makes the list of combinations better readable
133      }
134    }
135  }
136
137  /// <summary>
138  /// Enumerator which can enumerate all elements of a list of enumerators
139  /// </summary>
140  /// <typeparam name="T"></typeparam>
141  public class EnumeratorCollectionEnumerator<T> : IEnumerator<T> {
142    private List<IEnumerator<T>> enumerators = new List<IEnumerator<T>>();
143    private IEnumerator<IEnumerator<T>> currentEnumerator;
144
145    public EnumeratorCollectionEnumerator() { }
146
147    public void AddEnumerator(IEnumerator<T> enumerator) {
148      enumerators.Add(enumerator);
149    }
150
151    public void Dispose() { }
152
153    public T Current {
154      get { return currentEnumerator.Current.Current; }
155    }
156
157    object IEnumerator.Current {
158      get { return this.Current; }
159    }
160
161    public bool MoveNext() {
162      bool ok = currentEnumerator.Current.MoveNext();
163      if (!ok) {
164        ok = currentEnumerator.MoveNext();
165        if (!ok)
166          return false;
167        else
168          return this.MoveNext();
169      }
170      return true;
171    }
172
173    public void Reset() {
174      foreach (var enu in enumerators) {
175        enu.Reset();
176      }
177      currentEnumerator = enumerators.GetEnumerator();
178      currentEnumerator.Reset();
179      currentEnumerator.MoveNext();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.