Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.LocalSearch/3.3/LocalSearchMainLoop.cs @ 5735

Last change on this file since 5735 was 5735, checked in by svonolfe, 13 years ago

Implemented review comments of swagner (#1425)

File size: 12.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace 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    public LookupParameter<IntValue> EvaluatedMovesParameter {
73      get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
74    }
75    #endregion
76
77    [StorableConstructor]
78    private LocalSearchMainLoop(bool deserializing) : base(deserializing) { }
79    public LocalSearchMainLoop(string iterationsName, string bestQualityName)
80      : base() {
81        Initialize(iterationsName, bestQualityName);
82    }
83    public LocalSearchMainLoop()
84      : base() {
85      Initialize("Iterations", "BestQuality");
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(string iterationsName, string bestQualityName) {
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 LookupParameter<DoubleValue>("BestQuality", "The value which represents the best quality fjo."));
100      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
101      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
102      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
103      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
104
105      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
106      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
108
109      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
110      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
111      #endregion
112
113      #region Create operators
114      VariableCreator variableCreator = new VariableCreator();
115      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
116      Assigner bestQualityInitializer = new Assigner();
117      Placeholder analyzer1 = new Placeholder();
118      ResultsCollector resultsCollector1 = new ResultsCollector();
119      SubScopesProcessor mainProcessor = new SubScopesProcessor();
120      Placeholder moveGenerator = new Placeholder();
121      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
122      Placeholder moveEvaluator = new Placeholder();
123      SubScopesCounter subScopesCounter = new SubScopesCounter();
124      BestSelector bestSelector = new BestSelector();
125      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
126      UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
127      QualityComparator qualityComparator = new QualityComparator();
128      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
129      Placeholder moveMaker = new Placeholder();
130      Assigner bestQualityUpdater = new Assigner();
131      ResultsCollector resultsCollector2 = new ResultsCollector();
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      ConditionalBranch iterationsTermination = new ConditionalBranch();
138
139      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(iterationsName, new IntValue(0))); // Class LocalSearch expects this to be called Iterations
140      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(bestQualityName, new DoubleValue(0)));
141
142      bestQualityInitializer.Name = "Initialize BestQuality";
143      bestQualityInitializer.LeftSideParameter.ActualName = bestQualityName;
144      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
145
146      analyzer1.Name = "Analyzer (placeholder)";
147      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
148
149      resultsCollector1.CopyValue = new BoolValue(false);
150      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(iterationsName));
151      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>(bestQualityName, null, bestQualityName));
152      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
153
154      moveGenerator.Name = "MoveGenerator (placeholder)";
155      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
156
157      moveEvaluationProcessor.Parallel = new BoolValue(true);
158
159      moveEvaluator.Name = "MoveEvaluator (placeholder)";
160      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
161
162      subScopesCounter.Name = "Increment EvaluatedMoves";
163      subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
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 = bestQualityName;
181      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
182
183      resultsCollector2.CopyValue = new BoolValue(false);
184      resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>(bestQualityName, null, bestQualityName));
185      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
186
187      analyzer2.Name = "Analyzer (placeholder)";
188      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
189
190      subScopesRemover.RemoveAllSubScopes = true;
191
192      iterationsCounter.Name = "Iterations Counter";
193      iterationsCounter.Increment = new IntValue(1);
194      iterationsCounter.ValueParameter.ActualName = iterationsName;
195
196      iterationsComparator.Name = "Iterations >= MaximumIterations";
197      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
198      iterationsComparator.LeftSideParameter.ActualName = iterationsName;
199      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
200      iterationsComparator.ResultParameter.ActualName = "Terminate";
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 = mainProcessor;
214      mainProcessor.Operators.Add(moveGenerator);
215      mainProcessor.Successor = iterationsCounter;
216      moveGenerator.Successor = moveEvaluationProcessor;
217      moveEvaluationProcessor.Operator = moveEvaluator;
218      moveEvaluationProcessor.Successor = subScopesCounter;
219      moveEvaluator.Successor = null;
220      subScopesCounter.Successor = bestSelector;
221      bestSelector.Successor = moveMakingProcessor;
222      moveMakingProcessor.Operators.Add(new EmptyOperator());
223      moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
224      moveMakingProcessor.Successor = mergingReducer;
225      selectedMoveMakingProcessor.Operator = qualityComparator;
226      qualityComparator.Successor = improvesQualityBranch;
227      improvesQualityBranch.TrueBranch = moveMaker;
228      improvesQualityBranch.FalseBranch = null;
229      improvesQualityBranch.Successor = null;
230      moveMaker.Successor = bestQualityUpdater;
231      bestQualityUpdater.Successor = null;
232      mergingReducer.Successor = analyzer2;
233      analyzer2.Successor = subScopesRemover;
234      subScopesRemover.Successor = null;
235      iterationsCounter.Successor = iterationsComparator;
236      iterationsComparator.Successor = iterationsTermination;
237      iterationsTermination.TrueBranch = null;
238      iterationsTermination.FalseBranch = mainProcessor;
239      #endregion
240    }
241
242    public override IOperation Apply() {
243      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
244        return null;
245      return base.Apply();
246    }
247  }
248}
Note: See TracBrowser for help on using the repository browser.