#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;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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).")]
[StorableClass]
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(bool deserializing) : base(deserializing) { }
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();
SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
Assigner bestQualityInitializer = new Assigner();
Placeholder analyzer1 = new Placeholder();
ResultsCollector resultsCollector1 = new ResultsCollector();
LeftSelector leftSelector = new LeftSelector();
SubScopesProcessor mainProcessor = new SubScopesProcessor();
UniformSubScopesProcessor mutationProcessor = new UniformSubScopesProcessor();
Placeholder mutator = new Placeholder();
Placeholder evaluator = new Placeholder();
IntCounter evaluatedMovesCounter = new IntCounter();
Placeholder selector = new Placeholder();
SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
QualityComparator qualityComparator = new QualityComparator();
ConditionalBranch improvesQualityBranch = new ConditionalBranch();
Assigner bestQualityUpdater = new Assigner();
Placeholder analyzer2 = new Placeholder();
RightReducer rightReducer = new RightReducer();
RightReducer rightReducer2 = new RightReducer();
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("BestQuality", new DoubleValue(0)));
variableCreator.CollectedValues.Add(new ValueParameter("EvaluatedMoves", new IntValue(0)));
bestQualityInitializer.Name = "Initialize BestQuality";
bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.ActualName;
analyzer1.Name = "Analyzer (placeholder)";
analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
resultsCollector1.CopyValue = new BoolValue(false);
resultsCollector1.CollectedValues.Add(new LookupParameter("Iterations"));
resultsCollector1.CollectedValues.Add(new LookupParameter("Best Quality", null, "BestQuality"));
resultsCollector1.CollectedValues.Add(new LookupParameter("Evaluated Moves", null, "EvaluatedMoves"));
resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
leftSelector.CopySelected = new BoolValue(true);
leftSelector.NumberOfSelectedSubScopesParameter.ActualName = SampleSizeParameter.Name;
mutationProcessor.Parallel = new BoolValue(true);
mutator.Name = "(Mutator)";
mutator.OperatorParameter.ActualName = MutatorParameter.Name;
evaluator.Name = "(Evaluator)";
evaluator.OperatorParameter.ActualName = "Evaluator";
evaluatedMovesCounter.Name = "EvaluatedMoves++";
evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
evaluatedMovesCounter.Increment = new IntValue(1);
selector.Name = "Selector (placeholder)";
selector.OperatorParameter.ActualName = SelectorParameter.Name;
qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
qualityComparator.RightSideParameter.ActualName = "BestQuality";
qualityComparator.ResultParameter.ActualName = "IsBetter";
improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
bestQualityUpdater.Name = "Update BestQuality";
bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
analyzer2.Name = "Analyzer (placeholder)";
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(false);
resultsCollector2.CollectedValues.Add(new LookupParameter("Evaluated Moves", null, "EvaluatedMoves"));
resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
iterationsTermination.Name = "Iterations Termination Condition";
iterationsTermination.ConditionParameter.ActualName = "Terminate";
#endregion
#region Create operator graph
OperatorGraph.InitialOperator = variableCreator;
variableCreator.Successor = subScopesProcessor0;
subScopesProcessor0.Operators.Add(bestQualityInitializer);
subScopesProcessor0.Successor = resultsCollector1;
bestQualityInitializer.Successor = analyzer1;
analyzer1.Successor = null;
resultsCollector1.Successor = leftSelector;
leftSelector.Successor = mainProcessor;
mainProcessor.Operators.Add(new EmptyOperator());
mainProcessor.Operators.Add(mutationProcessor);
mainProcessor.Successor = rightReducer2;
mutationProcessor.Operator = mutator;
mutationProcessor.Successor = selector;
mutator.Successor = evaluator;
evaluator.Successor = evaluatedMovesCounter;
evaluatedMovesCounter.Successor = null;
selector.Successor = moveMakingProcessor;
moveMakingProcessor.Operators.Add(new EmptyOperator());
moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
moveMakingProcessor.Successor = rightReducer;
selectedMoveMakingProcessor.Operator = qualityComparator;
selectedMoveMakingProcessor.Successor = null;
qualityComparator.Successor = improvesQualityBranch;
improvesQualityBranch.TrueBranch = bestQualityUpdater;
improvesQualityBranch.FalseBranch = null;
improvesQualityBranch.Successor = analyzer2;
bestQualityUpdater.Successor = null;
analyzer2.Successor = null;
rightReducer.Successor = null;
rightReducer2.Successor = iterationsCounter;
iterationsCounter.Successor = iterationsComparator;
iterationsComparator.Successor = resultsCollector2;
resultsCollector2.Successor = iterationsTermination;
iterationsTermination.TrueBranch = null;
iterationsTermination.FalseBranch = leftSelector;
#endregion
}
public override IOperation Apply() {
return base.Apply();
}
}
}