#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Selection;
namespace HeuristicLab.Algorithms.EvolutionStrategy {
///
/// An operator which represents the main loop of an evolution strategy (EvolutionStrategy).
///
[Item("EvolutionStrategyMainLoop", "An operator which represents the main loop of an evolution strategy (EvolutionStrategy).")]
[StorableClass]
public sealed class EvolutionStrategyMainLoop : AlgorithmOperator {
#region Parameter properties
public ValueLookupParameter RandomParameter {
get { return (ValueLookupParameter)Parameters["Random"]; }
}
public ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
public SubScopesLookupParameter QualityParameter {
get { return (SubScopesLookupParameter)Parameters["Quality"]; }
}
public ValueLookupParameter BestKnownQualityParameter {
get { return (ValueLookupParameter)Parameters["BestKnownQuality"]; }
}
public ValueLookupParameter PopulationSizeParameter {
get { return (ValueLookupParameter)Parameters["PopulationSize"]; }
}
public ValueLookupParameter ParentsPerChildParameter {
get { return (ValueLookupParameter)Parameters["ParentsPerChild"]; }
}
public ValueLookupParameter ChildrenParameter {
get { return (ValueLookupParameter)Parameters["Children"]; }
}
public ValueLookupParameter PlusSelectionParameter {
get { return (ValueLookupParameter)Parameters["PlusSelection"]; }
}
public ValueLookupParameter MaximumGenerationsParameter {
get { return (ValueLookupParameter)Parameters["MaximumGenerations"]; }
}
public ValueLookupParameter MutatorParameter {
get { return (ValueLookupParameter)Parameters["Mutator"]; }
}
public ValueLookupParameter RecombinatorParameter {
get { return (ValueLookupParameter)Parameters["Recombinator"]; }
}
public ValueLookupParameter EvaluatorParameter {
get { return (ValueLookupParameter)Parameters["Evaluator"]; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public ValueLookupParameter VisualizerParameter {
get { return (ValueLookupParameter)Parameters["Visualizer"]; }
}
public LookupParameter VisualizationParameter {
get { return (LookupParameter)Parameters["Visualization"]; }
}
private ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
private ValueLookupParameter StrategyParameterManipulatorParameter {
get { return (ValueLookupParameter)Parameters["StrategyParameterManipulator"]; }
}
private ValueLookupParameter StrategyParameterCrossoverParameter {
get { return (ValueLookupParameter)Parameters["StrategyParameterCrossover"]; }
}
public IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
#endregion
[StorableConstructor]
private EvolutionStrategyMainLoop(bool deserializing) : base() { }
public EvolutionStrategyMainLoop()
: base() {
Initialize();
}
private void Initialize() {
#region Create parameters
Parameters.Add(new ValueLookupParameter("Random", "A pseudo random number generator."));
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false."));
Parameters.Add(new SubScopesLookupParameter("Quality", "The value which represents the quality of a solution."));
Parameters.Add(new ValueLookupParameter("BestKnownQuality", "The best known quality value found so far."));
Parameters.Add(new ValueLookupParameter("PopulationSize", "µ (mu) - the size of the population."));
Parameters.Add(new ValueLookupParameter("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
Parameters.Add(new ValueLookupParameter("Children", "λ (lambda) - the size of the offspring population."));
Parameters.Add(new ValueLookupParameter("MaximumGenerations", "The maximum number of generations which should be processed."));
Parameters.Add(new ValueLookupParameter("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
Parameters.Add(new ValueLookupParameter("Mutator", "The operator used to mutate solutions."));
Parameters.Add(new ValueLookupParameter("Recombinator", "The operator used to cross solutions."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The operator used to evaluate solutions."));
Parameters.Add(new ValueLookupParameter("Results", "The variable collection where results should be stored."));
Parameters.Add(new ValueLookupParameter("Visualizer", "The operator used to visualize solutions."));
Parameters.Add(new LookupParameter("Visualization", "The item which represents the visualization of solutions."));
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
Parameters.Add(new ValueLookupParameter("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
Parameters.Add(new ValueLookupParameter("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
#endregion
#region Create operators
VariableCreator variableCreator = new VariableCreator();
BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator1 = new BestAverageWorstQualityCalculator();
DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
Placeholder visualizer1 = new Placeholder();
ResultsCollector resultsCollector = new ResultsCollector();
WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
Comparator useRecombinationComparator = new Comparator();
ConditionalBranch useRecombinationBranch = new ConditionalBranch();
ChildrenCreator childrenCreator = new ChildrenCreator();
UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
Placeholder recombinator = new Placeholder();
Placeholder strategyRecombinator = new Placeholder();
Placeholder strategyMutator1 = new Placeholder();
Placeholder mutator1 = new Placeholder();
Placeholder evaluator1 = new Placeholder();
SubScopesRemover subScopesRemover = new SubScopesRemover();
UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
Placeholder strategyMutator2 = new Placeholder();
Placeholder mutator2 = new Placeholder();
Placeholder evaluator2 = new Placeholder();
ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
MergingReducer plusReplacement = new MergingReducer();
RightReducer commaReplacement = new RightReducer();
BestSelector bestSelector = new BestSelector();
RightReducer rightReducer = new RightReducer();
IntCounter intCounter = new IntCounter();
Comparator comparator = new Comparator();
BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator2 = new BestAverageWorstQualityCalculator();
DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
Placeholder visualizer2 = new Placeholder();
ConditionalBranch conditionalBranch = new ConditionalBranch();
variableCreator.CollectedValues.Add(new ValueParameter("Generations", new IntValue(0)));
bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
bestAverageWorstQualityCalculator1.AverageQualityParameter.ActualName = "CurrentAverageQuality";
bestAverageWorstQualityCalculator1.BestQualityParameter.ActualName = "CurrentBestQuality";
bestAverageWorstQualityCalculator1.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestAverageWorstQualityCalculator1.QualityParameter.ActualName = QualityParameter.Name;
bestAverageWorstQualityCalculator1.WorstQualityParameter.ActualName = "CurrentWorstQuality";
dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Current Best Quality", null, "CurrentBestQuality"));
dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Current Average Quality", null, "CurrentAverageQuality"));
dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Current Worst Quality", null, "CurrentWorstQuality"));
dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Best Quality", null, "BestQuality"));
dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name));
dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
visualizer1.Name = "Visualizer (placeholder)";
visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
resultsCollector.CollectedValues.Add(new LookupParameter("Generations"));
resultsCollector.CollectedValues.Add(new LookupParameter("Current Best Quality", null, "CurrentBestQuality"));
resultsCollector.CollectedValues.Add(new LookupParameter("Current Average Quality", null, "CurrentAverageQuality"));
resultsCollector.CollectedValues.Add(new LookupParameter("Current Worst Quality", null, "CurrentWorstQuality"));
resultsCollector.CollectedValues.Add(new LookupParameter("Best Quality", null, "BestQuality"));
resultsCollector.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name));
resultsCollector.CollectedValues.Add(new LookupParameter("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
resultsCollector.CollectedValues.Add(new LookupParameter("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
resultsCollector.CollectedValues.Add(new LookupParameter("Solution Visualization", null, VisualizationParameter.Name));
resultsCollector.CollectedValues.Add(new LookupParameter("Qualities"));
resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
selector.Name = "ES Random Selector";
selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
useRecombinationComparator.Name = "ParentsPerChild > 1";
useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
useRecombinationBranch.Name = "Use Recombination?";
useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
childrenCreator.ParentsPerChild = null;
childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
recombinator.Name = "Recombinator (placeholder)";
recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
mutator1.Name = "Mutator (placeholder)";
mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
evaluator1.Name = "Evaluator (placeholder)";
evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
subScopesRemover.RemoveAllSubScopes = true;
strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
mutator2.Name = "Mutator (placeholder)";
mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
evaluator2.Name = "Evaluator (placeholder)";
evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
bestSelector.CopySelected = new BoolValue(false);
bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
bestSelector.QualityParameter.ActualName = QualityParameter.Name;
intCounter.Increment = new IntValue(1);
intCounter.ValueParameter.ActualName = "Generations";
comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
comparator.LeftSideParameter.ActualName = "Generations";
comparator.ResultParameter.ActualName = "Terminate";
comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
bestAverageWorstQualityCalculator2.AverageQualityParameter.ActualName = "CurrentAverageQuality";
bestAverageWorstQualityCalculator2.BestQualityParameter.ActualName = "CurrentBestQuality";
bestAverageWorstQualityCalculator2.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestAverageWorstQualityCalculator2.QualityParameter.ActualName = QualityParameter.Name;
bestAverageWorstQualityCalculator2.WorstQualityParameter.ActualName = "CurrentWorstQuality";
dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Current Best Quality", null, "CurrentBestQuality"));
dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Current Average Quality", null, "CurrentAverageQuality"));
dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Current Worst Quality", null, "CurrentWorstQuality"));
dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Best Quality", null, "BestQuality"));
dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name));
dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
visualizer2.Name = "Visualizer (placeholder)";
visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
conditionalBranch.ConditionParameter.ActualName = "Terminate";
#endregion
#region Create operator graph
OperatorGraph.InitialOperator = variableCreator;
variableCreator.Successor = bestQualityMemorizer1;
bestQualityMemorizer1.Successor = bestQualityMemorizer2;
bestQualityMemorizer2.Successor = bestAverageWorstQualityCalculator1;
bestAverageWorstQualityCalculator1.Successor = dataTableValuesCollector1;
dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
qualityDifferenceCalculator1.Successor = visualizer1;
visualizer1.Successor = resultsCollector;
resultsCollector.Successor = selector;
selector.Successor = subScopesProcessor1;
subScopesProcessor1.Operators.Add(new EmptyOperator());
subScopesProcessor1.Operators.Add(useRecombinationComparator);
subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
useRecombinationComparator.Successor = useRecombinationBranch;
useRecombinationBranch.TrueBranch = childrenCreator;
useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
useRecombinationBranch.Successor = null;
childrenCreator.Successor = uniformSubScopesProcessor1;
uniformSubScopesProcessor1.Operator = recombinator;
uniformSubScopesProcessor1.Successor = null;
recombinator.Successor = strategyRecombinator;
strategyRecombinator.Successor = strategyMutator1;
strategyMutator1.Successor = mutator1;
mutator1.Successor = evaluator1;
evaluator1.Successor = subScopesRemover;
subScopesRemover.Successor = null;
uniformSubScopesProcessor2.Operator = strategyMutator2;
uniformSubScopesProcessor2.Successor = null;
strategyMutator2.Successor = mutator2;
mutator2.Successor = evaluator2;
plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
plusOrCommaReplacementBranch.Successor = bestSelector;
bestSelector.Successor = rightReducer;
rightReducer.Successor = intCounter;
intCounter.Successor = comparator;
comparator.Successor = bestQualityMemorizer3;
bestQualityMemorizer3.Successor = bestQualityMemorizer4;
bestQualityMemorizer4.Successor = bestAverageWorstQualityCalculator2;
bestAverageWorstQualityCalculator2.Successor = dataTableValuesCollector2;
dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
qualityDifferenceCalculator2.Successor = visualizer2;
visualizer2.Successor = conditionalBranch;
conditionalBranch.FalseBranch = selector;
conditionalBranch.TrueBranch = null;
conditionalBranch.Successor = null;
#endregion
}
}
}