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