ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
NamedSolutionValueParser.cs
Go to the documentation of this file.
1using System.Globalization;
2using System.Text.RegularExpressions;
3
5
6/// <summary>
7/// Parses solver text solutions using portable variable names v_&lt;id&gt;.
8/// </summary>
9public static partial class NamedSolutionValueParser
10{
11 /// <summary>Parses a text solution file.</summary>
12 public static IReadOnlyDictionary<int, double> ParseFile(
13 string path)
14 {
15 if (string.IsNullOrWhiteSpace(path) ||
16 !File.Exists(path))
17 {
18 return new Dictionary<int, double>();
19 }
20
21 return ParseLines(
22 File.ReadLines(path));
23 }
24
25 /// <summary>Parses portable variable/value pairs from text lines.</summary>
26 public static IReadOnlyDictionary<int, double> ParseLines(
27 IEnumerable<string> lines)
28 {
29 ArgumentNullException.ThrowIfNull(lines);
30
31 var values =
32 new Dictionary<int, double>();
33
34 foreach (string line in lines)
35 {
36 if (string.IsNullOrWhiteSpace(line))
37 {
38 continue;
39 }
40
41 Match nameMatch =
42 VariableNameRegex().Match(line);
43
44 if (!nameMatch.Success ||
45 !int.TryParse(
46 nameMatch.Groups[1].Value,
47 NumberStyles.Integer,
48 CultureInfo.InvariantCulture,
49 out int variableId))
50 {
51 continue;
52 }
53
54 string suffix =
55 line[
56 (nameMatch.Index +
57 nameMatch.Length)..];
58
59 Match numberMatch =
60 NumberRegex().Match(suffix);
61
62 if (!numberMatch.Success ||
63 !double.TryParse(
64 numberMatch.Value,
65 NumberStyles.Float,
66 CultureInfo.InvariantCulture,
67 out double value) ||
68 !double.IsFinite(value))
69 {
70 continue;
71 }
72
73 values[variableId] =
74 value;
75 }
76
77 return values;
78 }
79
80 [GeneratedRegex(
81 @"\bv_(\d+)\b",
82 RegexOptions.CultureInvariant)]
83 private static partial Regex VariableNameRegex();
84
85 [GeneratedRegex(
86 @"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?",
87 RegexOptions.CultureInvariant)]
88 private static partial Regex NumberRegex();
89}
Parses solver text solutions using portable variable names v_<id>.
static IReadOnlyDictionary< int, double > ParseFile(string path)
Parses a text solution file.
static IReadOnlyDictionary< int, double > ParseLines(IEnumerable< string > lines)
Parses portable variable/value pairs from text lines.