Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4436 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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