Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on VNS main loop (#1425)

File size: 13.3 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      ConditionalBranch improvesQualityBranch2 = new ConditionalBranch();
129
130      Assigner bestQualityUpdater = new Assigner();
131      ScopeCleaner cleaner = new ScopeCleaner();
132      ParentCopyCrossover originalRestorer = new ParentCopyCrossover();
133
134      IntCounter indexCounter = new IntCounter();
135      Assigner indexResetter = new Assigner();
136
137      SubScopesRemover remover1 = new SubScopesRemover();
138      SubScopesRemover remover2 = new SubScopesRemover();
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<DoubleValue>("Index", new DoubleValue(0)));
149      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("Continue", new BoolValue(false)));
150      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
151
152      bestQualityInitializer.Name = "Initialize BestQuality";
153      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
154      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
155
156      analyzer1.Name = "Analyzer (placeholder)";
157      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
158
159      resultsCollector1.CopyValue = new BoolValue(false);
160      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
161      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
162      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
163
164      iteration.Name = "Iteration";
165
166      iterationInit.Name = "Init iteration";
167      iterationInit.LeftSideParameter.ActualName = "Index";
168      iterationInit.RightSideParameter.Value = new IntValue(0);
169
170      createChild.Name = "Create child";
171      createChild.ParentsPerChild.Value = 1;
172
173      parentCloner.Name = "Copy parent";
174
175      qualityAssigner.Name = "Assign quality";
176      qualityAssigner.LeftSideParameter.ActualName = "OriginalQuality";
177      qualityAssigner.RightSideParameter.ActualName = QualityParameter.Name;
178
179      shaking.Name = "Shaking operator (placeholder)";
180      shaking.OperatorParameter.ActualName = ShakingParameter.Name;
181
182      localImprovement.Name = "Local improvement operator (placeholder)";
183      localImprovement.OperatorParameter.ActualName = LocalImprovementParameter.Name;
184
185      evaluator.Name = "Evaluation operator (placeholder)";
186      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
187
188      qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
189      qualityComparator.RightSideParameter.ActualName = "OriginalQuality";
190      qualityComparator.ResultParameter.ActualName = "IsBetter";
191
192      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
193      improvesQualityBranch2.ConditionParameter.ActualName = "IsBetter";
194
195      bestQualityUpdater.Name = "Update BestQuality";
196      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
197      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
198
199      cleaner.Name = "Clean scope";
200      cleaner.ClearSubScopesParameter.Value = new BoolValue(false);
201      originalRestorer.Name = "Restore original solution";
202
203      indexCounter.Name = "Count index";
204      indexCounter.Increment.Value = 1;
205      indexCounter.ValueParameter.ActualName = "Index";
206
207      indexResetter.Name = "Reset index";
208      indexResetter.LeftSideParameter.ActualName = "Index";
209      indexResetter.RightSideParameter.Value = new IntValue(0);
210
211      remover1.Name = remover2.Name = "Remove subscope";
212
213      analyzer2.Name = "Analyzer (placeholder)";
214      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
215
216      originalRestorer.Name = "Restore parent";
217
218      iterationsCounter.Name = "Iterations Counter";
219      iterationsCounter.Increment = new IntValue(1);
220      iterationsCounter.ValueParameter.ActualName = "Iterations";
221
222      iterationsComparator.Name = "Iterations >= MaximumIterations";
223      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
224      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
225      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
226      iterationsComparator.ResultParameter.ActualName = "Terminate";
227
228      iterationsTermination.Name = "Iterations Termination Condition";
229      iterationsTermination.ConditionParameter.ActualName = "Terminate";
230
231      indexTermination.Name = "Index Termination Condition";
232      indexTermination.ConditionParameter.ActualName = "Continue";
233      #endregion
234
235      #region Create operator graph
236      OperatorGraph.InitialOperator = variableCreator;
237      variableCreator.Successor = subScopesProcessor0;
238      subScopesProcessor0.Operators.Add(bestQualityInitializer);
239      subScopesProcessor0.Successor = resultsCollector1;
240      //////////
241      bestQualityInitializer.Successor = analyzer1;
242      /////////
243      resultsCollector1.Successor = iteration;
244      iteration.OperatorGraph.InitialOperator = iterationInit;
245      iteration.Successor = iterationsCounter;
246      iterationInit.Successor = createChild;
247
248      createChild.Successor = childProcessor;
249      childProcessor.Operators.Add(parentCloner);
250      childProcessor.Successor = indexTermination;
251      /////////
252      parentCloner.Successor = qualityAssigner;
253      qualityAssigner.Successor = shaking;
254      shaking.Successor = localImprovement;
255      localImprovement.Successor = evaluator;
256      evaluator.Successor = qualityComparator;
257      qualityComparator.Successor = improvesQualityBranch;
258      improvesQualityBranch.TrueBranch = bestQualityUpdater;
259      improvesQualityBranch.FalseBranch = cleaner;
260
261      bestQualityUpdater.Successor = indexResetter;
262      indexResetter.Successor = remover1;
263
264      cleaner.Successor = originalRestorer;
265      originalRestorer.Successor = remover2;
266      remover2.Successor = indexCounter;
267      /////////
268      indexTermination.TrueBranch = improvesQualityBranch2;
269      indexTermination.FalseBranch = null;
270
271      improvesQualityBranch2.TrueBranch = null;
272      improvesQualityBranch2.FalseBranch = createChild;
273
274      iterationsCounter.Successor = iterationsComparator;
275      iterationsComparator.Successor = iterationsTermination;
276      iterationsTermination.TrueBranch = null;
277      iterationsTermination.FalseBranch = iteration;
278      #endregion
279    }
280
281    public override IOperation Apply() {
282      if (LocalImprovementParameter.ActualValue == null || EvaluatorParameter.ActualValue == null)
283        return null;
284      return base.Apply();
285    }
286  }
287}
Note: See TracBrowser for help on using the repository browser.