Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/16/21 14:23:41 (3 years ago)
Author:
gkronber
Message:

#3073 merged reintegration branch to trunk

Location:
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/IntervalUtil.cs

    r17884 r17902  
    1 using System;
     1#region License Information
     2
     3/* HeuristicLab
     4 * Copyright (C) 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
     23using System;
    224using System.Collections.Generic;
    325using System.Linq;
     
    628namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    729  public static class IntervalUtil {
    8     public static double IntervalConstraintViolation(
    9       IntervalConstraint constraint, IBoundsEstimator estimator, IntervalCollection intervalCollection,
     30    public static IEnumerable<double> GetConstraintViolations(
     31      IEnumerable<ShapeConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection,
    1032      ISymbolicExpressionTree solution) {
    11       var variableRanges = intervalCollection.GetReadonlyDictionary();
     33      return constraints.Select(constraint => GetConstraintViolation(constraint, estimator, intervalCollection, solution)).ToList();
     34    }
    1235
    13       if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) {
     36    public static double GetConstraintViolation(
     37      ShapeConstraint constraint, IBoundsEstimator estimator, IntervalCollection variableRanges,
     38      ISymbolicExpressionTree tree) {
     39      var varRanges = variableRanges.GetReadonlyDictionary();
     40
     41      if (constraint.Variable != null && !varRanges.ContainsKey(constraint.Variable)) {
    1442        throw new ArgumentException(
    1543          $"The given variable {constraint.Variable} in the constraint does not exist in the model.",
    16           nameof(IntervalConstraintsParser));
     44          nameof(constraint));
    1745      }
    1846
    19       //Create new variable ranges for defined regions
     47      // Create new variable ranges for defined regions
    2048      var regionRanges = new IntervalCollection();
    21       foreach (var kvp in variableRanges) {
    22         if (kvp.Key != constraint.Target && constraint.Regions.GetReadonlyDictionary().TryGetValue(kvp.Key, out var val)) {
     49      foreach (var kvp in varRanges) {
     50        if (constraint.Regions.GetReadonlyDictionary().TryGetValue(kvp.Key, out var val)) {
    2351          regionRanges.AddInterval(kvp.Key, val);
    2452        } else {
     
    2755      }
    2856
    29       var error = 0.0;
    3057      if (!constraint.IsDerivative) {
    31         error = estimator.CheckConstraint(solution, regionRanges, constraint);
     58        return estimator.GetConstraintViolation(tree, regionRanges, constraint);
    3259      } else {
    33         var tree = solution;
    3460        for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
    3561          if (!estimator.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree)) {
    36             throw new ArgumentException("Cube, Root, Power symbols are not supported.");
     62            throw new ArgumentException("The tree contains an unsupported symbol.");
    3763          }
    3864
     
    4066        }
    4167
    42         error = estimator.CheckConstraint(tree, regionRanges, constraint);
     68        return estimator.GetConstraintViolation(tree, regionRanges, constraint);
    4369      }
    44 
    45       return error;
    46     }
    47 
    48     public static IEnumerable<double> IntervalConstraintsViolation(
    49       IEnumerable<IntervalConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection,
    50       ISymbolicExpressionTree solution) {
    51       return constraints.Select(constraint => IntervalConstraintViolation(constraint, estimator, intervalCollection, solution)).ToList();
    5270    }
    5371  }
Note: See TracChangeset for help on using the changeset viewer.