Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5445 was 5445, checked in by swagner, 13 years ago

Updated year of copyrights (#1406)

File size: 12.1 KB
RevLine 
[3078]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3078]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    }
[5356]72    public LookupParameter<IntValue> EvaluatedMovesParameter {
73      get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
[3078]74    }
75    #endregion
76
[3145]77    [StorableConstructor]
[4722]78    private LocalSearchMainLoop(bool deserializing) : base(deserializing) { }
[3103]79    public LocalSearchMainLoop()
[3078]80      : base() {
[3145]81      Initialize();
82    }
[4722]83    private LocalSearchMainLoop(LocalSearchMainLoop original, Cloner cloner)
84      : base(original, cloner) {
85    }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new LocalSearchMainLoop(this, cloner);
88    }
[3145]89
90    private void Initialize() {
[3078]91      #region Create parameters
92      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
93      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
94      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3134]95      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
[3078]96      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
97      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
98      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
99
100      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
101      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
[3134]103
[3809]104      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
[5356]105      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
[3078]106      #endregion
107
108      #region Create operators
109      VariableCreator variableCreator = new VariableCreator();
[3636]110      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
111      Assigner bestQualityInitializer = new Assigner();
112      Placeholder analyzer1 = new Placeholder();
[3621]113      ResultsCollector resultsCollector1 = new ResultsCollector();
[3636]114      SubScopesProcessor mainProcessor = new SubScopesProcessor();
[3078]115      Placeholder moveGenerator = new Placeholder();
[3193]116      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3078]117      Placeholder moveEvaluator = new Placeholder();
[5353]118      SubScopesCounter subScopesCounter = new SubScopesCounter();
[3096]119      BestSelector bestSelector = new BestSelector();
[3636]120      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
[3809]121      UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
[3078]122      QualityComparator qualityComparator = new QualityComparator();
123      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
124      Placeholder moveMaker = new Placeholder();
[3636]125      Assigner bestQualityUpdater = new Assigner();
[3809]126      MergingReducer mergingReducer = new MergingReducer();
127      Placeholder analyzer2 = new Placeholder();
[3078]128      SubScopesRemover subScopesRemover = new SubScopesRemover();
129      IntCounter iterationsCounter = new IntCounter();
130      Comparator iterationsComparator = new Comparator();
131      ConditionalBranch iterationsTermination = new ConditionalBranch();
132
[3750]133      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class LocalSearch expects this to be called Iterations
[3636]134      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
[3078]135
[3636]136      bestQualityInitializer.Name = "Initialize BestQuality";
137      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
138      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
[3078]139
[3621]140      analyzer1.Name = "Analyzer (placeholder)";
141      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3134]142
[3679]143      resultsCollector1.CopyValue = new BoolValue(false);
[3636]144      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
145      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
146      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3134]147
[3078]148      moveGenerator.Name = "MoveGenerator (placeholder)";
[3143]149      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
[3078]150
[5353]151      moveEvaluationProcessor.Parallel = new BoolValue(true);
152
[3078]153      moveEvaluator.Name = "MoveEvaluator (placeholder)";
[3143]154      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
[3078]155
[5353]156      subScopesCounter.Name = "Increment EvaluatedMoves";
[5356]157      subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
[3636]158
[3096]159      bestSelector.CopySelected = new BoolValue(false);
[3143]160      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
[3096]161      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
[3143]162      bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
[3078]163
[3143]164      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
165      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
[3078]166      qualityComparator.ResultParameter.ActualName = "IsBetter";
167
168      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
169
170      moveMaker.Name = "MoveMaker (placeholder)";
[3143]171      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3078]172
[3636]173      bestQualityUpdater.Name = "Update BestQuality";
174      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
175      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
176
[3809]177      analyzer2.Name = "Analyzer (placeholder)";
178      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
179
[3078]180      subScopesRemover.RemoveAllSubScopes = true;
181
182      iterationsCounter.Name = "Iterations Counter";
183      iterationsCounter.Increment = new IntValue(1);
184      iterationsCounter.ValueParameter.ActualName = "Iterations";
185
[3143]186      iterationsComparator.Name = "Iterations >= MaximumIterations";
187      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
[3078]188      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
[3143]189      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
190      iterationsComparator.ResultParameter.ActualName = "Terminate";
[3078]191
192      iterationsTermination.Name = "Iterations Termination Condition";
[3143]193      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3078]194      #endregion
195
196      #region Create operator graph
197      OperatorGraph.InitialOperator = variableCreator;
[3636]198      variableCreator.Successor = subScopesProcessor0;
199      subScopesProcessor0.Operators.Add(bestQualityInitializer);
200      subScopesProcessor0.Successor = resultsCollector1;
201      bestQualityInitializer.Successor = analyzer1;
[3621]202      analyzer1.Successor = null;
[5353]203      resultsCollector1.Successor = mainProcessor;
[3636]204      mainProcessor.Operators.Add(moveGenerator);
[3078]205      mainProcessor.Successor = iterationsCounter;
206      moveGenerator.Successor = moveEvaluationProcessor;
207      moveEvaluationProcessor.Operator = moveEvaluator;
[5353]208      moveEvaluationProcessor.Successor = subScopesCounter;
209      moveEvaluator.Successor = null;
210      subScopesCounter.Successor = bestSelector;
[3809]211      bestSelector.Successor = moveMakingProcessor;
212      moveMakingProcessor.Operators.Add(new EmptyOperator());
213      moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
214      moveMakingProcessor.Successor = mergingReducer;
215      selectedMoveMakingProcessor.Operator = qualityComparator;
[3078]216      qualityComparator.Successor = improvesQualityBranch;
217      improvesQualityBranch.TrueBranch = moveMaker;
[3134]218      improvesQualityBranch.FalseBranch = null;
219      improvesQualityBranch.Successor = null;
[3636]220      moveMaker.Successor = bestQualityUpdater;
221      bestQualityUpdater.Successor = null;
[3809]222      mergingReducer.Successor = analyzer2;
223      analyzer2.Successor = subScopesRemover;
224      subScopesRemover.Successor = null;
[3078]225      iterationsCounter.Successor = iterationsComparator;
[5353]226      iterationsComparator.Successor = iterationsTermination;
[3143]227      iterationsTermination.TrueBranch = null;
228      iterationsTermination.FalseBranch = mainProcessor;
[3078]229      #endregion
230    }
[3715]231
232    public override IOperation Apply() {
233      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
234        return null;
235      return base.Apply();
236    }
[3078]237  }
238}
Note: See TracBrowser for help on using the repository browser.