Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3121 was 3103, checked in by abeham, 15 years ago

Renamed Algorithms.LS to Algorithms.LocalSearch #921

File size: 9.5 KB
Line 
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;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.LocalSearch {
32  /// <summary>
33  /// An operator which represents a local search.
34  /// </summary>
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).")]
36  [StorableClass]
37  public class LocalSearchMainLoop : AlgorithmOperator {
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    }
48    public LookupParameter<DoubleValue> MoveQualityParameter {
49      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
50    }
51    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
52      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
53    }
54    public ValueLookupParameter<VariableCollection> ResultsParameter {
55      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
56    }
57    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
59    }
60    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
62    }
63    public ValueLookupParameter<IOperator> MoveMakerParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
65    }
66
67    private ScopeParameter CurrentScopeParameter {
68      get { return (ScopeParameter)Parameters["CurrentScope"]; }
69    }
70    public IScope CurrentScope {
71      get { return CurrentScopeParameter.ActualValue; }
72    }
73    #endregion
74
75    public LocalSearchMainLoop()
76      : base() {
77      #region Create parameters
78      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
79      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
80      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
81      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
82      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
83      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
84
85      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
86      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
87      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
88     
89      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
90      #endregion
91
92      #region Create operators
93      VariableCreator variableCreator = new VariableCreator();
94      ResultsCollector resultsCollector = new ResultsCollector();
95      UniformSequentialSubScopesProcessor mainProcessor = new UniformSequentialSubScopesProcessor();
96      Placeholder moveGenerator = new Placeholder();
97      UniformSequentialSubScopesProcessor moveEvaluationProcessor = new UniformSequentialSubScopesProcessor();
98      Placeholder moveEvaluator = new Placeholder();
99      BestSelector bestSelector = new BestSelector();
100      RightReducer rightReducer = new RightReducer();
101      UniformSequentialSubScopesProcessor moveMakingProcessor = new UniformSequentialSubScopesProcessor();
102      QualityComparator qualityComparator = new QualityComparator();
103      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
104      Placeholder moveMaker = new Placeholder();
105      SubScopesRemover subScopesRemover = new SubScopesRemover();
106      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
107      IntCounter iterationsCounter = new IntCounter();
108      Comparator iterationsComparator = new Comparator();
109      ConditionalBranch iterationsTermination = new ConditionalBranch();
110      EmptyOperator finished = new EmptyOperator();
111
112      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
113      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
114
115      mainProcessor.Name = "Solution processor (UniformSequentialSubScopesProcessor)";
116
117      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
118      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
119      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
120      resultsCollector.ResultsParameter.ActualName = "Results";
121
122      moveGenerator.Name = "MoveGenerator (placeholder)";
123      moveGenerator.OperatorParameter.ActualName = "MoveGenerator";
124
125      moveEvaluator.Name = "MoveEvaluator (placeholder)";
126      moveEvaluator.OperatorParameter.ActualName = "MoveEvaluator";
127
128      bestSelector.CopySelected = new BoolValue(false);
129      bestSelector.MaximizationParameter.ActualName = "Maximization";
130      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
131      bestSelector.QualityParameter.ActualName = "MoveQuality";
132
133      moveMakingProcessor.Name = "MoveMaking processor (UniformSequentialSubScopesProcessor)";
134
135      qualityComparator.LeftSideParameter.ActualName = "MoveQuality";
136      qualityComparator.RightSideParameter.ActualName = "Quality";
137      qualityComparator.ResultParameter.ActualName = "IsBetter";
138
139      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
140
141      moveMaker.Name = "MoveMaker (placeholder)";
142      moveMaker.OperatorParameter.ActualName = "MoveMaker";
143
144      subScopesRemover.RemoveAllSubScopes = true;
145
146      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
147      valuesCollector.DataTableParameter.ActualName = "Qualities";
148
149      iterationsCounter.Name = "Iterations Counter";
150      iterationsCounter.Increment = new IntValue(1);
151      iterationsCounter.ValueParameter.ActualName = "Iterations";
152
153      iterationsComparator.Name = "Iterations Comparator";
154      iterationsComparator.Comparison = new Comparison(ComparisonType.Less);
155      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
156      iterationsComparator.RightSideParameter.ActualName = "MaximumIterations";
157      iterationsComparator.ResultParameter.ActualName = "IterationsCondition";
158
159      iterationsTermination.Name = "Iterations Termination Condition";
160      iterationsTermination.ConditionParameter.ActualName = "IterationsCondition";
161      #endregion
162
163      #region Create operator graph
164      OperatorGraph.InitialOperator = variableCreator;
165      variableCreator.Successor = mainProcessor;
166      mainProcessor.Operator = resultsCollector;
167      mainProcessor.Successor = iterationsCounter;
168      resultsCollector.Successor = moveGenerator;
169      moveGenerator.Successor = moveEvaluationProcessor;
170      moveEvaluationProcessor.Operator = moveEvaluator;
171      moveEvaluationProcessor.Successor = bestSelector;
172      bestSelector.Successor = rightReducer;
173      rightReducer.Successor = moveMakingProcessor;
174      moveMakingProcessor.Operator = qualityComparator;
175      moveMakingProcessor.Successor = subScopesRemover;
176      subScopesRemover.Successor = valuesCollector;
177      qualityComparator.Successor = improvesQualityBranch;
178      improvesQualityBranch.TrueBranch = moveMaker;
179      iterationsCounter.Successor = iterationsComparator;
180      iterationsComparator.Successor = iterationsTermination;
181      iterationsTermination.TrueBranch = mainProcessor;
182      iterationsTermination.FalseBranch = finished;
183      #endregion
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.