#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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Selection;
namespace HeuristicLab.Algorithms.ALPS {
[Item("LayerUpdator", "Operator which manages the reseeding and creation of layers.")]
[StorableClass]
public sealed class LayerUpdator : AlgorithmOperator {
public SolutionsCreator SolutionsCreator {
get { return OperatorGraph.Iterate().OfType().First(); }
}
[StorableConstructor]
private LayerUpdator(bool deserializing)
: base(deserializing) { }
private LayerUpdator(LayerUpdator original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new LayerUpdator(this, cloner);
}
public LayerUpdator(IOperator mainOperator)
: base() {
var incGenSlr = new IntCounter() { Name = "Incr. GenerationsSinceLastRefresh" };
var genSlrEqAgeGap = new Comparator() { Name = "GenerationsSinceLastRefresh >= AgeGap" };
var updateLayersBranch = new ConditionalBranch() { Name = "UpdateLayers?" };
var maxLayerReached = new Comparator() { Name = "MaxLayersReached = OpenLayers >= NumberOfLayers" };
var maxLayerReachedBranch = new ConditionalBranch() { Name = "MaxLayersReached?" };
var openNewLayerCalculator = new ExpressionCalculator() { Name = "OpenNewLayer = Generations >= AgeLimits[OpenLayers - 1]" };
var openNewLayerBranch = new ConditionalBranch() { Name = "OpenNewLayer?" };
var layerCreator = new LayerCreator() { Name = "Create new Layer" };
var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
var newLayerSelector = new RightSelector();
var newLayerProsessor = new SubScopesProcessor() { Name = "NewLayerProcessor" };
var newLayerLayerProcessor = new LayerUniformSubScopesProcessor();
var newLayerReducer = new MergingReducer();
var layerInitializer = new Assigner() { Name = "Init Layer" };
var correctAgeProcessor = new UniformSubScopesProcessor() { Name = "Hack: Decrement Age" };
var correctAgeDecrementor = new IntCounter() { Name = "Age = Age - 1" };
var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
var newResultCollectionAssigner = new Assigner() { Name = "Empty Results" };
var regenerateLayerZeroSelector = new LeftSelector();
var regenerateLayerZeroProsessor = new SubScopesProcessor() { Name = "LayerZeroProcessor" };
var regenerateLayerZeroLayerProsessor = new LayerUniformSubScopesProcessor();
var regenerateLayerZeroReducer = new MergingReducer();
var layerSorter = new LayerSorter();
var subScopesRemover = new SubScopesRemover();
var solutionsCreator = new SolutionsCreator();
var initializeAgeProsessor = new UniformSubScopesProcessor();
var initializeAge = new VariableCreator() { Name = "Initialize Age" };
var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
var resetGenSlr = new Assigner() { Name = "Reset GenerationsSinceLastRefresh" };
OperatorGraph.InitialOperator = incGenSlr;
incGenSlr.ValueParameter.ActualName = "GenerationsSinceLastRefresh";
incGenSlr.Increment = new IntValue(1);
incGenSlr.Successor = genSlrEqAgeGap;
genSlrEqAgeGap.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
genSlrEqAgeGap.RightSideParameter.ActualName = "AgeGap";
genSlrEqAgeGap.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
genSlrEqAgeGap.ResultParameter.ActualName = "UpdateLayers";
genSlrEqAgeGap.Successor = updateLayersBranch;
updateLayersBranch.ConditionParameter.ActualName = "UpdateLayers";
updateLayersBranch.TrueBranch = maxLayerReached;
maxLayerReached.LeftSideParameter.ActualName = "OpenLayers";
maxLayerReached.RightSideParameter.ActualName = "NumberOfLayers";
maxLayerReached.ResultParameter.ActualName = "MaxLayerReached";
maxLayerReached.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
maxLayerReached.Successor = maxLayerReachedBranch;
maxLayerReachedBranch.ConditionParameter.ActualName = "MaxLayerReached";
maxLayerReachedBranch.FalseBranch = openNewLayerCalculator;
maxLayerReachedBranch.Successor = regenerateLayerZeroSelector;
openNewLayerCalculator.CollectedValues.Add(new LookupParameter("AgeLimits"));
openNewLayerCalculator.CollectedValues.Add(new LookupParameter("Generations"));
openNewLayerCalculator.CollectedValues.Add(new LookupParameter("NumberOfLayers"));
openNewLayerCalculator.CollectedValues.Add(new LookupParameter("OpenLayers"));
openNewLayerCalculator.ExpressionResultParameter.ActualName = "OpenNewLayer";
openNewLayerCalculator.ExpressionParameter.Value = new StringValue("Generations 1 + AgeLimits OpenLayers 1 - [] >");
openNewLayerCalculator.Successor = openNewLayerBranch;
openNewLayerBranch.ConditionParameter.ActualName = "OpenNewLayer";
openNewLayerBranch.TrueBranch = layerCreator;
layerCreator.Successor = newLayerSelector;
newLayerSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
newLayerSelector.CopySelected = new BoolValue(false);
newLayerSelector.Successor = newLayerProsessor;
newLayerProsessor.Operators.Add(new EmptyOperator());
newLayerProsessor.Operators.Add(newLayerLayerProcessor);
newLayerProsessor.Successor = newLayerReducer;
newLayerLayerProcessor.Operator = incrEvaluatedSolutionsForNewLayer;
newLayerReducer.Successor = incrOpenLayers;
layerInitializer.LeftSideParameter.ActualName = "Layer";
layerInitializer.RightSideParameter.ActualName = "OpenLayers";
layerInitializer.Successor = correctAgeProcessor;
correctAgeProcessor.Operator = correctAgeDecrementor;
correctAgeProcessor.Successor = incrEvaluatedSolutionsForNewLayer;
correctAgeDecrementor.Increment = new IntValue(-1);
correctAgeDecrementor.ValueParameter.ActualName = "Age";
incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = "EvaluatedSolutions";
incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
incrEvaluatedSolutionsForNewLayer.Successor = mainOperator;
newResultCollectionAssigner.LeftSideParameter.ActualName = "LayerResults";
newResultCollectionAssigner.RightSideParameter.Value = new ResultCollection();
newResultCollectionAssigner.Successor = mainOperator;
// TODO mainOperator increment age
incrOpenLayers.ValueParameter.ActualName = "OpenLayers";
incrOpenLayers.Increment = new IntValue(1);
incrOpenLayers.Successor = newLayerResultsCollector;
newLayerResultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter("LayerResults", "Result set for each layer", "LayerResults"));
newLayerResultsCollector.CopyValue = new BoolValue(false);
newLayerResultsCollector.Successor = null;
regenerateLayerZeroSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
regenerateLayerZeroSelector.CopySelected = new BoolValue(false);
regenerateLayerZeroSelector.Successor = regenerateLayerZeroProsessor;
regenerateLayerZeroProsessor.Operators.Add(new EmptyOperator());
regenerateLayerZeroProsessor.Operators.Add(regenerateLayerZeroLayerProsessor);
regenerateLayerZeroProsessor.Successor = regenerateLayerZeroReducer;
regenerateLayerZeroLayerProsessor.Operator = subScopesRemover;
regenerateLayerZeroReducer.Successor = layerSorter;
layerSorter.Successor = resetGenSlr;
subScopesRemover.Successor = solutionsCreator;
solutionsCreator.Successor = initializeAgeProsessor;
initializeAgeProsessor.Operator = initializeAge;
initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
initializeAge.CollectedValues.Add(new ValueParameter("Age", new IntValue(0)));
initializeAge.Successor = null;
incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
incrEvaluatedSolutionsAfterReseeding.Successor = null;
resetGenSlr.LeftSideParameter.ActualName = "GenerationsSinceLastRefresh";
resetGenSlr.RightSideParameter.Value = new IntValue(0);
resetGenSlr.Successor = null;
}
}
}