[7128] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Selection;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator which represents a local search.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [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).")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public class LocalAnalysisMainLoop : AlgorithmOperator {
|
---|
| 39 |
|
---|
| 40 | #region Parameter properties
|
---|
| 41 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 42 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 43 | }
|
---|
| 44 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 45 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 46 | }
|
---|
| 47 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 48 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 49 | }
|
---|
| 50 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
| 51 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 52 | }
|
---|
| 53 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 54 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 55 | }
|
---|
| 56 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 57 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 58 | }
|
---|
| 59 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 60 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 61 | }
|
---|
| 62 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 63 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 64 | }
|
---|
| 65 | public ValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
| 66 | get { return (ValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 67 | }
|
---|
| 68 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 69 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 70 | }
|
---|
| 71 | #endregion
|
---|
| 72 |
|
---|
| 73 | [StorableConstructor]
|
---|
| 74 | protected LocalAnalysisMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 75 | protected LocalAnalysisMainLoop(LocalAnalysisMainLoop original, Cloner cloner) : base(original, cloner) { }
|
---|
| 76 | public LocalAnalysisMainLoop()
|
---|
| 77 | : base() {
|
---|
| 78 | Initialize();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 82 | return new LocalAnalysisMainLoop(this, cloner);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private void Initialize() {
|
---|
| 86 | #region Create parameters
|
---|
| 87 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 88 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 89 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 90 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 91 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
| 92 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 93 |
|
---|
| 94 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "Mutation Operator."));
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator that selects how to continue."));
|
---|
| 96 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "Number of mutation samples."));
|
---|
| 97 |
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
| 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | #region Create operators
|
---|
| 102 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 103 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
| 104 | Assigner bestQualityInitializer = new Assigner();
|
---|
| 105 | Placeholder analyzer1 = new Placeholder();
|
---|
| 106 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 107 | LeftSelector leftSelector = new LeftSelector();
|
---|
| 108 | SubScopesProcessor mainProcessor = new SubScopesProcessor();
|
---|
| 109 | UniformSubScopesProcessor mutationProcessor = new UniformSubScopesProcessor();
|
---|
| 110 | Placeholder mutator = new Placeholder();
|
---|
| 111 | Placeholder evaluator = new Placeholder();
|
---|
| 112 | IntCounter evaluatedMovesCounter = new IntCounter();
|
---|
| 113 | Placeholder selector = new Placeholder();
|
---|
| 114 | SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
|
---|
| 115 | UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
|
---|
| 116 | QualityComparator qualityComparator = new QualityComparator();
|
---|
| 117 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
| 118 | Assigner bestQualityUpdater = new Assigner();
|
---|
| 119 | Placeholder analyzer2 = new Placeholder();
|
---|
| 120 | RightReducer rightReducer = new RightReducer();
|
---|
| 121 | RightReducer rightReducer2 = new RightReducer();
|
---|
| 122 | IntCounter iterationsCounter = new IntCounter();
|
---|
| 123 | Comparator iterationsComparator = new Comparator();
|
---|
| 124 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
| 125 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
| 126 |
|
---|
| 127 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
| 128 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
|
---|
| 129 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
|
---|
| 130 |
|
---|
| 131 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
| 132 | bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 133 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.ActualName;
|
---|
| 134 |
|
---|
| 135 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 136 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 137 |
|
---|
| 138 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
| 139 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
| 140 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
| 141 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
| 142 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 143 |
|
---|
| 144 | leftSelector.CopySelected = new BoolValue(true);
|
---|
| 145 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = SampleSizeParameter.Name;
|
---|
| 146 |
|
---|
| 147 | mutationProcessor.Parallel = new BoolValue(true);
|
---|
| 148 |
|
---|
| 149 | mutator.Name = "(Mutator)";
|
---|
| 150 | mutator.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 151 |
|
---|
| 152 | evaluator.Name = "(Evaluator)";
|
---|
| 153 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
| 154 |
|
---|
| 155 | evaluatedMovesCounter.Name = "EvaluatedMoves++";
|
---|
| 156 | evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
|
---|
| 157 | evaluatedMovesCounter.Increment = new IntValue(1);
|
---|
| 158 |
|
---|
| 159 | selector.Name = "Selector (placeholder)";
|
---|
| 160 | selector.OperatorParameter.ActualName = SelectorParameter.Name;
|
---|
| 161 |
|
---|
| 162 | qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
|
---|
| 163 | qualityComparator.RightSideParameter.ActualName = "BestQuality";
|
---|
| 164 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
| 165 |
|
---|
| 166 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
| 167 |
|
---|
| 168 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
| 169 | bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 170 | bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 171 |
|
---|
| 172 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 173 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 174 |
|
---|
| 175 | iterationsCounter.Name = "Iterations Counter";
|
---|
| 176 | iterationsCounter.Increment = new IntValue(1);
|
---|
| 177 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
| 178 |
|
---|
| 179 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
| 180 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 181 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
| 182 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 183 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
| 184 |
|
---|
| 185 | resultsCollector2.CopyValue = new BoolValue(false);
|
---|
| 186 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
| 187 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 188 |
|
---|
| 189 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
| 190 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
| 191 | #endregion
|
---|
| 192 |
|
---|
| 193 | #region Create operator graph
|
---|
| 194 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 195 | variableCreator.Successor = subScopesProcessor0;
|
---|
| 196 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
| 197 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
| 198 | bestQualityInitializer.Successor = analyzer1;
|
---|
| 199 | analyzer1.Successor = null;
|
---|
| 200 | resultsCollector1.Successor = leftSelector;
|
---|
| 201 | leftSelector.Successor = mainProcessor;
|
---|
| 202 | mainProcessor.Operators.Add(new EmptyOperator());
|
---|
| 203 | mainProcessor.Operators.Add(mutationProcessor);
|
---|
| 204 | mainProcessor.Successor = rightReducer2;
|
---|
| 205 | mutationProcessor.Operator = mutator;
|
---|
| 206 | mutationProcessor.Successor = selector;
|
---|
| 207 | mutator.Successor = evaluator;
|
---|
| 208 | evaluator.Successor = evaluatedMovesCounter;
|
---|
| 209 | evaluatedMovesCounter.Successor = null;
|
---|
| 210 | selector.Successor = moveMakingProcessor;
|
---|
| 211 | moveMakingProcessor.Operators.Add(new EmptyOperator());
|
---|
| 212 | moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
|
---|
| 213 | moveMakingProcessor.Successor = rightReducer;
|
---|
| 214 | selectedMoveMakingProcessor.Operator = qualityComparator;
|
---|
| 215 | selectedMoveMakingProcessor.Successor = null;
|
---|
| 216 | qualityComparator.Successor = improvesQualityBranch;
|
---|
| 217 | improvesQualityBranch.TrueBranch = bestQualityUpdater;
|
---|
| 218 | improvesQualityBranch.FalseBranch = null;
|
---|
| 219 | improvesQualityBranch.Successor = analyzer2;
|
---|
| 220 | bestQualityUpdater.Successor = null;
|
---|
| 221 | analyzer2.Successor = null;
|
---|
| 222 | rightReducer.Successor = null;
|
---|
| 223 | rightReducer2.Successor = iterationsCounter;
|
---|
| 224 | iterationsCounter.Successor = iterationsComparator;
|
---|
| 225 | iterationsComparator.Successor = resultsCollector2;
|
---|
| 226 | resultsCollector2.Successor = iterationsTermination;
|
---|
| 227 | iterationsTermination.TrueBranch = null;
|
---|
| 228 | iterationsTermination.FalseBranch = leftSelector;
|
---|
| 229 | #endregion
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | public override IOperation Apply() {
|
---|
| 233 | return base.Apply();
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|