source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearch.cs @ 7786

Last change on this file since 7786 was 7786, checked in by jkarder, 8 years ago

#1331:

  • fixed bug in path relinking selection
  • fixed bug in ScatterSearch.Prepare()
  • added custom interface (IImprovementOperator) for Scatter Search specific improvement operators
  • switched from diversity calculation to similarity calculation
  • separated IPathRelinker from ICrossover
  • changed TestFunctionsImprovementOperator to use reflection for evaluating functions
  • changed SolutionPoolUpdateMethod to use similarity calculation for solution comparison
  • improved TravelingSalesmanImprovementOperator
  • improved operator graph
  • removed specific operators used to evaluate TestFunctions problems
  • removed custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • adjusted event handling
  • changed access levels
  • minor code improvements
File size: 19.3 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 System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35using HeuristicLab.Selection;
36
37namespace HeuristicLab.Algorithms.ScatterSearch {
38  /// <summary>
39  /// A scatter search algorithm.
40  /// </summary>
41  [Item("Scatter Search", "A scatter search algorithm.")]
42  [Creatable("Algorithms")]
43  [StorableClass]
44  public sealed class ScatterSearch : HeuristicOptimizationEngineAlgorithm, IStorableContent {
45    public string Filename { get; set; }
46
47    #region Problem Properties
48    public override Type ProblemType {
49      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
50    }
51    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
52      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
53      set { base.Problem = value; }
54    }
55    #endregion
56
57    #region Parameter Properties
58    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
59      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
60    }
61    public ConstrainedValueParameter<ICrossover> CrossoverParameter {
62      get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
63    }
64    public IValueParameter<BoolValue> ExecutePathRelinkingParameter {
65      get { return (IValueLookupParameter<BoolValue>)Parameters["ExecutePathRelinking"]; }
66    }
67    public ConstrainedValueParameter<IImprovementOperator> ImproverParameter {
68      get { return (ConstrainedValueParameter<IImprovementOperator>)Parameters["Improver"]; }
69    }
70    public IValueParameter<IntValue> MaximumIterationsParameter {
71      get { return (IValueParameter<IntValue>)Parameters["MaximumIterations"]; }
72    }
73    public IValueParameter<IntValue> NumberOfHighQualitySolutionsParameter {
74      get { return (IValueParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
75    }
76    public ConstrainedValueParameter<IPathRelinker> PathRelinkerParameter {
77      get { return (ConstrainedValueParameter<IPathRelinker>)Parameters["PathRelinker"]; }
78    }
79    public IValueParameter<IntValue> PopulationSizeParameter {
80      get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; }
81    }
82    public IValueParameter<IntValue> ReferenceSetSizeParameter {
83      get { return (IValueParameter<IntValue>)Parameters["ReferenceSetSize"]; }
84    }
85    public IValueParameter<IntValue> SeedParameter {
86      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
87    }
88    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
89      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
90    }
91    public ConstrainedValueParameter<SimilarityCalculator> SimilarityCalculatorParameter {
92      get { return (ConstrainedValueParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }
93    }
94    #endregion
95
96    #region Properties
97    private MultiAnalyzer Analyzer {
98      get { return AnalyzerParameter.Value; }
99      set { AnalyzerParameter.Value = value; }
100    }
101    private ICrossover Crossover {
102      get { return CrossoverParameter.Value; }
103      set { CrossoverParameter.Value = value; }
104    }
105    private BoolValue ExecutePathRelinking {
106      get { return ExecutePathRelinkingParameter.Value; }
107      set { ExecutePathRelinkingParameter.Value = value; }
108    }
109    private IImprovementOperator Improver {
110      get { return ImproverParameter.Value; }
111      set { ImproverParameter.Value = value; }
112    }
113    private IntValue MaximumIterations {
114      get { return MaximumIterationsParameter.Value; }
115      set { MaximumIterationsParameter.Value = value; }
116    }
117    private IntValue NumberOfHighQualitySolutions {
118      get { return NumberOfHighQualitySolutionsParameter.Value; }
119      set { NumberOfHighQualitySolutionsParameter.Value = value; }
120    }
121    private IPathRelinker PathRelinker {
122      get { return PathRelinkerParameter.Value; }
123      set { PathRelinkerParameter.Value = value; }
124    }
125    private IntValue PopulationSize {
126      get { return PopulationSizeParameter.Value; }
127      set { PopulationSizeParameter.Value = value; }
128    }
129    private IntValue ReferenceSetSize {
130      get { return ReferenceSetSizeParameter.Value; }
131      set { ReferenceSetSizeParameter.Value = value; }
132    }
133    private IntValue Seed {
134      get { return SeedParameter.Value; }
135      set { SeedParameter.Value = value; }
136    }
137    private BoolValue SetSeedRandomly {
138      get { return SetSeedRandomlyParameter.Value; }
139      set { SetSeedRandomlyParameter.Value = value; }
140    }
141    private SimilarityCalculator SimilarityCalculator {
142      get { return SimilarityCalculatorParameter.Value; }
143      set { SimilarityCalculatorParameter.Value = value; }
144    }
145    private RandomCreator RandomCreator {
146      get { return (RandomCreator)OperatorGraph.InitialOperator; }
147    }
148    private SolutionsCreator SolutionsCreator {
149      get { return (SolutionsCreator)RandomCreator.Successor; }
150    }
151    private ScatterSearchMainLoop MainLoop {
152      get { return FindMainLoop(SolutionsCreator.Successor); }
153    }
154
155    [Storable]
156    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
157    #endregion
158
159    [StorableConstructor]
160    private ScatterSearch(bool deserializing) : base(deserializing) { }
161    [StorableHook(HookType.AfterDeserialization)]
162    private void AfterDeserialization() {
163      Initialize();
164    }
165    private ScatterSearch(ScatterSearch original, Cloner cloner)
166      : base(original, cloner) {
167      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
168      Initialize();
169    }
170    public override IDeepCloneable Clone(Cloner cloner) {
171      return new ScatterSearch(this, cloner);
172    }
173    public ScatterSearch()
174      : base() {
175      #region Create parameters
176      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", new MultiAnalyzer()));
177      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover"));
178      Parameters.Add(new ValueParameter<BoolValue>("ExecutePathRelinking", new BoolValue(true)));
179      Parameters.Add(new ConstrainedValueParameter<IImprovementOperator>("Improver"));
180      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", new IntValue(100)));
181      Parameters.Add(new ValueParameter<IntValue>("NumberOfHighQualitySolutions", new IntValue(10)));
182      Parameters.Add(new ConstrainedValueParameter<IPathRelinker>("PathRelinker"));
183      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", new IntValue(300)));
184      Parameters.Add(new ValueParameter<IntValue>("ReferenceSetSize", new IntValue(100)));
185      Parameters.Add(new ValueParameter<IntValue>("Seed", new IntValue(0)));
186      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", new BoolValue(true)));
187      Parameters.Add(new ConstrainedValueParameter<SimilarityCalculator>("SimilarityCalculator"));
188      #endregion
189
190      #region Create operators
191      RandomCreator randomCreator = new RandomCreator();
192      SolutionsCreator solutionsCreator = new SolutionsCreator();
193      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
194      Placeholder solutionEvaluator = new Placeholder();
195      Placeholder solutionImprover = new Placeholder();
196      BestSelector bestSelector = new BestSelector();
197      ScatterSearchMainLoop mainLoop = new ScatterSearchMainLoop();
198      #endregion
199
200      #region Create operator graph
201      OperatorGraph.InitialOperator = randomCreator;
202      randomCreator.RandomParameter.ActualName = "Random";
203      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
204      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
205      randomCreator.Successor = solutionsCreator;
206
207      solutionsCreator.Name = "DiversificationGenerationMethod";
208      solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize";
209      solutionsCreator.Successor = uniformSubScopesProcessor;
210
211      uniformSubScopesProcessor.Operator = solutionImprover;
212      uniformSubScopesProcessor.Successor = bestSelector;
213
214      solutionImprover.Name = "SolutionImprover";
215      solutionImprover.OperatorParameter.ActualName = "Improver";
216      solutionImprover.Successor = solutionEvaluator;
217
218      solutionEvaluator.Name = "SolutionEvaluator";
219      solutionEvaluator.OperatorParameter.ActualName = "Evaluator";
220      solutionEvaluator.Successor = null;
221
222      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = NumberOfHighQualitySolutionsParameter.Name;
223      bestSelector.CopySelected = new BoolValue(false);
224      bestSelector.Successor = mainLoop;
225
226      mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
227      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
228      mainLoop.ResultsParameter.ActualName = "Results";
229      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
230      mainLoop.IterationsParameter.ActualName = "Iterations";
231      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
232      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
233      mainLoop.NumberOfHighQualitySolutionsParameter.ActualName = NumberOfHighQualitySolutionsParameter.Name;
234      mainLoop.Successor = null;
235      #endregion
236
237      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
238      ParameterizeAnalyzers();
239      UpdateAnalyzers();
240
241      Initialize();
242    }
243
244    public override void Prepare() {
245      if (Problem != null) base.Prepare();
246    }
247
248    #region Events
249    protected override void OnProblemChanged() {
250      ParameterizeStochasticOperator(Problem.SolutionCreator);
251      ParameterizeStochasticOperator(Problem.Evaluator);
252      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
253      ParameterizeAnalyzers();
254      ParameterizeMainLoop();
255      ParameterizeSolutionsCreator();
256      UpdateAnalyzers();
257      UpdateCrossovers();
258      UpdatePathRelinkers();
259      UpdateSimilarityCalculators();
260      UpdateImprovers();
261      UpdateSimilarityCalculators();
262      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
263      base.OnProblemChanged();
264    }
265    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
266      ParameterizeStochasticOperator(Problem.SolutionCreator);
267      ParameterizeSolutionsCreator();
268      base.Problem_SolutionCreatorChanged(sender, e);
269    }
270    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
271      ParameterizeStochasticOperator(Problem.Evaluator);
272      ParameterizeSolutionsCreator();
273      ParameterizeMainLoop();
274      ParameterizeAnalyzers();
275      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
276      base.Problem_EvaluatorChanged(sender, e);
277    }
278    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
279      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
280      UpdateAnalyzers();
281      UpdateCrossovers();
282      UpdatePathRelinkers();
283      UpdateSimilarityCalculators();
284      UpdateImprovers();
285      ParameterizeMainLoop();
286      ParameterizeAnalyzers();
287      base.Problem_OperatorsChanged(sender, e);
288    }
289    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
290      ParameterizeMainLoop();
291      ParameterizeAnalyzers();
292    }
293    #endregion
294
295    #region Helpers
296    private void Initialize() {
297      if (Problem != null) {
298        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
299      }
300    }
301    private void UpdateAnalyzers() {
302      Analyzer.Operators.Clear();
303      if (Problem != null) {
304        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
305          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
306            param.Depth = 1;
307          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
308        }
309      }
310      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
311    }
312    private void UpdateCrossovers() {
313      ICrossover oldCrossover = CrossoverParameter.Value;
314      CrossoverParameter.ValidValues.Clear();
315      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
316
317      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
318        CrossoverParameter.ValidValues.Add(crossover);
319
320      if (oldCrossover != null) {
321        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
322        if (crossover != null) CrossoverParameter.Value = crossover;
323        else oldCrossover = null;
324      }
325      if (oldCrossover == null && defaultCrossover != null)
326        CrossoverParameter.Value = defaultCrossover;
327    }
328    private void UpdateImprovers() {
329      IImprovementOperator oldImprover = ImproverParameter.Value;
330      ImproverParameter.ValidValues.Clear();
331      IImprovementOperator defaultImprover = Problem.Operators.OfType<IImprovementOperator>().FirstOrDefault();
332
333      foreach (IImprovementOperator improver in ApplicationManager.Manager.GetInstances<IImprovementOperator>().OrderBy(x => x.Name))
334        ImproverParameter.ValidValues.Add(improver);
335
336      foreach (IImprovementOperator improver in Problem.Operators.OfType<IImprovementOperator>().OrderBy(x => x.Name))
337        ImproverParameter.ValidValues.Add(improver);
338
339      if (oldImprover != null) {
340        IImprovementOperator improver = ImproverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldImprover.GetType());
341        if (improver != null) ImproverParameter.Value = improver;
342        else oldImprover = null;
343      }
344      if (oldImprover == null && defaultImprover != null)
345        ImproverParameter.Value = defaultImprover;
346    }
347    private void UpdatePathRelinkers() {
348      IPathRelinker oldPathRelinker = PathRelinkerParameter.Value;
349      PathRelinkerParameter.ValidValues.Clear();
350      IPathRelinker defaultPathRelinker = Problem.Operators.OfType<IPathRelinker>().FirstOrDefault();
351
352      foreach (IPathRelinker pathRelinker in ApplicationManager.Manager.GetInstances<IPathRelinker>().OrderBy(x => x.Name))
353        PathRelinkerParameter.ValidValues.Add(pathRelinker);
354
355      foreach (IPathRelinker pathRelinker in Problem.Operators.OfType<IPathRelinker>().OrderBy(x => x.Name))
356        PathRelinkerParameter.ValidValues.Add(pathRelinker);
357
358      if (oldPathRelinker != null) {
359        IPathRelinker pathRelinker = PathRelinkerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldPathRelinker.GetType());
360        if (pathRelinker != null) PathRelinkerParameter.Value = pathRelinker;
361        else oldPathRelinker = null;
362      }
363      if (oldPathRelinker == null && defaultPathRelinker != null)
364        PathRelinkerParameter.Value = defaultPathRelinker;
365    }
366    private void UpdateSimilarityCalculators() {
367      SimilarityCalculator oldDiversityCalculator = SimilarityCalculatorParameter.Value;
368      SimilarityCalculatorParameter.ValidValues.Clear();
369      SimilarityCalculator defaultDiversityCalculator = Problem.Operators.OfType<SimilarityCalculator>().FirstOrDefault();
370
371      foreach (SimilarityCalculator diversityCalculator in ApplicationManager.Manager.GetInstances<SimilarityCalculator>().OrderBy(x => x.Name))
372        SimilarityCalculatorParameter.ValidValues.Add(diversityCalculator);
373
374      foreach (SimilarityCalculator diversityCalculator in Problem.Operators.OfType<SimilarityCalculator>().OrderBy(x => x.Name))
375        SimilarityCalculatorParameter.ValidValues.Add(diversityCalculator);
376
377      if (oldDiversityCalculator != null) {
378        SimilarityCalculator diversityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDiversityCalculator.GetType());
379        if (diversityCalculator != null) SimilarityCalculatorParameter.Value = diversityCalculator;
380        else oldDiversityCalculator = null;
381      }
382      if (oldDiversityCalculator == null && defaultDiversityCalculator != null)
383        SimilarityCalculatorParameter.Value = defaultDiversityCalculator;
384    }
385    private void ParameterizeSolutionsCreator() {
386      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
387      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
388    }
389    private void ParameterizeMainLoop() {
390      if (Problem != null) {
391        MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
392        MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
393        MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
394      }
395    }
396    private void ParameterizeStochasticOperator(IOperator op) {
397      if (op is IStochasticOperator) {
398        IStochasticOperator stOp = (IStochasticOperator)op;
399        stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
400        stOp.RandomParameter.Hidden = true;
401      }
402    }
403    private void ParameterizeAnalyzers() {
404      qualityAnalyzer.ResultsParameter.ActualName = "Results";
405      qualityAnalyzer.ResultsParameter.Hidden = true;
406      if (Problem != null) {
407        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
408        qualityAnalyzer.MaximizationParameter.Hidden = true;
409        qualityAnalyzer.QualityParameter.Hidden = false;
410        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
411        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
412      } else {
413        qualityAnalyzer.MaximizationParameter.Hidden = false;
414        qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
415      }
416    }
417    private ScatterSearchMainLoop FindMainLoop(IOperator start) {
418      IOperator mainLoop = start;
419      while (mainLoop != null && !(mainLoop is ScatterSearchMainLoop))
420        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
421      return mainLoop as ScatterSearchMainLoop;
422    }
423    #endregion
424  }
425}
Note: See TracBrowser for help on using the repository browser.