Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3629 was 3621, checked in by abeham, 15 years ago

Updated local search to make use of Analyzers #999

File size: 11.4 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> MoveAnalyzerParameter {
71      get { return (ValueLookupParameter<IOperator>)Parameters["MoveAnalyzer"]; }
72    }
73    public ValueLookupParameter<IOperator> AnalyzerParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
75    }
76
77    private ScopeParameter CurrentScopeParameter {
78      get { return (ScopeParameter)Parameters["CurrentScope"]; }
79    }
80    public IScope CurrentScope {
81      get { return CurrentScopeParameter.ActualValue; }
82    }
83    #endregion
84
85    [StorableConstructor]
86    private LocalSearchMainLoop(bool deserializing) : base() { }
87    public LocalSearchMainLoop()
88      : base() {
89      Initialize();
90    }
91
92    private void Initialize() {
93      #region Create parameters
94      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
95      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
96      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
97      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
98      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
99      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
100      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
101
102      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
104      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
105
106      Parameters.Add(new ValueLookupParameter<IOperator>("MoveAnalyzer", "The operator used to analyze the moves in each iteration."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each iteration."));
108      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
109      #endregion
110
111      #region Create operators
112      VariableCreator variableCreator = new VariableCreator();
113      ResultsCollector resultsCollector1 = new ResultsCollector();
114      UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor();
115      Placeholder analyzer1 = new Placeholder();
116      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
117      Placeholder moveGenerator = new Placeholder();
118      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
119      Placeholder moveEvaluator = new Placeholder();
120      Placeholder moveAnalyzer = new Placeholder();
121      BestSelector bestSelector = new BestSelector();
122      RightReducer rightReducer = new RightReducer();
123      UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor();
124      QualityComparator qualityComparator = new QualityComparator();
125      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
126      Placeholder moveMaker = new Placeholder();
127      SubScopesRemover subScopesRemover = new SubScopesRemover();
128      IntCounter iterationsCounter = new IntCounter();
129      Comparator iterationsComparator = new Comparator();
130      ResultsCollector resultsCollector2 = new ResultsCollector();
131      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
132      Placeholder analyzer2 = new Placeholder();
133      ConditionalBranch iterationsTermination = new ConditionalBranch();
134
135      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
136
137      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
138      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
139
140      analyzer1.Name = "Analyzer (placeholder)";
141      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
142
143      mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)";
144
145      moveGenerator.Name = "MoveGenerator (placeholder)";
146      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
147
148      moveEvaluator.Name = "MoveEvaluator (placeholder)";
149      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
150
151      moveAnalyzer.Name = "MoveAnalyzer (placeholder)";
152      moveAnalyzer.OperatorParameter.ActualName = MoveAnalyzerParameter.Name;
153
154      bestSelector.CopySelected = new BoolValue(false);
155      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
156      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
157      bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
158
159      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
160
161      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
162      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
163      qualityComparator.ResultParameter.ActualName = "IsBetter";
164
165      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
166
167      moveMaker.Name = "MoveMaker (placeholder)";
168      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
169
170      subScopesRemover.RemoveAllSubScopes = true;
171
172      iterationsCounter.Name = "Iterations Counter";
173      iterationsCounter.Increment = new IntValue(1);
174      iterationsCounter.ValueParameter.ActualName = "Iterations";
175
176      iterationsComparator.Name = "Iterations >= MaximumIterations";
177      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
178      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
179      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
180      iterationsComparator.ResultParameter.ActualName = "Terminate";
181
182      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
183      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
184
185      analyzer2.Name = "Analyzer (placeholder)";
186      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
187
188      iterationsTermination.Name = "Iterations Termination Condition";
189      iterationsTermination.ConditionParameter.ActualName = "Terminate";
190      #endregion
191
192      #region Create operator graph
193      OperatorGraph.InitialOperator = variableCreator;
194      variableCreator.Successor = resultsCollector1;
195      resultsCollector1.Successor = uniformSubScopesProcessor0;
196      uniformSubScopesProcessor0.Operator = analyzer1;
197      uniformSubScopesProcessor0.Successor = mainProcessor;
198      analyzer1.Successor = null;
199      mainProcessor.Operator = moveGenerator;
200      mainProcessor.Successor = iterationsCounter;
201      moveGenerator.Successor = moveEvaluationProcessor;
202      moveEvaluationProcessor.Operator = moveEvaluator;
203      moveEvaluationProcessor.Successor = moveAnalyzer;
204      moveAnalyzer.Successor = bestSelector;
205      bestSelector.Successor = rightReducer;
206      rightReducer.Successor = moveMakingProcessor;
207      moveMakingProcessor.Operator = qualityComparator;
208      moveMakingProcessor.Successor = subScopesRemover;
209      subScopesRemover.Successor = null;
210      qualityComparator.Successor = improvesQualityBranch;
211      improvesQualityBranch.TrueBranch = moveMaker;
212      improvesQualityBranch.FalseBranch = null;
213      improvesQualityBranch.Successor = null;
214      moveMaker.Successor = null;
215      iterationsCounter.Successor = iterationsComparator;
216      iterationsComparator.Successor = resultsCollector2;
217      resultsCollector2.Successor = uniformSubScopesProcessor1;
218      uniformSubScopesProcessor1.Operator = analyzer2;
219      uniformSubScopesProcessor1.Successor = iterationsTermination;
220      analyzer2.Successor = null;
221      iterationsTermination.TrueBranch = null;
222      iterationsTermination.FalseBranch = mainProcessor;
223      #endregion
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.