Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated naming of operators (#1425)

File size: 13.5 KB
RevLine 
[5603]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;
[5622]34using HeuristicLab.Optimization;
[5603]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    }
[5735]71    public ValueLookupParameter<ILocalImprovementOperator> LocalImprovementParameter {
72      get { return (ValueLookupParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
[5603]73    }
[5642]74    public ValueLookupParameter<IShakingOperator> ShakingParameter {
75      get { return (ValueLookupParameter<IShakingOperator>)Parameters["Shaking"]; }
[5603]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."));
[5735]104      Parameters.Add(new ValueLookupParameter<ILocalImprovementOperator>("LocalImprovement", "The local improvement operation."));
[5642]105      Parameters.Add(new ValueLookupParameter<IShakingOperator>("Shaking", "The shaking operation."));
[5603]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
[5735]118      SubScopesCloner createChild = new SubScopesCloner();
[5603]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();
[5622]125      IntCounter evalCounter = new IntCounter();
[5603]126
127      QualityComparator qualityComparator = new QualityComparator();
[5735]128      ConditionalBranch improvesQualityBranch1 = new ConditionalBranch();
[5609]129      ConditionalBranch improvesQualityBranch2 = new ConditionalBranch();
[5603]130
131      Assigner bestQualityUpdater = new Assigner();
132
[5735]133      BestSelector bestSelector = new BestSelector();
134      RightReducer rightReducer = new RightReducer();
135
[5603]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
[5735]147      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
[5642]148      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Index", new IntValue(0)));
[5609]149      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("Continue", new BoolValue(false)));
[5610]150      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("IsBetter", new BoolValue(false)));
[5603]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);
[5735]161      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
[5603]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
[5738]171      createChild.Name = "Clone solution";
[5603]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
[5735]186      evalCounter.Name = "Count evaluations";
[5622]187      evalCounter.Increment.Value = 1;
188      evalCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
189
[5609]190      qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
191      qualityComparator.RightSideParameter.ActualName = "OriginalQuality";
[5603]192      qualityComparator.ResultParameter.ActualName = "IsBetter";
193
[5735]194      improvesQualityBranch1.ConditionParameter.ActualName = "IsBetter";
[5609]195      improvesQualityBranch2.ConditionParameter.ActualName = "IsBetter";
[5603]196
197      bestQualityUpdater.Name = "Update BestQuality";
198      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
199      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
200
[5735]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;
[5603]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);
[5735]219      iterationsCounter.ValueParameter.ActualName = "Iterations";
[5603]220
221      iterationsComparator.Name = "Iterations >= MaximumIterations";
222      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
[5735]223      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
[5603]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;
[5642]236      variableCreator.Successor = subScopesProcessor0;
[5603]237      subScopesProcessor0.Operators.Add(bestQualityInitializer);
[5642]238      subScopesProcessor0.Successor = analyzer1;
239      analyzer1.Successor = resultsCollector1;
[5603]240      /////////
241      resultsCollector1.Successor = iteration;
[5610]242
[5603]243      iteration.OperatorGraph.InitialOperator = iterationInit;
244      iteration.Successor = iterationsCounter;
245      iterationInit.Successor = createChild;
246
247      createChild.Successor = childProcessor;
[5735]248      childProcessor.Operators.Add(new EmptyOperator());
249      childProcessor.Operators.Add(qualityAssigner);
250      childProcessor.Successor = bestSelector;
[5603]251      /////////
252      qualityAssigner.Successor = shaking;
[5622]253      shaking.Successor = evaluator;
254      evaluator.Successor = evalCounter;
255      evalCounter.Successor = localImprovement;
256      localImprovement.Successor = qualityComparator;
[5735]257      qualityComparator.Successor = improvesQualityBranch1;
258      improvesQualityBranch1.TrueBranch = bestQualityUpdater;
259      improvesQualityBranch1.FalseBranch = indexCounter;
[5603]260
261      bestQualityUpdater.Successor = indexResetter;
[5735]262      indexResetter.Successor = null;
[5603]263
[5735]264      indexCounter.Successor = null;
[5603]265      /////////
[5735]266      bestSelector.Successor = rightReducer;
267      rightReducer.Successor = analyzer2;
[5622]268      analyzer2.Successor = indexTermination;
[5609]269      indexTermination.TrueBranch = improvesQualityBranch2;
270      indexTermination.FalseBranch = null;
[5603]271
[5609]272      improvesQualityBranch2.TrueBranch = null;
273      improvesQualityBranch2.FalseBranch = createChild;
274
[5603]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.