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