Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/AfterCrossoverCombinedOperator.cs @ 8709

Last change on this file since 8709 was 8709, checked in by ascheibe, 11 years ago

#1886 added an operator that combines all operators that need to be executed after crossover

File size: 4.0 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.Operators;
25using HeuristicLab.Optimization;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
31  [Item("AfterCrossoverCombinedOperator", "An operator that contains all operators that need to be executed after crossover.")]
32  [StorableClass]
33  public class AfterCrossoverCombinedOperator : MultiOperator<IOperator> {
34
35    private CrossoverPerformanceAnalyzer cxAnalyzer;
36
37    private ScopeParameter CurrentScopeParameter {
38      get { return (ScopeParameter)Parameters["CurrentScope"]; }
39    }
40    public IScope CurrentScope {
41      get { return CurrentScopeParameter.ActualValue; }
42    }
43    public ILookupParameter<IEvaluator> EvaluatorParameter {
44      get { return ((LookupParameter<IEvaluator>)Parameters["Evaluator"]); }
45    }
46    public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
47      get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
48    }
49
50    [StorableConstructor]
51    private AfterCrossoverCombinedOperator(bool deserializing) : base(deserializing) { }
52    private AfterCrossoverCombinedOperator(AfterCrossoverCombinedOperator original, Cloner cloner)
53      : base(original, cloner) {
54    }
55
56    public AfterCrossoverCombinedOperator()
57      : base() {
58      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose variables and sub-scopes should be removed."));
59      Parameters.Add(new LookupParameter<IEvaluator>("Evaluator", "The operator which is used to evaluate new solutions."));
60      Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
61
62      SimilarityCalculatorParameter.ValueChanged += new System.EventHandler(SimilarityCalculatorParameter_ValueChanged);
63
64      cxAnalyzer = new CrossoverPerformanceAnalyzer();
65      var varDuplicator = new VariableDuplicator();
66
67      Operators.Add(cxAnalyzer);
68      Operators.Add(varDuplicator);
69    }
70
71    void SimilarityCalculatorParameter_ValueChanged(object sender, System.EventArgs e) {
72      SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
73      SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
74      cxAnalyzer.SimilarityCalculatorParameter.Value = SimilarityCalculatorParameter.Value;
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new AfterCrossoverCombinedOperator(this, cloner);
79    }
80
81    public override IOperation Apply() {
82      if (!Operators.Contains(EvaluatorParameter.ActualValue)) {
83        Operators.Insert(0, EvaluatorParameter.ActualValue);
84      }
85
86      OperationCollection next = new OperationCollection(base.Apply());
87      OperationCollection inner = new OperationCollection();
88      inner.Parallel = false;
89
90      for (int i = 0; i < Operators.Count; i++) {
91        inner.Add(ExecutionContext.CreateOperation(Operators[i], CurrentScope));
92      }
93      next.Insert(0, inner);
94      return next;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.