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
RevLine 
[5580]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5580]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
[17317]22using System;
[5618]23using System.Linq;
[17317]24using HEAL.Attic;
[5580]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[18086]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5580]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[16723]33  [StorableType("E9876DF8-ACFA-41C8-93B7-FA40C57CE459")]
[17695]34  public abstract class SymbolicDataAnalysisMultiObjectiveProblem<T, U> : SymbolicDataAnalysisProblem<T, U>, ISymbolicDataAnalysisMultiObjectiveProblem
[18086]35    where T : class, IDataAnalysisProblemData
[17695]36    where U : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<T> {
[5580]37    private const string MaximizationParameterName = "Maximization";
38    private const string BestKnownQualityParameterName = "BestKnownQuality";
39
40    #region parameter properties
[5618]41    public IValueParameter<BoolArray> MaximizationParameter {
42      get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
[5580]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; }
[5618]52      set { MaximizationParameter.Value = value; }
[5580]53    }
54    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
55      get { return Evaluator; }
56    }
57    #endregion
58
59    [StorableConstructor]
[16723]60    protected SymbolicDataAnalysisMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
[17695]61    protected SymbolicDataAnalysisMultiObjectiveProblem(SymbolicDataAnalysisMultiObjectiveProblem<T, U> original, Cloner cloner)
[5685]62      : base(original, cloner) {
63      RegisterEventHandler();
64    }
[5580]65
[17695]66    public SymbolicDataAnalysisMultiObjectiveProblem(T problemData, U evaluator)
67      : base(problemData, evaluator) {
[5580]68      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized."));
[5685]69
70      ParameterizeOperators();
71      RegisterEventHandler();
[5580]72    }
[5618]73
[5685]74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() {
76      RegisterEventHandler();
77    }
78
79    private void RegisterEventHandler() {
[17317]80      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
81      MaximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
[5685]82    }
83
[5618]84    protected override void OnEvaluatorChanged() {
85      base.OnEvaluatorChanged();
[17317]86      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
[5618]87      Maximization = new BoolArray(Evaluator.Maximization.ToArray());
[5685]88      ParameterizeOperators();
[5618]89    }
[5685]90
[17317]91    private void QualitiesParameter_ActualNameChanged(object sender, EventArgs e) {
[5685]92      ParameterizeOperators();
93    }
94
[17317]95    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
96      OnMaximizationChanged();
97    }
98
[5685]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      }
[18086]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      }
[5685]120    }
[17317]121
122    public event EventHandler MaximizationChanged;
123    protected void OnMaximizationChanged() {
124      MaximizationChanged?.Invoke(this, EventArgs.Empty);
125    }
[5580]126  }
127}
Note: See TracBrowser for help on using the repository browser.