Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17510 was 17510, checked in by chaider, 4 years ago

#2971

  • Changed constraint violation table to ResultParameter
  • Changed symbol check to include DerivativeCalculator symbols
  • Moved SymbolicRegressionConstraintAnalyzer to plugins root
  • Added Power and Root to IntervalInterpreter
File size: 5.9 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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, ISymbolicExpressionTreeAnalyzer {
38    private const string ConstraintViolationsResultName = "Constraint Violations";
39    private const string ProblemDataParameterName = "ProblemData";
40    private const string ConstraintViolationParameterName = "ConstraintViolations";
41
42    #region parameter properties
43    public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter {
44      get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; }
45    }
46
47    public IResultParameter<DataTable> ConstraintViolationParameter =>
48      (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
49    #endregion
50
51    public override bool EnabledByDefault => false;
52
53    [StorableConstructor]
54    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
55    }
56
57    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
62    }
63
64    public SymbolicRegressionConstraintAnalyzer() : base(){
65      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
66      Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName, "Shows the number of constraint violations!"));
67      ConstraintViolationParameter.DefaultValue = new DataTable(ConstraintViolationParameterName) {
68        VisualProperties = {
69          XAxisTitle = "Generations",
70          YAxisTitle = "Constraint Violations",
71        }
72      };
73    }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77    }
78
79    public override IOperation Apply() {
80      var problemData = RegressionProblemDataParameter.ActualValue;
81      var trees = SymbolicExpressionTreeParameter.ActualValue;
82
83      var results = ResultCollectionParameter.ActualValue;
84      var constraints = problemData.IntervalConstraints.EnabledConstraints;
85      var variableRanges = problemData.VariableRanges.GetIntervals();
86      var newDataTable = ConstraintViolationParameter.ActualValue;
87
88      if(newDataTable.Rows.Count == 0) {
89        foreach (var constraint in constraints) {
90          newDataTable.Rows.Add(new DataRow(constraint.Expression));
91        }
92      }
93
94      var interpreter = new IntervalInterpreter();
95      foreach (var constraint in constraints) {
96        var violations = trees.Select(tree => SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree)).Count(satisfied => !satisfied);
97        newDataTable.Rows[constraint.Expression].Values.Add(violations);
98      }
99     
100      return base.Apply();
101    }
102
103    public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
104      IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
105
106      if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
107        throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
108
109      Interval resultInterval;
110
111      if (!constraint.IsDerivative) {
112        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
113      } else {
114        var tree = solution;
115        for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
116            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
117        }
118        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
119      }
120
121      var satisfied = constraint.Interval.Contains(resultInterval);
122      return satisfied;
123    }
124
125    public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
126      var intervalInterpreter = new IntervalInterpreter();
127      foreach (var constraint in constraints) {
128        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
129          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
130
131        var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
132        if (!satisfied) return false;
133      }
134      return true;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.