Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3073

  • added a check in Interval/IntervalConstraintsParser to throw an exception for multiple variables with the same key
File size: 11.8 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                        @"\s*(<(\S*)>)?"); // 17, 18
66
67         
68          if (match.Success) {
69            if (match.Groups.Count < 19) throw new ArgumentException("The target-constraint is not complete.", line);
70
71            var targetVariable = match.Groups[1].Value.Trim();
72            if (targetVariable.StartsWith("'") && targetVariable.EndsWith("'"))
73              targetVariable = targetVariable.Substring(1, targetVariable.Length - 2);
74
75            if (targetVariable != target)
76              throw new
77                ArgumentException($"The target variable {targetVariable}  does not match the provided target {target}.",
78                                  line);
79
80            var lowerBound         = ParseIntervalBounds(match.Groups[6].Value);
81            var upperBound         = ParseIntervalBounds(match.Groups[8].Value);
82            var expression         = "Target:" + match.Groups[0].Value;
83            var parsedTarget       = match.Groups[1].Value.Trim();
84            var variable           = targetVariable;
85            var isEnabled          = true;
86            var numberOfDerivation = 0;
87            var interval           = new Interval(lowerBound, upperBound);
88            var weight = 1.0;
89
90            if (match.Groups[18].Success && !string.IsNullOrWhiteSpace(match.Groups[18].Value))
91              weight = ParseWeight(match.Groups[18].Value);
92
93            if (match.Groups[10].Success)
94            {
95              IDictionary<string, Interval> pairs = new Dictionary<string, Interval>();
96              // option variables found
97              for(int idx = 0; idx < match.Groups[10].Captures.Count; ++idx)
98              {
99                AddRegion(pairs,
100                  match.Groups[11].Captures[idx].Value,
101                  match.Groups[13].Captures[idx].Value,
102                  match.Groups[15].Captures[idx].Value);
103              }
104              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, pairs, weight, isEnabled);
105            }
106            else
107              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, weight, isEnabled);
108          }
109          else
110            throw new ArgumentException("The inserted target constraint is not valid.", line);
111
112          //Check for derivation
113        }
114        else if (trimmedLine.StartsWith("d") || trimmedLine.StartsWith("\u2202")) {
115          var match = Regex.Match(trimmedLine,
116                                    @"([d∂])" +
117                                    @"([²³]?)\s*" +
118                                    @"(['](.*)[']|(.*[^\s]))\s*" +
119                                    @"(\/)\s*" +
120                                    @"([d∂])\s*" +
121                                    @"(['](.*)[']|(.*[^\s²³]))\s*" +
122                                    @"([²³]?)\s*\bin\b\s*" +
123                                    @"([\[])\s*" +
124                                    @"(\S*)\s*" +
125                                    @"(\.{2})\s*" +
126                                    @"(\S*)\s*" +
127                                    @"([\]])" +
128                                    @"(" +
129                                      @"\s*,\s*(\S*)\s*=\s*" + // 18: variable name
130                                      @"([(])\s*" +
131                                      @"(\S*)\s*" + // 20: region lower bound
132                                      @"(\.{2})\s*" +
133                                      @"(\S*)\s*" + // 22: region upper bound
134                                      @"([)])" +
135                                    @")*" +
136                                    @"\s*(<(\S*)>)?"); // 24, 25
137
138          if (match.Success) {
139            if (match.Groups.Count < 26)
140              throw new ArgumentException("The given derivation-constraint is not complete.", line);
141
142            var derivationTarget   = match.Groups[3].Value.Trim();
143            var derivationVariable = match.Groups[8].Value.Trim();
144
145            if (match.Groups[3].Value.Trim().StartsWith("'") && match.Groups[3].Value.Trim().EndsWith("'"))
146              derivationTarget = derivationTarget.Substring(1, derivationTarget.Length - 2);
147
148            if (match.Groups[8].Value.Trim().StartsWith("'") && match.Groups[8].Value.Trim().EndsWith("'"))
149              derivationVariable = derivationVariable.Substring(1, derivationVariable.Length - 2);
150
151            if (derivationTarget != target)
152              throw new
153                ArgumentException($"The target variable {derivationTarget}  does not match the provided target {target}.",
154                                  line);
155
156            if (variables.All(v => v != derivationVariable))
157              throw new ArgumentException($"The given variable {derivationVariable} does not exist in the dataset.",
158                                          line);
159
160            if (match.Groups[2].Value.Trim() != "" || match.Groups[11].Value.Trim() != "") {
161              if (match.Groups[2].Value.Trim() == "" || match.Groups[11].Value.Trim() == "")
162                throw new ArgumentException("Number of derivation has to be written on both sides.", line);
163              if (match.Groups[2].Value.Trim() != match.Groups[11].Value.Trim())
164                throw new ArgumentException("Derivation number is not equal on both sides.", line);
165            }
166
167            var lowerBound         = ParseIntervalBounds(match.Groups[13].Value);
168            var upperBound         = ParseIntervalBounds(match.Groups[15].Value);
169            var expression         = match.Groups[0].Value;
170            var parsedTarget       = derivationTarget;
171            var isEnabled          = true;
172            var variable           = derivationVariable;
173            var numberOfDerivation = ParseDerivationCount(match.Groups[2].Value.Trim());
174            var interval           = new Interval(lowerBound, upperBound);
175            var weight             = 1.0;
176
177            if(match.Groups[25].Success && !string.IsNullOrWhiteSpace(match.Groups[25].Value))
178              weight = ParseWeight(match.Groups[25].Value);
179
180            if(match.Groups[17].Success)
181            {
182              IDictionary<string, Interval> pairs = new Dictionary<string, Interval>();
183              // option variables found
184              for (int idx = 0; idx < match.Groups[17].Captures.Count; ++idx)
185              {
186                AddRegion(pairs,
187                  match.Groups[18].Captures[idx].Value,
188                  match.Groups[20].Captures[idx].Value,
189                  match.Groups[22].Captures[idx].Value);
190              }
191              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, pairs, weight, isEnabled);
192            } else
193              yield return new IntervalConstraint(expression, variable, parsedTarget, numberOfDerivation, interval, weight, isEnabled);
194          }
195          else
196            throw new ArgumentException("The inserted derivation constraint is not valid.", line);
197
198          //Check for comment
199        }
200        else if (trimmedLine.StartsWith("#") || trimmedLine == "") {
201          //If it is a comment just continue without saving anything
202        }
203        else {
204          throw new
205            ArgumentException("Error at your constraints definition constraints have to start with (Target: | d | \u2202 | #)",
206                              line);
207        }
208      }
209    }
210
211    private static void AddRegion(IDictionary<string, Interval> dict, string variable, string lb, string ub)
212    {
213      var regionLb = ParseIntervalBounds(lb);
214      var regionUb = ParseIntervalBounds(ub);
215      if (dict.ContainsKey(variable))
216        throw new ArgumentException("A constraint cannot contain multiple regions of the same variable.");
217      dict.Add(variable, new Interval(regionLb, regionUb));
218    }
219
220    private static double ParseIntervalBounds(string input) {
221      input = input.ToLower();
222      switch (input) {
223        case "+inf.":
224        case "inf.":
225          return double.PositiveInfinity;
226        case "-inf.":
227          return double.NegativeInfinity;
228        default: {
229          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
230            return value;
231          throw new ArgumentException("The given boundary is not a double value!");
232        }
233      }
234    }
235
236    private static double ParseWeight(string input)
237    {
238      if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
239        return value;
240      throw new ArgumentException("The given weight is not a double value!");
241    }
242
243    private static int ParseDerivationCount(string input) {
244      switch (input) {
245        case "":
246          return 1;
247        case "²":
248          return 2;
249        case "³":
250          return 3;
251        default:
252          int.TryParse(input, out var value);
253          return value;
254      }
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.