Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3494 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 15.0 KB
RevLine 
[3078]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;
[3376]23using HeuristicLab.Common;
[3078]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
[3103]32namespace HeuristicLab.Algorithms.LocalSearch {
[3078]33  /// <summary>
[3103]34  /// An operator which represents a local search.
[3078]35  /// </summary>
[3103]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).")]
[3078]37  [StorableClass]
[3145]38  public sealed class LocalSearchMainLoop : AlgorithmOperator {
[3078]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    }
[3134]49    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
51    }
[3078]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    }
[3134]70    public ValueLookupParameter<IOperator> VisualizerParameter {
71      get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
72    }
73    public LookupParameter<IItem> VisualizationParameter {
74      get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
75    }
[3078]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
[3145]85    [StorableConstructor]
86    private LocalSearchMainLoop(bool deserializing) : base() { }
[3103]87    public LocalSearchMainLoop()
[3078]88      : base() {
[3145]89      Initialize();
90    }
91
92    private void Initialize() {
[3078]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."));
[3134]97      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
[3078]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."));
[3134]105
106      Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
107      Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
[3078]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();
[3134]113      BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
114      BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
115      DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
116      QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
117      Placeholder visualizer1 = new Placeholder();
[3078]118      ResultsCollector resultsCollector = new ResultsCollector();
[3193]119      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
[3078]120      Placeholder moveGenerator = new Placeholder();
[3193]121      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3078]122      Placeholder moveEvaluator = new Placeholder();
[3096]123      BestSelector bestSelector = new BestSelector();
[3078]124      RightReducer rightReducer = new RightReducer();
[3193]125      UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor();
[3078]126      QualityComparator qualityComparator = new QualityComparator();
127      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
128      Placeholder moveMaker = new Placeholder();
129      SubScopesRemover subScopesRemover = new SubScopesRemover();
130      IntCounter iterationsCounter = new IntCounter();
131      Comparator iterationsComparator = new Comparator();
[3134]132      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
133      BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
134      DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
135      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
136      Placeholder visualizer2 = new Placeholder();
[3078]137      ConditionalBranch iterationsTermination = new ConditionalBranch();
138
139      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
140
[3134]141      bestQualityMemorizer1.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
142      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
143      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
[3078]144
[3134]145      bestQualityMemorizer2.BestQualityParameter.ActualName = "BestQuality";
146      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
147      bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
148
149      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
150      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
151      dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
152
153      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
154      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
155      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
156      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
157
158      visualizer1.Name = "Visualizer (placeholder)";
159      visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
160
[3078]161      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
[3134]162      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
163      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
164      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
165      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
166      resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
[3078]167      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
[3134]168      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
[3078]169
[3193]170      mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)";
[3134]171
[3078]172      moveGenerator.Name = "MoveGenerator (placeholder)";
[3143]173      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
[3078]174
175      moveEvaluator.Name = "MoveEvaluator (placeholder)";
[3143]176      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
[3078]177
[3096]178      bestSelector.CopySelected = new BoolValue(false);
[3143]179      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
[3096]180      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
[3143]181      bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
[3078]182
[3193]183      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
[3078]184
[3143]185      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
186      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
[3078]187      qualityComparator.ResultParameter.ActualName = "IsBetter";
188
189      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
190
191      moveMaker.Name = "MoveMaker (placeholder)";
[3143]192      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3078]193
194      subScopesRemover.RemoveAllSubScopes = true;
195
196      iterationsCounter.Name = "Iterations Counter";
197      iterationsCounter.Increment = new IntValue(1);
198      iterationsCounter.ValueParameter.ActualName = "Iterations";
199
[3143]200      iterationsComparator.Name = "Iterations >= MaximumIterations";
201      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
[3078]202      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
[3143]203      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
204      iterationsComparator.ResultParameter.ActualName = "Terminate";
[3078]205
[3134]206      bestQualityMemorizer3.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
207      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
208      bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
209
210      bestQualityMemorizer4.BestQualityParameter.ActualName = "BestQuality";
211      bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
212      bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
213
214      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
215      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
216      dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
217
218      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
219      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
220      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
221      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
222
223      visualizer2.Name = "Visualizer (placeholder)";
224      visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
225
[3078]226      iterationsTermination.Name = "Iterations Termination Condition";
[3143]227      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3078]228      #endregion
229
230      #region Create operator graph
231      OperatorGraph.InitialOperator = variableCreator;
[3134]232      variableCreator.Successor = bestQualityMemorizer1;
233      bestQualityMemorizer1.Successor = bestQualityMemorizer2;
234      bestQualityMemorizer2.Successor = dataTableValuesCollector1;
235      dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
236      qualityDifferenceCalculator1.Successor = visualizer1;
237      visualizer1.Successor = resultsCollector;
238      resultsCollector.Successor = mainProcessor;
239      mainProcessor.Operator = moveGenerator;
[3078]240      mainProcessor.Successor = iterationsCounter;
241      moveGenerator.Successor = moveEvaluationProcessor;
242      moveEvaluationProcessor.Operator = moveEvaluator;
[3096]243      moveEvaluationProcessor.Successor = bestSelector;
244      bestSelector.Successor = rightReducer;
[3078]245      rightReducer.Successor = moveMakingProcessor;
246      moveMakingProcessor.Operator = qualityComparator;
247      moveMakingProcessor.Successor = subScopesRemover;
[3134]248      subScopesRemover.Successor = null;
[3078]249      qualityComparator.Successor = improvesQualityBranch;
250      improvesQualityBranch.TrueBranch = moveMaker;
[3134]251      improvesQualityBranch.FalseBranch = null;
252      improvesQualityBranch.Successor = null;
253      moveMaker.Successor = null;
[3078]254      iterationsCounter.Successor = iterationsComparator;
[3134]255      iterationsComparator.Successor = bestQualityMemorizer3;
256      bestQualityMemorizer3.Successor = bestQualityMemorizer4;
257      bestQualityMemorizer4.Successor = dataTableValuesCollector2;
258      dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
259      qualityDifferenceCalculator2.Successor = visualizer2;
260      visualizer2.Successor = iterationsTermination;
[3143]261      iterationsTermination.TrueBranch = null;
262      iterationsTermination.FalseBranch = mainProcessor;
[3078]263      #endregion
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.