Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs @ 16756

Last change on this file since 16756 was 16756, checked in by chaider, 5 years ago

#2971 Changed Interval Constraints View
-Added View for successfully parsed Constraints
-Save Constraints as IntervalConstraints

File size: 7.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2018 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
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using HEAL.Attic;
28using HeuristicLab.Analysis;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
36  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
37  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
38    private const string ConstraintViolationsResultName = "Constraint Violations";
39
40    private const string ProblemDataParameterName = "ProblemData";
41    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
42
43    #region parameter properties
44    public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter {
45      get { return (ILookupParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
46    }
47    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
48      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
49    }
50    #endregion
51    #region properties
52
53    public RegressionProblemData RegressionProblemData {
54      get { return RegressionProblemDataParameter.ActualValue; }
55    }
56
57    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
58      get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
59    }
60    #endregion
61
62    public virtual bool EnabledByDefault {
63      get { return false; }
64    }
65
66    [StorableConstructor]
67    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
68    }
69
70    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner)
71      : 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    static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model,
88      IRegressionProblemData problemData) {
89      //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraints.Value);
90      var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints;
91      var constraintViolations = new Dictionary<IntervalConstraint, bool>();
92      var variableRanges = ((RegressionProblemData)problemData).VariableRanges.VariableIntervals;
93
94      foreach (var constraint in constraints) {
95        if (!constraintViolations.ContainsKey(constraint)) {
96          constraintViolations.Add(constraint, CheckConstraint(model, variableRanges, constraint));
97        }
98      }
99
100      return constraintViolations;
101    }
102
103    static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,
104      IRegressionProblemData problemData) {
105      return CheckConstraints(model.SymbolicExpressionTree, problemData);
106    }
107
108    static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges,
109      IntervalConstraint constraint) {
110      var intervalInterpreter = new IntervalInterpreter();
111      if (constraint.Variable != null) {
112        if (!constraint.IsDerivation) {
113          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model,
114            variableRanges);
115          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
116            constraint.InclusiveUpperBound)) {
117            return false;
118          }
119        } else {
120          var dTree = model;
121          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
122            dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);
123          }
124          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree,
125            variableRanges);
126          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
127            constraint.InclusiveUpperBound)) {
128            return false;
129          }
130        }
131      }
132      return true;
133    }
134
135    static bool CheckConstraint(ISymbolicRegressionModel model, Dictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
136      return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint);
137    }
138
139    public override IOperation Apply() {
140      var results = ResultCollectionParameter.ActualValue;
141      if (!results.ContainsKey(ConstraintViolationsResultName)) {
142        var newDataTable = new DataTable(ConstraintViolationsResultName);
143        results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.",
144          newDataTable));
145      }
146      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
147      var problemData = RegressionProblemData;
148      var constraintViolations = new Dictionary<string, int>();
149
150      //var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);
151      var constraints = problemData.IntervalConstraintsParameter.Value.Constraints;
152
153      if (dataTable.Rows.Count == 0) {
154        foreach (var constraint in constraints) {
155          if (!dataTable.Rows.ContainsKey(constraint.Expression)) {
156            dataTable.Rows.Add(new DataRow(constraint.Expression));
157          }
158        }
159      }
160
161      foreach (var constraint in constraints) {
162        constraintViolations.Add(constraint.Expression, 0);
163      }
164
165      foreach (var tree in this.SymbolicExpressionTree) {
166        var checkedConstraints = CheckConstraints(tree, problemData);
167
168        foreach (var kvp in checkedConstraints) {
169          if (!kvp.Value)
170            constraintViolations[kvp.Key.Expression]++;
171        }
172      }
173
174      foreach (var kvp in constraintViolations) {
175        dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
176      }
177      return base.Apply();
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.