Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSingleObjectiveProblem.cs @ 17317

Last change on this file since 17317 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
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 System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [StorableType("BC7E7414-64B5-4428-AA89-270F8EF6E35E")]
33  public abstract class SymbolicDataAnalysisSingleObjectiveProblem<T, U, V> : SymbolicDataAnalysisProblem<T, U, V>, ISymbolicDataAnalysisSingleObjectiveProblem
34    where T : class,IDataAnalysisProblemData
35    where U : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<T>
36    where V : class, ISymbolicDataAnalysisSolutionCreator {
37    private const string MaximizationParameterName = "Maximization";
38    private const string BestKnownQualityParameterName = "BestKnownQuality";
39
40    #region parameter properties
41    public IFixedValueParameter<BoolValue> MaximizationParameter {
42      get { return (IFixedValueParameter<BoolValue>)Parameters[MaximizationParameterName]; }
43    }
44    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
45      get { return MaximizationParameter; }
46    }
47    public IFixedValueParameter<DoubleValue> BestKnownQualityParameter {
48      get { return (IFixedValueParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
49    }
50    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
51      get { return BestKnownQualityParameter; }
52    }
53    #endregion
54
55    #region properties
56    public BoolValue Maximization {
57      get { return MaximizationParameter.Value; }
58    }
59    public DoubleValue BestKnownQuality {
60      get { return BestKnownQualityParameter.Value; }
61    }
62    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
63      get { return Evaluator; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    protected SymbolicDataAnalysisSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
69    protected SymbolicDataAnalysisSingleObjectiveProblem(SymbolicDataAnalysisSingleObjectiveProblem<T, U, V> original, Cloner cloner)
70      : base(original, cloner) {
71      RegisterEventHandler();
72      MaximizationParameter.Hidden = true;
73    }
74
75    protected SymbolicDataAnalysisSingleObjectiveProblem(T problemData, U evaluator, V solutionCreator)
76      : base(problemData, evaluator, solutionCreator) {
77      Parameters.Add(new FixedValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized."));
78      Parameters.Add(new FixedValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
79
80      MaximizationParameter.Hidden = true;
81      InitializeOperators();
82      RegisterEventHandler();
83    }
84
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      RegisterEventHandler();
88    }
89
90    private void InitializeOperators() {
91      Operators.Add(new SymbolicDataAnalysisAlleleFrequencyAnalyzer());
92      ParameterizeOperators();
93    }
94
95    private void RegisterEventHandler() {
96      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(QualityParameter_ActualNameChanged);
97      MaximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
98    }
99
100    protected override void OnEvaluatorChanged() {
101      base.OnEvaluatorChanged();
102      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(QualityParameter_ActualNameChanged);
103      Maximization.Value = base.Evaluator.Maximization;
104      ParameterizeOperators();
105    }
106
107    private void QualityParameter_ActualNameChanged(object sender, EventArgs e) {
108      ParameterizeOperators();
109    }
110
111    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
112      OnMaximizationChanged();
113    }
114
115    protected override void ParameterizeOperators() {
116      base.ParameterizeOperators();
117
118      foreach (var op in Operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
119        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
120        op.MaximizationParameter.ActualName = MaximizationParameterName;
121      }
122    }
123
124    public event EventHandler MaximizationChanged;
125    protected void OnMaximizationChanged() {
126      MaximizationChanged?.Invoke(this, EventArgs.Empty);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.