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

Last change on this file since 16851 was 16851, checked in by chaider, 3 years ago

#2791 Refactored Constraint Evaluator and Analyzer

File size: 5.7 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
41    #region parameter properties
42    public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter {
43      get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; }
44    }
45    #endregion
46
47    public override bool EnabledByDefault => false;
48
49    [StorableConstructor]
50    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
51    }
52
53    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
58    }
59
60    public SymbolicRegressionConstraintAnalyzer() : base(){
61      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
62    }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66    }
67
68    public override IOperation Apply() {
69      var problemData = RegressionProblemDataParameter.ActualValue;
70      var trees = SymbolicExpressionTreeParameter.ActualValue;
71
72      var results = ResultCollectionParameter.ActualValue;
73      var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
74      var variableRanges = problemData.VariableRanges.VariableIntervals;
75
76      if (!results.ContainsKey(ConstraintViolationsResultName)) {
77        var newDataTable = new DataTable(ConstraintViolationsResultName);
78          foreach (var constraint in constraints) {
79              newDataTable.Rows.Add(new DataRow(constraint.Expression));
80        }
81        results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable));
82      }
83      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
84
85      var interpreter = new IntervalInterpreter();
86      foreach (var constraint in constraints) {
87        int violations = 0;
88        foreach (var tree in trees) {
89          var satisfied = SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree);
90          if (!satisfied) violations++;
91        }
92        dataTable.Rows[constraint.Expression].Values.Add(violations);
93      }
94     
95
96      return base.Apply();
97    }
98
99    public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
100      IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
101
102      if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
103        throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
104
105      Interval resultInterval;
106
107      if (!constraint.IsDerivation) {
108        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
109      } else {
110        var tree = solution;
111        for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
112          tree = DerivativeCalculator.Derive(tree, constraint.Variable);
113        }
114        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
115      }
116
117      var satisfied = constraint.Interval.Contains(resultInterval, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound);
118      return satisfied;
119    }
120
121    public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
122      var intervalInterpreter = new IntervalInterpreter();
123      foreach (var constraint in constraints) {
124        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
125          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
126
127        var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
128        if (!satisfied) return false;
129      }
130      return true;
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.