Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/IntervalUtil.cs @ 17928

Last change on this file since 17928 was 17906, checked in by gkronber, 4 years ago

#3073 bugfixing

File size: 2.9 KB
Line 
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;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  public static class IntervalUtil {
30    public static IEnumerable<double> GetConstraintViolations(
31      IEnumerable<ShapeConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection,
32      ISymbolicExpressionTree solution) {
33      return constraints.Select(constraint => GetConstraintViolation(constraint, estimator, intervalCollection, solution)).ToList();
34    }
35
36    public static double GetConstraintViolation(
37      ShapeConstraint constraint, IBoundsEstimator estimator, IntervalCollection variableRanges,
38      ISymbolicExpressionTree tree) {
39      var varRanges = variableRanges.GetReadonlyDictionary();
40
41      if (!string.IsNullOrEmpty(constraint.Variable) && !varRanges.ContainsKey(constraint.Variable)) {
42        throw new ArgumentException(
43          $"No variable range found for variable {constraint.Variable} used in the constraints.",
44          nameof(constraint));
45      }
46
47      // Create new variable ranges for defined regions
48      var regionRanges = new IntervalCollection();
49      foreach (var kvp in varRanges) {
50        if (constraint.Regions.GetReadonlyDictionary().TryGetValue(kvp.Key, out var val)) {
51          regionRanges.AddInterval(kvp.Key, val);
52        } else {
53          regionRanges.AddInterval(kvp.Key, kvp.Value);
54        }
55      }
56
57      if (!constraint.IsDerivative) {
58        return estimator.GetConstraintViolation(tree, regionRanges, constraint);
59      } else {
60        for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
61          if (!estimator.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree)) {
62            throw new ArgumentException("The tree contains an unsupported symbol.");
63          }
64
65          tree = DerivativeCalculator.Derive(tree, constraint.Variable);
66        }
67
68        return estimator.GetConstraintViolation(tree, regionRanges, constraint);
69      }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.