Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.LocalSearch/3.3/LocalSearchMainLoop.cs @ 4044

Last change on this file since 4044 was 3809, checked in by abeham, 15 years ago

#893

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