Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationEngineAlgorithm.cs

Last change on this file was 17695, checked in by abeham, 4 years ago

#2521:

  • Moving solution creator parameter from problems to algorithms (breaking wiring in some HeuristicOptimizationProblems)
  • Disallowing evaluator or encoding changes in encoding-specific base problems (to avoid confusion in derived problems whether this needs to be handled or not)
  • Added private set to ReferenceParameter property (serialization)
File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// A base class for heuristic optimization algorithms using an execution engine.
31  /// </summary>
32  [Item("Heuristic Optimization Enigne Algorithm", "A base class for heuristic optimization algorithms using an execution engine.")]
33  [StorableType("A741CA8C-D4DC-4917-8F71-95EA31C97890")]
34  public abstract class HeuristicOptimizationEngineAlgorithm : EngineAlgorithm {
35    public new IHeuristicOptimizationProblem Problem {
36      get { return (IHeuristicOptimizationProblem)base.Problem; }
37      set { base.Problem = value; }
38    }
39
40    [Storable] public IConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; }
41    public ISolutionCreator SolutionCreator {
42      get => SolutionCreatorParameter.Value;
43      set {
44        if (!SolutionCreatorParameter.ValidValues.Contains(value))
45          SolutionCreatorParameter.ValidValues.Add(value);
46        SolutionCreatorParameter.Value = value;
47      }
48    }
49
50    protected HeuristicOptimizationEngineAlgorithm() : base() {
51      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
52
53      RegisterEventHandlers();
54    }
55    protected HeuristicOptimizationEngineAlgorithm(string name) : base(name) {
56      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
57
58      RegisterEventHandlers();
59    }
60    protected HeuristicOptimizationEngineAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) {
61      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
62
63      RegisterEventHandlers();
64    }
65    protected HeuristicOptimizationEngineAlgorithm(string name, string description) : base(name, description) {
66      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
67
68      RegisterEventHandlers();
69    }
70    protected HeuristicOptimizationEngineAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
71      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
72
73      RegisterEventHandlers();
74    }
75
76    [StorableConstructor]
77    protected HeuristicOptimizationEngineAlgorithm(StorableConstructorFlag _) : base(_) { }
78    protected HeuristicOptimizationEngineAlgorithm(HeuristicOptimizationEngineAlgorithm original, Cloner cloner) : base(original, cloner) {
79      SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter);
80
81      RegisterEventHandlers();
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      RegisterEventHandlers();
87    }
88
89    #region Events
90    private void RegisterEventHandlers() {
91      ParameterChangeHandler<ISolutionCreator>.Create(SolutionCreatorParameter, SolutionCreatorOnChanged);
92    }
93    protected override void DeregisterProblemEvents() {
94      Problem.EvaluatorChanged -= Problem_EvaluatorChanged;
95      base.DeregisterProblemEvents();
96    }
97    protected override void RegisterProblemEvents() {
98      base.RegisterProblemEvents();
99      Problem.EvaluatorChanged += Problem_EvaluatorChanged;
100    }
101    protected override void OnProblemChanged() {
102      base.OnProblemChanged();
103      SolutionCreatorParameter.Repopulate(Problem.Operators);
104    }
105
106    protected virtual void SolutionCreatorOnChanged() { }
107    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
108
109    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
110      base.Problem_OperatorsChanged(sender, e);
111      SolutionCreatorParameter.Repopulate(Problem.Operators);
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.