source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs @ 16590

Last change on this file since 16590 was 16590, checked in by chaider, 6 months ago

#2971
Interval-Parser:

  • Changed Parser ==> after last group there can be now an infinite amount of whitepsaces
  • Save userinput as property
  • Save variable case-sensitive

NamedIntervals

  • Changed Storable attribute from IEnumerable to KeyValuePair, because old Persistance cant handle IEnumerable

Added SymbolicRegressionConstraintAnalyzer
SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator

  • Fixed checking if a given interval is in another interval
File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Text.RegularExpressions;
8using System.Threading.Tasks;
9using HeuristicLab.Optimization;
10
11namespace HeuristicLab.Problems.DataAnalysis {
12  public class IntervalConstraintsParser {
13    public IntervalConstraintsParser() {}
14    private List<IntervalConstraint> intervalConstraints = new List<IntervalConstraint>();
15    public List<IntervalConstraint> Parse(string input) {
16      var options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
17      var matches = Regex.Matches(input, @"^(.*)\bin\b\s*([\[\]])(.*[^\s])(\s*\.\.\s*)([^\s].*)([\[\]])\r?\s*$", options);
18
19      for (var i = 0; i < matches.Count; ++i) {
20        if (matches[i].Groups.Count == 7) { 
21          var intervalConstraint = new IntervalConstraint();
22          //pattern 1 = defintion
23          var definition = Regex.Replace(matches[i].Groups[1].Value, @"\s *", "");
24          if (Regex.IsMatch(definition, @"\/")) {
25            var splitted = Regex.Split(definition.Replace(" ", string.Empty), @"\/");
26            var match = Regex.Match(splitted[0], @"([d∂])([0-9]|[²³])?(.*[^\s*])");
27            if (match.Success) {
28              intervalConstraint.NumberOfDerivation = match.Groups[2].Success ? ParseDerivationCount(match.Groups[2].Value) : 1;
29              intervalConstraint.Definition = match.Groups[3].Value;
30              intervalConstraint.IsDerivation = true;
31              var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^²³])([²³])?");
32              if (formulation.Success) {
33                intervalConstraint.Variable = formulation.Groups[2].Success ? formulation.Groups[2].Value : "";
34              }
35            } else {
36              throw new ArgumentException($"An error occured in the derivation part: {splitted[0]}");
37            }
38          } else {
39            intervalConstraint.Definition = Regex.Match(definition, @".*[^.\s]*").Value;
40            intervalConstraint.IsDerivation = false;
41          }
42          intervalConstraint.Derivaiton = matches[i].Groups[0].Value;
43          intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "[");
44          intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]");
45          intervalConstraint.Interval = new Interval(ParseIntervalBounds(matches[i].Groups[3].Value.Replace(" ", string.Empty)), 
46            ParseIntervalBounds(matches[i].Groups[5].Value.Replace(" ", string.Empty)));
47
48          intervalConstraints.Add(intervalConstraint);
49        } else {
50          throw new ArgumentException($"The given constraint: {matches[i].Value} is not valid.");
51        }
52      }
53
54      return intervalConstraints;
55    }
56
57    private static double ParseIntervalBounds(string input) {
58      input = input.ToLower();
59      switch (input) {
60        case "+inf.":
61        case "inf.":
62          return double.PositiveInfinity;
63        case "-inf.":
64          return double.NegativeInfinity;
65        default: {
66          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) {
67            return value;
68          } else {
69            throw new ArgumentException("The given Interval in the constraint is not valid.");
70          }
71        }
72      }
73    }
74
75    private static int ParseDerivationCount(string input) {
76      switch (input) {
77        case "²":
78          return 2;
79        case "³":
80          return 3;
81        default:
82          int.TryParse(input, out var value);
83          return value;
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.