Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17906 was 17906, checked in by gkronber, 19 months ago

#3073 bugfixing

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