Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16713 was 16713, checked in by bburlacu, 5 years ago

#2971: Make IntervalConstraintsParser class static, adapt code. Add SymbolicRegressionConstraintSatisfactionEvaluator.

File size: 6.2 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 HEAL.Attic;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33
34namespace 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
39    private const string ProblemDataParameterName = "ProblemData";
40    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
41
42    #region parameter properties
43    public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter {
44      get { return (ILookupParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
45    }
46    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
47      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
48    }
49    #endregion
50    #region properties
51
52    public RegressionProblemData RegressionProblemData {
53      get { return RegressionProblemDataParameter.ActualValue; }
54    }
55
56    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
57      get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
58    }
59    #endregion
60
61    public virtual bool EnabledByDefault {
62      get { return false; }
63    }
64
65    [StorableConstructor]
66    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
67    }
68
69    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner)
70      : base(original, cloner) {
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
75    }
76
77    public SymbolicRegressionConstraintAnalyzer() {
78      Parameters.Add(new LookupParameter<RegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
79      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter for symbolic data analysis expression trees."));
80    }
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84    }
85
86    public override IOperation Apply() {
87      var results = ResultCollectionParameter.ActualValue;
88      var intervalInterpreter = new IntervalInterpreter();
89      if (!results.ContainsKey(ConstraintViolationsResultName)) {
90        var newDataTable = new DataTable(ConstraintViolationsResultName);
91        results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.",
92          newDataTable));
93      }
94
95      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
96      var problemData = RegressionProblemData;
97
98      var constraintViolations = new Dictionary<string, int>();
99
100      var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);
101      var variableRanges = problemData.VariableRanges.VariableIntervals;
102
103      if (dataTable.Rows.Count == 0) {
104        foreach (var constraint in constraints) {
105          if (!dataTable.Rows.ContainsKey(constraint.Expression)) {
106            dataTable.Rows.Add(new DataRow(constraint.Expression));
107          }
108        }
109      }
110
111      foreach (var constraint in constraints) {
112        constraintViolations.Add(constraint.Expression, 0);
113      }
114
115      foreach (var tree in this.SymbolicExpressionTree) {
116        foreach (var constraint in constraints) {
117          if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
118            throw new ArgumentException(
119              $"The given variable {constraint.Variable} in the constraint does not exists in the model.",
120              nameof(IntervalConstraintsParser));
121          if (!constraint.IsDerivation) {
122            var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree,
123              variableRanges);
124            if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
125              constraint.InclusiveUpperBound)) {
126              constraintViolations[constraint.Expression]++;
127            }
128          } else {
129            var dTree = tree;
130            for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
131              dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);
132            }
133
134            var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree,
135              variableRanges);
136            if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
137              constraint.InclusiveUpperBound)) {
138              constraintViolations[constraint.Expression]++;
139            }
140          }
141        }
142      }
143
144      foreach (var kvp in constraintViolations) {
145        dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
146      }
147      return base.Apply();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.