#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.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.GeneticAlgorithm {
///
/// An operator which represents the main loop of a genetic algorithm.
///
[Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
[StorableClass]
public sealed class GeneticAlgorithmMainLoop : 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 SelectorParameter {
get { return (ValueLookupParameter)Parameters["Selector"]; }
}
public ValueLookupParameter CrossoverParameter {
get { return (ValueLookupParameter)Parameters["Crossover"]; }
}
public ValueLookupParameter MutationProbabilityParameter {
get { return (ValueLookupParameter)Parameters["MutationProbability"]; }
}
public ValueLookupParameter MutatorParameter {
get { return (ValueLookupParameter)Parameters["Mutator"]; }
}
public ValueLookupParameter EvaluatorParameter {
get { return (ValueLookupParameter)Parameters["Evaluator"]; }
}
public ValueLookupParameter ElitesParameter {
get { return (ValueLookupParameter)Parameters["Elites"]; }
}
public ValueLookupParameter MaximumGenerationsParameter {
get { return (ValueLookupParameter)Parameters["MaximumGenerations"]; }
}
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"]; }
}
public IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
#endregion
[StorableConstructor]
private GeneticAlgorithmMainLoop(bool deserializing) : base() { }
public GeneticAlgorithmMainLoop()
: 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("Selector", "The operator used to select solutions for reproduction."));
Parameters.Add(new ValueLookupParameter("Crossover", "The operator used to cross solutions."));
Parameters.Add(new ValueLookupParameter("MutationProbability", "The probability that the mutation operator is applied on a solution."));
Parameters.Add(new ValueLookupParameter("Mutator", "The operator used to mutate solutions."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The operator used to evaluate solutions."));
Parameters.Add(new ValueLookupParameter("Elites", "The numer of elite solutions which are kept in each generation."));
Parameters.Add(new ValueLookupParameter("MaximumGenerations", "The maximum number of generations which should be processed."));
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 genetic algorithm should be applied."));
#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();
Placeholder selector = new Placeholder();
SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
ChildrenCreator childrenCreator = new ChildrenCreator();
UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
Placeholder crossover = new Placeholder();
StochasticBranch stochasticBranch = new StochasticBranch();
Placeholder mutator = new Placeholder();
Placeholder evaluator = new Placeholder();
SubScopesRemover subScopesRemover = new SubScopesRemover();
SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
BestSelector bestSelector = new BestSelector();
RightReducer rightReducer = new RightReducer();
MergingReducer mergingReducer = new MergingReducer();
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 = "Maximization";
bestQualityMemorizer1.QualityParameter.ActualName = "Quality";
bestQualityMemorizer2.BestQualityParameter.ActualName = "BestKnownQuality";
bestQualityMemorizer2.MaximizationParameter.ActualName = "Maximization";
bestQualityMemorizer2.QualityParameter.ActualName = "Quality";
bestAverageWorstQualityCalculator1.AverageQualityParameter.ActualName = "CurrentAverageQuality";
bestAverageWorstQualityCalculator1.BestQualityParameter.ActualName = "CurrentBestQuality";
bestAverageWorstQualityCalculator1.MaximizationParameter.ActualName = "Maximization";
bestAverageWorstQualityCalculator1.QualityParameter.ActualName = "Quality";
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, "BestKnownQuality"));
dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
qualityDifferenceCalculator1.FirstQualityParameter.ActualName = "BestKnownQuality";
qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
visualizer1.Name = "Visualizer";
visualizer1.OperatorParameter.ActualName = "Visualizer";
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, "BestKnownQuality"));
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, "Visualization"));
resultsCollector.CollectedValues.Add(new LookupParameter("Qualities"));
resultsCollector.ResultsParameter.ActualName = "Results";
selector.Name = "Selector";
selector.OperatorParameter.ActualName = "Selector";
childrenCreator.ParentsPerChild = new IntValue(2);
crossover.Name = "Crossover";
crossover.OperatorParameter.ActualName = "Crossover";
stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
stochasticBranch.RandomParameter.ActualName = "Random";
mutator.Name = "Mutator";
mutator.OperatorParameter.ActualName = "Mutator";
evaluator.Name = "Evaluator";
evaluator.OperatorParameter.ActualName = "Evaluator";
subScopesRemover.RemoveAllSubScopes = true;
bestSelector.CopySelected = new BoolValue(false);
bestSelector.MaximizationParameter.ActualName = "Maximization";
bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
bestSelector.QualityParameter.ActualName = "Quality";
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 = "MaximumGenerations";
bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
bestQualityMemorizer3.MaximizationParameter.ActualName = "Maximization";
bestQualityMemorizer3.QualityParameter.ActualName = "Quality";
bestQualityMemorizer4.BestQualityParameter.ActualName = "BestKnownQuality";
bestQualityMemorizer4.MaximizationParameter.ActualName = "Maximization";
bestQualityMemorizer4.QualityParameter.ActualName = "Quality";
bestAverageWorstQualityCalculator2.AverageQualityParameter.ActualName = "CurrentAverageQuality";
bestAverageWorstQualityCalculator2.BestQualityParameter.ActualName = "CurrentBestQuality";
bestAverageWorstQualityCalculator2.MaximizationParameter.ActualName = "Maximization";
bestAverageWorstQualityCalculator2.QualityParameter.ActualName = "Quality";
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, "BestKnownQuality"));
dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
qualityDifferenceCalculator2.FirstQualityParameter.ActualName = "BestKnownQuality";
qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
visualizer2.Name = "Visualizer";
visualizer2.OperatorParameter.ActualName = "Visualizer";
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(childrenCreator);
subScopesProcessor1.Successor = subScopesProcessor2;
childrenCreator.Successor = uniformSubScopesProcessor;
uniformSubScopesProcessor.Operator = crossover;
uniformSubScopesProcessor.Successor = null;
crossover.Successor = stochasticBranch;
stochasticBranch.FirstBranch = mutator;
stochasticBranch.SecondBranch = null;
stochasticBranch.Successor = evaluator;
mutator.Successor = null;
evaluator.Successor = subScopesRemover;
subScopesRemover.Successor = null;
subScopesProcessor2.Operators.Add(bestSelector);
subScopesProcessor2.Operators.Add(new EmptyOperator());
subScopesProcessor2.Successor = mergingReducer;
bestSelector.Successor = rightReducer;
rightReducer.Successor = null;
mergingReducer.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
}
}
}