source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/LayerCreator.cs @ 13037

Last change on this file since 13037 was 13037, checked in by pfleck, 7 years ago

#2269 When creating a new layer, reset the old results (e.g. quality chart) to NaN to symbolize that the layer did not exist during that time.

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.ALPS {
34  [Item("LayerCreator", "An operator which creates a new layer by cloning the oldest one and setting the variables accordingly.")]
35  [StorableClass]
36  public sealed class LayerCreator : SingleSuccessorOperator {
37
38    /*private static readonly ISet<string> SavedVariables = new HashSet<string>{
39      "LocalRandom", "Layer", "LayerEvaluatedSolutions", "NumSubScopes"
40    };*/
41
42    private ILookupParameter<IntValue> OpenLayersParameter {
43      get { return (ILookupParameter<IntValue>)Parameters["OpenLayers"]; }
44    }
45
46    [StorableConstructor]
47    private LayerCreator(bool deserializing) : base(deserializing) { }
48
49    private LayerCreator(LayerCreator original, Cloner cloner)
50      : base(original, cloner) {
51    }
52    public LayerCreator()
53      : base() {
54      Parameters.Add(new LookupParameter<IntValue>("OpenLayers"));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new LayerCreator(this, cloner);
59    }
60
61    public override IOperation Apply() {
62      var scopes = ExecutionContext.Scope.SubScopes;
63      if (scopes.Count < 1)
64        throw new ArgumentException("At least one sub-scope must exist.");
65
66      var lastSubScope = scopes.Last();
67      var clone = (IScope)lastSubScope.Clone(new Cloner());
68
69      int number;
70      if (int.TryParse(clone.Name, out number))
71        clone.Name = (number + 1).ToString();
72
73      scopes.Add(clone);
74
75      // Set new layernumber
76      clone.Variables["Layer"].Value = new IntValue(OpenLayersParameter.ActualValue.Value);
77
78      // Decrement ages, because MainOperator is goint to increment it
79      foreach (var solution in clone.SubScopes)
80        ((IntValue)solution.Variables["Age"].Value).Value -= 1;
81
82      // Reset existing values in the results to NaN to symbolize that no layer existed during that duration
83      var results = (ResultCollection)clone.Variables["LayerResults"].Value;
84      ResetResults(results);
85
86      return base.Apply();
87    }
88
89    private void ResetResults(ResultCollection results) {
90      var values = results.Select(r => r.Value);
91
92      // Reset all values within results in results
93      foreach (var resultsCollection in values.OfType<ResultCollection>())
94        ResetResults(resultsCollection);
95
96      // Reset values
97      foreach (var dataTable in values.OfType<DataTable>()) {
98        foreach (var row in dataTable.Rows)
99          for (int i = 0; i < row.Values.Count; i++)
100            row.Values[i] = double.NaN;
101      }
102    }
103
104  }
105}
Note: See TracBrowser for help on using the repository browser.