Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSearchMainLoop.cs @ 4905

Last change on this file since 4905 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 15.3 KB
RevLine 
[3044]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;
[4722]23using HeuristicLab.Common;
[3044]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
[3226]27using HeuristicLab.Optimization.Operators;
[3044]28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
[3100]32namespace HeuristicLab.Algorithms.TabuSearch {
[3044]33  /// <summary>
34  /// An operator which represents a tabu search.
35  /// </summary>
[3100]36  [Item("TabuSearchMainLoop", "An operator which represents the main loop of a tabu search.")]
[3044]37  [StorableClass]
[3141]38  public sealed class TabuSearchMainLoop : AlgorithmOperator {
[3044]39    #region Parameter properties
40    public ValueLookupParameter<IRandom> RandomParameter {
41      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
42    }
[3048]43    public ValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
[3044]45    }
[3074]46    public LookupParameter<DoubleValue> QualityParameter {
47      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
[3044]48    }
[3141]49    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
51    }
[3074]52    public LookupParameter<DoubleValue> MoveQualityParameter {
53      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
54    }
[3104]55    public LookupParameter<BoolValue> MoveTabuParameter {
56      get { return (LookupParameter<BoolValue>)Parameters["MoveTabu"]; }
57    }
[3048]58    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
59      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
[3044]60    }
[3048]61    public ValueLookupParameter<IntValue> TabuTenureParameter {
62      get { return (ValueLookupParameter<IntValue>)Parameters["TabuTenure"]; }
[3044]63    }
64    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
65      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
66    }
[3074]67    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
68      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
[3044]69    }
[3074]70    public ValueLookupParameter<IOperator> MoveMakerParameter {
71      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
[3044]72    }
[3340]73    public ValueLookupParameter<IOperator> TabuCheckerParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["TabuChecker"]; }
[3044]75    }
[3340]76    public ValueLookupParameter<IOperator> TabuMakerParameter {
77      get { return (ValueLookupParameter<IOperator>)Parameters["TabuMaker"]; }
[3044]78    }
[3636]79    public ValueLookupParameter<IOperator> AnalyzerParameter {
80      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3044]81    }
[3141]82    public ValueLookupParameter<VariableCollection> ResultsParameter {
83      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
84    }
[3044]85    #endregion
86
[3145]87    [StorableConstructor]
[4722]88    private TabuSearchMainLoop(bool deserializing) : base(deserializing) { }
[3100]89    public TabuSearchMainLoop()
[3044]90      : base() {
[3145]91      Initialize();
92    }
[4722]93    private TabuSearchMainLoop(TabuSearchMainLoop original, Cloner cloner)
94      : base(original, cloner) {
95    }
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new TabuSearchMainLoop(this, cloner);
98    }
[3145]99
100    private void Initialize() {
[3044]101      #region Create parameters
102      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
[3048]103      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
[3074]104      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3141]105      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
[3074]106      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
[3104]107      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The value that indicates if a move is tabu or not."));
[3048]108      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
109      Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The length of the tabu list, and also means the number of iterations a move is kept tabu"));
[3145]110
[3044]111      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
[3074]113      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
[3340]114      Parameters.Add(new ValueLookupParameter<IOperator>("TabuChecker", "The operator that checks whether a move is tabu."));
115      Parameters.Add(new ValueLookupParameter<IOperator>("TabuMaker", "The operator that declares a move tabu."));
[3044]116
[3809]117      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
[3141]118      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
[3044]119      #endregion
120
121      #region Create operators
122      VariableCreator variableCreator = new VariableCreator();
[3636]123      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
124      Assigner bestQualityInitializer = new Assigner();
125      Placeholder analyzer1 = new Placeholder();
126      ResultsCollector resultsCollector1 = new ResultsCollector();
[3679]127      ResultsCollector resultsCollector2 = new ResultsCollector();
[3521]128      SubScopesProcessor solutionProcessor = new SubScopesProcessor();
[3044]129      Placeholder moveGenerator = new Placeholder();
[3193]130      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3074]131      Placeholder moveEvaluator = new Placeholder();
[3636]132      IntCounter evaluatedMovesCounter = new IntCounter();
[3340]133      Placeholder tabuChecker = new Placeholder();
[3044]134      SubScopesSorter moveQualitySorter = new SubScopesSorter();
135      TabuSelector tabuSelector = new TabuSelector();
[3195]136      ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
[3521]137      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
[3809]138      UniformSubScopesProcessor selectedMoveMakingProcesor = new UniformSubScopesProcessor();
[3340]139      Placeholder tabuMaker = new Placeholder();
[3044]140      Placeholder moveMaker = new Placeholder();
[3809]141      MergingReducer mergingReducer = new MergingReducer();
142      Placeholder analyzer2 = new Placeholder();
[3521]143      SubScopesRemover subScopesRemover = new SubScopesRemover();
[3195]144      ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
[3636]145      BestQualityMemorizer bestQualityUpdater = new BestQualityMemorizer();
[3044]146      IntCounter iterationsCounter = new IntCounter();
147      Comparator iterationsComparator = new Comparator();
[3679]148      ResultsCollector resultsCollector3 = new ResultsCollector();
[3044]149      ConditionalBranch iterationsTermination = new ConditionalBranch();
150
[3750]151      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class TabuSearch expects this to be called Iterations
[3636]152      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
[3195]153      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
[3636]154      variableCreator.CollectedValues.Add(new ValueParameter<ItemList<IItem>>("TabuList", new ItemList<IItem>()));
155      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
[3044]156
[3636]157      bestQualityInitializer.Name = "Initialize BestQuality";
158      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
159      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
[3141]160
[3636]161      analyzer1.Name = "Analyzer (placeholder)";
162      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3679]163
164      resultsCollector1.CopyValue = new BoolValue(false);
[3636]165      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
166      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
167      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3044]168
[3679]169      resultsCollector2.CopyValue = new BoolValue(true);
170      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
171      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
172
[3044]173      moveGenerator.Name = "MoveGenerator (placeholder)";
[3141]174      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
[3044]175
[3074]176      moveEvaluator.Name = "MoveEvaluator (placeholder)";
[3141]177      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
[3044]178
[3636]179      evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
180      evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
181      evaluatedMovesCounter.Increment = new IntValue(1);
182
[3340]183      tabuChecker.Name = "TabuChecker (placeholder)";
184      tabuChecker.OperatorParameter.ActualName = TabuCheckerParameter.Name;
[3044]185
[3141]186      moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
187      moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
[3044]188
[3521]189      tabuSelector.AspirationParameter.Value = new BoolValue(true);
190      tabuSelector.BestQualityParameter.ActualName = "BestQuality";
191      tabuSelector.CopySelected = new BoolValue(false);
192      tabuSelector.EmptyNeighborhoodParameter.ActualName = "EmptyNeighborhood";
193      tabuSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
194      tabuSelector.MoveQualityParameter.ActualName = MoveQualityParameter.Name;
195      tabuSelector.MoveTabuParameter.ActualName = MoveTabuParameter.Name;
[4068]196
[3193]197      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
[3044]198
[3195]199      emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
200      emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
201
[3340]202      tabuMaker.Name = "TabuMaker (placeholder)";
203      tabuMaker.OperatorParameter.ActualName = TabuMakerParameter.Name;
[3044]204
205      moveMaker.Name = "MoveMaker (placeholder)";
[3141]206      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3044]207
[3809]208      analyzer2.Name = "Analyzer (placeholder)";
209      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
210
[3521]211      subScopesRemover.RemoveAllSubScopes = true;
[3044]212
[3636]213      bestQualityUpdater.Name = "Update BestQuality";
214      bestQualityUpdater.MaximizationParameter.ActualName = MaximizationParameter.Name;
215      bestQualityUpdater.QualityParameter.ActualName = QualityParameter.Name;
216      bestQualityUpdater.BestQualityParameter.ActualName = "BestQuality";
217
[3044]218      iterationsCounter.Name = "Iterations Counter";
[3048]219      iterationsCounter.Increment = new IntValue(1);
[3044]220      iterationsCounter.ValueParameter.ActualName = "Iterations";
221
[3143]222      iterationsComparator.Name = "Iterations >= MaximumIterations";
223      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
[3044]224      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
[3141]225      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
[3143]226      iterationsComparator.ResultParameter.ActualName = "Terminate";
[3044]227
[3679]228      resultsCollector3.CopyValue = new BoolValue(true);
229      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
230      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
231
[3521]232      emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
233      emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
234
[3044]235      iterationsTermination.Name = "Iterations Termination Condition";
[3143]236      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3044]237      #endregion
238
239      #region Create operator graph
[3636]240      OperatorGraph.InitialOperator = variableCreator;
241      variableCreator.Successor = subScopesProcessor0;
242      subScopesProcessor0.Operators.Add(bestQualityInitializer);
243      subScopesProcessor0.Successor = resultsCollector1;
[3809]244      bestQualityInitializer.Successor = analyzer1;
245      analyzer1.Successor = null;
[3679]246      resultsCollector1.Successor = resultsCollector2;
247      resultsCollector2.Successor = solutionProcessor;
[3521]248      solutionProcessor.Operators.Add(moveGenerator);
249      solutionProcessor.Successor = iterationsCounter;
[3044]250      moveGenerator.Successor = moveEvaluationProcessor;
[3074]251      moveEvaluationProcessor.Operator = moveEvaluator;
[3809]252      moveEvaluationProcessor.Successor = moveQualitySorter;
[3636]253      moveEvaluator.Successor = evaluatedMovesCounter;
254      evaluatedMovesCounter.Successor = tabuChecker;
[3340]255      tabuChecker.Successor = null;
[3636]256      moveQualitySorter.Successor = tabuSelector;
[3195]257      tabuSelector.Successor = emptyNeighborhoodBranch1;
[3809]258      emptyNeighborhoodBranch1.FalseBranch = moveMakingProcessor;
[3195]259      emptyNeighborhoodBranch1.TrueBranch = null;
[3521]260      emptyNeighborhoodBranch1.Successor = subScopesRemover;
[3809]261      moveMakingProcessor.Operators.Add(new EmptyOperator());
262      moveMakingProcessor.Operators.Add(selectedMoveMakingProcesor);
263      moveMakingProcessor.Successor = mergingReducer;
264      selectedMoveMakingProcesor.Operator = tabuMaker;
265      selectedMoveMakingProcesor.Successor = null;
[3340]266      tabuMaker.Successor = moveMaker;
[3141]267      moveMaker.Successor = null;
[3809]268      mergingReducer.Successor = analyzer2;
269      analyzer2.Successor = null;
[3521]270      subScopesRemover.Successor = null;
[3044]271      iterationsCounter.Successor = iterationsComparator;
[3809]272      iterationsComparator.Successor = resultsCollector3;
[3679]273      resultsCollector3.Successor = emptyNeighborhoodBranch2;
[3521]274      emptyNeighborhoodBranch2.TrueBranch = null;
275      emptyNeighborhoodBranch2.FalseBranch = iterationsTermination;
276      emptyNeighborhoodBranch2.Successor = null;
[3143]277      iterationsTermination.TrueBranch = null;
[3521]278      iterationsTermination.FalseBranch = solutionProcessor;
[3044]279      #endregion
280    }
[3715]281
282    public override IOperation Apply() {
283      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null
284        || TabuCheckerParameter.ActualValue == null || TabuMakerParameter.ActualValue == null)
285        return null;
286      return base.Apply();
287    }
[3044]288  }
289}
Note: See TracBrowser for help on using the repository browser.