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.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> AnalyzerParameter {
|
---|
70 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private ScopeParameter CurrentScopeParameter {
|
---|
74 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
75 | }
|
---|
76 | public IScope CurrentScope {
|
---|
77 | get { return CurrentScopeParameter.ActualValue; }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | private LocalSearchMainLoop(bool deserializing) : base(deserializing) { }
|
---|
83 | public LocalSearchMainLoop()
|
---|
84 | : base() {
|
---|
85 | Initialize();
|
---|
86 | }
|
---|
87 | private LocalSearchMainLoop(LocalSearchMainLoop original, Cloner cloner)
|
---|
88 | : base(original, cloner) {
|
---|
89 | }
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new LocalSearchMainLoop(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void Initialize() {
|
---|
95 | #region Create parameters
|
---|
96 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
97 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
98 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
99 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
101 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
102 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
103 |
|
---|
104 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
107 |
|
---|
108 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
109 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Create operators
|
---|
113 | VariableCreator variableCreator = new VariableCreator();
|
---|
114 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
115 | Assigner bestQualityInitializer = new Assigner();
|
---|
116 | Placeholder analyzer1 = new Placeholder();
|
---|
117 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
118 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
119 | SubScopesProcessor mainProcessor = new SubScopesProcessor();
|
---|
120 | Placeholder moveGenerator = new Placeholder();
|
---|
121 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
122 | Placeholder moveEvaluator = new Placeholder();
|
---|
123 | IntCounter evaluatedMovesCounter = new IntCounter();
|
---|
124 | Placeholder moveAnalyzer = new Placeholder();
|
---|
125 | BestSelector bestSelector = new BestSelector();
|
---|
126 | SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
|
---|
127 | UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
|
---|
128 | QualityComparator qualityComparator = new QualityComparator();
|
---|
129 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
130 | Placeholder moveMaker = new Placeholder();
|
---|
131 | Assigner bestQualityUpdater = new Assigner();
|
---|
132 | MergingReducer mergingReducer = new MergingReducer();
|
---|
133 | Placeholder analyzer2 = new Placeholder();
|
---|
134 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
135 | IntCounter iterationsCounter = new IntCounter();
|
---|
136 | Comparator iterationsComparator = new Comparator();
|
---|
137 | ResultsCollector resultsCollector3 = new ResultsCollector();
|
---|
138 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
139 |
|
---|
140 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class LocalSearch expects this to be called Iterations
|
---|
141 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
|
---|
142 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
|
---|
143 |
|
---|
144 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
145 | bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
|
---|
146 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
147 |
|
---|
148 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
149 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
150 |
|
---|
151 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
152 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
153 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
154 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
155 |
|
---|
156 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
157 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
158 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
159 |
|
---|
160 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
161 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
162 |
|
---|
163 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
164 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
165 |
|
---|
166 | evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
|
---|
167 | evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
|
---|
168 | evaluatedMovesCounter.Increment = new IntValue(1);
|
---|
169 |
|
---|
170 | bestSelector.CopySelected = new BoolValue(false);
|
---|
171 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
172 | bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
173 | bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
174 |
|
---|
175 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
176 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
177 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
178 |
|
---|
179 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
180 |
|
---|
181 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
182 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
183 |
|
---|
184 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
185 | bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
|
---|
186 | bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
187 |
|
---|
188 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
189 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
190 |
|
---|
191 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
192 |
|
---|
193 | iterationsCounter.Name = "Iterations Counter";
|
---|
194 | iterationsCounter.Increment = new IntValue(1);
|
---|
195 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
196 |
|
---|
197 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
198 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
199 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
200 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
201 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
202 |
|
---|
203 | resultsCollector3.CopyValue = new BoolValue(true);
|
---|
204 | resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
205 | resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
206 |
|
---|
207 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
208 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
209 | #endregion
|
---|
210 |
|
---|
211 | #region Create operator graph
|
---|
212 | OperatorGraph.InitialOperator = variableCreator;
|
---|
213 | variableCreator.Successor = subScopesProcessor0;
|
---|
214 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
215 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
216 | bestQualityInitializer.Successor = analyzer1;
|
---|
217 | analyzer1.Successor = null;
|
---|
218 | resultsCollector1.Successor = resultsCollector2;
|
---|
219 | resultsCollector2.Successor = mainProcessor;
|
---|
220 | mainProcessor.Operators.Add(moveGenerator);
|
---|
221 | mainProcessor.Successor = iterationsCounter;
|
---|
222 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
223 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
224 | moveEvaluationProcessor.Successor = moveAnalyzer;
|
---|
225 | moveEvaluator.Successor = evaluatedMovesCounter;
|
---|
226 | evaluatedMovesCounter.Successor = null;
|
---|
227 | moveAnalyzer.Successor = bestSelector;
|
---|
228 | bestSelector.Successor = moveMakingProcessor;
|
---|
229 | moveMakingProcessor.Operators.Add(new EmptyOperator());
|
---|
230 | moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
|
---|
231 | moveMakingProcessor.Successor = mergingReducer;
|
---|
232 | selectedMoveMakingProcessor.Operator = qualityComparator;
|
---|
233 | qualityComparator.Successor = improvesQualityBranch;
|
---|
234 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
235 | improvesQualityBranch.FalseBranch = null;
|
---|
236 | improvesQualityBranch.Successor = null;
|
---|
237 | moveMaker.Successor = bestQualityUpdater;
|
---|
238 | bestQualityUpdater.Successor = null;
|
---|
239 | mergingReducer.Successor = analyzer2;
|
---|
240 | analyzer2.Successor = subScopesRemover;
|
---|
241 | subScopesRemover.Successor = null;
|
---|
242 | iterationsCounter.Successor = iterationsComparator;
|
---|
243 | iterationsComparator.Successor = resultsCollector3;
|
---|
244 | resultsCollector3.Successor = iterationsTermination;
|
---|
245 | iterationsTermination.TrueBranch = null;
|
---|
246 | iterationsTermination.FalseBranch = mainProcessor;
|
---|
247 | #endregion
|
---|
248 | }
|
---|
249 |
|
---|
250 | public override IOperation Apply() {
|
---|
251 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
|
---|
252 | return null;
|
---|
253 | return base.Apply();
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|