#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.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.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 ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
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 AnalyzerParameter {
get { return (ValueLookupParameter)Parameters["Analyzer"]; }
}
private ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
public IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
#endregion
[StorableConstructor]
private GeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
private GeneticAlgorithmMainLoop(GeneticAlgorithmMainLoop original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GeneticAlgorithmMainLoop(this, cloner);
}
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 ScopeTreeLookupParameter("Quality", "The value which represents the quality of a solution."));
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("Analyzer", "The operator used to analyze each generation."));
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();
ResultsCollector resultsCollector1 = new ResultsCollector();
Placeholder analyzer1 = new Placeholder();
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();
ResultsCollector resultsCollector2 = new ResultsCollector();
Placeholder analyzer2 = new Placeholder();
ConditionalBranch conditionalBranch = new ConditionalBranch();
variableCreator.CollectedValues.Add(new ValueParameter("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
resultsCollector1.CollectedValues.Add(new LookupParameter("Generations"));
resultsCollector1.ResultsParameter.ActualName = "Results";
analyzer1.Name = "Analyzer";
analyzer1.OperatorParameter.ActualName = "Analyzer";
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";
resultsCollector2.CollectedValues.Add(new LookupParameter("Generations"));
resultsCollector2.ResultsParameter.ActualName = "Results";
analyzer2.Name = "Analyzer";
analyzer2.OperatorParameter.ActualName = "Analyzer";
conditionalBranch.ConditionParameter.ActualName = "Terminate";
#endregion
#region Create operator graph
OperatorGraph.InitialOperator = variableCreator;
variableCreator.Successor = resultsCollector1;
resultsCollector1.Successor = analyzer1;
analyzer1.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 = resultsCollector2;
resultsCollector2.Successor = analyzer2;
analyzer2.Successor = conditionalBranch;
conditionalBranch.FalseBranch = selector;
conditionalBranch.TrueBranch = null;
conditionalBranch.Successor = null;
#endregion
}
public override IOperation Apply() {
if (CrossoverParameter.ActualValue == null)
return null;
return base.Apply();
}
}
}