#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Algorithms.GeneticAlgorithm;
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("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
[StorableClass]
public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
#region Parameter Properties
public ILookupParameter MaximumGenerationsParameter {
get { return (ILookupParameter)Parameters["MaximumGenerations"]; }
}
public ILookupParameter AnalyzerParameter {
get { return (ILookupParameter)Parameters["Analyzer"]; }
}
public ILookupParameter LayerAnalyzerParameter {
get { return (ILookupParameter)Parameters["LayerAnalyzer"]; }
}
#endregion
public GeneticAlgorithmMainLoop MainOperator {
get { return OperatorGraph.Iterate().OfType().First(); }
}
public EldersEmigrator EldersEmigrator {
get { return OperatorGraph.Iterate().OfType().First(); }
}
public LayerUpdator LayerUpdator {
get { return OperatorGraph.Iterate().OfType().First(); }
}
[StorableConstructor]
private AlpsGeneticAlgorithmMainLoop(bool deserializing)
: base(deserializing) { }
private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new AlpsGeneticAlgorithmMainLoop(this, cloner);
}
public AlpsGeneticAlgorithmMainLoop()
: base() {
Parameters.Add(new LookupParameter("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
Parameters.Add(new LookupParameter("Analyzer", "The operator used to the analyze all individuals."));
Parameters.Add(new LookupParameter("LayerAnalyzer", "The operator used to analyze each layer."));
var variableCreator = new VariableCreator() { Name = "Initialize" };
var initLayerAnalyzerProcessor = new SubScopesProcessor();
var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
var resultsCollector = new ResultsCollector();
var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
var matingPoolProcessor = new UniformSubScopesProcessor();
var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
OperatorGraph.InitialOperator = variableCreator;
variableCreator.CollectedValues.Add(new ValueParameter("Generations", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("GenerationsSinceLastRefresh", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("OpenLayers", new IntValue(1)));
variableCreator.Successor = initLayerAnalyzerProcessor;
initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
layerVariableCreator.CollectedValues.Add(new ValueParameter("LayerEvaluatedSolutions"));
layerVariableCreator.CollectedValues.Add(new ValueParameter("Results"));
layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
initLayerAnalyzerPlaceholder.Successor = null;
initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
initAnalyzerPlaceholder.Successor = resultsCollector;
resultsCollector.CollectedValues.Add(new LookupParameter("Generations"));
resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter("LayerResults", "Result set for each layer", "Results"));
resultsCollector.CollectedValues.Add(new LookupParameter("OpenLayers"));
resultsCollector.CopyValue = new BoolValue(false);
resultsCollector.Successor = matingPoolCreator;
matingPoolCreator.Successor = matingPoolProcessor;
matingPoolProcessor.Parallel.Value = true;
matingPoolProcessor.Operator = initializeLayer;
matingPoolProcessor.Successor = generationsIcrementor;
initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
initializeLayer.RightSideParameter.Value = new IntValue(0);
initializeLayer.Successor = mainOperator;
generationsIcrementor.ValueParameter.ActualName = "Generations";
generationsIcrementor.Increment = new IntValue(1);
generationsIcrementor.Successor = evaluatedSolutionsReducer;
evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
evaluatedSolutionsReducer.Successor = eldersEmigrator;
mainOperator.Successor = layerAnalyzerPlaceholder;
layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
layerAnalyzerPlaceholder.Successor = null;
eldersEmigrator.Successor = layerUpdator;
layerUpdator.Successor = analyzerPlaceholder;
analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
analyzerPlaceholder.Successor = generationsComparator;
generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
generationsComparator.LeftSideParameter.ActualName = "Generations";
generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
generationsComparator.Successor = terminateBranch;
terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
terminateBranch.FalseBranch = matingPoolCreator;
}
private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
var mainLoop = new GeneticAlgorithmMainLoop();
var selector = mainLoop.OperatorGraph.Iterate().OfType().First(o => o.OperatorParameter.ActualName == "Selector");
var crossover = mainLoop.OperatorGraph.Iterate().OfType().First(o => o.OperatorParameter.ActualName == "Crossover");
var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType().First();
var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType().First();
// Operator starts with selector
mainLoop.OperatorGraph.InitialOperator = selector;
// Insert AgeCalculator between crossover and its successor
var crossoverSuccessor = crossover.Successor;
var ageCalculator = new DataReducer() { Name = "Calculate Age" };
ageCalculator.ParameterToReduce.ActualName = "Age";
ageCalculator.TargetParameter.ActualName = "Age";
ageCalculator.ReductionOperation.Value = null;
ageCalculator.ReductionOperation.ActualName = "AgeInheritance";
ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
crossover.Successor = ageCalculator;
ageCalculator.Successor = crossoverSuccessor;
// When counting the evaluated solutions, write in LayerEvaluatedSolutions
subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
// Instead of generational loop after merging of elites, increment ages of all individuals
var processor = new UniformSubScopesProcessor();
var incrementor = new IntCounter() { Name = "Increment Age" };
processor.Operator = incrementor;
processor.Successor = null;
incrementor.ValueParameter.ActualName = "Age";
incrementor.Increment = new IntValue(1);
incrementor.Successor = null;
elitesMerger.Successor = processor;
// Parameterize
foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType())
stochasticOperator.RandomParameter.ActualName = "LocalRandom";
foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType())
stochasticBranch.RandomParameter.ActualName = "LocalRandom";
return mainLoop;
}
}
}