Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971
Interval-Parser:

  • Changed Parser ==> after last group there can be now an infinite amount of whitepsaces
  • Save userinput as property
  • Save variable case-sensitive

NamedIntervals

  • Changed Storable attribute from IEnumerable to KeyValuePair, because old Persistance cant handle IEnumerable

Added SymbolicRegressionConstraintAnalyzer
SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator

  • Fixed checking if a given interval is in another interval
File size: 7.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.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Operators;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
37  [StorableClass]
38  public class SymbolicRegressionConstraintAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    private const string ResultCollectionParameterName = "Results";
40    private const string RegressionSolutionQualitiesResultName = "Constraint Violations";
41
42
43    public ILookupParameter<ResultCollection> ResultCollectionParameter {
44      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
45    }
46
47
48    public virtual bool EnabledByDefault {
49      get { return false; }
50    }
51
52    [StorableConstructor]
53    protected SymbolicRegressionConstraintAnalyzer(bool deserializing) : base(deserializing) {
54    }
55
56    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner)
57      : base(original, cloner) {
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
62    }
63
64    public SymbolicRegressionConstraintAnalyzer() {
65      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName,
66        "The result collection to store the analysis results."));
67
68    }
69
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      // BackwardsCompatibility3.3
73    }
74
75    public override IOperation Apply() {
76      var results = ResultCollectionParameter.ActualValue;
77      IntervalConstraintsParser parser = new IntervalConstraintsParser();
78      var intervalInterpreter = new IntervalInterpreter();
79      if (!results.ContainsKey(RegressionSolutionQualitiesResultName)) {
80        var newDataTable = new DataTable(RegressionSolutionQualitiesResultName);
81        results.Add(new Result(RegressionSolutionQualitiesResultName, "Chart displaying the constraint violatoins.",
82          newDataTable));
83      }
84
85      var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value;
86
87      foreach (var result in results.Where(r => r.Value is IRegressionSolution)) {
88        var solution = (ISymbolicRegressionSolution)result.Value;
89        var constraints =
90          parser.Parse(((RegressionProblemData)solution.ProblemData).IntervalConstraintsParameter.Value.Value);
91        var variableRanges = ((RegressionProblemData)solution.ProblemData).VariableRangesParameter.Value
92          .VariableIntervals;
93
94        if (dataTable.Rows.Count == 0) {
95          foreach (var constraint in constraints) {
96            if (!dataTable.Rows.ContainsKey(constraint.Derivaiton)) {
97              dataTable.Rows.Add(new DataRow(constraint.Derivaiton));
98            }
99          }
100        }
101
102        foreach (var constraint in constraints) {
103          if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
104            throw new ArgumentException(
105              $"The given variable {constraint.Variable} in the constraint does not exists in the model.",
106              nameof(IntervalConstraintsParser));
107          var numberOfViolations = dataTable.Rows[constraint.Derivaiton].Values.Count > 0
108            ? dataTable.Rows[constraint.Derivaiton].Values.Last()
109            : 0;
110          if (!constraint.IsDerivation) {
111            var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree,
112              variableRanges);
113            if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
114              constraint.InclusiveUpperBound)) {
115              dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1);
116            } else {
117              dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations);
118            }
119          } else {
120            var tree = solution.Model.SymbolicExpressionTree;
121            for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
122              tree = DerivativeCalculator.Derive(tree, constraint.Variable);
123            }
124
125            var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree,
126              variableRanges);
127            if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
128              constraint.InclusiveUpperBound)) {
129              dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1);
130            } else {
131              dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations);
132            }
133          }
134        }
135
136      }
137      return base.Apply();
138    }
139
140    private static bool IntervalInBoundaries(Interval i1, Interval i2, bool inclusiveLower, bool inclusiveUpper) {
141      if (double.IsNegativeInfinity(i1.LowerBound) && double.IsPositiveInfinity(i1.UpperBound))
142        return true;
143      //Left-unbounded and right-bounded:
144      if (double.IsNegativeInfinity(i1.LowerBound)) {
145        if (inclusiveUpper)
146          return i2.LowerBound <= i1.UpperBound && i2.UpperBound <= i1.UpperBound;
147        return i2.LowerBound < i1.UpperBound && i2.UpperBound < i1.UpperBound;
148      }
149
150      //Left-bounded and right-unbounded:
151      if (double.IsPositiveInfinity(i1.UpperBound)) {
152        if (inclusiveLower)
153          return i2.LowerBound >= i1.LowerBound && i2.UpperBound >= i1.LowerBound;
154        return i2.LowerBound > i1.LowerBound && i2.UpperBound > i1.LowerBound;
155      }
156
157      //Proper and bounded:
158      //Closed:
159      if (inclusiveLower && inclusiveUpper) {
160        return i1.LowerBound <= i2.LowerBound && i2.UpperBound <= i1.UpperBound;
161      }
162
163      //Open:
164      if (!inclusiveLower && !inclusiveUpper) {
165        return i1.LowerBound < i2.LowerBound && i2.UpperBound < i1.UpperBound;
166      }
167
168      //Left-closed, right-open:
169      if (inclusiveLower) {
170        return i1.LowerBound <= i2.LowerBound && i2.UpperBound < i1.UpperBound;
171      }
172
173      //Left-open, right-closed:
174      return i1.LowerBound < i2.LowerBound && i2.UpperBound <= i1.UpperBound;
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.