Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Algorithms.VariableNeighborhoodSearch/3.3/VariableNeighborhoodSearchMainLoop.cs @ 6452

Last change on this file since 6452 was 5753, checked in by svonolfe, 14 years ago

Integrated VNS into trunk (#1425)

File size: 13.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Selection;
34using HeuristicLab.Optimization;
35
36namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
37  /// <summary>
38  /// An operator which represents a variable neighborhood search.
39  /// </summary>
40  [Item("VariableNeighborhoodSearchMainLoop", "An operator which represents the main loop of a variable neighborhood search.")]
41  [StorableClass]
42  public sealed class VariableNeighborhoodSearchMainLoop : AlgorithmOperator {
43    #region Parameter properties
44    public ValueLookupParameter<IRandom> RandomParameter {
45      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
46    }
47    public ValueLookupParameter<BoolValue> MaximizationParameter {
48      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
49    }
50    public LookupParameter<DoubleValue> QualityParameter {
51      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
54      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
55    }
56    public ValueLookupParameter<IOperator> EvaluatorParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
58    }
59    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
60      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
61    }
62    public ValueLookupParameter<VariableCollection> ResultsParameter {
63      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
64    }
65    public ValueLookupParameter<IOperator> AnalyzerParameter {
66      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
67    }
68    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
69      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
70    }
71    public ValueLookupParameter<ILocalImprovementOperator> LocalImprovementParameter {
72      get { return (ValueLookupParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
73    }
74    public ValueLookupParameter<IShakingOperator> ShakingParameter {
75      get { return (ValueLookupParameter<IShakingOperator>)Parameters["Shaking"]; }
76    }
77    #endregion
78
79    [StorableConstructor]
80    private VariableNeighborhoodSearchMainLoop(bool deserializing) : base(deserializing) { }
81    public VariableNeighborhoodSearchMainLoop()
82      : base() {
83        Initialize();
84    }
85    private VariableNeighborhoodSearchMainLoop(VariableNeighborhoodSearchMainLoop original, Cloner cloner)
86      : base(original, cloner) {
87    }
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new VariableNeighborhoodSearchMainLoop(this, cloner);
90    }
91
92    private void Initialize() {
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."));
97      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
98      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
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>("Analyzer", "The operator used to analyze the solution."));
103      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
104      Parameters.Add(new ValueLookupParameter<ILocalImprovementOperator>("LocalImprovement", "The local improvement operation."));
105      Parameters.Add(new ValueLookupParameter<IShakingOperator>("Shaking", "The shaking operation."));
106      #endregion
107
108      #region Create operators
109      VariableCreator variableCreator = new VariableCreator();
110      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
111      Assigner bestQualityInitializer = new Assigner();
112      Placeholder analyzer1 = new Placeholder();
113      ResultsCollector resultsCollector1 = new ResultsCollector();
114
115      CombinedOperator iteration = new CombinedOperator();
116      Assigner iterationInit = new Assigner();
117
118      SubScopesCloner createChild = new SubScopesCloner();
119      SubScopesProcessor childProcessor = new SubScopesProcessor();
120
121      Assigner qualityAssigner = new Assigner();
122      Placeholder shaking = new Placeholder();
123      Placeholder localImprovement = new Placeholder();
124      Placeholder evaluator = new Placeholder();
125      IntCounter evalCounter = new IntCounter();
126
127      QualityComparator qualityComparator = new QualityComparator();
128      ConditionalBranch improvesQualityBranch1 = new ConditionalBranch();
129      ConditionalBranch improvesQualityBranch2 = new ConditionalBranch();
130
131      Assigner bestQualityUpdater = new Assigner();
132
133      BestSelector bestSelector = new BestSelector();
134      RightReducer rightReducer = new RightReducer();
135
136      IntCounter indexCounter = new IntCounter();
137      Assigner indexResetter = new Assigner();
138
139      Placeholder analyzer2 = new Placeholder();
140
141      ConditionalBranch indexTermination = new ConditionalBranch();
142
143      IntCounter iterationsCounter = new IntCounter();
144      Comparator iterationsComparator = new Comparator();
145      ConditionalBranch iterationsTermination = new ConditionalBranch();
146
147      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
148      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Index", new IntValue(0)));
149      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("Continue", new BoolValue(false)));
150      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("IsBetter", new BoolValue(false)));
151      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
152
153      bestQualityInitializer.Name = "Initialize BestQuality";
154      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
155      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
156
157      analyzer1.Name = "Analyzer (placeholder)";
158      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
159
160      resultsCollector1.CopyValue = new BoolValue(false);
161      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
162      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
163      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
164
165      iteration.Name = "Iteration";
166
167      iterationInit.Name = "Init iteration";
168      iterationInit.LeftSideParameter.ActualName = "Index";
169      iterationInit.RightSideParameter.Value = new IntValue(0);
170
171      createChild.Name = "Clone solution";
172
173      qualityAssigner.Name = "Assign quality";
174      qualityAssigner.LeftSideParameter.ActualName = "OriginalQuality";
175      qualityAssigner.RightSideParameter.ActualName = QualityParameter.Name;
176
177      shaking.Name = "Shaking operator (placeholder)";
178      shaking.OperatorParameter.ActualName = ShakingParameter.Name;
179
180      localImprovement.Name = "Local improvement operator (placeholder)";
181      localImprovement.OperatorParameter.ActualName = LocalImprovementParameter.Name;
182
183      evaluator.Name = "Evaluation operator (placeholder)";
184      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
185
186      evalCounter.Name = "Count evaluations";
187      evalCounter.Increment.Value = 1;
188      evalCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
189
190      qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
191      qualityComparator.RightSideParameter.ActualName = "OriginalQuality";
192      qualityComparator.ResultParameter.ActualName = "IsBetter";
193
194      improvesQualityBranch1.ConditionParameter.ActualName = "IsBetter";
195      improvesQualityBranch2.ConditionParameter.ActualName = "IsBetter";
196
197      bestQualityUpdater.Name = "Update BestQuality";
198      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
199      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
200
201      bestSelector.CopySelected = new BoolValue(false);
202      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
203      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
204      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
205
206      indexCounter.Name = "Count index";
207      indexCounter.Increment.Value = 1;
208      indexCounter.ValueParameter.ActualName = "Index";
209
210      indexResetter.Name = "Reset index";
211      indexResetter.LeftSideParameter.ActualName = "Index";
212      indexResetter.RightSideParameter.Value = new IntValue(0);
213
214      analyzer2.Name = "Analyzer (placeholder)";
215      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
216
217      iterationsCounter.Name = "Iterations Counter";
218      iterationsCounter.Increment = new IntValue(1);
219      iterationsCounter.ValueParameter.ActualName = "Iterations";
220
221      iterationsComparator.Name = "Iterations >= MaximumIterations";
222      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
223      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
224      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
225      iterationsComparator.ResultParameter.ActualName = "Terminate";
226
227      iterationsTermination.Name = "Iterations Termination Condition";
228      iterationsTermination.ConditionParameter.ActualName = "Terminate";
229
230      indexTermination.Name = "Index Termination Condition";
231      indexTermination.ConditionParameter.ActualName = "Continue";
232      #endregion
233
234      #region Create operator graph
235      OperatorGraph.InitialOperator = variableCreator;
236      variableCreator.Successor = subScopesProcessor0;
237      subScopesProcessor0.Operators.Add(bestQualityInitializer);
238      subScopesProcessor0.Successor = analyzer1;
239      analyzer1.Successor = resultsCollector1;
240      /////////
241      resultsCollector1.Successor = iteration;
242
243      iteration.OperatorGraph.InitialOperator = iterationInit;
244      iteration.Successor = iterationsCounter;
245      iterationInit.Successor = createChild;
246
247      createChild.Successor = childProcessor;
248      childProcessor.Operators.Add(new EmptyOperator());
249      childProcessor.Operators.Add(qualityAssigner);
250      childProcessor.Successor = bestSelector;
251      /////////
252      qualityAssigner.Successor = shaking;
253      shaking.Successor = evaluator;
254      evaluator.Successor = evalCounter;
255      evalCounter.Successor = localImprovement;
256      localImprovement.Successor = qualityComparator;
257      qualityComparator.Successor = improvesQualityBranch1;
258      improvesQualityBranch1.TrueBranch = bestQualityUpdater;
259      improvesQualityBranch1.FalseBranch = indexCounter;
260
261      bestQualityUpdater.Successor = indexResetter;
262      indexResetter.Successor = null;
263
264      indexCounter.Successor = null;
265      /////////
266      bestSelector.Successor = rightReducer;
267      rightReducer.Successor = analyzer2;
268      analyzer2.Successor = indexTermination;
269      indexTermination.TrueBranch = improvesQualityBranch2;
270      indexTermination.FalseBranch = null;
271
272      improvesQualityBranch2.TrueBranch = null;
273      improvesQualityBranch2.FalseBranch = createChild;
274
275      iterationsCounter.Successor = iterationsComparator;
276      iterationsComparator.Successor = iterationsTermination;
277      iterationsTermination.TrueBranch = null;
278      iterationsTermination.FalseBranch = iteration;
279      #endregion
280    }
281
282    public override IOperation Apply() {
283      if (LocalImprovementParameter.ActualValue == null || EvaluatorParameter.ActualValue == null)
284        return null;
285      return base.Apply();
286    }
287  }
288}
Note: See TracBrowser for help on using the repository browser.