Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionConstraintAnalyzer.cs @ 17898

Last change on this file since 17898 was 17898, checked in by gkronber, 3 years ago

#3076 refactoring to prepare for branch reintegration

File size: 5.9 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("Symbolic regression constraint analyser", "Analyzes the number of shape-constraint violations of symbolic regression models.")]
34  public class SymbolicRegressionConstraintAnalyzer : 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 SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) { }
62
63    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) :
64      base(original, cloner) { }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
68    }
69
70    public SymbolicRegressionConstraintAnalyzer() {
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      if (Parameters.ContainsKey(InfeasibleSolutionsParameterName)) return;
98      Parameters.Add(new ResultParameter<DataTable>(InfeasibleSolutionsParameterName,
99        "The number of infeasible solutions."));
100
101      InfeasibleSolutionsParameter.DefaultValue = new DataTable(InfeasibleSolutionsParameterName) {
102        VisualProperties = {
103          XAxisTitle = "Generations",
104          YAxisTitle = "Infeasible solutions"
105        }
106      };
107    }
108
109    public override IOperation Apply() {
110      var problemData = RegressionProblemData;
111      var trees = SymbolicExpressionTree.ToArray();
112
113      var results = ResultCollection;
114      var constraints = problemData.ShapeConstraints.EnabledConstraints;
115      var variableRanges = problemData.VariableRanges;
116      var constraintViolationsTable = ConstraintViolations;
117      var estimator = new IntervalArithBoundsEstimator();
118
119      if (constraintViolationsTable.Rows.Any())
120        foreach (var constraint in constraints)
121          constraintViolationsTable.Rows.Add(new DataRow(constraint.ToString()));
122
123      foreach (var constraint in constraints) {
124        var numViolations = trees.Count(tree => IntervalUtil.GetConstraintViolation(constraint, estimator, variableRanges, tree) > 0.0);
125        constraintViolationsTable.Rows[constraint.ToString()].Values.Add(numViolations);
126      }
127
128      var constraintUnsatisfiedSolutionsDataTable = InfeasibleSolutions;
129      if (constraintUnsatisfiedSolutionsDataTable.Rows.Count == 0)
130        constraintUnsatisfiedSolutionsDataTable.Rows.Add(new DataRow(InfeasibleSolutionsParameterName));
131
132      constraintUnsatisfiedSolutionsDataTable.Rows[InfeasibleSolutionsParameterName]
133        .Values
134        .Add(trees.Count(t => IntervalUtil.GetConstraintViolations(constraints, estimator, variableRanges, t).Any(x => x > 0.0)));
135
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.