LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalSolutionMappingResult.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Xml.Serialization;
4using LotSizingDataModel.Solution;
5
7
8/// <summary>
9/// Describes the result of mapping a mathematical solver result
10/// back to a normalized lot-sizing solution.
11/// </summary>
12[Serializable]
13[XmlType(TypeName = "mathematicalSolutionMappingResult")]
15{
16 private readonly List<string> _diagnostics =
17 new();
18
19 /// <summary>
20 /// Initializes an empty mathematical-solution mapping result.
21 /// </summary>
23 {
25 string.Empty;
26 }
27
28 /// <summary>
29 /// Gets or sets a value indicating whether mapping
30 /// succeeded.
31 /// </summary>
32 [XmlAttribute("isSuccessful")]
33 public bool IsSuccessful
34 {
35 get;
36 set;
37 }
38
39 /// <summary>
40 /// Gets or sets the number of mathematical variable values
41 /// processed during mapping.
42 /// </summary>
43 [XmlAttribute("processedValueCount")]
45 {
46 get;
47 set;
48 }
49
50 /// <summary>
51 /// Gets or sets the number of mathematical variable values
52 /// ignored during mapping.
53 /// </summary>
54 [XmlAttribute("ignoredValueCount")]
56 {
57 get;
58 set;
59 }
60
61 /// <summary>
62 /// Gets or sets the number of decision categories processed
63 /// during mapping.
64 /// </summary>
65 [XmlAttribute("processedCategoryCount")]
67 {
68 get;
69 set;
70 }
71
72 /// <summary>
73 /// Gets or sets the mapping duration.
74 /// </summary>
75 [XmlIgnore]
76 public TimeSpan MappingDuration
77 {
78 get;
79 set;
80 }
81
82 /// <summary>
83 /// Gets or sets the mapping duration in milliseconds for XML
84 /// serialization.
85 /// </summary>
86 [XmlAttribute("mappingDurationMilliseconds")]
88 {
89 get =>
90 MappingDuration.TotalMilliseconds;
91
92 set =>
94 TimeSpan.FromMilliseconds(
95 value);
96 }
97
98 /// <summary>
99 /// Gets or sets the mapping failure message.
100 /// </summary>
101 [XmlElement("failureMessage")]
102 public string FailureMessage
103 {
104 get;
105 set;
106 }
107
108 /// <summary>
109 /// Gets the mapping diagnostics.
110 /// </summary>
111 [XmlArray("diagnostics")]
112 [XmlArrayItem("diagnostic")]
113 public List<string> Diagnostics =>
114 _diagnostics;
115
116 /// <summary>
117 /// Gets or sets the normalized lot-sizing solution produced
118 /// by the mapping process.
119 /// </summary>
120 [XmlElement("solution")]
121 public LotSizingSolution? Solution
122 {
123 get;
124 set;
125 }
126
127 /// <summary>
128 /// Adds a non-empty diagnostic message.
129 /// </summary>
130 /// <param name="diagnostic">
131 /// Diagnostic message.
132 /// </param>
133 public void AddDiagnostic(
134 string? diagnostic)
135 {
136 if (!string.IsNullOrWhiteSpace(
137 diagnostic))
138 {
139 _diagnostics.Add(
140 diagnostic.Trim());
141 }
142 }
143
144 /// <summary>
145 /// Adds several diagnostic messages.
146 /// </summary>
147 /// <param name="diagnostics">
148 /// Diagnostic messages.
149 /// </param>
150 /// <exception cref="ArgumentNullException">
151 /// Thrown when <paramref name="diagnostics"/> is
152 /// <see langword="null"/>.
153 /// </exception>
154 public void AddDiagnostics(
155 IEnumerable<string> diagnostics)
156 {
157 ArgumentNullException.ThrowIfNull(
158 diagnostics);
159
160 foreach (
161 string diagnostic
162 in diagnostics)
163 {
165 diagnostic);
166 }
167 }
168
169 /// <summary>
170 /// Creates a successful mathematical-solution mapping result.
171 /// </summary>
172 /// <param name="solution">
173 /// Mapped lot-sizing solution.
174 /// </param>
175 /// <param name="mappingDuration">
176 /// Mapping duration.
177 /// </param>
178 /// <returns>
179 /// Successful mapping result.
180 /// </returns>
181 /// <exception cref="ArgumentNullException">
182 /// Thrown when <paramref name="solution"/> is
183 /// <see langword="null"/>.
184 /// </exception>
186 LotSizingSolution solution,
187 TimeSpan mappingDuration)
188 {
189 ArgumentNullException.ThrowIfNull(
190 solution);
191
193 {
195 true,
196
198 mappingDuration,
199
200 Solution =
201 solution
202 };
203 }
204
205 /// <summary>
206 /// Creates a failed mathematical-solution mapping result.
207 /// </summary>
208 /// <param name="failureMessage">
209 /// Mapping failure message.
210 /// </param>
211 /// <param name="mappingDuration">
212 /// Elapsed mapping duration.
213 /// </param>
214 /// <returns>
215 /// Failed mapping result.
216 /// </returns>
218 string failureMessage,
219 TimeSpan mappingDuration)
220 {
221 var result =
223 {
225 false,
226
228 mappingDuration,
229
231 failureMessage?.Trim() ??
232 string.Empty
233 };
234
235 result.AddDiagnostic(
236 failureMessage);
237
238 return result;
239 }
240
241 /// <summary>
242 /// Validates the mathematical-solution mapping result.
243 /// </summary>
244 /// <exception cref="InvalidOperationException">
245 /// Thrown when the result is inconsistent.
246 /// </exception>
247 public void EnsureValid()
248 {
249 if (MappingDuration < TimeSpan.Zero)
250 {
251 throw new InvalidOperationException(
252 "Mapping duration cannot be negative.");
253 }
254
255 if (ProcessedValueCount < 0 ||
256 IgnoredValueCount < 0 ||
258 {
259 throw new InvalidOperationException(
260 "Mapping counters cannot be negative.");
261 }
262
263 if (IsSuccessful &&
264 Solution is null)
265 {
266 throw new InvalidOperationException(
267 "A successful mapping result must contain a " +
268 "lot-sizing solution.");
269 }
270
271 if (!IsSuccessful &&
272 string.IsNullOrWhiteSpace(
274 {
275 throw new InvalidOperationException(
276 "A failed mapping result must contain a failure " +
277 "message.");
278 }
279 }
280}
void EnsureValid()
Validates the mathematical-solution mapping result.
bool IsSuccessful
Gets or sets a value indicating whether mapping succeeded.
int ProcessedCategoryCount
Gets or sets the number of decision categories processed during mapping.
MathematicalSolutionMappingResult()
Initializes an empty mathematical-solution mapping result.
int IgnoredValueCount
Gets or sets the number of mathematical variable values ignored during mapping.
int ProcessedValueCount
Gets or sets the number of mathematical variable values processed during mapping.
LotSizingSolution? Solution
Gets or sets the normalized lot-sizing solution produced by the mapping process.
static MathematicalSolutionMappingResult Failure(string failureMessage, TimeSpan mappingDuration)
Creates a failed mathematical-solution mapping result.
void AddDiagnostics(IEnumerable< string > diagnostics)
Adds several diagnostic messages.
double MappingDurationMilliseconds
Gets or sets the mapping duration in milliseconds for XML serialization.
void AddDiagnostic(string? diagnostic)
Adds a non-empty diagnostic message.
static MathematicalSolutionMappingResult Success(LotSizingSolution solution, TimeSpan mappingDuration)
Creates a successful mathematical-solution mapping result.