1 | using System.Linq;
|
---|
2 | using HeuristicLab.Analysis;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Operators;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Algorithms.IslandALPS {
|
---|
12 | /// <summary>
|
---|
13 | /// An Operator which creates a Subscope and then copies the layers from each island grouped by Layernumber into the new
|
---|
14 | /// scope. Then the operator executes the specified Operator on each group and afterwards deletes the created scope.
|
---|
15 | /// G newScope - G
|
---|
16 | /// /|\ /\ /|\
|
---|
17 | /// I I I => L1 L2 I I I
|
---|
18 | /// / \ / \
|
---|
19 | /// L1 L2 L1 L2
|
---|
20 | /// </summary>
|
---|
21 | [Item("GroupedLayerOperator", "")]
|
---|
22 | [StorableClass]
|
---|
23 | public class GroupedLayerOperator : InstrumentedOperator {
|
---|
24 | [StorableConstructor]
|
---|
25 | public GroupedLayerOperator(bool deserializing) : base(deserializing) {}
|
---|
26 |
|
---|
27 | public GroupedLayerOperator(GroupedLayerOperator original, Cloner cloner) : base(original, cloner) {}
|
---|
28 |
|
---|
29 | public GroupedLayerOperator() {
|
---|
30 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the operator gets executed."));
|
---|
31 | Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all grouped scopes."));
|
---|
32 | //add results colletion to save the list of group result in
|
---|
33 | }
|
---|
34 |
|
---|
35 | private ScopeParameter CurrentScopeParameter {
|
---|
36 | get { return (ScopeParameter) Parameters["CurrentScope"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private OperatorParameter OperatorParameter {
|
---|
40 | get { return (OperatorParameter) Parameters["Operator"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IScope CurrentScope {
|
---|
44 | get { return CurrentScopeParameter.ActualValue; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IOperator Operator {
|
---|
48 | get { return OperatorParameter.Value; }
|
---|
49 | set { OperatorParameter.Value = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private IScope GroupScope {
|
---|
53 | get {
|
---|
54 | if (CurrentScope.Variables.ContainsKey("GroupScope")) {
|
---|
55 | return CurrentScope.Variables["GroupScope"].Value as IScope;
|
---|
56 | }
|
---|
57 | var scope = new Scope();
|
---|
58 | CurrentScope.Variables.Add(new Variable("GroupScope", scope));
|
---|
59 | return scope;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
64 | return new GroupedLayerOperator(this, cloner);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IOperation InstrumentedApply() {
|
---|
68 | //create the subscope where the individuals get copied to.
|
---|
69 | var scope = GroupScope;
|
---|
70 | //descend to find the scope with individuals to copy
|
---|
71 | foreach (var islandScope in CurrentScope.SubScopes) {
|
---|
72 | var numScope = 0;
|
---|
73 | foreach (var layerScope in islandScope.SubScopes) {
|
---|
74 | //copy the individuals to the apropriate group (based on order in the ScopeTree, e.g. first layer into first group)
|
---|
75 | IScope analyzerScope;
|
---|
76 | if (scope.SubScopes.Count <= numScope) {
|
---|
77 | if (!scope.SubScopes.Any()) {
|
---|
78 | analyzerScope = new Scope();
|
---|
79 | var rc = new ResultCollection();
|
---|
80 | analyzerScope.Variables.Add(new Variable("GroupResults", rc));
|
---|
81 | analyzerScope.Variables.Add(new Variable("LayerNumber", new IntValue(numScope)));
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | analyzerScope = scope.SubScopes.First().Clone() as IScope;
|
---|
85 | var rc = (ResultCollection) analyzerScope.Variables["GroupResults"].Value;
|
---|
86 | ClearHistoryRecursively(rc);
|
---|
87 | analyzerScope.Variables["LayerNumber"].Value = new IntValue(numScope);
|
---|
88 | //TODO: let a result collector update the lookups ?
|
---|
89 | }
|
---|
90 |
|
---|
91 | scope.SubScopes.Add(analyzerScope);
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | analyzerScope = scope.SubScopes[numScope];
|
---|
95 | }
|
---|
96 | //delete old individuals
|
---|
97 | analyzerScope.SubScopes.Clear();
|
---|
98 | foreach (var individual in layerScope.SubScopes) {
|
---|
99 | analyzerScope.SubScopes.Add(individual.Clone() as IScope);
|
---|
100 | }
|
---|
101 | numScope++;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | //execute the operator on each grouped scope
|
---|
106 | var next = new OperationCollection(base.InstrumentedApply());
|
---|
107 | foreach (var scopeGroup in scope.SubScopes) {
|
---|
108 | var inner = new OperationCollection();
|
---|
109 | inner.Add(ExecutionContext.CreateOperation(Operator, scopeGroup));
|
---|
110 | next.Insert(0, inner);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | return next;
|
---|
115 | }
|
---|
116 |
|
---|
117 | //TODO: HL-INTEGRATION :Copied from ResultsHistoryWiper, When integrate create static apply method and delete this
|
---|
118 | private static void ClearHistoryRecursively(ResultCollection results) {
|
---|
119 | var values = results.Select(r => r.Value);
|
---|
120 |
|
---|
121 | // Reset all values within results in results
|
---|
122 | foreach (var resultsCollection in values.OfType<ResultCollection>())
|
---|
123 | ClearHistoryRecursively(resultsCollection);
|
---|
124 |
|
---|
125 | // Reset values
|
---|
126 | foreach (var dataTable in values.OfType<DataTable>()) {
|
---|
127 | foreach (var row in dataTable.Rows)
|
---|
128 | for (var i = 0; i < row.Values.Count; i++)
|
---|
129 | row.Values[i] = double.NaN;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | } |
---|