Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalConstraintsParser.cs @ 17723

Last change on this file since 17723 was 17723, checked in by dpiringe, 4 years ago

#3073

  • added regions in IntervalConstraint
  • modified IntervalConstraintsParser to detect regions
File size: 10.9 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Globalization;
27using System.Linq;
28using System.Text.RegularExpressions;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  public static class IntervalConstraintsParser {
32    public static IEnumerable<IntervalConstraint> ParseInput(string              inputText, string target,
33                                                             IEnumerable<string> variables) {
34      if (string.IsNullOrEmpty(inputText)) throw new ArgumentNullException("No input text has been provided.");
35      if (string.IsNullOrEmpty(target)) throw new ArgumentNullException("No target variable has been provided.");
36      if (variables == null) throw new ArgumentNullException("No variables have been provided.");
37      if (!variables.Any()) throw new ArgumentException("Variables are empty.");
38
39      var lines = inputText.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.None);
40      foreach (var line in lines) {
41        var trimmedLine = line.TrimStart();
42        //Check for target-variable constraint
43        if (trimmedLine.StartsWith("Target:")) {
44          var start            = "Target:".Length;
45          var end              = trimmedLine.Length;
46          var targetConstraint = trimmedLine.Substring(start, end - start);
47
48          var match =
49          Regex.Match(targetConstraint,
50                        @"(['](.*)[']|(.*[^\s]))" +
51                        @"\s*(\bin\b)" +
52                        @"\s*([\[])" +
53                        @"\s*(\S*)" + // 6: interval lower bound
54                        @"\s*(\.{2})" +
55                        @"\s*(\S*)" + // 8: interval upper bound
56                        @"\s*([\]])" +
57                        @"(" +
58                          @"\s*,\s*(\S*)\s*=\s*" + // 11: variable name
59                          @"([(])\s*" +
60                          @"(\S*)\s*" + // 13: region lower bound
61                          @"(\.{2})\s*" +
62                          @"(\S*)\s*" + // 15: region upper bound
63                          @"([)])" +
64                        @")*");
65         
66          if (match.Success) {
67            if (match.Groups.Count < 9) throw new ArgumentException("The target-constraint is not complete.", line);
68
69            var targetVariable = match.Groups[1].Value.Trim();
70            if (targetVariable.StartsWith("'") && targetVariable.EndsWith("'"))
71              targetVariable = targetVariable.Substring(1, targetVariable.Length - 2);
72
73            if (targetVariable != target)
74              throw new
75                ArgumentException($"The target variable {targetVariable}  does not match the provided target {target}.",
76                                  line);
77
78            var lowerBound         = ParseIntervalBounds(match.Groups[6].Value);
79            var upperBound         = ParseIntervalBounds(match.Groups[8].Value);
80            var expression         = "Target:" + match.Groups[0].Value;
81            var parsedTarget       = match.Groups[1].Value.Trim();
82            var variable           = targetVariable;
83            var isEnabled          = true;
84            var numberOfDerivation = 0;
85            var interval           = new Interval(lowerBound, upperBound);
86
87            if (match.Groups[10].Success)
88            {
89              IDictionary<string, Interval> pairs = new Dictionary<string, Interval>();
90              // option variables found
91              for(int idx = 0; idx < match.Groups[10].Captures.Count; ++idx)
92              {
93                var inputVar = match.Groups[11].Captures[idx].Value;
94                var regionLb = ParseIntervalBounds(match.Groups[13].Captures[idx].Value);
95                var regionUb = ParseIntervalBounds(match.Groups[15].Captures[idx].Value);
96                pairs.Add(inputVar, new Interval(regionLb, regionUb));
97              }
98              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, pairs, isEnabled);
99            }
100            else
101            {
102              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, isEnabled);
103            }
104          }
105          else {
106            throw new ArgumentException("The inserted target constraint is not valid.", line);
107          }
108
109          //Check for derivation
110        }
111        else if (trimmedLine.StartsWith("d") || trimmedLine.StartsWith("\u2202")) {
112          var match = Regex.Match(trimmedLine,
113                                    @"([d∂])" +
114                                    @"([²³]?)\s*" +
115                                    @"(['](.*)[']|(.*[^\s]))\s*" +
116                                    @"(\/)\s*" +
117                                    @"([d∂])\s*" +
118                                    @"(['](.*)[']|(.*[^\s²³]))\s*" +
119                                    @"([²³]?)\s*\bin\b\s*" +
120                                    @"([\[])\s*" +
121                                    @"(\S*)\s*" +
122                                    @"(\.{2})\s*" +
123                                    @"(\S*)\s*" +
124                                    @"([\]])" +
125                                    @"(" +
126                                      @"\s*,\s*(\S*)\s*=\s*" + // 18: variable name
127                                      @"([(])\s*" +
128                                      @"(\S*)\s*" + // 20: region lower bound
129                                      @"(\.{2})\s*" +
130                                      @"(\S*)\s*" + // 22: region upper bound
131                                      @"([)])" +
132                                    @")*");
133
134          if (match.Success) {
135            if (match.Groups.Count < 24)
136              throw new ArgumentException("The given derivation-constraint is not complete.", line);
137
138            var derivationTarget   = match.Groups[3].Value.Trim();
139            var derivationVariable = match.Groups[8].Value.Trim();
140
141            if (match.Groups[3].Value.Trim().StartsWith("'") && match.Groups[3].Value.Trim().EndsWith("'"))
142              derivationTarget = derivationTarget.Substring(1, derivationTarget.Length - 2);
143
144            if (match.Groups[8].Value.Trim().StartsWith("'") && match.Groups[8].Value.Trim().EndsWith("'"))
145              derivationVariable = derivationVariable.Substring(1, derivationVariable.Length - 2);
146
147            if (derivationTarget != target)
148              throw new
149                ArgumentException($"The target variable {derivationTarget}  does not match the provided target {target}.",
150                                  line);
151
152            if (variables.All(v => v != derivationVariable))
153              throw new ArgumentException($"The given variable {derivationVariable} does not exist in the dataset.",
154                                          line);
155
156            if (match.Groups[2].Value.Trim() != "" || match.Groups[11].Value.Trim() != "") {
157              if (match.Groups[2].Value.Trim() == "" || match.Groups[11].Value.Trim() == "")
158                throw new ArgumentException("Number of derivation has to be written on both sides.", line);
159              if (match.Groups[2].Value.Trim() != match.Groups[11].Value.Trim())
160                throw new ArgumentException("Derivation number is not equal on both sides.", line);
161            }
162
163            var lowerBound         = ParseIntervalBounds(match.Groups[13].Value);
164            var upperBound         = ParseIntervalBounds(match.Groups[15].Value);
165            var expression         = match.Groups[0].Value;
166            var parsedTarget       = derivationTarget;
167            var isEnabled          = true;
168            var variable           = derivationVariable;
169            var numberOfDerivation = ParseDerivationCount(match.Groups[2].Value.Trim());
170            var interval           = new Interval(lowerBound, upperBound);
171
172            if(match.Groups[17].Success)
173            {
174              IDictionary<string, Interval> pairs = new Dictionary<string, Interval>();
175              // option variables found
176              for (int idx = 0; idx < match.Groups[17].Captures.Count; ++idx)
177              {
178                var inputVar = match.Groups[18].Captures[idx].Value;
179                var regionLb = ParseIntervalBounds(match.Groups[20].Captures[idx].Value);
180                var regionUb = ParseIntervalBounds(match.Groups[22].Captures[idx].Value);
181                pairs.Add(inputVar, new Interval(regionLb, regionUb));
182              }
183              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, pairs, isEnabled);
184            } else {
185              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, isEnabled);
186            }
187          }
188          else {
189            throw new ArgumentException("The inserted derivation constraint is not valid.", line);
190          }
191
192          //Check for comment
193        }
194        else if (trimmedLine.StartsWith("#") || trimmedLine == "") {
195          //If it is a comment just continue without saving anything
196        }
197        else {
198          throw new
199            ArgumentException("Error at your constraints definition constraints have to start with (Target: | d | \u2202 | #)",
200                              line);
201        }
202      }
203    }
204
205    private static double ParseIntervalBounds(string input) {
206      input = input.ToLower();
207      switch (input) {
208        case "+inf.":
209        case "inf.":
210          return double.PositiveInfinity;
211        case "-inf.":
212          return double.NegativeInfinity;
213        default: {
214          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
215            return value;
216          throw new ArgumentException("The given boundary is not a double value!");
217        }
218      }
219    }
220
221    private static int ParseDerivationCount(string input) {
222      switch (input) {
223        case "":
224          return 1;
225        case "²":
226          return 2;
227        case "³":
228          return 3;
229        default:
230          int.TryParse(input, out var value);
231          return value;
232      }
233    }
234  }
235}
Note: See TracBrowser for help on using the repository browser.