[5144] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Collections;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 9 | public class ParameterCombinationsEnumerator : IEnumerator<IOptimizable> {
|
---|
| 10 | private IOptimizable node;
|
---|
| 11 | private List<IEnumerator> enumerators;
|
---|
[5184] | 12 | private EnumeratorCollectionEnumerator<IItem> valueEnumerator;
|
---|
| 13 | private bool initialized = false;
|
---|
[5144] | 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 {
|
---|
[5184] | 24 | get {
|
---|
| 25 | if (!initialized)
|
---|
| 26 | throw new SystemException("Enumeration not started. Call MoveNext!");
|
---|
| 27 | return Current;
|
---|
| 28 | }
|
---|
[5144] | 29 | }
|
---|
| 30 |
|
---|
| 31 | public void Dispose() { }
|
---|
| 32 |
|
---|
| 33 | public bool MoveNext() {
|
---|
[5184] | 34 | if (!initialized) {
|
---|
| 35 | foreach (var enu in enumerators) {
|
---|
| 36 | enu.Reset();
|
---|
| 37 | if (!enu.MoveNext())
|
---|
| 38 | return false;
|
---|
[5144] | 39 | }
|
---|
[5184] | 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 | }
|
---|
[5144] | 51 |
|
---|
[5184] | 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;
|
---|
[5144] | 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;
|
---|
[5184] | 77 | initialized = false;
|
---|
[5144] | 78 |
|
---|
| 79 | var pc = node as IParameterConfiguration;
|
---|
| 80 | if (pc != null) {
|
---|
[5184] | 81 | valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
|
---|
[5144] | 82 |
|
---|
| 83 | foreach (var valueConfiguration in pc.ValueConfigurations.CheckedItems) {
|
---|
[5277] | 84 | if (valueConfiguration.Value.Optimize) {
|
---|
| 85 | var enumerator = new ParameterCombinationsEnumerator(valueConfiguration.Value);
|
---|
[5144] | 86 | enumerator.Reset();
|
---|
[5184] | 87 | valueEnumerator.AddEnumerator(enumerator);
|
---|
| 88 | } else {
|
---|
[5277] | 89 | valueEnumerator.AddEnumerator(new List<IItem> { valueConfiguration.Value }.GetEnumerator());
|
---|
[5144] | 90 | }
|
---|
| 91 | }
|
---|
[5184] | 92 | valueEnumerator.Reset();
|
---|
| 93 | enumerators.Add(valueEnumerator);
|
---|
[5144] | 94 | }
|
---|
| 95 |
|
---|
| 96 | var vc = node as IValueConfiguration;
|
---|
| 97 | if (vc != null) {
|
---|
| 98 | if (vc.RangeConstraint != null) {
|
---|
[5184] | 99 | valueEnumerator = new EnumeratorCollectionEnumerator<IItem>();
|
---|
| 100 | valueEnumerator.AddEnumerator(vc.RangeConstraint.GetCombinations().GetEnumerator());
|
---|
| 101 | valueEnumerator.Reset();
|
---|
[5144] | 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 | }
|
---|
[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 |
|
---|
| 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 | }
|
---|
[5144] | 160 | }
|
---|
| 161 | }
|
---|