#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ALPS { [Item("LayerCreator", "An operator which creates a new layer by cloning the current oldest one.")] [StorableClass] public sealed class LayerCreator : SingleSuccessorOperator { private ILookupParameter OpenLayersParameter { get { return (ILookupParameter)Parameters["OpenLayers"]; } } public OperatorParameter NewLayerOperatorParameter { get { return (OperatorParameter)Parameters["NewLayerOperator"]; } } public IOperator NewLayerOperator { get { return NewLayerOperatorParameter.Value; } set { NewLayerOperatorParameter.Value = value; } } [StorableConstructor] private LayerCreator(bool deserializing) : base(deserializing) { } private LayerCreator(LayerCreator original, Cloner cloner) : base(original, cloner) { } public LayerCreator() : base() { Parameters.Add(new LookupParameter("OpenLayers")); Parameters.Add(new OperatorParameter("NewLayerOperator")); } public override IDeepCloneable Clone(Cloner cloner) { return new LayerCreator(this, cloner); } public override IOperation Apply() { var layersScope = ExecutionContext.Scope.SubScopes; if (layersScope.Count < 1) throw new ArgumentException("At least one layer must exist."); var newLayer = (IScope)layersScope.Last().Clone(); int number; if (int.TryParse(newLayer.Name, out number)) newLayer.Name = (number + 1).ToString(); layersScope.Add(newLayer); // Set new layer number newLayer.Variables["Layer"].Value = new IntValue(OpenLayersParameter.ActualValue.Value); // Decrement ages, because MainOperator is goint to increment it foreach (var solution in newLayer.SubScopes) ((DoubleValue)solution.Variables["Age"].Value).Value -= 1; // Reset existing values in the results to NaN to symbolize that no layer existed during that duration var results = (ResultCollection)newLayer.Variables["LayerResults"].Value; ResetResults(results); var next = new OperationCollection(base.Apply()); next.Insert(0, ExecutionContext.CreateChildOperation(NewLayerOperator, newLayer)); return next; } private void ResetResults(ResultCollection results) { var values = results.Select(r => r.Value); // Reset all values within results in results foreach (var resultsCollection in values.OfType()) ResetResults(resultsCollection); // Reset values foreach (var dataTable in values.OfType()) { foreach (var row in dataTable.Rows) for (int i = 0; i < row.Values.Count; i++) row.Values[i] = double.NaN; } } } }