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

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

#2269

  • Changed the age type from int to double.
  • Changed EldersSelector to make use of a ScopeTreeLookupParameter.
  • Removed unused operators in LayerUpdator.
File size: 3.5 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    private ILookupParameter<IntValue> OpenLayersParameter {
38      get { return (ILookupParameter<IntValue>)Parameters["OpenLayers"]; }
39    }
40
41    [StorableConstructor]
42    private LayerCreator(bool deserializing) : base(deserializing) { }
43
44    private LayerCreator(LayerCreator original, Cloner cloner)
45      : base(original, cloner) {
46    }
47    public LayerCreator()
48      : base() {
49      Parameters.Add(new LookupParameter<IntValue>("OpenLayers"));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new LayerCreator(this, cloner);
54    }
55
56    public override IOperation Apply() {
57      var scopes = ExecutionContext.Scope.SubScopes;
58      if (scopes.Count < 1)
59        throw new ArgumentException("At least one sub-scope must exist.");
60
61      var lastSubScope = scopes.Last();
62      var clone = (IScope)lastSubScope.Clone(new Cloner());
63
64      int number;
65      if (int.TryParse(clone.Name, out number))
66        clone.Name = (number + 1).ToString();
67
68      scopes.Add(clone);
69
70      // Set new layernumber
71      clone.Variables["Layer"].Value = new IntValue(OpenLayersParameter.ActualValue.Value);
72
73      // Decrement ages, because MainOperator is goint to increment it
74      foreach (var solution in clone.SubScopes)
75        ((DoubleValue)solution.Variables["Age"].Value).Value -= 1;
76
77      // Reset existing values in the results to NaN to symbolize that no layer existed during that duration
78      var results = (ResultCollection)clone.Variables["LayerResults"].Value;
79      ResetResults(results);
80
81      return base.Apply();
82    }
83
84    private void ResetResults(ResultCollection results) {
85      var values = results.Select(r => r.Value);
86
87      // Reset all values within results in results
88      foreach (var resultsCollection in values.OfType<ResultCollection>())
89        ResetResults(resultsCollection);
90
91      // Reset values
92      foreach (var dataTable in values.OfType<DataTable>()) {
93        foreach (var row in dataTable.Rows)
94          for (int i = 0; i < row.Values.Count; i++)
95            row.Values[i] = double.NaN;
96      }
97    }
98
99  }
100}
Note: See TracBrowser for help on using the repository browser.