#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.ALPS {
[Item("AlpsGeneticAlgorithmMainOperator", "An operator that represents the core of an ALPS genetic algorithm.")]
[StorableClass]
public sealed class AlpsGeneticAlgorithmMainOperator : AlgorithmOperator {
#region Parameter properties
public IValueLookupParameter RandomParameter {
get { return (IValueLookupParameter)Parameters["Random"]; }
}
public IValueLookupParameter EvaluatorParameter {
get { return (IValueLookupParameter)Parameters["Evaluator"]; }
}
public IValueLookupParameter EvaluatedSolutionsParameter {
get { return (IValueLookupParameter)Parameters["EvaluatedSolutions"]; }
}
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["Quality"]; }
}
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public IValueLookupParameter PopulationSizeParameter {
get { return (IValueLookupParameter)Parameters["PopulationSize"]; }
}
public IValueLookupParameter SelectorParameter {
get { return (IValueLookupParameter)Parameters["Selector"]; }
}
public IValueLookupParameter CrossoverParameter {
get { return (IValueLookupParameter)Parameters["Crossover"]; }
}
public IValueLookupParameter MutatorParameter {
get { return (IValueLookupParameter)Parameters["Mutator"]; }
}
public IValueLookupParameter MutationProbabilityParameter {
get { return (IValueLookupParameter)Parameters["MutationProbability"]; }
}
public IValueLookupParameter ElitesParameter {
get { return (IValueLookupParameter)Parameters["Elites"]; }
}
public IValueLookupParameter ReevaluateElitesParameter {
get { return (IValueLookupParameter)Parameters["ReevaluateElites"]; }
}
public IValueLookupParameter PlusSelectionParameter {
get { return (IValueLookupParameter)Parameters["PlusSelection"]; }
}
public IScopeTreeLookupParameter AgeParameter {
get { return (IScopeTreeLookupParameter)Parameters["Age"]; }
}
public IValueLookupParameter AgeInheritanceParameter {
get { return (IValueLookupParameter)Parameters["AgeInheritance"]; }
}
public IValueLookupParameter AgeIncrementParameter {
get { return (IValueLookupParameter)Parameters["AgeIncrement"]; }
}
#endregion
[StorableConstructor]
private AlpsGeneticAlgorithmMainOperator(bool deserializing) : base(deserializing) { }
private AlpsGeneticAlgorithmMainOperator(AlpsGeneticAlgorithmMainOperator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new AlpsGeneticAlgorithmMainOperator(this, cloner);
}
public AlpsGeneticAlgorithmMainOperator()
: base() {
Parameters.Add(new ValueLookupParameter("Random", "A pseudo random number generator."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
Parameters.Add(new ValueLookupParameter("EvaluatedSolutions", "The number of times solutions have been evaluated."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The value which represents the quality of a solution."));
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false."));
Parameters.Add(new ValueLookupParameter("PopulationSize", "The size of the population of solutions in each layer."));
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("Mutator", "The operator used to mutate solutions."));
Parameters.Add(new ValueLookupParameter("MutationProbability", "The probability that the mutation operator is applied on a solution."));
Parameters.Add(new ValueLookupParameter("Elites", "The numer of elite solutions which are kept in each generation."));
Parameters.Add(new ValueLookupParameter("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
Parameters.Add(new ValueLookupParameter("PlusSelection", "Include the parents in the selection of the invividuals for the next generation."));
Parameters.Add(new ScopeTreeLookupParameter("Age", "The age of individuals."));
Parameters.Add(new ValueLookupParameter("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent."));
Parameters.Add(new ValueLookupParameter("AgeIncrement", "The value the age the individuals is incremented if they survives a generation."));
var numberOfSelectedParentsCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedParents = 2 * (PopulationSize - (PlusSelection ? 0 : Elites))" };
var selector = new Placeholder() { Name = "Selector (Placeholder)" };
var subScopesProcessor1 = new SubScopesProcessor();
var childrenCreator = new ChildrenCreator();
var uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
var crossover = new Placeholder() { Name = "Crossover (Placeholder)" };
var stochasticBranch = new StochasticBranch() { Name = "MutationProbability" };
var mutator = new Placeholder() { Name = "Mutator (Placeholder)" };
var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
var subScopesRemover = new SubScopesRemover();
var uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
var evaluator = new Placeholder() { Name = "Evaluator (Placeholder)" };
var subScopesCounter = new SubScopesCounter() { Name = "Increment EvaluatedSolutions" };
var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
var replacementMergingReducer = new MergingReducer();
var replacementBestSelector = new BestSelector();
var replacementRightReducer = new RightReducer();
var subScopesProcessor2 = new SubScopesProcessor();
var bestSelector = new BestSelector();
var rightReducer = new RightReducer();
var mergingReducer = new MergingReducer();
var reevaluateElitesBranch = new ConditionalBranch() { Name = "Reevaluate elites ?" };
var incrementAgeProcessor = new UniformSubScopesProcessor();
var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
OperatorGraph.InitialOperator = numberOfSelectedParentsCalculator;
numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter(PopulationSizeParameter.Name));
numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter(ElitesParameter.Name));
numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter(PlusSelectionParameter.Name));
numberOfSelectedParentsCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
numberOfSelectedParentsCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 0 Elites PlusSelection if - 2 * toint");
numberOfSelectedParentsCalculator.Successor = selector;
selector.OperatorParameter.ActualName = SelectorParameter.Name;
selector.Successor = subScopesProcessor1;
subScopesProcessor1.Operators.Add(new EmptyOperator());
subScopesProcessor1.Operators.Add(childrenCreator);
subScopesProcessor1.Successor = replacementBranch;
childrenCreator.ParentsPerChild = new IntValue(2);
childrenCreator.Successor = uniformSubScopesProcessor1;
uniformSubScopesProcessor1.Operator = crossover;
uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
crossover.Successor = stochasticBranch;
stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
stochasticBranch.FirstBranch = mutator;
stochasticBranch.SecondBranch = null;
stochasticBranch.Successor = ageCalculator;
mutator.OperatorParameter.ActualName = MutatorParameter.Name;
mutator.Successor = null;
ageCalculator.ParameterToReduce.ActualName = AgeParameter.Name;
ageCalculator.TargetParameter.ActualName = AgeParameter.Name;
ageCalculator.WeightParameter.ActualName = AgeInheritanceParameter.Name;
ageCalculator.Successor = subScopesRemover;
subScopesRemover.RemoveAllSubScopes = true;
subScopesRemover.Successor = null;
uniformSubScopesProcessor2.Parallel.Value = true;
uniformSubScopesProcessor2.Operator = evaluator;
uniformSubScopesProcessor2.Successor = subScopesCounter;
evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
evaluator.Successor = null;
subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
subScopesCounter.AccumulateParameter.Value = new BoolValue(true);
subScopesCounter.Successor = null;
replacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
replacementBranch.TrueBranch = replacementMergingReducer;
replacementBranch.FalseBranch = subScopesProcessor2;
replacementBranch.Successor = incrementAgeProcessor;
replacementMergingReducer.Successor = replacementBestSelector;
replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
replacementBestSelector.CopySelected = new BoolValue(false);
replacementBestSelector.Successor = replacementRightReducer;
replacementRightReducer.Successor = reevaluateElitesBranch;
subScopesProcessor2.Operators.Add(bestSelector);
subScopesProcessor2.Operators.Add(new EmptyOperator());
subScopesProcessor2.Successor = mergingReducer;
bestSelector.CopySelected = new BoolValue(false);
bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
bestSelector.QualityParameter.ActualName = QualityParameter.Name;
bestSelector.Successor = rightReducer;
rightReducer.Successor = reevaluateElitesBranch;
mergingReducer.Successor = null;
reevaluateElitesBranch.ConditionParameter.ActualName = ReevaluateElitesParameter.Name;
reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor2;
reevaluateElitesBranch.FalseBranch = null;
reevaluateElitesBranch.Successor = null;
incrementAgeProcessor.Operator = ageIncrementor;
incrementAgeProcessor.Successor = null;
ageIncrementor.ValueParameter.ActualName = AgeParameter.Name;
ageIncrementor.IncrementParameter.Value = null;
ageIncrementor.IncrementParameter.ActualName = AgeIncrementParameter.Name;
ageIncrementor.Successor = null;
}
}
}