Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

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