#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.SteadyState {
[Item("AlpsSsGeneticAlgorithmMainLoop", "An ALPS steady-state genetic algorithm main loop operator.")]
[StorableClass]
public class AlpsSsGeneticAlgorithmMainLoop : AlgorithmOperator {
#region Parameter Properties
public ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public ILookupParameter MaximumIterationsParameter {
get { return (ILookupParameter)Parameters["MaximumIterations"]; }
}
public ILookupParameter AnalyzerParameter {
get { return (ILookupParameter)Parameters["Analyzer"]; }
}
public ILookupParameter LayerAnalyzerParameter {
get { return (ILookupParameter)Parameters["LayerAnalyzer"]; }
}
#endregion
[StorableConstructor]
private AlpsSsGeneticAlgorithmMainLoop(bool deserializing)
: base(deserializing) { }
private AlpsSsGeneticAlgorithmMainLoop(AlpsSsGeneticAlgorithmMainLoop original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new AlpsSsGeneticAlgorithmMainLoop(this, cloner);
}
public AlpsSsGeneticAlgorithmMainLoop()
: base() {
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The value which represents the quality of a solution."));
Parameters.Add(new LookupParameter("MaximumIterations", "The maximum number of iterations 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 resultsColletor = new ResultsCollector();
var layersProcessor = new NamedSubScopeProcessor() { Name = "Process Layers" };
var initializeBatchIteration = new Assigner() { Name = "Initialize BatchIterations" };
var randomLayerProcessor = new RandomLayerProcessor() { Name = "Select a layer" };
var isLayerZeroComperator = new Comparator() { Name = "IsLayerZero = Layer == 0" };
var isLayerZeroBranch = new ConditionalBranch() { Name = "IsLayerZero?" };
var isDoInitBranch = new ConditionalBranch() { Name = "DoInit?" };
var setTargetIndedxToNextInit = new Assigner() { Name = "TargetIndex = NextInit" };
var incrementNextInit = new IntCounter() { Name = "Incr. NextInit" };
var checkInitFinished = new Comparator() { Name = "DoInit = NextInit < LayerSize" };
var workingScopeProcessor = new NamedSubScopeProcessor() { Name = "WorkingScope Processor" };
var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" };
var initializeAgeProcessor = new UniformSubScopesProcessor();
var initializeAge = new Assigner() { Name = "Initialize Age" };
var removeEmptySubscope = new SubScopesRemover() { Name = "Remove empty subscope (from solutioncreator)" };
var layerSorter = new SubScopesSorter() { Name = "Sort Layer" };
var selectRandomTargetIndex = new RandomIntAssigner();
var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" };
var matingPoolProcessor = new NamedSubScopeProcessor() { Name = "Process MatingPool" };
var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" };
var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" };
var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" };
var mainOperator = new AlpsSsGeneticAlgorithmMainOperator();
var reactivateInit = new Assigner() { Name = "DoInit = true" };
var resetNextInit = new Assigner() { Name = "NextInit = 1" };
var resetTargetIndex = new Assigner() { Name = "TargetIndex = MatingPoolSize" };
var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" };
var incrementEvaluations = new IntCounter() { Name = "Incr. EvaluatedSolutions" };
var incrementBatchIterations = new IntCounter() { Name = "Incr. BatchIterations" };
var batchFinishedComperator = new Comparator() { Name = "BatchFinished = BatchIterations >= BatchSize" };
var batchFinishedBranch = new ConditionalBranch() { Name = "BatchFinished?" };
var incrIterations = new IntCounter() { Name = "Incr. Iterations" };
var updateResultEvaluatedSolutions = new IntCounter() { Name = "Update ResultEvaluatedSolutions" };
var layerAnalyzerProcessor = new UniformSubScopesProcessor();
var layerAnalyzer = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
var analyzer = new Placeholder() { Name = "Analyzer (Placeholder)" };
var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" };
var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
OperatorGraph.InitialOperator = variableCreator;
variableCreator.CollectedValues.Add(new ValueParameter("DoInit", new BoolValue(false)));
variableCreator.CollectedValues.Add(new ValueParameter("NextInit", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("OpenLayers", new IntValue(1)));
variableCreator.CollectedValues.Add(new ValueParameter("TargetIndex", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("MatingPoolSize", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("ValidParents", new BoolValue(false)));
variableCreator.CollectedValues.Add(new ValueParameter("IsLayerZero", new BoolValue(false)));
variableCreator.CollectedValues.Add(new ValueParameter("BatchIterations", new IntValue(0)));
variableCreator.Successor = resultsColletor;
resultsColletor.CollectedValues.Add(new LookupParameter("Iterations"));
resultsColletor.Successor = layersProcessor;
layersProcessor.TargetScopeParameter.ActualName = "LayersScope";
layersProcessor.Operator = initializeBatchIteration;
initializeBatchIteration.LeftSideParameter.ActualName = "BatchIterations";
initializeBatchIteration.RightSideParameter.Value = new IntValue(0);
initializeBatchIteration.Successor = randomLayerProcessor;
randomLayerProcessor.Operator = isLayerZeroComperator;
randomLayerProcessor.Successor = incrementBatchIterations;
isLayerZeroComperator.LeftSideParameter.ActualName = "Layer";
isLayerZeroComperator.RightSideParameter.Value = new IntValue(0);
isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero";
isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal);
isLayerZeroComperator.Successor = isLayerZeroBranch;
isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero";
isLayerZeroBranch.TrueBranch = isDoInitBranch;
isLayerZeroBranch.FalseBranch = layerSorter;
isLayerZeroBranch.Successor = tryMoveUp;
isDoInitBranch.ConditionParameter.ActualName = "DoInit";
isDoInitBranch.TrueBranch = setTargetIndedxToNextInit;
isDoInitBranch.FalseBranch = layerSorter;
setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex";
setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit";
setTargetIndedxToNextInit.Successor = incrementNextInit;
incrementNextInit.ValueParameter.ActualName = "NextInit";
incrementNextInit.Increment = new IntValue(1);
incrementNextInit.Successor = checkInitFinished;
checkInitFinished.LeftSideParameter.ActualName = "NextInit";
checkInitFinished.RightSideParameter.ActualName = "LayerSize";
checkInitFinished.Comparison = new Comparison(ComparisonType.Less);
checkInitFinished.ResultParameter.ActualName = "DoInit";
checkInitFinished.Successor = workingScopeProcessor;
workingScopeProcessor.Operator = createRandomIndividual;
workingScopeProcessor.TargetScopeParameter.ActualName = "WorkingScope";
createRandomIndividual.NumberOfSolutions = new IntValue(1);
createRandomIndividual.Successor = initializeAgeProcessor;
initializeAgeProcessor.Operator = initializeAge;
initializeAgeProcessor.Successor = removeEmptySubscope;
initializeAge.LeftSideParameter.ActualName = "EvalsCreated";
initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions";
layerSorter.ValueParameter.ActualName = "Quality";
layerSorter.DescendingParameter.ActualName = "Maximization";
layerSorter.Successor = selectRandomTargetIndex;
selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
selectRandomTargetIndex.MinimumParameter.ActualName = "Elites";
selectRandomTargetIndex.MinimumParameter.Value = null;
selectRandomTargetIndex.MaximumParameter.ActualName = "LayerSize";
selectRandomTargetIndex.MaximumParameter.Value = null;
selectRandomTargetIndex.Successor = matingPoolCreator;
matingPoolCreator.Successor = matingPoolProcessor;
matingPoolProcessor.Operator = matingPoolSize;
matingPoolProcessor.TargetScopeParameter.ActualName = "WorkingScope";
matingPoolSize.ValueParameter.ActualName = "MatingPoolSize";
matingPoolSize.AccumulateParameter.Value = new BoolValue(false);
matingPoolSize.Successor = matingPoolSizeMin2;
matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize";
matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2);
matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents";
matingPoolSizeMin2.Successor = validParentsBranch;
validParentsBranch.ConditionParameter.ActualName = "ValidParents";
validParentsBranch.TrueBranch = mainOperator;
validParentsBranch.FalseBranch = reactivateInit;
reactivateInit.Successor = resetNextInit;
reactivateInit.LeftSideParameter.ActualName = "DoInit";
reactivateInit.RightSideParameter.Value = new BoolValue(true);
resetNextInit.Successor = resetTargetIndex;
resetNextInit.LeftSideParameter.ActualName = "NextInit";
resetNextInit.RightSideParameter.Value = new IntValue(1);
resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
resetTargetIndex.RightSideParameter.ActualName = "MatingPoolSize";
resetTargetIndex.Successor = createRandomIndividual;
tryMoveUp.Successor = incrementEvaluations;
incrementEvaluations.ValueParameter.ActualName = "EvaluatedSolutions";
incrementEvaluations.Increment = new IntValue(1);
incrementBatchIterations.ValueParameter.ActualName = "BatchIterations";
incrementBatchIterations.Increment = new IntValue(1);
incrementBatchIterations.Successor = batchFinishedComperator;
batchFinishedComperator.LeftSideParameter.ActualName = "BatchIterations";
batchFinishedComperator.RightSideParameter.ActualName = "BatchSize";
batchFinishedComperator.ResultParameter.ActualName = "BatchFinished";
batchFinishedComperator.Successor = batchFinishedBranch;
batchFinishedBranch.ConditionParameter.ActualName = "BatchFinished";
batchFinishedBranch.TrueBranch = incrIterations;
batchFinishedBranch.FalseBranch = randomLayerProcessor;
incrIterations.ValueParameter.ActualName = "Iterations";
incrIterations.Increment = new IntValue(1);
incrIterations.Successor = updateResultEvaluatedSolutions;
updateResultEvaluatedSolutions.ValueParameter.ActualName = "ResultEvaluatedSolutions";
updateResultEvaluatedSolutions.IncrementParameter.ActualName = "BatchSize";
updateResultEvaluatedSolutions.IncrementParameter.Value = null;
updateResultEvaluatedSolutions.Successor = layerAnalyzerProcessor;
layerAnalyzerProcessor.Operator = layerAnalyzer;
layerAnalyzerProcessor.Successor = analyzer;
layerAnalyzer.OperatorParameter.ActualName = "LayerAnalyzer";
analyzer.OperatorParameter.ActualName = "Analyzer";
analyzer.Successor = iterationsComparator;
iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
iterationsComparator.LeftSideParameter.ActualName = "Iterations";
iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
iterationsComparator.ResultParameter.ActualName = "Terminate";
iterationsComparator.Successor = terminateBranch;
terminateBranch.ConditionParameter.ActualName = "Terminate";
terminateBranch.FalseBranch = initializeBatchIteration;
}
}
}