Free cookie consent management tool by TermsFeed Policy Generator

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

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

#893

  • prevented exception in the algorithms when an operator parameter from the problem is missing
File size: 13.2 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    }
[3621]70    public ValueLookupParameter<IOperator> MoveAnalyzerParameter {
71      get { return (ValueLookupParameter<IOperator>)Parameters["MoveAnalyzer"]; }
[3134]72    }
[3621]73    public ValueLookupParameter<IOperator> AnalyzerParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3134]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
[3636]106      Parameters.Add(new ValueLookupParameter<IOperator>("MoveAnalyzer", "The operator used to analyze the moves."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
[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();
[3636]113      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
114      Assigner bestQualityInitializer = new Assigner();
115      Placeholder analyzer1 = new Placeholder();
[3621]116      ResultsCollector resultsCollector1 = new ResultsCollector();
[3679]117      ResultsCollector resultsCollector2 = new ResultsCollector();
[3636]118      SubScopesProcessor mainProcessor = new SubScopesProcessor();
[3078]119      Placeholder moveGenerator = new Placeholder();
[3193]120      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3078]121      Placeholder moveEvaluator = new Placeholder();
[3636]122      IntCounter evaluatedMovesCounter = new IntCounter();
[3621]123      Placeholder moveAnalyzer = new Placeholder();
[3096]124      BestSelector bestSelector = new BestSelector();
[3078]125      RightReducer rightReducer = new RightReducer();
[3636]126      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
[3078]127      QualityComparator qualityComparator = new QualityComparator();
128      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
129      Placeholder moveMaker = new Placeholder();
[3636]130      Assigner bestQualityUpdater = new Assigner();
[3078]131      SubScopesRemover subScopesRemover = new SubScopesRemover();
132      IntCounter iterationsCounter = new IntCounter();
133      Comparator iterationsComparator = new Comparator();
[3636]134      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
135      Placeholder analyzer2 = new Placeholder();
[3679]136      ResultsCollector resultsCollector3 = new ResultsCollector();
[3078]137      ConditionalBranch iterationsTermination = new ConditionalBranch();
138
139      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
[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
[3621]169      moveAnalyzer.Name = "MoveAnalyzer (placeholder)";
170      moveAnalyzer.OperatorParameter.ActualName = MoveAnalyzerParameter.Name;
171
[3096]172      bestSelector.CopySelected = new BoolValue(false);
[3143]173      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
[3096]174      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
[3143]175      bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
[3078]176
[3143]177      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
178      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
[3078]179      qualityComparator.ResultParameter.ActualName = "IsBetter";
180
181      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
182
183      moveMaker.Name = "MoveMaker (placeholder)";
[3143]184      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3078]185
[3636]186      bestQualityUpdater.Name = "Update BestQuality";
187      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
188      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.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
[3636]202      analyzer2.Name = "Analyzer (placeholder)";
203      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
204
[3679]205      resultsCollector3.CopyValue = new BoolValue(true);
206      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
207      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
[3134]208
[3078]209      iterationsTermination.Name = "Iterations Termination Condition";
[3143]210      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3078]211      #endregion
212
213      #region Create operator graph
214      OperatorGraph.InitialOperator = variableCreator;
[3636]215      variableCreator.Successor = subScopesProcessor0;
216      subScopesProcessor0.Operators.Add(bestQualityInitializer);
217      subScopesProcessor0.Successor = resultsCollector1;
218      bestQualityInitializer.Successor = analyzer1;
[3621]219      analyzer1.Successor = null;
[3679]220      resultsCollector1.Successor = resultsCollector2;
221      resultsCollector2.Successor = mainProcessor;
[3636]222      mainProcessor.Operators.Add(moveGenerator);
[3078]223      mainProcessor.Successor = iterationsCounter;
224      moveGenerator.Successor = moveEvaluationProcessor;
225      moveEvaluationProcessor.Operator = moveEvaluator;
[3621]226      moveEvaluationProcessor.Successor = moveAnalyzer;
[3636]227      moveEvaluator.Successor = evaluatedMovesCounter;
228      evaluatedMovesCounter.Successor = null;
[3621]229      moveAnalyzer.Successor = bestSelector;
[3096]230      bestSelector.Successor = rightReducer;
[3078]231      rightReducer.Successor = moveMakingProcessor;
[3636]232      moveMakingProcessor.Operators.Add(qualityComparator);
[3078]233      moveMakingProcessor.Successor = subScopesRemover;
[3134]234      subScopesRemover.Successor = null;
[3078]235      qualityComparator.Successor = improvesQualityBranch;
236      improvesQualityBranch.TrueBranch = moveMaker;
[3134]237      improvesQualityBranch.FalseBranch = null;
238      improvesQualityBranch.Successor = null;
[3636]239      moveMaker.Successor = bestQualityUpdater;
240      bestQualityUpdater.Successor = null;
[3078]241      iterationsCounter.Successor = iterationsComparator;
[3636]242      iterationsComparator.Successor = subScopesProcessor1;
243      subScopesProcessor1.Operators.Add(analyzer2);
[3679]244      subScopesProcessor1.Successor = resultsCollector3;
[3621]245      analyzer2.Successor = null;
[3679]246      resultsCollector3.Successor = iterationsTermination;
[3143]247      iterationsTermination.TrueBranch = null;
248      iterationsTermination.FalseBranch = mainProcessor;
[3078]249      #endregion
250    }
[3715]251
252    public override IOperation Apply() {
253      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
254        return null;
255      return base.Apply();
256    }
[3078]257  }
258}
Note: See TracBrowser for help on using the repository browser.