1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Analysis;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
35 | [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
|
---|
36 | public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
|
---|
37 | private const string ConstraintViolationsResultName = "Constraint Violations";
|
---|
38 | private const string ProblemDataParameterName = "ProblemData";
|
---|
39 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
|
---|
40 |
|
---|
41 | #region parameter properties
|
---|
42 |
|
---|
43 | public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter {
|
---|
44 | get { return (ILookupParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
|
---|
48 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | #region properties
|
---|
54 |
|
---|
55 | public RegressionProblemData RegressionProblemData {
|
---|
56 | get { return RegressionProblemDataParameter.ActualValue; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
|
---|
60 | get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | public override bool EnabledByDefault => false;
|
---|
66 |
|
---|
67 | [StorableConstructor]
|
---|
68 | protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
75 | return new SymbolicRegressionConstraintAnalyzer(this, cloner);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public SymbolicRegressionConstraintAnalyzer() {
|
---|
79 | Parameters.Add(new LookupParameter<RegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
|
---|
80 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter for symbolic data analysis expression trees."));
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
84 | private void AfterDeserialization() {
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, IRegressionProblemData problemData) {
|
---|
88 | var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
|
---|
89 | var constraintViolations = new Dictionary<IntervalConstraint, bool>();
|
---|
90 | var variableRanges = problemData.VariableRanges.VariableIntervals;
|
---|
91 | foreach (var constraint in constraints) {
|
---|
92 | if (!constraintViolations.ContainsKey(constraint)) {
|
---|
93 | constraintViolations.Add(constraint, CheckConstraint(model, variableRanges, constraint));
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | return constraintViolations;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, IRegressionProblemData problemData) {
|
---|
101 | return CheckConstraints(model.SymbolicExpressionTree, problemData);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static bool CheckConstraint(ISymbolicExpressionTree model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
|
---|
105 | var intervalInterpreter = new IntervalInterpreter();
|
---|
106 | if (!constraint.IsDerivation) {
|
---|
107 | var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model, variableRanges);
|
---|
108 | if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | } else {
|
---|
112 | var dTree = model;
|
---|
113 | for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
|
---|
114 | dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);
|
---|
115 | }
|
---|
116 |
|
---|
117 | var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree, variableRanges);
|
---|
118 | if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) {
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static bool CheckConstraint(ISymbolicRegressionModel model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
|
---|
127 | return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public override IOperation Apply() {
|
---|
131 | var results = ResultCollectionParameter.ActualValue;
|
---|
132 | if (!results.ContainsKey(ConstraintViolationsResultName)) {
|
---|
133 | var newDataTable = new DataTable(ConstraintViolationsResultName);
|
---|
134 | results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable));
|
---|
135 | }
|
---|
136 |
|
---|
137 | var dataTable = (DataTable) results[ConstraintViolationsResultName].Value;
|
---|
138 | var problemData = RegressionProblemData;
|
---|
139 | var constraintViolations = new Dictionary<string, int>();
|
---|
140 | var constraints = problemData.IntervalConstraintsParameter.Value.Constraints.Where(x => x.Enabled);
|
---|
141 | if (dataTable.Rows.Count == 0) {
|
---|
142 | foreach (var constraint in constraints) {
|
---|
143 | if (!dataTable.Rows.ContainsKey(constraint.Expression)) {
|
---|
144 | dataTable.Rows.Add(new DataRow(constraint.Expression));
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | foreach (var constraint in constraints) {
|
---|
150 | constraintViolations.Add(constraint.Expression, 0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | foreach (var tree in this.SymbolicExpressionTree) {
|
---|
154 | var checkedConstraints = CheckConstraints(tree, problemData);
|
---|
155 | foreach (var kvp in checkedConstraints) {
|
---|
156 | if (!kvp.Value) constraintViolations[kvp.Key.Expression]++;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | foreach (var kvp in constraintViolations) {
|
---|
161 | dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
|
---|
162 | }
|
---|
163 |
|
---|
164 | return base.Apply();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|