Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs @ 7771

Last change on this file since 7771 was 7744, checked in by jkarder, 13 years ago

#1331:

  • added problem specific improvement operators (KnapsackImprovementOperator, TravelingSalesmanImprovementOperator)
  • added custom interface (IScatterSearchTargetProcessor) for Scatter Search specific operators that use a target parameter
  • added custom operator (OffspringProcessor) to process multiple children that were created by crossovers
  • extracted diversity calculation and added problem/encoding specific operators (BinaryVectorDiversityCalculator, PermutationDiversityCalculator) for it
  • added parameters and adjusted types
  • adjusted event handling
  • minor code improvements
File size: 16.6 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;
30
31namespace HeuristicLab.Algorithms.ScatterSearch {
32  /// <summary>
33  /// An operator which represents a scatter search.
34  /// </summary>
35  [Item("ScatterSearchMainLoop", "An operator which represents the main loop of a scatter search.")]
36  [StorableClass]
37  public sealed class ScatterSearchMainLoop : AlgorithmOperator {
38    #region Parameter properties
39    public IValueLookupParameter<IMultiAnalyzer> AnalyzerParameter {
40      get { return (IValueLookupParameter<IMultiAnalyzer>)Parameters["Analyzer"]; }
41    }
42    public IValueLookupParameter<ICrossover> CrossoverParameter {
43      get { return (IValueLookupParameter<ICrossover>)Parameters["Crossover"]; }
44    }
45    public IValueLookupParameter<IOperator> ImproverParameter {
46      get { return (IValueLookupParameter<IOperator>)Parameters["Improver"]; }
47    }
48    public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter {
49      get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; }
50    }
51    public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
52      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
53    }
54    public IValueLookupParameter<IntValue> PopulationSizeParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
56    }
57    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
59    }
60    public IValueLookupParameter<DoubleValue> BestKnownQualityParameter {
61      get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
62    }
63    public IValueLookupParameter<IEvaluator> EvaluatorParameter {
64      get { return (IValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; }
65    }
66    public IValueLookupParameter<IntValue> IterationsParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters["Iterations"]; }
68    }
69    public IValueLookupParameter<BoolValue> MaximizationParameter {
70      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
71    }
72    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
73      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
74    }
75    public IValueLookupParameter<DoubleValue> QualityParameter {
76      get { return (IValueLookupParameter<DoubleValue>)Parameters["Quality"]; }
77    }
78    public IValueLookupParameter<IRandom> RandomParameter {
79      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
80    }
81    public IValueLookupParameter<VariableCollection> ResultsParameter {
82      get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
83    }
84    public IValueLookupParameter<IntValue> SampleSizeParameter {
85      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
86    }
87    public IValueLookupParameter<ISolutionCreator> SolutionCreatorParameter {
88      get { return (IValueLookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
89    }
90    #endregion
91
92    #region Properties
93    private IMultiAnalyzer Analyzer {
94      get { return AnalyzerParameter.ActualValue; }
95      set { AnalyzerParameter.ActualValue = value; }
96    }
97    private ICrossover Crossover {
98      get { return CrossoverParameter.ActualValue; }
99      set { CrossoverParameter.ActualValue = value; }
100    }
101    private IOperator Improver {
102      get { return ImproverParameter.ActualValue; }
103      set { ImproverParameter.ActualValue = value; }
104    }
105    private DiversityCalculator DiversityCalculator {
106      get { return DiversityCalculatorParameter.ActualValue; }
107      set { DiversityCalculatorParameter.ActualValue = value; }
108    }
109    private IntValue NumberOfHighQualitySolutions {
110      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
111      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
112    }
113    private IntValue PopulationSize {
114      get { return PopulationSizeParameter.ActualValue; }
115      set { PopulationSizeParameter.ActualValue = value; }
116    }
117    private IntValue ReferenceSetSize {
118      get { return ReferenceSetSizeParameter.ActualValue; }
119      set { ReferenceSetSizeParameter.ActualValue = value; }
120    }
121    private DoubleValue BestKnownQuality {
122      get { return BestKnownQualityParameter.ActualValue; }
123      set { BestKnownQualityParameter.ActualValue = value; }
124    }
125    private IEvaluator Evaluator {
126      get { return EvaluatorParameter.ActualValue; }
127      set { EvaluatorParameter.ActualValue = value; }
128    }
129    private IntValue Iterations {
130      get { return IterationsParameter.ActualValue; }
131      set { IterationsParameter.ActualValue = value; }
132    }
133    private BoolValue Maximization {
134      get { return MaximizationParameter.ActualValue; }
135      set { MaximizationParameter.ActualValue = value; }
136    }
137    private IntValue MaximumIterations {
138      get { return MaximumIterationsParameter.ActualValue; }
139      set { MaximumIterationsParameter.ActualValue = value; }
140    }
141    private DoubleValue Quality {
142      get { return QualityParameter.ActualValue; }
143      set { QualityParameter.ActualValue = value; }
144    }
145    private IRandom Random {
146      get { return RandomParameter.ActualValue; }
147      set { RandomParameter.ActualValue = value; }
148    }
149    private VariableCollection Results {
150      get { return ResultsParameter.ActualValue; }
151      set { ResultsParameter.ActualValue = value; }
152    }
153    private IntValue SampleSize {
154      get { return SampleSizeParameter.ActualValue; }
155      set { SampleSizeParameter.ActualValue = value; }
156    }
157    private ISolutionCreator SolutionCreator {
158      get { return SolutionCreatorParameter.ActualValue; }
159      set { SolutionCreatorParameter.ActualValue = value; }
160    }
161    #endregion
162
163    [StorableConstructor]
164    private ScatterSearchMainLoop(bool deserializing) : base(deserializing) { }
165    private ScatterSearchMainLoop(ScatterSearchMainLoop original, Cloner cloner) : base(original, cloner) { }
166    public ScatterSearchMainLoop() : base() { Initialize(); }
167
168    public override IDeepCloneable Clone(Cloner cloner) {
169      return new ScatterSearchMainLoop(this, cloner);
170    }
171
172    private void Initialize() {
173      #region Create parameters
174      Parameters.Add(new ValueLookupParameter<IMultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves."));
175      Parameters.Add(new ValueLookupParameter<ICrossover>("Crossover", "The operator used to combine solutions."));
176      Parameters.Add(new ValueLookupParameter<IOperator>("Improver", "The operator used to improve solutions."));
177      Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator", "The operator used to calculate the diversity of two solutions."));
178      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions that should be added to the reference set."));
179      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
180      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
181      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The problem's best known quality value found so far."));
182      Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator", "The operator which is used to evaluate new solutions."));
183      Parameters.Add(new ValueLookupParameter<IntValue>("Iterations", "The number of iterations performed."));
184      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
185      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
186      Parameters.Add(new ValueLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
187      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
188      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
189      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators."));
190      Parameters.Add(new ValueLookupParameter<ISolutionCreator>("SolutionCreator", "The operator which is used to create new solutions."));
191      #endregion
192
193      #region Create operators
194      Assigner assigner1 = new Assigner();
195      Assigner assigner2 = new Assigner();
196      ChildrenCreator childrenCreator = new ChildrenCreator();
197      Comparator iterationsChecker = new Comparator();
198      ConditionalBranch newSolutionsBranch = new ConditionalBranch();
199      ConditionalBranch terminateBranch = new ConditionalBranch();
200      IntCounter interationsCounter = new IntCounter();
201      OffspringProcessor offspringProcessor = new OffspringProcessor();
202      Placeholder analyzer = new Placeholder();
203      Placeholder crossover = new Placeholder();
204      Placeholder solutionEvaluator1 = new Placeholder();
205      Placeholder solutionEvaluator2 = new Placeholder();
206      Placeholder solutionImprover1 = new Placeholder();
207      Placeholder solutionImprover2 = new Placeholder();
208      PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod();
209      ResultsCollector resultsCollector = new ResultsCollector();
210      ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod();
211      SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod();
212      SolutionsCreator solutionsCreator = new SolutionsCreator();
213      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
214      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
215      SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor();
216      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
217      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
218      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
219      #endregion
220
221      #region Create operator graph
222      OperatorGraph.InitialOperator = interationsCounter;
223      assigner1.Name = "NewSolutions = true";
224      assigner1.LeftSideParameter.ActualName = "NewSolutions";
225      assigner1.RightSideParameter.Value = new BoolValue(true);
226      assigner1.Successor = newSolutionsBranch;
227
228      assigner2.Name = "NewSolutions = false";
229      assigner2.LeftSideParameter.ActualName = "NewSolutions";
230      assigner2.RightSideParameter.Value = new BoolValue(false);
231      assigner2.Successor = uniformSubScopesProcessor1;
232
233      childrenCreator.Name = "SubsetGenerator";
234      childrenCreator.ParentsPerChildParameter.Value = new IntValue(2);
235      childrenCreator.Successor = assigner2;
236
237      iterationsChecker.Name = "IterationsChecker";
238      iterationsChecker.Comparison.Value = ComparisonType.GreaterOrEqual;
239      iterationsChecker.LeftSideParameter.ActualName = "Iterations";
240      iterationsChecker.RightSideParameter.ActualName = "MaximumIterations";
241      iterationsChecker.ResultParameter.ActualName = "Terminate";
242      iterationsChecker.Successor = terminateBranch;
243
244      solutionImprover2.Name = "SolutionImprover";
245      solutionImprover2.OperatorParameter.ActualName = "Improver";
246      solutionImprover2.Successor = solutionEvaluator2;
247
248      solutionEvaluator2.Name = "SolutionEvaluator";
249      solutionEvaluator2.OperatorParameter.ActualName = "Evaluator";
250      solutionEvaluator2.Successor = null;
251
252      newSolutionsBranch.Name = "NewSolutionChecker";
253      newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions";
254      newSolutionsBranch.TrueBranch = subScopesProcessor1;
255      newSolutionsBranch.FalseBranch = populationRebuildMethod;
256      newSolutionsBranch.Successor = null;
257
258      terminateBranch.Name = "TerminateChecker";
259      terminateBranch.ConditionParameter.ActualName = "Terminate";
260      terminateBranch.TrueBranch = new EmptyOperator();
261      terminateBranch.FalseBranch = referenceSetUpdateMethod;
262      terminateBranch.Successor = null;
263
264      interationsCounter.Name = "IterationCounter";
265      interationsCounter.IncrementParameter.Value = new IntValue(1);
266      interationsCounter.ValueParameter.ActualName = "Iterations";
267      interationsCounter.Successor = resultsCollector;
268
269      offspringProcessor.Successor = subScopesProcessor2;
270
271      analyzer.Name = "Analyzer";
272      analyzer.OperatorParameter.ActualName = "Analyzer";
273
274      crossover.Name = "Crossover";
275      crossover.OperatorParameter.ActualName = "Crossover";
276      crossover.Successor = offspringProcessor;
277
278      solutionImprover1.Name = "SolutionImprover";
279      solutionImprover1.OperatorParameter.ActualName = "Improver";
280      solutionImprover1.Successor = solutionEvaluator1;
281
282      solutionEvaluator1.Name = "SolutionEvaluator";
283      solutionEvaluator1.OperatorParameter.ActualName = "Evaluator";
284      solutionEvaluator1.Successor = null;
285
286      populationRebuildMethod.Successor = subScopesProcessor3;
287
288      resultsCollector.CopyValue = new BoolValue(false);
289      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
290      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameter.Name, null, BestKnownQualityParameter.Name));
291      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
292      resultsCollector.Successor = iterationsChecker;
293
294      referenceSetUpdateMethod.Successor = assigner1;
295
296      solutionPoolUpdateMethod.Successor = analyzer;
297
298      solutionsCreator.Name = "DiversificationGenerationMethod";
299      solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize";
300      solutionsCreator.Successor = uniformSubScopesProcessor3;
301
302      subScopesProcessor1.DepthParameter.Value = new IntValue(1);
303      subScopesProcessor1.Operators.Add(new EmptyOperator());
304      subScopesProcessor1.Operators.Add(childrenCreator);
305      subScopesProcessor1.Successor = newSolutionsBranch;
306
307      subScopesProcessor2.DepthParameter.Value = new IntValue(1);
308      subScopesProcessor2.Operators.Add(new EmptyOperator());
309      subScopesProcessor2.Operators.Add(new EmptyOperator());
310      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor2);
311      subScopesProcessor2.Successor = null;
312
313      subScopesProcessor3.DepthParameter.Value = new IntValue(1);
314      subScopesProcessor3.Operators.Add(solutionsCreator);
315      subScopesProcessor3.Operators.Add(new EmptyOperator());
316      subScopesProcessor3.Successor = interationsCounter;
317
318      uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1);
319      uniformSubScopesProcessor1.Operator = crossover;
320      uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod;
321
322      uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(1);
323      uniformSubScopesProcessor2.Operator = solutionImprover1;
324      uniformSubScopesProcessor2.Successor = null;
325
326      uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1);
327      uniformSubScopesProcessor3.Operator = solutionImprover2;
328      uniformSubScopesProcessor3.Successor = null;
329      #endregion
330    }
331
332    public override IOperation Apply() {
333      return base.Apply();
334    }
335  }
336}
Note: See TracBrowser for help on using the repository browser.