#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.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 ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
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(); }
}
[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 ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false.") { Hidden = true });
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The value which represents the quality of a solution."));
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 = AlpsGeneticAlgorithmMainOperator.Create();
var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
var eldersEmigrator = CreateEldersEmigrator();
var layerOpener = CreateLayerOpener();
var layerReseeder = CreateReseeder();
var layerAnalyzerProcessor = new UniformSubScopesProcessor();
var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
var termination = new TerminationOperator();
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("LayerResults"));
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", "LayerResults"));
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;
eldersEmigrator.Successor = layerOpener;
layerOpener.Successor = layerReseeder;
layerReseeder.Successor = layerAnalyzerProcessor;
layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
layerAnalyzerProcessor.Successor = analyzerPlaceholder;
layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
analyzerPlaceholder.Successor = termination;
termination.ContinueBranch = matingPoolCreator;
}
private static CombinedOperator CreateEldersEmigrator() {
var eldersEmigrator = new CombinedOperator() { Name = "Emigrate Elders" };
var selectorProsessor = new UniformSubScopesProcessor();
var eldersSelector = new EldersSelector();
var shiftToRightMigrator = new UnidirectionalRingMigrator();
var mergingProsessor = new UniformSubScopesProcessor();
var mergingReducer = new MergingReducer();
var subScopesCounter = new SubScopesCounter();
var countCalculator = new ExpressionCalculator() { Name = "LayerPopulationSize = Min(LayerPopulationSize, PopulationSize)" };
var bestSelector = new BestSelector();
var rightReducer = new RightReducer();
eldersEmigrator.OperatorGraph.InitialOperator = selectorProsessor;
selectorProsessor.Operator = eldersSelector;
selectorProsessor.Successor = shiftToRightMigrator;
shiftToRightMigrator.ClockwiseMigrationParameter.Value = new BoolValue(true);
shiftToRightMigrator.Successor = mergingProsessor;
mergingProsessor.Operator = mergingReducer;
mergingReducer.Successor = subScopesCounter;
subScopesCounter.ValueParameter.ActualName = "LayerPopulationSize";
subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
subScopesCounter.Successor = countCalculator;
countCalculator.CollectedValues.Add(new LookupParameter("PopulationSize"));
countCalculator.CollectedValues.Add(new LookupParameter("LayerPopulationSize"));
countCalculator.ExpressionParameter.Value = new StringValue("LayerPopulationSize PopulationSize LayerPopulationSize PopulationSize < if toint");
countCalculator.ExpressionResultParameter.ActualName = "LayerPopulationSize";
countCalculator.Successor = bestSelector;
bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "LayerPopulationSize";
bestSelector.CopySelected = new BoolValue(false);
bestSelector.Successor = rightReducer;
return eldersEmigrator;
}
private static CombinedOperator CreateLayerOpener() {
var layerOpener = new CombinedOperator() { Name = "Open new Layer if needed" };
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 Layer" };
var createChildrenViaCrossover = AlpsGeneticAlgorithmMainOperator.Create();
var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
layerOpener.OperatorGraph.InitialOperator = 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;
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.NewLayerOperator = createChildrenViaCrossover;
layerCreator.Successor = incrOpenLayers;
createChildrenViaCrossover.Successor = incrEvaluatedSolutionsForNewLayer;
incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = "EvaluatedSolutions";
incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
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;
return layerOpener;
}
private static CombinedOperator CreateReseeder() {
var reseeder = new CombinedOperator() { Name = "Reseed Layer Zero if needed" };
var reseedingController = new ReseedingController() { Name = "Reseeding needed (Generation % AgeGap == 0)?" };
var removeIndividuals = new SubScopesRemover();
var createIndividuals = new SolutionsCreator();
var initializeAgeProsessor = new UniformSubScopesProcessor();
var initializeAge = new VariableCreator() { Name = "Initialize Age" };
var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
reseeder.OperatorGraph.InitialOperator = reseedingController;
reseedingController.FirstLayerOperator = removeIndividuals;
removeIndividuals.Successor = createIndividuals;
createIndividuals.NumberOfSolutionsParameter.ActualName = "PopulationSize";
createIndividuals.Successor = initializeAgeProsessor;
initializeAgeProsessor.Operator = initializeAge;
initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
initializeAge.CollectedValues.Add(new ValueParameter("Age", new DoubleValue(0)));
incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
return reseeder;
}
}
}