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.Analysis;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.LocalSearch {
|
---|
32 | /// <summary>
|
---|
33 | /// An operator which represents a local search.
|
---|
34 | /// </summary>
|
---|
35 | [Item("LocalSearchMainLoop", "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).")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class LocalSearchMainLoop : AlgorithmOperator {
|
---|
38 | #region Parameter properties
|
---|
39 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
40 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
41 | }
|
---|
42 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
43 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
44 | }
|
---|
45 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
46 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
47 | }
|
---|
48 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
49 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
50 | }
|
---|
51 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
52 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
53 | }
|
---|
54 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
55 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
56 | }
|
---|
57 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
58 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
59 | }
|
---|
60 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
61 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
62 | }
|
---|
63 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
64 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
65 | }
|
---|
66 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
67 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
68 | }
|
---|
69 | public ValueLookupParameter<IOperator> VisualizerParameter {
|
---|
70 | get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
|
---|
71 | }
|
---|
72 | public LookupParameter<IItem> VisualizationParameter {
|
---|
73 | get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private ScopeParameter CurrentScopeParameter {
|
---|
77 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
78 | }
|
---|
79 | public IScope CurrentScope {
|
---|
80 | get { return CurrentScopeParameter.ActualValue; }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | private LocalSearchMainLoop(bool deserializing) : base() { }
|
---|
86 | public LocalSearchMainLoop()
|
---|
87 | : base() {
|
---|
88 | Initialize();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void Initialize() {
|
---|
92 | #region Create parameters
|
---|
93 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
94 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
95 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
96 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
97 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
98 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
99 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
100 |
|
---|
101 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
102 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
104 |
|
---|
105 | Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
|
---|
106 | Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
|
---|
107 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | #region Create operators
|
---|
111 | VariableCreator variableCreator = new VariableCreator();
|
---|
112 | BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
|
---|
113 | BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
|
---|
114 | DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
|
---|
115 | QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
|
---|
116 | Placeholder visualizer1 = new Placeholder();
|
---|
117 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
118 | UniformSequentialSubScopesProcessor mainProcessor = new UniformSequentialSubScopesProcessor();
|
---|
119 | Placeholder moveGenerator = new Placeholder();
|
---|
120 | UniformSequentialSubScopesProcessor moveEvaluationProcessor = new UniformSequentialSubScopesProcessor();
|
---|
121 | Placeholder moveEvaluator = new Placeholder();
|
---|
122 | BestSelector bestSelector = new BestSelector();
|
---|
123 | RightReducer rightReducer = new RightReducer();
|
---|
124 | UniformSequentialSubScopesProcessor moveMakingProcessor = new UniformSequentialSubScopesProcessor();
|
---|
125 | QualityComparator qualityComparator = new QualityComparator();
|
---|
126 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
127 | Placeholder moveMaker = new Placeholder();
|
---|
128 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
129 | IntCounter iterationsCounter = new IntCounter();
|
---|
130 | Comparator iterationsComparator = new Comparator();
|
---|
131 | BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
|
---|
132 | BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
|
---|
133 | DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
|
---|
134 | QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
|
---|
135 | Placeholder visualizer2 = new Placeholder();
|
---|
136 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
137 |
|
---|
138 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
139 |
|
---|
140 | bestQualityMemorizer1.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
141 | bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
142 | bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
|
---|
143 |
|
---|
144 | bestQualityMemorizer2.BestQualityParameter.ActualName = "BestQuality";
|
---|
145 | bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
146 | bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
|
---|
147 |
|
---|
148 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
|
---|
149 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
|
---|
150 | dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
|
---|
151 |
|
---|
152 | qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
153 | qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
154 | qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
155 | qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
|
---|
156 |
|
---|
157 | visualizer1.Name = "Visualizer (placeholder)";
|
---|
158 | visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
|
---|
159 |
|
---|
160 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
161 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
162 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
|
---|
163 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
|
---|
164 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
|
---|
165 | resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
|
---|
166 | resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
|
---|
167 | resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
168 |
|
---|
169 | mainProcessor.Name = "Solution processor (UniformSequentialSubScopesProcessor)";
|
---|
170 |
|
---|
171 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
172 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
173 |
|
---|
174 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
175 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
176 |
|
---|
177 | bestSelector.CopySelected = new BoolValue(false);
|
---|
178 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
179 | bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
180 | bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
181 |
|
---|
182 | moveMakingProcessor.Name = "MoveMaking processor (UniformSequentialSubScopesProcessor)";
|
---|
183 |
|
---|
184 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
185 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
186 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
187 |
|
---|
188 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
189 |
|
---|
190 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
191 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
192 |
|
---|
193 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
194 |
|
---|
195 | iterationsCounter.Name = "Iterations Counter";
|
---|
196 | iterationsCounter.Increment = new IntValue(1);
|
---|
197 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
198 |
|
---|
199 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
200 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
201 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
202 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
203 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
204 |
|
---|
205 | bestQualityMemorizer3.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
206 | bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
207 | bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
|
---|
208 |
|
---|
209 | bestQualityMemorizer4.BestQualityParameter.ActualName = "BestQuality";
|
---|
210 | bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
211 | bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
|
---|
212 |
|
---|
213 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
|
---|
214 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
|
---|
215 | dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
|
---|
216 |
|
---|
217 | qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
218 | qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
219 | qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
220 | qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
|
---|
221 |
|
---|
222 | visualizer2.Name = "Visualizer (placeholder)";
|
---|
223 | visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
|
---|
224 |
|
---|
225 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
226 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
227 | #endregion
|
---|
228 |
|
---|
229 | #region Create operator graph
|
---|
230 | OperatorGraph.InitialOperator = variableCreator;
|
---|
231 | variableCreator.Successor = bestQualityMemorizer1;
|
---|
232 | bestQualityMemorizer1.Successor = bestQualityMemorizer2;
|
---|
233 | bestQualityMemorizer2.Successor = dataTableValuesCollector1;
|
---|
234 | dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
|
---|
235 | qualityDifferenceCalculator1.Successor = visualizer1;
|
---|
236 | visualizer1.Successor = resultsCollector;
|
---|
237 | resultsCollector.Successor = mainProcessor;
|
---|
238 | mainProcessor.Operator = moveGenerator;
|
---|
239 | mainProcessor.Successor = iterationsCounter;
|
---|
240 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
241 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
242 | moveEvaluationProcessor.Successor = bestSelector;
|
---|
243 | bestSelector.Successor = rightReducer;
|
---|
244 | rightReducer.Successor = moveMakingProcessor;
|
---|
245 | moveMakingProcessor.Operator = qualityComparator;
|
---|
246 | moveMakingProcessor.Successor = subScopesRemover;
|
---|
247 | subScopesRemover.Successor = null;
|
---|
248 | qualityComparator.Successor = improvesQualityBranch;
|
---|
249 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
250 | improvesQualityBranch.FalseBranch = null;
|
---|
251 | improvesQualityBranch.Successor = null;
|
---|
252 | moveMaker.Successor = null;
|
---|
253 | iterationsCounter.Successor = iterationsComparator;
|
---|
254 | iterationsComparator.Successor = bestQualityMemorizer3;
|
---|
255 | bestQualityMemorizer3.Successor = bestQualityMemorizer4;
|
---|
256 | bestQualityMemorizer4.Successor = dataTableValuesCollector2;
|
---|
257 | dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
|
---|
258 | qualityDifferenceCalculator2.Successor = visualizer2;
|
---|
259 | visualizer2.Successor = iterationsTermination;
|
---|
260 | iterationsTermination.TrueBranch = null;
|
---|
261 | iterationsTermination.FalseBranch = mainProcessor;
|
---|
262 | #endregion
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|