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