Free cookie consent management tool by TermsFeed Policy Generator

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, 5 years ago

#2791 Refactored Constraint Evaluator and Analyzer

File size: 5.7 KB
RevLine 
[10596]1#region License Information
2
3/* HeuristicLab
[16844]4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10596]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
[16851]24using System;
[16592]25using System.Collections.Generic;
[16747]26using System.Linq;
[16713]27using HEAL.Attic;
[10596]28using HeuristicLab.Analysis;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
[16590]31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[10596]32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[16628]36  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
[16851]37  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
[16592]38    private const string ConstraintViolationsResultName = "Constraint Violations";
39    private const string ProblemDataParameterName = "ProblemData";
[16590]40
[16592]41    #region parameter properties
[16851]42    public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter {
43      get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; }
[10596]44    }
[16592]45    #endregion
[16800]46
47    public override bool EnabledByDefault => false;
[10596]48
49    [StorableConstructor]
[16628]50    protected SymbolicRegressionConstraintAnalyzer(StorableConstructorFlag _) : base(_) {
[16590]51    }
52
[16800]53    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
[16590]54    }
55
[10596]56    public override IDeepCloneable Clone(Cloner cloner) {
[16590]57      return new SymbolicRegressionConstraintAnalyzer(this, cloner);
[10596]58    }
59
[16851]60    public SymbolicRegressionConstraintAnalyzer() : base(){
61      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem."));
[10596]62    }
63
[13582]64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66    }
67
[16851]68    public override IOperation Apply() {
69      var problemData = RegressionProblemDataParameter.ActualValue;
70      var trees = SymbolicExpressionTreeParameter.ActualValue;
71
72      var results = ResultCollectionParameter.ActualValue;
[16800]73      var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
74      var variableRanges = problemData.VariableRanges.VariableIntervals;
[16851]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();
[16747]86      foreach (var constraint in constraints) {
[16851]87        int violations = 0;
88        foreach (var tree in trees) {
89          var satisfied = SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree);
90          if (!satisfied) violations++;
[16747]91        }
[16851]92        dataTable.Rows[constraint.Expression].Values.Add(violations);
[16747]93      }
[16851]94     
[16747]95
[16851]96      return base.Apply();
[16747]97    }
98
[16851]99    public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
100      IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
[16747]101
[16851]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
[16800]107      if (!constraint.IsDerivation) {
[16851]108        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
[16800]109      } else {
[16851]110        var tree = solution;
[16800]111        for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
[16851]112          tree = DerivativeCalculator.Derive(tree, constraint.Variable);
[16800]113        }
[16851]114        resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
[16747]115      }
[16800]116
[16851]117      var satisfied = constraint.Interval.Contains(resultInterval, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound);
118      return satisfied;
[16747]119    }
120
[16851]121    public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
122      var intervalInterpreter = new IntervalInterpreter();
[16592]123      foreach (var constraint in constraints) {
[16851]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));
[16592]126
[16851]127        var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution);
128        if (!satisfied) return false;
[16592]129      }
[16851]130      return true;
[16590]131    }
[10596]132  }
133}
Note: See TracBrowser for help on using the repository browser.