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;
|
---|
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 {
|
---|
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 | } |
---|