Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs @ 8086

Last change on this file since 8086 was 8086, checked in by jkarder, 12 years ago

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
File size: 19.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Algorithms.ScatterSearch {
33  /// <summary>
34  /// An operator which represents a scatter search.
35  /// </summary>
36  [Item("ScatterSearchMainLoop", "An operator which represents a scatter search.")]
37  [StorableClass]
38  public sealed class ScatterSearchMainLoop : AlgorithmOperator {
39    #region Parameter properties
40    public IValueLookupParameter<IMultiAnalyzer> AnalyzerParameter {
41      get { return (IValueLookupParameter<IMultiAnalyzer>)Parameters["Analyzer"]; }
42    }
43    public IValueLookupParameter<DoubleValue> BestKnownQualityParameter {
44      get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
45    }
46    public IValueLookupParameter<ICrossover> CrossoverParameter {
47      get { return (IValueLookupParameter<ICrossover>)Parameters["Crossover"]; }
48    }
49    public IValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
50      get { return (IValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
51    }
52    public IValueLookupParameter<IEvaluator> EvaluatorParameter {
53      get { return (IValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; }
54    }
55    public IValueLookupParameter<BoolValue> ExecutePathRelinkingParameter {
56      get { return (IValueLookupParameter<BoolValue>)Parameters["ExecutePathRelinking"]; }
57    }
58    public IValueLookupParameter<IImprovementOperator> ImproverParameter {
59      get { return (IValueLookupParameter<IImprovementOperator>)Parameters["Improver"]; }
60    }
61    public IValueLookupParameter<IntValue> IterationsParameter {
62      get { return (IValueLookupParameter<IntValue>)Parameters["Iterations"]; }
63    }
64    public IValueLookupParameter<BoolValue> MaximizationParameter {
65      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
66    }
67    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
68      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
69    }
70    public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
71      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
72    }
73    public IValueLookupParameter<IPathRelinker> PathRelinkerParameter {
74      get { return (IValueLookupParameter<IPathRelinker>)Parameters["PathRelinker"]; }
75    }
76    public IValueLookupParameter<IntValue> PopulationSizeParameter {
77      get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
78    }
79    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
80      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
81    }
82    public IValueLookupParameter<DoubleValue> QualityParameter {
83      get { return (IValueLookupParameter<DoubleValue>)Parameters["Quality"]; }
84    }
85    public IValueLookupParameter<IRandom> RandomParameter {
86      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
87    }
88    public IValueLookupParameter<VariableCollection> ResultsParameter {
89      get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
90    }
91    public IValueLookupParameter<ISimilarityCalculator> SimilarityCalculatorParameter {
92      get { return (IValueLookupParameter<ISimilarityCalculator>)Parameters["SimilarityCalculator"]; }
93    }
94    public IValueLookupParameter<IItem> TargetParameter {
95      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
96    }
97    #endregion
98
99    #region Properties
100    private IMultiAnalyzer Analyzer {
101      get { return AnalyzerParameter.ActualValue; }
102      set { AnalyzerParameter.ActualValue = value; }
103    }
104    private DoubleValue BestKnownQuality {
105      get { return BestKnownQualityParameter.ActualValue; }
106      set { BestKnownQualityParameter.ActualValue = value; }
107    }
108    private ICrossover Crossover {
109      get { return CrossoverParameter.ActualValue; }
110      set { CrossoverParameter.ActualValue = value; }
111    }
112    private IntValue EvaluatedSolutions {
113      get { return EvaluatedSolutionsParameter.ActualValue; }
114      set { EvaluatedSolutionsParameter.ActualValue = value; }
115    }
116    private IEvaluator Evaluator {
117      get { return EvaluatorParameter.ActualValue; }
118      set { EvaluatorParameter.ActualValue = value; }
119    }
120    private BoolValue ExecutePathRelinking {
121      get { return ExecutePathRelinkingParameter.ActualValue; }
122      set { ExecutePathRelinkingParameter.ActualValue = value; }
123    }
124    private IImprovementOperator Improver {
125      get { return ImproverParameter.ActualValue; }
126      set { ImproverParameter.ActualValue = value; }
127    }
128    private IntValue Iterations {
129      get { return IterationsParameter.ActualValue; }
130      set { IterationsParameter.ActualValue = value; }
131    }
132    private BoolValue Maximization {
133      get { return MaximizationParameter.ActualValue; }
134      set { MaximizationParameter.ActualValue = value; }
135    }
136    private IntValue MaximumIterations {
137      get { return MaximumIterationsParameter.ActualValue; }
138      set { MaximumIterationsParameter.ActualValue = value; }
139    }
140    private IntValue NumberOfHighQualitySolutions {
141      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
142      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
143    }
144    private IPathRelinker PathRelinker {
145      get { return PathRelinkerParameter.ActualValue; }
146      set { PathRelinkerParameter.ActualValue = value; }
147    }
148    private IntValue PopulationSize {
149      get { return PopulationSizeParameter.ActualValue; }
150      set { PopulationSizeParameter.ActualValue = value; }
151    }
152    private IntValue ReferenceSetSize {
153      get { return ReferenceSetSizeParameter.ActualValue; }
154      set { ReferenceSetSizeParameter.ActualValue = value; }
155    }
156    private DoubleValue Quality {
157      get { return QualityParameter.ActualValue; }
158      set { QualityParameter.ActualValue = value; }
159    }
160    private IRandom Random {
161      get { return RandomParameter.ActualValue; }
162      set { RandomParameter.ActualValue = value; }
163    }
164    private VariableCollection Results {
165      get { return ResultsParameter.ActualValue; }
166      set { ResultsParameter.ActualValue = value; }
167    }
168    private ISimilarityCalculator SimilarityCalculator {
169      get { return SimilarityCalculatorParameter.ActualValue; }
170      set { SimilarityCalculatorParameter.ActualValue = value; }
171    }
172    private IItem Target {
173      get { return TargetParameter.ActualValue; }
174      set { TargetParameter.ActualValue = value; }
175    }
176    #endregion
177
178    [StorableConstructor]
179    private ScatterSearchMainLoop(bool deserializing) : base(deserializing) { }
180    private ScatterSearchMainLoop(ScatterSearchMainLoop original, Cloner cloner) : base(original, cloner) { }
181    public ScatterSearchMainLoop() : base() { Initialize(); }
182
183    public override IDeepCloneable Clone(Cloner cloner) {
184      return new ScatterSearchMainLoop(this, cloner);
185    }
186
187    private void Initialize() {
188      #region Create parameters
189      Parameters.Add(new ValueLookupParameter<IMultiAnalyzer>("Analyzer", "The analyzer used to analyze each iteration."));
190      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
191      Parameters.Add(new ValueLookupParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
192      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
193      Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
194      Parameters.Add(new ValueLookupParameter<BoolValue>("ExecutePathRelinking", "True if path relinking should be executed instead of crossover, otherwise false."));
195      Parameters.Add(new ValueLookupParameter<IImprovementOperator>("Improver", "The operator used to improve solutions."));
196      Parameters.Add(new ValueLookupParameter<IntValue>("Iterations", "The number of iterations performed."));
197      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
198      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
199      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions in the reference set."));
200      Parameters.Add(new ValueLookupParameter<IPathRelinker>("PathRelinker", "The operator used to execute path relinking."));
201      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
202      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
203      Parameters.Add(new ValueLookupParameter<DoubleValue>("Quality", "This parameter is used for name translation only."));
204      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
205      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
206      Parameters.Add(new ValueLookupParameter<ISimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
207      Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
208      #endregion
209
210      #region Create operators
211      Placeholder analyzer = new Placeholder();
212      Assigner assigner1 = new Assigner();
213      Assigner assigner2 = new Assigner();
214      ChildrenCreator childrenCreator = new ChildrenCreator();
215      Placeholder crossover = new Placeholder();
216      Comparator iterationsChecker = new Comparator();
217      IntCounter iterationsCounter = new IntCounter();
218      MergingReducer mergingReducer = new MergingReducer();
219      ConditionalBranch executePathRelinkingBranch = new ConditionalBranch();
220      ConditionalBranch newSolutionsBranch = new ConditionalBranch();
221      OffspringProcessor offspringProcessor = new OffspringProcessor();
222      Placeholder pathRelinker = new Placeholder();
223      PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod();
224      ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod();
225      ResultsCollector resultsCollector = new ResultsCollector();
226      RightSelector rightSelector = new RightSelector();
227      Placeholder solutionEvaluator1 = new Placeholder();
228      Placeholder solutionEvaluator2 = new Placeholder();
229      Placeholder solutionImprover1 = new Placeholder();
230      Placeholder solutionImprover2 = new Placeholder();
231      SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod();
232      SolutionsCreator solutionsCreator = new SolutionsCreator();
233      DataReducer dataReducer1 = new DataReducer();
234      DataReducer dataReducer2 = new DataReducer();
235      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
236      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
237      SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor();
238      SubScopesProcessor subScopesProcessor4 = new SubScopesProcessor();
239      ConditionalBranch terminateBranch = new ConditionalBranch();
240      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
241      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
242      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
243      VariableCreator variableCreator = new VariableCreator();
244      #endregion
245
246      #region Create operator graph
247      OperatorGraph.InitialOperator = variableCreator;
248      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(IterationsParameter.Name, new IntValue(0)));
249      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("NewSolutions", new BoolValue(false)));
250      variableCreator.Successor = resultsCollector;
251
252      resultsCollector.CopyValue = new BoolValue(false);
253      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
254      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
255      resultsCollector.Successor = iterationsChecker;
256
257      iterationsChecker.Name = "IterationsChecker";
258      iterationsChecker.Comparison.Value = ComparisonType.GreaterOrEqual;
259      iterationsChecker.LeftSideParameter.ActualName = IterationsParameter.Name;
260      iterationsChecker.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
261      iterationsChecker.ResultParameter.ActualName = "Terminate";
262      iterationsChecker.Successor = terminateBranch;
263
264      terminateBranch.Name = "TerminateChecker";
265      terminateBranch.ConditionParameter.ActualName = "Terminate";
266      terminateBranch.FalseBranch = referenceSetUpdateMethod;
267
268      referenceSetUpdateMethod.Successor = assigner1;
269
270      assigner1.Name = "NewSolutions = true";
271      assigner1.LeftSideParameter.ActualName = "NewSolutions";
272      assigner1.RightSideParameter.Value = new BoolValue(true);
273      assigner1.Successor = subScopesProcessor1;
274
275      subScopesProcessor1.DepthParameter.Value = new IntValue(1);
276      subScopesProcessor1.Operators.Add(new EmptyOperator());
277      subScopesProcessor1.Operators.Add(childrenCreator);
278      subScopesProcessor1.Successor = newSolutionsBranch;
279
280      childrenCreator.Name = "SubsetGenerator";
281      childrenCreator.ParentsPerChildParameter.Value = new IntValue(2);
282      childrenCreator.Successor = assigner2;
283
284      assigner2.Name = "NewSolutions = false";
285      assigner2.LeftSideParameter.ActualName = "NewSolutions";
286      assigner2.RightSideParameter.Value = new BoolValue(false);
287      assigner2.Successor = uniformSubScopesProcessor1;
288
289      uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1);
290      uniformSubScopesProcessor1.Operator = executePathRelinkingBranch;
291      uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod;
292
293      executePathRelinkingBranch.Name = "ExecutePathRelinkingChecker";
294      executePathRelinkingBranch.ConditionParameter.ActualName = ExecutePathRelinkingParameter.ActualName;
295      executePathRelinkingBranch.TrueBranch = pathRelinker;
296      executePathRelinkingBranch.FalseBranch = crossover;
297
298      pathRelinker.Name = "PathRelinker";
299      pathRelinker.OperatorParameter.ActualName = PathRelinkerParameter.Name;
300      pathRelinker.Successor = offspringProcessor;
301
302      crossover.Name = "Crossover";
303      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
304      crossover.Successor = offspringProcessor;
305
306      offspringProcessor.TargetParameter.ActualName = TargetParameter.ActualName;
307      offspringProcessor.Successor = rightSelector;
308
309      rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
310      rightSelector.CopySelected = new BoolValue(false);
311      rightSelector.Successor = subScopesProcessor2;
312
313      subScopesProcessor2.DepthParameter.Value = new IntValue(1);
314      subScopesProcessor2.Operators.Add(new EmptyOperator());
315      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor2);
316      subScopesProcessor2.Successor = mergingReducer;
317
318      uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2);
319      uniformSubScopesProcessor2.Operator = solutionImprover1;
320      uniformSubScopesProcessor2.Successor = subScopesProcessor4;
321
322      solutionImprover1.Name = "SolutionImprover";
323      solutionImprover1.OperatorParameter.ActualName = ImproverParameter.Name;
324      solutionImprover1.Successor = solutionEvaluator1;
325
326      solutionEvaluator1.Name = "SolutionEvaluator";
327      solutionEvaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
328
329      subScopesProcessor4.Operators.Add(dataReducer1);
330
331      dataReducer1.Name = "Increment EvaluatedSolutions";
332      dataReducer1.ParameterToReduce.ActualName = "LocalEvaluatedSolutions";
333      dataReducer1.TargetParameter.ActualName = EvaluatedSolutionsParameter.Name;
334      dataReducer1.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
335      dataReducer1.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
336
337      solutionPoolUpdateMethod.QualityParameter.ActualName = QualityParameter.ActualName;
338      solutionPoolUpdateMethod.Successor = analyzer;
339
340      analyzer.Name = "Analyzer";
341      analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
342
343      newSolutionsBranch.Name = "NewSolutionsChecker";
344      newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions";
345      newSolutionsBranch.TrueBranch = subScopesProcessor1;
346      newSolutionsBranch.FalseBranch = populationRebuildMethod;
347
348      populationRebuildMethod.QualityParameter.ActualName = QualityParameter.ActualName;
349      populationRebuildMethod.Successor = subScopesProcessor3;
350
351      subScopesProcessor3.DepthParameter.Value = new IntValue(1);
352      subScopesProcessor3.Operators.Add(solutionsCreator);
353      subScopesProcessor3.Operators.Add(new EmptyOperator());
354      subScopesProcessor3.Successor = iterationsCounter;
355
356      solutionsCreator.Name = "DiversificationGenerationMethod";
357      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
358      solutionsCreator.Successor = uniformSubScopesProcessor3;
359
360      uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1);
361      uniformSubScopesProcessor3.Operator = solutionImprover2;
362      uniformSubScopesProcessor3.Successor = dataReducer2;
363
364      solutionImprover2.Name = "SolutionImprover";
365      solutionImprover2.OperatorParameter.ActualName = ImproverParameter.Name;
366      solutionImprover2.Successor = solutionEvaluator2;
367
368      solutionEvaluator2.Name = "SolutionEvaluator";
369      solutionEvaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
370
371      dataReducer2.Name = "Increment EvaluatedSolutions";
372      dataReducer2.ParameterToReduce.ActualName = "LocalEvaluatedSolutions";
373      dataReducer2.TargetParameter.ActualName = EvaluatedSolutionsParameter.Name;
374      dataReducer2.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
375      dataReducer2.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
376
377      iterationsCounter.Name = "IterationCounter";
378      iterationsCounter.IncrementParameter.Value = new IntValue(1);
379      iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;
380      iterationsCounter.Successor = resultsCollector;
381      #endregion
382    }
383
384    public override IOperation Apply() {
385      if (ImproverParameter.ActualValue == null)
386        return null;
387      return base.Apply();
388    }
389  }
390}
Note: See TracBrowser for help on using the repository browser.