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 @ 17892

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

#3076: refactoring to prepare for trunk reintegration

File size: 5.4 KB
Line 
1using System.Linq;
2using HEAL.Attic;
3using HeuristicLab.Analysis;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
11  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
12  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
13    private const string ProblemDataParameterName = "ProblemData";
14    private const string ConstraintViolationParameterName = "ConstraintViolations";
15    private const string ConstraintUnsatisfiedSolutionsParameterName = "Constraint Unsatisfied Solutions";
16
17    #region parameter properties
18
19    public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter =>
20      (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName];
21
22    public IResultParameter<DataTable> ConstraintViolationParameter =>
23      (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
24
25    public IResultParameter<DataTable> ConstraintUnsatisfiedSolutionsParameter =>
26      (IResultParameter<DataTable>)Parameters[ConstraintUnsatisfiedSolutionsParameterName];
27
28    #endregion
29
30    public override bool EnabledByDefault => false;
31    public static int Iterations { get; set; } = 0;
32
33    public IBoundsEstimator BoundsEstimator { get; set; } = new IntervalArithBoundsEstimator();
34
35    [StorableConstructor]
36    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) { }
37
38    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) :
39      base(original, cloner) { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
43    }
44
45    public SymbolicRegressionConstraintAnalyzer() {
46      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName,
47        "The problem data of the symbolic data analysis problem."));
48      Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName,
49        "Shows the number of constraint violations!"));
50      Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
51        "Shows the number of solutions with unsatisfied constraints."));
52
53
54      ConstraintViolationParameter.DefaultValue = new DataTable(ConstraintViolationParameterName) {
55        VisualProperties = {
56          XAxisTitle = "Generations",
57          YAxisTitle = "Constraint Violations"
58        }
59      };
60
61      ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
62        VisualProperties = {
63          XAxisTitle = "Generations",
64          YAxisTitle = "Constraint Unsatisfied Solutions"
65        }
66      };
67    }
68
69    public override void InitializeState() {
70      Iterations = 0;
71      base.InitializeState();
72    }
73
74    public override void ClearState() {
75      Iterations = 0;
76      base.ClearState();
77    }
78
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      if (Parameters.ContainsKey(ConstraintUnsatisfiedSolutionsParameterName)) return;
83      Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
84        "Shows the number of solutions with unsatisfied constraints."));
85
86      ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
87        VisualProperties = {
88          XAxisTitle = "Generations",
89          YAxisTitle = "Constraint Unsatisfied Solutions"
90        }
91      };
92    }
93
94    public override IOperation Apply() {
95      Iterations++;
96
97      var problemData = RegressionProblemDataParameter.ActualValue;
98      var trees = SymbolicExpressionTreeParameter.ActualValue;
99
100      var results = ResultCollectionParameter.ActualValue;
101      var constraints = problemData.ShapeConstraints.EnabledConstraints;
102      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
103      var intervalCollection = problemData.VariableRanges;
104      var newDataTable = ConstraintViolationParameter.ActualValue;
105      var solutions = SymbolicExpressionTree.ToArray();
106
107      if (newDataTable.Rows.Count == 0)
108        foreach (var constraint in constraints)
109          newDataTable.Rows.Add(new DataRow(constraint.ToString()));
110
111      foreach (var constraint in constraints) {
112        var violations = trees.Count(tree => IntervalUtil.GetConstraintViolation(constraint, BoundsEstimator, intervalCollection, tree) > 0.0);
113
114        newDataTable.Rows[constraint.ToString()].Values.Add(violations);
115      }
116
117      var constraintUnsatisfiedSolutionsDataTable = ConstraintUnsatisfiedSolutionsParameter.ActualValue;
118      if (constraintUnsatisfiedSolutionsDataTable.Rows.Count == 0)
119        constraintUnsatisfiedSolutionsDataTable.Rows.Add(new DataRow(ConstraintUnsatisfiedSolutionsParameterName));
120
121      constraintUnsatisfiedSolutionsDataTable.Rows[ConstraintUnsatisfiedSolutionsParameterName]
122        .Values
123        .Add(solutions.Count(s => IntervalUtil.GetConstraintViolations(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0)));
124
125      return base.Apply();
126    }
127
128   
129  }
130}
Note: See TracBrowser for help on using the repository browser.