Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5142 was 4807, checked in by abeham, 14 years ago

#1244

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