LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
ExternalSolverResultUtilities.cs
Go to the documentation of this file.
3
5
6/// <summary>
7/// Shared helpers used by optional native-solver adapters.
8/// </summary>
10{
11 /// <summary>
12 /// Populates generic mathematical variable values from a dictionary keyed by
13 /// mathematical variable identifier.
14 /// </summary>
15 /// <param name="result">Result to populate.</param>
16 /// <param name="model">Source mathematical model.</param>
17 /// <param name="values">Solver values keyed by mathematical variable ID.</param>
18 public static void PopulateVariableValues(
21 IReadOnlyDictionary<int, double> values)
22 {
23 ArgumentNullException.ThrowIfNull(result);
24 ArgumentNullException.ThrowIfNull(model);
25 ArgumentNullException.ThrowIfNull(values);
26
27 foreach (MathematicalVariable variable in model.Variables)
28 {
29 if (!values.TryGetValue(variable.Id, out double value))
30 {
31 continue;
32 }
33
34 result.AddVariableValue(
36 variable.Id,
37 value,
38 variable.Name,
39 variable.DomainKey));
40 }
41 }
42
43 /// <summary>
44 /// Evaluates the mathematical objective from a complete or partial value map.
45 /// </summary>
46 /// <param name="model">Mathematical model.</param>
47 /// <param name="values">Values keyed by mathematical variable ID.</param>
48 /// <param name="objectiveValue">Evaluated objective when all referenced values exist.</param>
49 /// <returns>
50 /// <see langword="true"/> when every objective variable has a supplied value.
51 /// </returns>
52 public static bool TryEvaluateObjective(
54 IReadOnlyDictionary<int, double> values,
55 out double objectiveValue)
56 {
57 ArgumentNullException.ThrowIfNull(model);
58 ArgumentNullException.ThrowIfNull(values);
59
60 double value =
62
63 foreach (LinearTerm term in model.Objective.Expression.Terms)
64 {
65 if (!values.TryGetValue(term.VariableId, out double variableValue))
66 {
67 objectiveValue = default;
68 return false;
69 }
70
71 value +=
72 term.Coefficient * variableValue;
73 }
74
75 objectiveValue = value;
76 return double.IsFinite(value);
77 }
78
79 /// <summary>
80 /// Computes absolute and relative objective gaps when an incumbent and bound
81 /// are available.
82 /// </summary>
83 /// <param name="result">Result to update.</param>
84 public static void PopulateGapStatistics(
86 {
87 ArgumentNullException.ThrowIfNull(result);
88
89 if (!result.ObjectiveValue.HasValue ||
90 !result.BestBound.HasValue)
91 {
92 return;
93 }
94
95 result.AbsoluteGap =
96 Math.Abs(
97 result.ObjectiveValue.Value -
98 result.BestBound.Value);
99
100 result.RelativeGap =
101 result.AbsoluteGap.Value /
102 (1.0e-10 +
103 Math.Abs(result.ObjectiveValue.Value));
104 }
105
106 /// <summary>
107 /// Creates an isolated temporary directory for one solver invocation.
108 /// </summary>
109 /// <param name="solverToken">Short solver-specific directory token.</param>
110 /// <returns>Created temporary directory.</returns>
111 public static string CreateTemporaryDirectory(
112 string solverToken)
113 {
114 ArgumentException.ThrowIfNullOrWhiteSpace(solverToken);
115
116 string directory =
117 Path.Combine(
118 Path.GetTempPath(),
119 "LotSizingDataModel",
120 solverToken,
121 Guid.NewGuid().ToString("N"));
122
123 Directory.CreateDirectory(directory);
124 return directory;
125 }
126
127 /// <summary>
128 /// Removes a temporary directory without masking a solver result or error.
129 /// </summary>
130 /// <param name="directory">Directory to remove.</param>
131 public static void TryDeleteDirectory(
132 string directory)
133 {
134 if (string.IsNullOrWhiteSpace(directory))
135 {
136 return;
137 }
138
139 try
140 {
141 if (Directory.Exists(directory))
142 {
143 Directory.Delete(
144 directory,
145 recursive: true);
146 }
147 }
148 catch
149 {
150 // Temporary cleanup is deliberately best effort.
151 }
152 }
153
154 /// <summary>
155 /// Copies the generated portable LP model to the user-requested export path.
156 /// </summary>
157 /// <param name="generatedLpPath">Generated portable LP path.</param>
158 /// <param name="configuredExportPath">Configured export path.</param>
159 public static void ExportPortableModel(
160 string generatedLpPath,
161 string configuredExportPath)
162 {
163 ArgumentException.ThrowIfNullOrWhiteSpace(generatedLpPath);
164
165 string path =
166 string.IsNullOrWhiteSpace(configuredExportPath)
167 ? Path.GetFullPath("LotSizingModel.lp")
168 : Path.GetFullPath(configuredExportPath);
169
170 string? directory =
171 Path.GetDirectoryName(path);
172
173 if (!string.IsNullOrWhiteSpace(directory))
174 {
175 Directory.CreateDirectory(directory);
176 }
177
178 File.Copy(
179 generatedLpPath,
180 path,
181 overwrite: true);
182 }
183}
Stores the solver result for a solver-independent mathematical model.
Stores the value returned by a solver for one mathematical decision variable.
Shared helpers used by optional native-solver adapters.
static void TryDeleteDirectory(string directory)
Removes a temporary directory without masking a solver result or error.
static void PopulateVariableValues(MathematicalModelSolveResult result, MathematicalModel model, IReadOnlyDictionary< int, double > values)
Populates generic mathematical variable values from a dictionary keyed by mathematical variable ident...
static bool TryEvaluateObjective(MathematicalModel model, IReadOnlyDictionary< int, double > values, out double objectiveValue)
Evaluates the mathematical objective from a complete or partial value map.
static void PopulateGapStatistics(MathematicalModelSolveResult result)
Computes absolute and relative objective gaps when an incumbent and bound are available.
static string CreateTemporaryDirectory(string solverToken)
Creates an isolated temporary directory for one solver invocation.
static void ExportPortableModel(string generatedLpPath, string configuredExportPath)
Copies the generated portable LP model to the user-requested export path.
double Constant
Gets or sets the constant value of the expression.
Represents one coefficient-variable term in a linear expression.
Definition LinearTerm.cs:13
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
Definition LinearTerm.cs:49
Represents a solver-independent mathematical optimization model.
MathematicalObjective Objective
Gets or sets the mathematical objective.
LinearExpression Expression
Gets or sets the linear objective expression.
Represents one decision variable in a mathematical optimization model.
string DomainKey
Gets or sets an optional domain key used to map the mathematical variable back to a lot-sizing decisi...
int Id
Gets or sets the unique variable identifier within the mathematical model.