#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 HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Selection;
namespace HeuristicLab.Analysis.FitnessLandscape {
///
/// An operator which represents a local search.
///
[Item("LocalAnalysisMainLoop", "An operator which represents the main loop of a best improvement local search (if only a single move is generated in each iteration it is a first improvement local search).")]
[StorableType("68E3A940-FB49-49E1-8F81-4403FD4A7C33")]
public class LocalAnalysisMainLoop : AlgorithmOperator {
#region Parameter properties
public ValueLookupParameter RandomParameter {
get { return (ValueLookupParameter)Parameters["Random"]; }
}
public ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
public ValueLookupParameter BestKnownQualityParameter {
get { return (ValueLookupParameter)Parameters["BestKnownQuality"]; }
}
public LookupParameter QualityParameter {
get { return (LookupParameter)Parameters["Quality"]; }
}
public ValueLookupParameter MaximumIterationsParameter {
get { return (ValueLookupParameter)Parameters["MaximumIterations"]; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public ValueLookupParameter MutatorParameter {
get { return (ValueLookupParameter)Parameters["Mutator"]; }
}
public ValueLookupParameter SelectorParameter {
get { return (ValueLookupParameter)Parameters["Selector"]; }
}
public ValueLookupParameter SampleSizeParameter {
get { return (ValueLookupParameter)Parameters["SampleSize"]; }
}
public ValueLookupParameter AnalyzerParameter {
get { return (ValueLookupParameter)Parameters["Analyzer"]; }
}
#endregion
[StorableConstructor]
protected LocalAnalysisMainLoop(StorableConstructorFlag _) : base(_) { }
protected LocalAnalysisMainLoop(LocalAnalysisMainLoop original, Cloner cloner) : base(original, cloner) { }
public LocalAnalysisMainLoop()
: base() {
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new LocalAnalysisMainLoop(this, cloner);
}
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 LookupParameter("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("MaximumIterations", "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("Mutator", "Mutation Operator."));
Parameters.Add(new ValueLookupParameter("Selector", "The operator that selects how to continue."));
Parameters.Add(new ValueLookupParameter("SampleSize", "Number of mutation samples."));
Parameters.Add(new ValueLookupParameter("Analyzer", "The operator used to analyze the solution and moves."));
#endregion
#region Create operators
VariableCreator variableCreator = new VariableCreator();
Placeholder analyzer1 = new Placeholder();
SubScopesCounter evaluatedSolutionsCounter1 = new SubScopesCounter();
LeftSelector leftSelector = new LeftSelector();
SubScopesProcessor mainProcessor = new SubScopesProcessor();
UniformSubScopesProcessor mutationProcessor = new UniformSubScopesProcessor();
Placeholder mutator = new Placeholder();
Placeholder evaluator = new Placeholder();
SubScopesCounter evaluatedSolutionsCounter2 = new SubScopesCounter();
Placeholder selector = new Placeholder();
RightReducer rightReducer1 = new RightReducer();
RightReducer rightReducer2 = new RightReducer();
Placeholder analyzer2 = new Placeholder();
IntCounter iterationsCounter = new IntCounter();
Comparator iterationsComparator = new Comparator();
ResultsCollector resultsCollector2 = new ResultsCollector();
ConditionalBranch iterationsTermination = new ConditionalBranch();
variableCreator.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("EvaluatedSolutions", new IntValue(0)));
analyzer1.Name = "Analyzer (placeholder)";
analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
evaluatedSolutionsCounter1.Name = "EvaluatedSolutions++";
evaluatedSolutionsCounter1.AccumulateParameter.Value = new BoolValue(true);
evaluatedSolutionsCounter1.ValueParameter.ActualName = "EvaluatedSolutions";
leftSelector.CopySelected = new BoolValue(true);
leftSelector.NumberOfSelectedSubScopesParameter.ActualName = SampleSizeParameter.Name;
mainProcessor.Name = "Main Processsor";
mutationProcessor.Parallel = new BoolValue(false);
mutator.Name = "(Mutator)";
mutator.OperatorParameter.ActualName = MutatorParameter.Name;
evaluator.Name = "(Evaluator)";
evaluator.OperatorParameter.ActualName = "Evaluator";
evaluatedSolutionsCounter2.Name = "EvaluatedSolutions++";
evaluatedSolutionsCounter2.AccumulateParameter.Value = new BoolValue(true);
evaluatedSolutionsCounter2.ValueParameter.ActualName = "EvaluatedSolutions";
selector.Name = "(Selector)";
selector.OperatorParameter.ActualName = SelectorParameter.Name;
analyzer2.Name = "(Analyzer)";
analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
iterationsCounter.Name = "Iterations Counter";
iterationsCounter.Increment = new IntValue(1);
iterationsCounter.ValueParameter.ActualName = "Iterations";
iterationsComparator.Name = "Iterations >= MaximumIterations";
iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
iterationsComparator.LeftSideParameter.ActualName = "Iterations";
iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
iterationsComparator.ResultParameter.ActualName = "Terminate";
resultsCollector2.CopyValue = new BoolValue(true);
resultsCollector2.CollectedValues.Add(new LookupParameter("Iterations"));
resultsCollector2.CollectedValues.Add(new LookupParameter("EvaluatedSolutions"));
resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
iterationsTermination.Name = "Iterations Termination Condition";
iterationsTermination.ConditionParameter.ActualName = "Terminate";
#endregion
#region Create operator graph
OperatorGraph.InitialOperator = variableCreator;
variableCreator.Successor = evaluatedSolutionsCounter1;
evaluatedSolutionsCounter1.Successor = analyzer1;
analyzer1.Successor = leftSelector;
leftSelector.Successor = mainProcessor;
mainProcessor.Operators.Add(new EmptyOperator());
mainProcessor.Operators.Add(mutationProcessor);
mainProcessor.Successor = rightReducer2;
mutationProcessor.Operator = mutator;
mutationProcessor.Successor = evaluatedSolutionsCounter2;
mutator.Successor = evaluator;
evaluator.Successor = null;
evaluatedSolutionsCounter2.Successor = selector;
selector.Successor = rightReducer1;
rightReducer1.Successor = null;
rightReducer2.Successor = iterationsCounter;
iterationsCounter.Successor = analyzer2;
analyzer2.Successor = iterationsComparator;
iterationsComparator.Successor = iterationsTermination;
iterationsTermination.TrueBranch = resultsCollector2;
iterationsTermination.FalseBranch = leftSelector;
#endregion
}
}
}