1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
9 | public static class IntervalUtil {
|
---|
10 | public static double IntervalConstraintViolation(
|
---|
11 | IntervalConstraint constraint, IBoundsEstimator estimator, IntervalCollection intervalCollection,
|
---|
12 | ISymbolicExpressionTree solution) {
|
---|
13 | var variableRanges = intervalCollection.GetReadonlyDictionary();
|
---|
14 |
|
---|
15 | if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) {
|
---|
16 | throw new ArgumentException(
|
---|
17 | $"The given variable {constraint.Variable} in the constraint does not exist in the model.",
|
---|
18 | nameof(IntervalConstraintsParser));
|
---|
19 | }
|
---|
20 |
|
---|
21 | //Create new variable ranges for defined regions
|
---|
22 | var regionRanges = new IntervalCollection();
|
---|
23 | foreach (var kvp in variableRanges) {
|
---|
24 | if (kvp.Key != constraint.Target && constraint.Regions.GetReadonlyDictionary().TryGetValue(kvp.Key, out var val)) {
|
---|
25 | regionRanges.AddInterval(kvp.Key, val);
|
---|
26 | } else {
|
---|
27 | regionRanges.AddInterval(kvp.Key, kvp.Value);
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | var error = 0.0;
|
---|
32 | if (!constraint.IsDerivative) {
|
---|
33 | error = estimator.CheckConstraint(solution, regionRanges, constraint);
|
---|
34 | } else {
|
---|
35 | var tree = solution;
|
---|
36 | for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
|
---|
37 | if (!estimator.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree)) {
|
---|
38 | throw new ArgumentException("Cube, Root, Power symbols are not supported.");
|
---|
39 | }
|
---|
40 |
|
---|
41 | tree = DerivativeCalculator.Derive(tree, constraint.Variable);
|
---|
42 | }
|
---|
43 |
|
---|
44 | error = estimator.CheckConstraint(tree, regionRanges, constraint);
|
---|
45 | }
|
---|
46 |
|
---|
47 | return error;
|
---|
48 | //Interval resultInterval;
|
---|
49 | //if (!constraint.IsDerivative) {
|
---|
50 | // resultInterval = estimator.GetModelBound(solution, regionRanges);
|
---|
51 | //} else {
|
---|
52 | // var tree = solution;
|
---|
53 | // for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
|
---|
54 | // if (!estimator.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree)) {
|
---|
55 | // throw new ArgumentException("Cube, Root, Power symbols are not supported.");
|
---|
56 | // }
|
---|
57 |
|
---|
58 | // tree = DerivativeCalculator.Derive(tree, constraint.Variable);
|
---|
59 | // }
|
---|
60 |
|
---|
61 | // resultInterval = estimator.GetModelBound(tree, regionRanges);
|
---|
62 | //}
|
---|
63 |
|
---|
64 | //var error = 0.0;
|
---|
65 |
|
---|
66 | //if (!constraint.Interval.Contains(resultInterval.LowerBound)) {
|
---|
67 | // error += Math.Abs(resultInterval.LowerBound - constraint.Interval.LowerBound);
|
---|
68 | //}
|
---|
69 |
|
---|
70 | //if (!constraint.Interval.Contains(resultInterval.UpperBound)) {
|
---|
71 | // error += Math.Abs(resultInterval.UpperBound - constraint.Interval.UpperBound);
|
---|
72 | //}
|
---|
73 |
|
---|
74 | //error *= constraint.Weight;
|
---|
75 |
|
---|
76 | //return error;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static IEnumerable<double> IntervalConstraintsViolation(
|
---|
80 | IEnumerable<IntervalConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection,
|
---|
81 | ISymbolicExpressionTree solution) {
|
---|
82 | return constraints.Select(constraint => IntervalConstraintViolation(constraint, estimator, intervalCollection, solution)).ToList();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | } |
---|