Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisMultiObjectiveProblem.cs @ 18242

Last change on this file since 18242 was 18086, checked in by mkommend, 3 years ago

#2521: Merged trunk changes into branch.

File size: 5.2 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.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableType("E9876DF8-ACFA-41C8-93B7-FA40C57CE459")]
34  public abstract class SymbolicDataAnalysisMultiObjectiveProblem<T, U> : SymbolicDataAnalysisProblem<T, U>, ISymbolicDataAnalysisMultiObjectiveProblem
35    where T : class, IDataAnalysisProblemData
36    where U : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<T> {
37    private const string MaximizationParameterName = "Maximization";
38    private const string BestKnownQualityParameterName = "BestKnownQuality";
39
40    #region parameter properties
41    public IValueParameter<BoolArray> MaximizationParameter {
42      get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
43    }
44    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
45      get { return MaximizationParameter; }
46    }
47    #endregion
48
49    #region properties
50    public BoolArray Maximization {
51      get { return MaximizationParameter.Value; }
52      set { MaximizationParameter.Value = value; }
53    }
54    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
55      get { return Evaluator; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    protected SymbolicDataAnalysisMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
61    protected SymbolicDataAnalysisMultiObjectiveProblem(SymbolicDataAnalysisMultiObjectiveProblem<T, U> original, Cloner cloner)
62      : base(original, cloner) {
63      RegisterEventHandler();
64    }
65
66    public SymbolicDataAnalysisMultiObjectiveProblem(T problemData, U evaluator)
67      : base(problemData, evaluator) {
68      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized."));
69
70      ParameterizeOperators();
71      RegisterEventHandler();
72    }
73
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() {
76      RegisterEventHandler();
77    }
78
79    private void RegisterEventHandler() {
80      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
81      MaximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
82    }
83
84    protected override void OnEvaluatorChanged() {
85      base.OnEvaluatorChanged();
86      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
87      Maximization = new BoolArray(Evaluator.Maximization.ToArray());
88      ParameterizeOperators();
89    }
90
91    private void QualitiesParameter_ActualNameChanged(object sender, EventArgs e) {
92      ParameterizeOperators();
93    }
94
95    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
96      OnMaximizationChanged();
97    }
98
99    protected override void ParameterizeOperators() {
100      base.ParameterizeOperators();
101      foreach (var op in Operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
102        op.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
103        op.MaximizationParameter.ActualName = MaximizationParameterName;
104      }
105
106      // these two crossover operators are compatible with single-objective problems only so we remove them from the operators collection
107      bool pred(IItem x) {
108        return x is SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> || x is SymbolicDataAnalysisExpressionContextAwareCrossover<T>;
109      };
110      Operators.RemoveAll(pred);
111      // if a multi crossover is present, remove them from its operator collection
112      var cx = Operators.FirstOrDefault(x => x is MultiSymbolicDataAnalysisExpressionCrossover<T>);
113      if (cx != null) {
114        var multiCrossover = (MultiSymbolicDataAnalysisExpressionCrossover<T>)cx;
115        var items = multiCrossover.Operators.Where(pred).Cast<ISymbolicExpressionTreeCrossover>().ToList();
116        foreach (var item in items) {
117          multiCrossover.Operators.Remove(item);
118        }
119      }
120    }
121
122    public event EventHandler MaximizationChanged;
123    protected void OnMaximizationChanged() {
124      MaximizationChanged?.Invoke(this, EventArgs.Empty);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.