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;
|
---|
12 | IEnumerator valueEnumerator;
|
---|
13 |
|
---|
14 | public ParameterCombinationsEnumerator(IOptimizable node) {
|
---|
15 | this.node = node;
|
---|
16 | this.enumerators = new List<IEnumerator>();
|
---|
17 | }
|
---|
18 |
|
---|
19 | public IOptimizable Current {
|
---|
20 | get { return node; }
|
---|
21 | }
|
---|
22 | object IEnumerator.Current {
|
---|
23 | get { return Current; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void Dispose() { }
|
---|
27 |
|
---|
28 | public bool MoveNext() {
|
---|
29 | int i = 0;
|
---|
30 | bool ok = false;
|
---|
31 | while (!ok && i < enumerators.Count) {
|
---|
32 | if (enumerators[i].MoveNext()) {
|
---|
33 | ok = true;
|
---|
34 | } else {
|
---|
35 | i++;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (ok) {
|
---|
40 | for (int k = i - 1; k >= 0; k--) {
|
---|
41 | enumerators[k].Reset();
|
---|
42 | enumerators[k].MoveNext();
|
---|
43 | }
|
---|
44 | } else {
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 |
|
---|
48 | var pc = node as IParameterConfiguration;
|
---|
49 | if (pc != null && valueEnumerator != null) {
|
---|
50 | pc.ActualValue = ((IValueConfiguration)valueEnumerator.Current).ActualValue;
|
---|
51 | pc.UpdateActualValueIndexToItem((IValueConfiguration)valueEnumerator.Current);
|
---|
52 | }
|
---|
53 | var vc = node as IValueConfiguration;
|
---|
54 | if (vc != null && valueEnumerator != null) {
|
---|
55 | vc.ActualValue.Value = (IItem)valueEnumerator.Current;
|
---|
56 | }
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void Reset() {
|
---|
61 | enumerators.Clear();
|
---|
62 | valueEnumerator = null;
|
---|
63 |
|
---|
64 | var pc = node as IParameterConfiguration;
|
---|
65 | if (pc != null) {
|
---|
66 | valueEnumerator = pc.ValueConfigurations.CheckedItems.ToArray().GetEnumerator();
|
---|
67 | //valueEnumerator.Reset();
|
---|
68 | enumerators.Add(valueEnumerator);
|
---|
69 |
|
---|
70 | foreach (var valueConfiguration in pc.ValueConfigurations.CheckedItems) {
|
---|
71 | if (valueConfiguration.Optimize) {
|
---|
72 | var enumerator = new ParameterCombinationsEnumerator(valueConfiguration);
|
---|
73 | enumerator.Reset();
|
---|
74 | enumerator.MoveNext();
|
---|
75 | enumerators.Add(enumerator);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | var vc = node as IValueConfiguration;
|
---|
81 | if (vc != null) {
|
---|
82 | if (vc.RangeConstraint != null) {
|
---|
83 | valueEnumerator = vc.RangeConstraint.GetCombinations().ToArray().GetEnumerator();
|
---|
84 | //valueEnumerator.Reset();
|
---|
85 | //enumerator.MoveNext();
|
---|
86 | enumerators.Add(valueEnumerator);
|
---|
87 | } else {
|
---|
88 | foreach (var parameterConfiguration in vc.ParameterConfigurations) {
|
---|
89 | if (parameterConfiguration.Optimize) {
|
---|
90 | var enumerator = new ParameterCombinationsEnumerator(parameterConfiguration);
|
---|
91 | enumerator.Reset();
|
---|
92 | enumerator.MoveNext();
|
---|
93 | enumerators.Add(enumerator);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|