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

Last change on this file since 13095 was 13095, checked in by pfleck, 4 years ago

#2269

  • Added the possibility of continuous reseeding (percentage based reseeding of layer 0).
  • Restructured operator graph.
  • Deleted LayerUpdator (replaced by LayerOpener`)
  • Deleted LayerSorter.
  • Moved preparing of GeneticAlgorithmMainLoop to AlpsGeneticAlgorithmMainOperator.
File size: 4.0 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    public OperatorParameter NewLayerOperatorParameter {
41      get { return (OperatorParameter)Parameters["NewLayerOperator"]; }
42    }
43
44    public IOperator NewLayerOperator {
45      get { return NewLayerOperatorParameter.Value; }
46      set { NewLayerOperatorParameter.Value = value; }
47    }
48
49    [StorableConstructor]
50    private LayerCreator(bool deserializing) : base(deserializing) { }
51
52    private LayerCreator(LayerCreator original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public LayerCreator()
56      : base() {
57      Parameters.Add(new LookupParameter<IntValue>("OpenLayers"));
58      Parameters.Add(new OperatorParameter("NewLayerOperator"));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new LayerCreator(this, cloner);
63    }
64
65    public override IOperation Apply() {
66      var layersScope = ExecutionContext.Scope.SubScopes;
67      if (layersScope.Count < 1)
68        throw new ArgumentException("At least one layer must exist.");
69
70      var newLayer = (IScope)layersScope.Last().Clone();
71
72      int number;
73      if (int.TryParse(newLayer.Name, out number))
74        newLayer.Name = (number + 1).ToString();
75
76      layersScope.Add(newLayer);
77
78      // Set new layer number
79      newLayer.Variables["Layer"].Value = new IntValue(OpenLayersParameter.ActualValue.Value);
80
81      // Decrement ages, because MainOperator is goint to increment it
82      foreach (var solution in newLayer.SubScopes)
83        ((DoubleValue)solution.Variables["Age"].Value).Value -= 1;
84
85      // Reset existing values in the results to NaN to symbolize that no layer existed during that duration
86      var results = (ResultCollection)newLayer.Variables["LayerResults"].Value;
87      ResetResults(results);
88
89      var next = new OperationCollection(base.Apply());
90      next.Insert(0, ExecutionContext.CreateChildOperation(NewLayerOperator, newLayer));
91      return next;
92    }
93
94    private void ResetResults(ResultCollection results) {
95      var values = results.Select(r => r.Value);
96
97      // Reset all values within results in results
98      foreach (var resultsCollection in values.OfType<ResultCollection>())
99        ResetResults(resultsCollection);
100
101      // Reset values
102      foreach (var dataTable in values.OfType<DataTable>()) {
103        foreach (var row in dataTable.Rows)
104          for (int i = 0; i < row.Values.Count; i++)
105            row.Values[i] = double.NaN;
106      }
107    }
108
109  }
110}
Note: See TracBrowser for help on using the repository browser.