Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs @ 17690

Last change on this file since 17690 was 17317, checked in by abeham, 5 years ago

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/2839_HiveProjectManagement/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.csmergedeligible
    /stable/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.csmergedeligible
    /trunk/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.csmergedeligible
File size: 4.8 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.Data;
27using HeuristicLab.Parameters;
28
29namespace HeuristicLab.Optimization {
30  [Item("Single-Objective Heuristic OptimizationProblem", "A base class for single-objective heuristic optimization problems.")]
31  [StorableType("DFD5588E-6AB2-4712-9083-A405EF21226F")]
32  public abstract class SingleObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, ISingleObjectiveHeuristicOptimizationProblem
33    where T : class, ISingleObjectiveEvaluator
34    where U : class, ISolutionCreator {
35    private const string MaximizationParameterName = "Maximization";
36    private const string BestKnownQualityParameterName = "BestKnownQuality";
37
38    [StorableConstructor]
39    protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
40    protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
41      : base(original, cloner) {
42      RegisterEventHandlers();
43    }
44    protected SingleObjectiveHeuristicOptimizationProblem()
45      : base() {
46      Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
47      Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
48
49      RegisterEventHandlers();
50    }
51
52    protected SingleObjectiveHeuristicOptimizationProblem(T evaluator, U solutionCreator)
53      : base(evaluator, solutionCreator) {
54      Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
55      Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
56
57      RegisterEventHandlers();
58    }
59
60    private void RegisterEventHandlers() {
61      MaximizationParameter.ValueChanged += MaximizationParameterOnValueChanged;
62    }
63
64    private void MaximizationParameterOnValueChanged(object sender, EventArgs e) {
65      OnMaximizationChanged();
66    }
67
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
70      // BackwardsCompatibility3.3
71      #region Backwards compatible code (remove with 3.4)
72      if (BestKnownQualityParameter is ValueParameter<DoubleValue>) {
73        Parameters.Remove(BestKnownQualityParameterName);
74        Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
75      }
76      #endregion
77      RegisterEventHandlers();
78    }
79    public ValueParameter<BoolValue> MaximizationParameter {
80      get { return (ValueParameter<BoolValue>)Parameters[MaximizationParameterName]; }
81    }
82    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
83      get { return MaximizationParameter; }
84    }
85    public BoolValue Maximization {
86      get { return MaximizationParameter.Value; }
87      set {
88        if (Maximization == value) return;
89        MaximizationParameter.Value = value;
90      }
91    }
92
93    public IValueParameter<DoubleValue> BestKnownQualityParameter {
94      get { return (IValueParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
95    }
96    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
97      get { return BestKnownQualityParameter; }
98    }
99    public DoubleValue BestKnownQuality {
100      get { return BestKnownQualityParameter.Value; }
101      set { BestKnownQualityParameter.Value = value; }
102    }
103
104    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
105      get { return Evaluator; }
106    }
107
108    public event EventHandler MaximizationChanged;
109    protected void OnMaximizationChanged() {
110      MaximizationChanged?.Invoke(this, EventArgs.Empty);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.