Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16755 was 16747, checked in by chaider, 6 years ago

#2971 Added static CheckConstrain and CheckConstraints methods to SymbolicRegressionConstraintAnalyzer. Changed Apply method to use new CheckConstraints method

File size: 7.3 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 constraintViolations = new Dictionary<IntervalConstraint, bool>();
91      var variableRanges = ((RegressionProblemData)problemData).VariableRanges.VariableIntervals;
92
93      foreach (var constraint in constraints) {
94        if (!constraintViolations.ContainsKey(constraint)) {
95          constraintViolations.Add(constraint, CheckConstraint(model, variableRanges, constraint));
96        }
97      }
98
99      return constraintViolations;
100    }
101
102    static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,
103      IRegressionProblemData problemData) {
104      return CheckConstraints(model.SymbolicExpressionTree, problemData);
105    }
106
107    static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges,
108      IntervalConstraint constraint) {
109      var intervalInterpreter = new IntervalInterpreter();
110      if (constraint.Variable != null) {
111        if (!constraint.IsDerivation) {
112          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model,
113            variableRanges);
114          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
115            constraint.InclusiveUpperBound)) {
116            return false;
117          }
118        } else {
119          var dTree = model;
120          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
121            dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);
122          }
123          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree,
124            variableRanges);
125          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
126            constraint.InclusiveUpperBound)) {
127            return false;
128          }
129        }
130      }
131      return true;
132    }
133
134    static bool CheckConstraint(ISymbolicRegressionModel model, Dictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
135      return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint);
136    }
137
138    public override IOperation Apply() {
139      var results = ResultCollectionParameter.ActualValue;
140      if (!results.ContainsKey(ConstraintViolationsResultName)) {
141        var newDataTable = new DataTable(ConstraintViolationsResultName);
142        results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.",
143          newDataTable));
144      }
145      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
146      var problemData = RegressionProblemData;
147      var constraintViolations = new Dictionary<string, int>();
148
149      var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);
150
151      if (dataTable.Rows.Count == 0) {
152        foreach (var constraint in constraints) {
153          if (!dataTable.Rows.ContainsKey(constraint.Expression)) {
154            dataTable.Rows.Add(new DataRow(constraint.Expression));
155          }
156        }
157      }
158
159      foreach (var constraint in constraints) {
160        constraintViolations.Add(constraint.Expression, 0);
161      }
162
163      foreach (var tree in this.SymbolicExpressionTree) {
164        var checkedConstraints = CheckConstraints(tree, problemData);
165
166        foreach (var kvp in checkedConstraints) {
167          if (!kvp.Value)
168            constraintViolations[kvp.Key.Expression]++;
169        }
170      }
171
172      foreach (var kvp in constraintViolations) {
173        dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
174      }
175      return base.Apply();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.