1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.IO;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Text;
|
---|
7 | using System.Text.RegularExpressions;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|