Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/IntervalUtil.cs @ 17768

Last change on this file since 17768 was 17768, checked in by chaider, 4 years ago

#3073

  • Removed Region class and used IntervallCollection instead
  • Changed Parser to work with IntervalColletions
  • Moved CheckConstraint methods from Analyzer to IntervalUtil class
  • Added CheckConstraint method to interface to check if an interval is in a given constraint
  • Added possibility to stop splitting as soon as a constraint is fulfiled
File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7
8namespace 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 Interval 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 * constraint.Weight;
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
83            .Select(constraint => IntervalConstraintViolation(constraint, estimator, intervalCollection, solution))
84            .ToList();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.