LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelBuildResult.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Xml.Serialization;
5
7
8/// <summary>
9/// Describes the result of building a solver-independent
10/// mathematical model from a lot-sizing instance.
11/// </summary>
12[Serializable]
13[XmlType(TypeName = "mathematicalModelBuildResult")]
15{
16 private readonly List<string> _diagnostics =
17 new();
18
19 /// <summary>
20 /// Initializes an empty mathematical-model build result.
21 /// </summary>
23 {
25 string.Empty;
26
28 string.Empty;
29
31 string.Empty;
32
34 string.Empty;
35 }
36
37 /// <summary>
38 /// Gets or sets the requested formulation identifier.
39 /// </summary>
40 [XmlAttribute("requestedFormulationId")]
42 {
43 get;
44 set;
45 }
46
47 /// <summary>
48 /// Gets or sets the selected formulation identifier.
49 /// </summary>
50 [XmlAttribute("selectedFormulationId")]
52 {
53 get;
54 set;
55 }
56
57 /// <summary>
58 /// Gets or sets the selected formulation name.
59 /// </summary>
60 [XmlAttribute("selectedFormulationName")]
62 {
63 get;
64 set;
65 }
66
67 /// <summary>
68 /// Gets or sets a value indicating whether model
69 /// construction succeeded.
70 /// </summary>
71 [XmlAttribute("isSuccessful")]
72 public bool IsSuccessful
73 {
74 get;
75 set;
76 }
77
78 /// <summary>
79 /// Gets or sets a value indicating whether formulation
80 /// fallback was used.
81 /// </summary>
82 [XmlAttribute("usedFallback")]
83 public bool UsedFallback
84 {
85 get;
86 set;
87 }
88
89 /// <summary>
90 /// Gets or sets the model-construction duration.
91 /// </summary>
92 [XmlIgnore]
93 public TimeSpan BuildDuration
94 {
95 get;
96 set;
97 }
98
99 /// <summary>
100 /// Gets or sets the model-construction duration in
101 /// milliseconds for XML serialization.
102 /// </summary>
103 [XmlAttribute("buildDurationMilliseconds")]
105 {
106 get =>
107 BuildDuration.TotalMilliseconds;
108
109 set =>
111 TimeSpan.FromMilliseconds(
112 value);
113 }
114
115 /// <summary>
116 /// Gets or sets the failure message.
117 /// </summary>
118 [XmlElement("failureMessage")]
119 public string FailureMessage
120 {
121 get;
122 set;
123 }
124
125 /// <summary>
126 /// Gets the diagnostic messages produced during selection
127 /// and model construction.
128 /// </summary>
129 [XmlArray("diagnostics")]
130 [XmlArrayItem("diagnostic")]
131 public List<string> Diagnostics =>
132 _diagnostics;
133
134 /// <summary>
135 /// Gets or sets the generated mathematical model.
136 /// </summary>
137 [XmlElement("model")]
139 {
140 get;
141 set;
142 }
143
144 /// <summary>
145 /// Adds a non-empty diagnostic message.
146 /// </summary>
147 /// <param name="diagnostic">
148 /// Diagnostic message.
149 /// </param>
150 public void AddDiagnostic(
151 string? diagnostic)
152 {
153 if (!string.IsNullOrWhiteSpace(
154 diagnostic))
155 {
156 _diagnostics.Add(
157 diagnostic.Trim());
158 }
159 }
160
161 /// <summary>
162 /// Adds several diagnostic messages.
163 /// </summary>
164 /// <param name="diagnostics">
165 /// Diagnostic messages.
166 /// </param>
167 /// <exception cref="ArgumentNullException">
168 /// Thrown when <paramref name="diagnostics"/> is
169 /// <see langword="null"/>.
170 /// </exception>
171 public void AddDiagnostics(
172 IEnumerable<string> diagnostics)
173 {
174 ArgumentNullException.ThrowIfNull(
175 diagnostics);
176
177 foreach (
178 string diagnostic
179 in diagnostics)
180 {
182 diagnostic);
183 }
184 }
185
186 /// <summary>
187 /// Creates a successful mathematical-model build result.
188 /// </summary>
189 /// <param name="selection">
190 /// Formulation-selection result.
191 /// </param>
192 /// <param name="model">
193 /// Generated mathematical model.
194 /// </param>
195 /// <param name="buildDuration">
196 /// Model-construction duration.
197 /// </param>
198 /// <returns>
199 /// Successful build result.
200 /// </returns>
203 MathematicalModel model,
204 TimeSpan buildDuration)
205 {
206 ArgumentNullException.ThrowIfNull(
207 selection);
208
209 ArgumentNullException.ThrowIfNull(
210 model);
211
212 var result =
214 {
216 selection.RequestedFormulationId,
217
219 selection.SelectedFormulationId,
220
222 selection.SelectedFormulationName,
223
225 true,
226
228 selection.UsedFallback,
229
231 buildDuration,
232
233 Model =
234 model
235 };
236
237 result.AddDiagnostics(
238 selection.Diagnostics);
239
240 return result;
241 }
242
243 /// <summary>
244 /// Creates a failed mathematical-model build result.
245 /// </summary>
246 /// <param name="selection">
247 /// Optional formulation-selection result.
248 /// </param>
249 /// <param name="failureMessage">
250 /// Failure message.
251 /// </param>
252 /// <param name="buildDuration">
253 /// Elapsed model-construction duration.
254 /// </param>
255 /// <returns>
256 /// Failed build result.
257 /// </returns>
260 string failureMessage,
261 TimeSpan buildDuration)
262 {
263 var result =
265 {
267 selection?.RequestedFormulationId ??
268 string.Empty,
269
271 selection?.SelectedFormulationId ??
272 string.Empty,
273
275 selection?.SelectedFormulationName ??
276 string.Empty,
277
279 false,
280
282 selection?.UsedFallback ??
283 false,
284
286 buildDuration,
287
289 failureMessage?.Trim() ??
290 string.Empty
291 };
292
293 if (selection is not null)
294 {
295 result.AddDiagnostics(
296 selection.Diagnostics);
297 }
298
299 result.AddDiagnostic(
300 failureMessage);
301
302 return result;
303 }
304}
double BuildDurationMilliseconds
Gets or sets the model-construction duration in milliseconds for XML serialization.
List< string > Diagnostics
Gets the diagnostic messages produced during selection and model construction.
string SelectedFormulationId
Gets or sets the selected formulation identifier.
string RequestedFormulationId
Gets or sets the requested formulation identifier.
string SelectedFormulationName
Gets or sets the selected formulation name.
MathematicalModelBuildResult()
Initializes an empty mathematical-model build result.
void AddDiagnostic(string? diagnostic)
Adds a non-empty diagnostic message.
void AddDiagnostics(IEnumerable< string > diagnostics)
Adds several diagnostic messages.
MathematicalModel? Model
Gets or sets the generated mathematical model.
static MathematicalModelBuildResult Success(MathematicalModelFormulationSelectionResult selection, MathematicalModel model, TimeSpan buildDuration)
Creates a successful mathematical-model build result.
TimeSpan BuildDuration
Gets or sets the model-construction duration.
bool UsedFallback
Gets or sets a value indicating whether formulation fallback was used.
bool IsSuccessful
Gets or sets a value indicating whether model construction succeeded.
static MathematicalModelBuildResult Failure(MathematicalModelFormulationSelectionResult? selection, string failureMessage, TimeSpan buildDuration)
Creates a failed mathematical-model build result.
Describes the result of selecting a mathematical formulation for a lot-sizing instance.
bool UsedFallback
Gets or sets a value indicating whether the selected formulation differs from the requested formulati...
List< string > Diagnostics
Gets diagnostic messages produced during formulation selection.
Represents a solver-independent mathematical optimization model.