Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ShapeConstraintsAnalyzer.cs @ 18068

Last change on this file since 18068 was 17958, checked in by gkronber, 4 years ago

#3073: refactoring ShapeConstrainedRegressionProblem as discussed with MKo and CHa

File size: 5.4 KB
RevLine 
[17898]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.Linq;
[17611]23using HEAL.Attic;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
[17958]33  [Item("ShapeConstraintsAnalyzer", "Analyzes the number of shape constraint violations of symbolic regression models.")]
[17901]34  public class ShapeConstraintsAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
[17766]35    private const string ProblemDataParameterName = "ProblemData";
[17898]36    private const string ConstraintViolationsParameterName = "ConstraintViolations";
37    private const string InfeasibleSolutionsParameterName = "InfeasibleSolutions";
[17611]38
39    #region parameter properties
40
41    public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter =>
[17769]42      (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName];
[17611]43
[17898]44    public IResultParameter<DataTable> ConstraintViolationsParameter =>
45      (IResultParameter<DataTable>) Parameters[ConstraintViolationsParameterName];
[17611]46
[17898]47    public IResultParameter<DataTable> InfeasibleSolutionsParameter =>
48      (IResultParameter<DataTable>)Parameters[InfeasibleSolutionsParameterName];
[17792]49
[17611]50    #endregion
51
[17898]52    #region properties
53    public IRegressionProblemData RegressionProblemData => RegressionProblemDataParameter.ActualValue;
54    public DataTable ConstraintViolations => ConstraintViolationsParameter.ActualValue;
55    public DataTable InfeasibleSolutions => InfeasibleSolutionsParameter.ActualValue;
56    #endregion
57
[17611]58    public override bool EnabledByDefault => false;
59
60    [StorableConstructor]
[17901]61    protected ShapeConstraintsAnalyzer(StorableConstructorFlag _) : base(_) { }
[17611]62
[17901]63    protected ShapeConstraintsAnalyzer(ShapeConstraintsAnalyzer original, Cloner cloner) :
[17611]64      base(original, cloner) { }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
[17901]67      return new ShapeConstraintsAnalyzer(this, cloner);
[17611]68    }
69
[17901]70    public ShapeConstraintsAnalyzer() {
[17611]71      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName,
[17733]72        "The problem data of the symbolic data analysis problem."));
[17898]73      Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationsParameterName,
74        "The number of constraint violations."));
75      Parameters.Add(new ResultParameter<DataTable>(InfeasibleSolutionsParameterName,
76        "The number of infeasible solutions."));
[17733]77
[17766]78
[17898]79      ConstraintViolationsParameter.DefaultValue = new DataTable(ConstraintViolationsParameterName) {
[17769]80        VisualProperties = {
81          XAxisTitle = "Generations",
[17898]82          YAxisTitle = "Constraint violations"
[17769]83        }
[17733]84      };
[17735]85
[17898]86      InfeasibleSolutionsParameter.DefaultValue = new DataTable(InfeasibleSolutionsParameterName) {
[17792]87        VisualProperties = {
88          XAxisTitle = "Generations",
[17898]89          YAxisTitle = "Infeasible solutions"
[17792]90        }
91      };
[17611]92    }
93
[17733]94
[17611]95    [StorableHook(HookType.AfterDeserialization)]
[17899]96    private void AfterDeserialization() { }
[17735]97
[17766]98    public override IOperation Apply() {
[17958]99      var problemData = (IShapeConstrainedRegressionProblemData)RegressionProblemData;
[17898]100      var trees = SymbolicExpressionTree.ToArray();
[17733]101
[17898]102      var results = ResultCollection;
[17892]103      var constraints = problemData.ShapeConstraints.EnabledConstraints;
[17898]104      var variableRanges = problemData.VariableRanges;
105      var constraintViolationsTable = ConstraintViolations;
106      var estimator = new IntervalArithBoundsEstimator();
[17611]107
[17906]108      if (!constraintViolationsTable.Rows.Any())
[17611]109        foreach (var constraint in constraints)
[17898]110          constraintViolationsTable.Rows.Add(new DataRow(constraint.ToString()));
[17611]111
112      foreach (var constraint in constraints) {
[17898]113        var numViolations = trees.Count(tree => IntervalUtil.GetConstraintViolation(constraint, estimator, variableRanges, tree) > 0.0);
114        constraintViolationsTable.Rows[constraint.ToString()].Values.Add(numViolations);
[17611]115      }
116
[17958]117      var infeasibleSolutionsDataTable = InfeasibleSolutions;
118      if (infeasibleSolutionsDataTable.Rows.Count == 0)
119        infeasibleSolutionsDataTable.Rows.Add(new DataRow(InfeasibleSolutionsParameterName));
[17792]120
[17958]121      infeasibleSolutionsDataTable.Rows[InfeasibleSolutionsParameterName]
[17792]122        .Values
[17898]123        .Add(trees.Count(t => IntervalUtil.GetConstraintViolations(constraints, estimator, variableRanges, t).Any(x => x > 0.0)));
[17792]124
[17611]125      return base.Apply();
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.