Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4485 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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