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

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

#1331:

  • added EvaluatedSolutions parameter
  • SingleObjectiveTestFunctionImprovementOperator now retrieves the bounds from the scope tree
  • SingleObjectiveTestFunctionProblem now assigns the bounds to the similarity calculator
File size: 18.7 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<ISolutionCreator> SolutionCreatorParameter {
95      get { return (IValueLookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
96    }
97    public IValueLookupParameter<IItem> TargetParameter {
98      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
99    }
100    #endregion
101
102    #region Properties
103    private IMultiAnalyzer Analyzer {
104      get { return AnalyzerParameter.ActualValue; }
105      set { AnalyzerParameter.ActualValue = value; }
106    }
107    private DoubleValue BestKnownQuality {
108      get { return BestKnownQualityParameter.ActualValue; }
109      set { BestKnownQualityParameter.ActualValue = value; }
110    }
111    private ICrossover Crossover {
112      get { return CrossoverParameter.ActualValue; }
113      set { CrossoverParameter.ActualValue = value; }
114    }
115    private IntValue EvaluatedSolutions {
116      get { return EvaluatedSolutionsParameter.ActualValue; }
117      set { EvaluatedSolutionsParameter.ActualValue = value; }
118    }
119    private IEvaluator Evaluator {
120      get { return EvaluatorParameter.ActualValue; }
121      set { EvaluatorParameter.ActualValue = value; }
122    }
123    private BoolValue ExecutePathRelinking {
124      get { return ExecutePathRelinkingParameter.ActualValue; }
125      set { ExecutePathRelinkingParameter.ActualValue = value; }
126    }
127    private IImprovementOperator Improver {
128      get { return ImproverParameter.ActualValue; }
129      set { ImproverParameter.ActualValue = value; }
130    }
131    private IntValue Iterations {
132      get { return IterationsParameter.ActualValue; }
133      set { IterationsParameter.ActualValue = value; }
134    }
135    private BoolValue Maximization {
136      get { return MaximizationParameter.ActualValue; }
137      set { MaximizationParameter.ActualValue = value; }
138    }
139    private IntValue MaximumIterations {
140      get { return MaximumIterationsParameter.ActualValue; }
141      set { MaximumIterationsParameter.ActualValue = value; }
142    }
143    private IntValue NumberOfHighQualitySolutions {
144      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
145      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
146    }
147    private IPathRelinker PathRelinker {
148      get { return PathRelinkerParameter.ActualValue; }
149      set { PathRelinkerParameter.ActualValue = value; }
150    }
151    private IntValue PopulationSize {
152      get { return PopulationSizeParameter.ActualValue; }
153      set { PopulationSizeParameter.ActualValue = value; }
154    }
155    private IntValue ReferenceSetSize {
156      get { return ReferenceSetSizeParameter.ActualValue; }
157      set { ReferenceSetSizeParameter.ActualValue = value; }
158    }
159    private DoubleValue Quality {
160      get { return QualityParameter.ActualValue; }
161      set { QualityParameter.ActualValue = value; }
162    }
163    private IRandom Random {
164      get { return RandomParameter.ActualValue; }
165      set { RandomParameter.ActualValue = value; }
166    }
167    private VariableCollection Results {
168      get { return ResultsParameter.ActualValue; }
169      set { ResultsParameter.ActualValue = value; }
170    }
171    private ISimilarityCalculator SimilarityCalculator {
172      get { return SimilarityCalculatorParameter.ActualValue; }
173      set { SimilarityCalculatorParameter.ActualValue = value; }
174    }
175    private ISolutionCreator SolutionCreator {
176      get { return SolutionCreatorParameter.ActualValue; }
177      set { SolutionCreatorParameter.ActualValue = value; }
178    }
179    private IItem Target {
180      get { return TargetParameter.ActualValue; }
181      set { TargetParameter.ActualValue = value; }
182    }
183    #endregion
184
185    [StorableConstructor]
186    private ScatterSearchMainLoop(bool deserializing) : base(deserializing) { }
187    private ScatterSearchMainLoop(ScatterSearchMainLoop original, Cloner cloner) : base(original, cloner) { }
188    public ScatterSearchMainLoop() : base() { Initialize(); }
189
190    public override IDeepCloneable Clone(Cloner cloner) {
191      return new ScatterSearchMainLoop(this, cloner);
192    }
193
194    private void Initialize() {
195      #region Create parameters
196      Parameters.Add(new ValueLookupParameter<IMultiAnalyzer>("Analyzer"));
197      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality"));
198      Parameters.Add(new ValueLookupParameter<ICrossover>("Crossover"));
199      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions"));
200      Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator"));
201      Parameters.Add(new ValueLookupParameter<BoolValue>("ExecutePathRelinking"));
202      Parameters.Add(new ValueLookupParameter<IImprovementOperator>("Improver"));
203      Parameters.Add(new ValueLookupParameter<IntValue>("Iterations"));
204      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
205      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations"));
206      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
207      Parameters.Add(new ValueLookupParameter<IPathRelinker>("PathRelinker"));
208      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize"));
209      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
210      Parameters.Add(new ValueLookupParameter<DoubleValue>("Quality"));
211      Parameters.Add(new ValueLookupParameter<IRandom>("Random"));
212      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results"));
213      Parameters.Add(new ValueLookupParameter<ISimilarityCalculator>("SimilarityCalculator"));
214      Parameters.Add(new ValueLookupParameter<ISolutionCreator>("SolutionCreator"));
215      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
216      #endregion
217
218      #region Create operators
219      Placeholder analyzer = new Placeholder();
220      Assigner assigner1 = new Assigner();
221      Assigner assigner2 = new Assigner();
222      ChildrenCreator childrenCreator = new ChildrenCreator();
223      Placeholder crossover = new Placeholder();
224      Comparator iterationsChecker = new Comparator();
225      IntCounter iterationsCounter = new IntCounter();
226      MergingReducer mergingReducer = new MergingReducer();
227      ConditionalBranch executePathRelinkingBranch = new ConditionalBranch();
228      ConditionalBranch newSolutionsBranch = new ConditionalBranch();
229      OffspringProcessor offspringProcessor = new OffspringProcessor();
230      Placeholder pathRelinker = new Placeholder();
231      PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod();
232      ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod();
233      ResultsCollector resultsCollector = new ResultsCollector();
234      RightSelector rightSelector = new RightSelector();
235      Placeholder solutionEvaluator1 = new Placeholder();
236      Placeholder solutionEvaluator2 = new Placeholder();
237      Placeholder solutionImprover1 = new Placeholder();
238      Placeholder solutionImprover2 = new Placeholder();
239      SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod();
240      SolutionsCreator solutionsCreator = new SolutionsCreator();
241      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
242      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
243      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
244      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
245      SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor();
246      ConditionalBranch terminateBranch = new ConditionalBranch();
247      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
248      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
249      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
250      VariableCreator variableCreator = new VariableCreator();
251      #endregion
252
253      #region Create operator graph
254      OperatorGraph.InitialOperator = variableCreator;
255      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
256      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("NewSolutions", new BoolValue(false)));
257      variableCreator.Successor = resultsCollector;
258
259      resultsCollector.CopyValue = new BoolValue(false);
260      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
261      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
262      resultsCollector.Successor = iterationsChecker;
263
264      iterationsChecker.Name = "IterationsChecker";
265      iterationsChecker.Comparison.Value = ComparisonType.GreaterOrEqual;
266      iterationsChecker.LeftSideParameter.ActualName = "Iterations";
267      iterationsChecker.RightSideParameter.ActualName = "MaximumIterations";
268      iterationsChecker.ResultParameter.ActualName = "Terminate";
269      iterationsChecker.Successor = terminateBranch;
270
271      terminateBranch.Name = "TerminateChecker";
272      terminateBranch.ConditionParameter.ActualName = "Terminate";
273      terminateBranch.FalseBranch = referenceSetUpdateMethod;
274
275      referenceSetUpdateMethod.TargetParameter.ActualName = TargetParameter.ActualName;
276      referenceSetUpdateMethod.Successor = assigner1;
277
278      assigner1.Name = "NewSolutions = true";
279      assigner1.LeftSideParameter.ActualName = "NewSolutions";
280      assigner1.RightSideParameter.Value = new BoolValue(true);
281      assigner1.Successor = subScopesProcessor1;
282
283      subScopesProcessor1.DepthParameter.Value = new IntValue(1);
284      subScopesProcessor1.Operators.Add(new EmptyOperator());
285      subScopesProcessor1.Operators.Add(childrenCreator);
286      subScopesProcessor1.Successor = newSolutionsBranch;
287
288      childrenCreator.Name = "SubsetGenerator";
289      childrenCreator.ParentsPerChildParameter.Value = new IntValue(2);
290      childrenCreator.Successor = assigner2;
291
292      assigner2.Name = "NewSolutions = false";
293      assigner2.LeftSideParameter.ActualName = "NewSolutions";
294      assigner2.RightSideParameter.Value = new BoolValue(false);
295      assigner2.Successor = uniformSubScopesProcessor1;
296
297      uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1);
298      uniformSubScopesProcessor1.Operator = executePathRelinkingBranch;
299      uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod;
300
301      executePathRelinkingBranch.Name = "ExecutePathRelinkingChecker";
302      executePathRelinkingBranch.ConditionParameter.ActualName = ExecutePathRelinkingParameter.ActualName;
303      executePathRelinkingBranch.TrueBranch = pathRelinker;
304      executePathRelinkingBranch.FalseBranch = crossover;
305
306      pathRelinker.Name = "PathRelinker";
307      pathRelinker.OperatorParameter.ActualName = "PathRelinker";
308      pathRelinker.Successor = offspringProcessor;
309
310      crossover.Name = "Crossover";
311      crossover.OperatorParameter.ActualName = "Crossover";
312      crossover.Successor = offspringProcessor;
313
314      offspringProcessor.TargetParameter.ActualName = TargetParameter.ActualName;
315      offspringProcessor.Successor = rightSelector;
316
317      rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
318      rightSelector.CopySelected = new BoolValue(false);
319      rightSelector.Successor = subScopesProcessor2;
320
321      subScopesProcessor2.DepthParameter.Value = new IntValue(1);
322      subScopesProcessor2.Operators.Add(new EmptyOperator());
323      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor2);
324      subScopesProcessor2.Successor = mergingReducer;
325
326      uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2);
327      uniformSubScopesProcessor2.Operator = solutionImprover1;
328      uniformSubScopesProcessor2.Successor = subScopesCounter1;
329
330      solutionImprover1.Name = "SolutionImprover";
331      solutionImprover1.OperatorParameter.ActualName = "Improver";
332      solutionImprover1.Successor = solutionEvaluator1;
333
334      solutionEvaluator1.Name = "SolutionEvaluator";
335      solutionEvaluator1.OperatorParameter.ActualName = "Evaluator";
336
337      subScopesCounter1.Name = "Increment EvaluatedSolutions";
338      subScopesCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
339
340      solutionPoolUpdateMethod.QualityParameter.ActualName = QualityParameter.ActualName;
341      solutionPoolUpdateMethod.TargetParameter.ActualName = TargetParameter.ActualName;
342      solutionPoolUpdateMethod.Successor = analyzer;
343
344      analyzer.Name = "Analyzer";
345      analyzer.OperatorParameter.ActualName = "Analyzer";
346
347      newSolutionsBranch.Name = "NewSolutionsChecker";
348      newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions";
349      newSolutionsBranch.TrueBranch = subScopesProcessor1;
350      newSolutionsBranch.FalseBranch = populationRebuildMethod;
351
352      populationRebuildMethod.QualityParameter.ActualName = QualityParameter.ActualName;
353      populationRebuildMethod.Successor = subScopesProcessor3;
354
355      subScopesProcessor3.DepthParameter.Value = new IntValue(1);
356      subScopesProcessor3.Operators.Add(solutionsCreator);
357      subScopesProcessor3.Operators.Add(new EmptyOperator());
358      subScopesProcessor3.Successor = iterationsCounter;
359
360      solutionsCreator.Name = "DiversificationGenerationMethod";
361      solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize";
362      solutionsCreator.Successor = uniformSubScopesProcessor3;
363
364      uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1);
365      uniformSubScopesProcessor3.Operator = solutionImprover2;
366      uniformSubScopesProcessor3.Successor = subScopesCounter2;
367
368      solutionImprover2.Name = "SolutionImprover";
369      solutionImprover2.OperatorParameter.ActualName = "Improver";
370      solutionImprover2.Successor = solutionEvaluator2;
371
372      solutionEvaluator2.Name = "SolutionEvaluator";
373      solutionEvaluator2.OperatorParameter.ActualName = "Evaluator";
374
375      subScopesCounter2.Name = "Increment EvaluatedSolutions";
376      subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
377
378      iterationsCounter.Name = "IterationCounter";
379      iterationsCounter.IncrementParameter.Value = new IntValue(1);
380      iterationsCounter.ValueParameter.ActualName = "Iterations";
381      iterationsCounter.Successor = resultsCollector;
382      #endregion
383    }
384
385    public override IOperation Apply() {
386      if (ImproverParameter.ActualValue == null)
387        return null;
388      return base.Apply();
389    }
390  }
391}
Note: See TracBrowser for help on using the repository browser.