Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.VariableNeighborhoodSearch/3.3/VariableNeighborhoodSearchMainLoop.cs @ 5603

Last change on this file since 5603 was 5603, checked in by svonolfe, 13 years ago

Added first version of VNS main loop (WIP) #1425

File size: 12.8 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;
34
35namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
36  /// <summary>
37  /// An operator which represents a variable neighborhood search.
38  /// </summary>
39  [Item("VariableNeighborhoodSearchMainLoop", "An operator which represents the main loop of a variable neighborhood search.")]
40  [StorableClass]
41  public sealed class VariableNeighborhoodSearchMainLoop : AlgorithmOperator {
42    #region Parameter properties
43    public ValueLookupParameter<IRandom> RandomParameter {
44      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
45    }
46    public ValueLookupParameter<BoolValue> MaximizationParameter {
47      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
48    }
49    public LookupParameter<DoubleValue> QualityParameter {
50      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
53      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
54    }
55    public ValueLookupParameter<IOperator> EvaluatorParameter {
56      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
57    }
58    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
59      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
60    }
61    public ValueLookupParameter<VariableCollection> ResultsParameter {
62      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
63    }
64    public ValueLookupParameter<IOperator> AnalyzerParameter {
65      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
66    }
67    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
68      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
69    }
70    public ValueLookupParameter<ILocalImprovement> LocalImprovementParameter {
71      get { return (ValueLookupParameter<ILocalImprovement>)Parameters["LocalImprovement"]; }
72    }
73    public ValueLookupParameter<ShakingOperator> ShakingParameter {
74      get { return (ValueLookupParameter<ShakingOperator>)Parameters["Shaking"]; }
75    }
76    #endregion
77
78    [StorableConstructor]
79    private VariableNeighborhoodSearchMainLoop(bool deserializing) : base(deserializing) { }
80    public VariableNeighborhoodSearchMainLoop()
81      : base() {
82        Initialize();
83    }
84    private VariableNeighborhoodSearchMainLoop(VariableNeighborhoodSearchMainLoop original, Cloner cloner)
85      : base(original, cloner) {
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new VariableNeighborhoodSearchMainLoop(this, cloner);
89    }
90
91    private void Initialize() {
92      #region Create parameters
93      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
94      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
95      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
96      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
97      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."));
98      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
99      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
100
101      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
102      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
103      Parameters.Add(new ValueLookupParameter<ILocalImprovement>("LocalImprovement", "The local improvement operation."));
104      Parameters.Add(new ValueLookupParameter<ShakingOperator>("Shaking", "The shaking operation."));
105      #endregion
106
107      #region Create operators
108      VariableCreator variableCreator = new VariableCreator();
109      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
110      Assigner bestQualityInitializer = new Assigner();
111      Placeholder analyzer1 = new Placeholder();
112      ResultsCollector resultsCollector1 = new ResultsCollector();
113
114      CombinedOperator iteration = new CombinedOperator();
115      Assigner iterationInit = new Assigner();
116
117      ChildrenCreator createChild = new ChildrenCreator();
118      SubScopesProcessor childProcessor = new SubScopesProcessor();
119      ParentCopyCrossover parentCloner = new ParentCopyCrossover();
120
121      Assigner qualityAssigner = new Assigner();
122      Placeholder shaking = new Placeholder();
123      Placeholder localImprovement = new Placeholder();
124      Placeholder evaluator = new Placeholder();
125
126      QualityComparator qualityComparator = new QualityComparator();
127      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
128
129      Assigner bestQualityUpdater = new Assigner();
130      ScopeCleaner cleaner = new ScopeCleaner();
131      ParentCopyCrossover originalRestorer = new ParentCopyCrossover();
132
133      IntCounter indexCounter = new IntCounter();
134      Assigner indexResetter = new Assigner();
135
136      SubScopesRemover remover = new SubScopesRemover();
137      Placeholder analyzer2 = new Placeholder();
138
139      ConditionalBranch indexTermination = new ConditionalBranch();
140
141      IntCounter iterationsCounter = new IntCounter();
142      Comparator iterationsComparator = new Comparator();
143      ConditionalBranch iterationsTermination = new ConditionalBranch();
144
145      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
146      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Index", new DoubleValue(0)));
147      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
148
149      bestQualityInitializer.Name = "Initialize BestQuality";
150      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
151      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
152
153      analyzer1.Name = "Analyzer (placeholder)";
154      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
155
156      resultsCollector1.CopyValue = new BoolValue(false);
157      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
158      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
159      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
160
161      iteration.Name = "Iteration";
162
163      iterationInit.Name = "Init iteration";
164      iterationInit.LeftSideParameter.ActualName = "Index";
165      iterationInit.RightSideParameter.Value = new IntValue(0);
166
167      createChild.Name = "Create child";
168      createChild.ParentsPerChild.Value = 1;
169
170      parentCloner.Name = "Copy parent";
171
172      qualityAssigner.Name = "Assign quality";
173      qualityAssigner.LeftSideParameter.ActualName = "OriginalQuality";
174      qualityAssigner.RightSideParameter.ActualName = QualityParameter.Name;
175
176      shaking.Name = "Shaking operator (placeholder)";
177      shaking.OperatorParameter.ActualName = ShakingParameter.Name;
178
179      localImprovement.Name = "Local improvement operator (placeholder)";
180      localImprovement.OperatorParameter.ActualName = LocalImprovementParameter.Name;
181
182      evaluator.Name = "Evaluation operator (placeholder)";
183      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
184
185      qualityComparator.LeftSideParameter.ActualName = "OriginalQuality";
186      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
187      qualityComparator.ResultParameter.ActualName = "IsBetter";
188
189      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
190
191      bestQualityUpdater.Name = "Update BestQuality";
192      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
193      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
194
195      cleaner.Name = "Clean scope";
196      originalRestorer.Name = "Restore original solution";
197
198      indexCounter.Name = "Count index";
199      indexCounter.Increment.Value = 1;
200      indexCounter.ValueParameter.ActualName = "Index";
201
202      indexResetter.Name = "Reset index";
203      indexResetter.LeftSideParameter.ActualName = "Index";
204      indexResetter.RightSideParameter.Value = new IntValue(0);
205
206      remover.Name = "Remove subscope";
207
208      analyzer2.Name = "Analyzer (placeholder)";
209      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
210
211      originalRestorer.Name = "Restore parent";
212
213      iterationsCounter.Name = "Iterations Counter";
214      iterationsCounter.Increment = new IntValue(1);
215      iterationsCounter.ValueParameter.ActualName = "Iterations";
216
217      iterationsComparator.Name = "Iterations >= MaximumIterations";
218      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
219      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
220      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
221      iterationsComparator.ResultParameter.ActualName = "Terminate";
222
223      iterationsTermination.Name = "Iterations Termination Condition";
224      iterationsTermination.ConditionParameter.ActualName = "Terminate";
225
226      indexTermination.Name = "Index Termination Condition";
227      indexTermination.ConditionParameter.ActualName = "Continue";
228      #endregion
229
230      #region Create operator graph
231      OperatorGraph.InitialOperator = variableCreator;
232      variableCreator.Successor = subScopesProcessor0;
233      subScopesProcessor0.Operators.Add(bestQualityInitializer);
234      subScopesProcessor0.Successor = resultsCollector1;
235      //////////
236      bestQualityInitializer.Successor = analyzer1;
237      /////////
238      resultsCollector1.Successor = iteration;
239      iteration.OperatorGraph.InitialOperator = iterationInit;
240      iteration.Successor = iterationsCounter;
241      iterationInit.Successor = createChild;
242
243      createChild.Successor = childProcessor;
244      childProcessor.Operators.Add(parentCloner);
245      childProcessor.Successor = indexTermination;
246      /////////
247      parentCloner.Successor = qualityAssigner;
248      qualityAssigner.Successor = shaking;
249      shaking.Successor = localImprovement;
250      localImprovement.Successor = evaluator;
251      evaluator.Successor = qualityComparator;
252      qualityComparator.Successor = improvesQualityBranch;
253      improvesQualityBranch.TrueBranch = bestQualityUpdater;
254      improvesQualityBranch.FalseBranch = cleaner;
255      improvesQualityBranch.Successor = remover;
256
257      bestQualityUpdater.Successor = indexResetter;
258
259      cleaner.Successor = originalRestorer;
260      originalRestorer.Successor = indexCounter;
261      /////////
262      indexTermination.TrueBranch = null;
263      indexTermination.FalseBranch = iterationInit;
264
265      iterationsCounter.Successor = iterationsComparator;
266      iterationsComparator.Successor = iterationsTermination;
267      iterationsTermination.TrueBranch = null;
268      iterationsTermination.FalseBranch = iteration;
269      #endregion
270    }
271
272    public override IOperation Apply() {
273      if (LocalImprovementParameter.ActualValue == null || EvaluatorParameter.ActualValue == null)
274        return null;
275      return base.Apply();
276    }
277  }
278}
Note: See TracBrowser for help on using the repository browser.