ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
CplexXmlSolutionParser.cs
Go to the documentation of this file.
1using System.Globalization;
2using System.Xml.Linq;
3
5
6/// <summary>
7/// Parses CPLEX XML .sol files written by the stand-alone CPLEX optimizer.
8/// </summary>
9internal static class CplexXmlSolutionParser
10{
11 internal static CplexXmlSolution Parse(
12 string path)
13 {
14 if (string.IsNullOrWhiteSpace(path) ||
15 !File.Exists(path))
16 {
17 return new CplexXmlSolution(
18 string.Empty,
19 new Dictionary<int, double>());
20 }
21
22 XDocument document =
23 XDocument.Load(
24 path,
25 LoadOptions.None);
26
27 XElement? header =
28 document
29 .Descendants()
30 .FirstOrDefault(
31 element =>
32 element.Name.LocalName ==
33 "header");
34
35 string status =
36 header?.Attribute(
37 "solutionStatusString")?.Value ??
38 string.Empty;
39
40 var values =
41 new Dictionary<int, double>();
42
43 foreach (XElement variable in
44 document.Descendants().Where(
45 element =>
46 element.Name.LocalName ==
47 "variable"))
48 {
49 string name =
50 variable.Attribute("name")?.Value ??
51 string.Empty;
52
53 if (!name.StartsWith(
54 "v_",
55 StringComparison.Ordinal) ||
56 !int.TryParse(
57 name.AsSpan(2),
58 NumberStyles.Integer,
59 CultureInfo.InvariantCulture,
60 out int id))
61 {
62 continue;
63 }
64
65 string valueText =
66 variable.Attribute("value")?.Value ??
67 string.Empty;
68
69 if (double.TryParse(
70 valueText,
71 NumberStyles.Float,
72 CultureInfo.InvariantCulture,
73 out double value) &&
74 double.IsFinite(value))
75 {
76 values[id] =
77 value;
78 }
79 }
80
81 return new CplexXmlSolution(
82 status,
83 values);
84 }
85}
86
87internal sealed record CplexXmlSolution(
88 string Status,
89 IReadOnlyDictionary<int, double> Values);
Parses CPLEX XML .sol files written by the stand-alone CPLEX optimizer.
record CplexXmlSolution(string Status, IReadOnlyDictionary< int, double > Values)