LotSizingDataModel.Instance 2.0.1
Lot-sizing instance representation, descriptors and problem characterization.
Loading...
Searching...
No Matches
LotSizingInstanceRecommendationFactory.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
4
6
7/// <summary>
8/// Creates, applies and refreshes solution-method
9/// recommendation reports for lot-sizing instances.
10/// </summary>
11/// <remarks>
12/// This factory coordinates the supply-chain fingerprint,
13/// the solution-method catalog and
14/// <see cref="SolutionMethodAdvisor"/>.
15/// </remarks>
17{
18 /// <summary>
19 /// Creates a recommendation report using the standard
20 /// solution-method catalog.
21 /// </summary>
22 /// <param name="instance">
23 /// Instance to evaluate.
24 /// </param>
25 /// <param name="recommendedScoreThreshold">
26 /// Minimum score required for a directly compatible
27 /// method to be marked as recommended.
28 /// </param>
29 /// <param name="includeIncompatibleMethods">
30 /// Value indicating whether incompatible methods must be
31 /// retained in the report.
32 /// </param>
33 /// <returns>
34 /// Newly generated recommendation report.
35 /// </returns>
38 LotSizingInstance instance,
39 double recommendedScoreThreshold =
41 .DefaultRecommendedScoreThreshold,
42 bool includeIncompatibleMethods = true)
43 {
44 ArgumentNullException.ThrowIfNull(instance);
45
46 SolutionMethodCatalog catalog =
49
50 return CreateReport(
51 instance,
52 catalog,
53 recommendedScoreThreshold,
54 includeIncompatibleMethods);
55 }
56
57 /// <summary>
58 /// Creates a recommendation report using a supplied
59 /// solution-method catalog.
60 /// </summary>
61 /// <param name="instance">
62 /// Instance to evaluate.
63 /// </param>
64 /// <param name="catalog">
65 /// Catalog containing the methods to evaluate.
66 /// </param>
67 /// <param name="recommendedScoreThreshold">
68 /// Minimum score required for a directly compatible
69 /// method to be marked as recommended.
70 /// </param>
71 /// <param name="includeIncompatibleMethods">
72 /// Value indicating whether incompatible methods must be
73 /// retained in the report.
74 /// </param>
75 /// <returns>
76 /// Newly generated recommendation report.
77 /// </returns>
80 LotSizingInstance instance,
82 double recommendedScoreThreshold =
84 .DefaultRecommendedScoreThreshold,
85 bool includeIncompatibleMethods = true)
86 {
87 ArgumentNullException.ThrowIfNull(instance);
88 ArgumentNullException.ThrowIfNull(catalog);
89
90 ValidateRecommendedScoreThreshold(
91 recommendedScoreThreshold);
92
93 catalog.EnsureValid();
94
95 string supplyChainFingerprint =
98 instance.SupplyChain);
99
100 IReadOnlyList<SolutionMethodRecommendation>
101 recommendations =
103 instance,
104 catalog,
105 recommendedScoreThreshold,
106 includeIncompatibleMethods);
107
108 var report =
110 advisorVersion:
112 catalogName:
113 catalog.CatalogName,
114 catalogVersion:
115 catalog.CatalogVersion,
116 supplyChainFingerprint:
117 supplyChainFingerprint,
118 recommendedScoreThreshold:
119 recommendedScoreThreshold);
120
121 report.ReplaceRecommendations(
122 recommendations);
123
124 report.EnsureValid();
125
126 return report;
127 }
128
129 /// <summary>
130 /// Creates a report from the standard catalog and stores
131 /// it in the instance.
132 /// </summary>
133 /// <param name="instance">
134 /// Instance to update.
135 /// </param>
136 /// <param name="recommendedScoreThreshold">
137 /// Minimum score required for a directly compatible
138 /// method to be marked as recommended.
139 /// </param>
140 /// <param name="includeIncompatibleMethods">
141 /// Value indicating whether incompatible methods must be
142 /// retained in the report.
143 /// </param>
144 /// <returns>
145 /// Report stored in the instance.
146 /// </returns>
149 LotSizingInstance instance,
150 double recommendedScoreThreshold =
152 .DefaultRecommendedScoreThreshold,
153 bool includeIncompatibleMethods = true)
154 {
155 ArgumentNullException.ThrowIfNull(instance);
156
159 instance,
160 recommendedScoreThreshold,
161 includeIncompatibleMethods);
162
164 instance,
165 report);
166
167 return report;
168 }
169
170 /// <summary>
171 /// Creates a report from a supplied catalog and stores it
172 /// in the instance.
173 /// </summary>
174 /// <param name="instance">
175 /// Instance to update.
176 /// </param>
177 /// <param name="catalog">
178 /// Catalog containing the methods to evaluate.
179 /// </param>
180 /// <param name="recommendedScoreThreshold">
181 /// Minimum score required for a directly compatible
182 /// method to be marked as recommended.
183 /// </param>
184 /// <param name="includeIncompatibleMethods">
185 /// Value indicating whether incompatible methods must be
186 /// retained in the report.
187 /// </param>
188 /// <returns>
189 /// Report stored in the instance.
190 /// </returns>
193 LotSizingInstance instance,
194 SolutionMethodCatalog catalog,
195 double recommendedScoreThreshold =
197 .DefaultRecommendedScoreThreshold,
198 bool includeIncompatibleMethods = true)
199 {
200 ArgumentNullException.ThrowIfNull(instance);
201 ArgumentNullException.ThrowIfNull(catalog);
202
205 instance,
206 catalog,
207 recommendedScoreThreshold,
208 includeIncompatibleMethods);
209
211 instance,
212 report);
213
214 return report;
215 }
216
217 /// <summary>
218 /// Stores an existing recommendation report in an
219 /// instance after checking its fingerprint.
220 /// </summary>
221 /// <param name="instance">
222 /// Instance to update.
223 /// </param>
224 /// <param name="report">
225 /// Report to apply.
226 /// </param>
227 public static void ApplyReport(
228 LotSizingInstance instance,
230 {
231 ArgumentNullException.ThrowIfNull(instance);
232 ArgumentNullException.ThrowIfNull(report);
233
234 report.EnsureValid();
235
236 string currentFingerprint =
239 instance.SupplyChain);
240
242 currentFingerprint))
243 {
244 throw new InvalidOperationException(
245 "The solution-method recommendation report " +
246 "does not match the current supply-chain " +
247 "data.");
248 }
249
251 report);
252
253 instance.Touch();
254 }
255
256 /// <summary>
257 /// Determines whether the report stored in an instance is
258 /// current for its supply-chain data.
259 /// </summary>
260 /// <param name="instance">
261 /// Instance whose report must be checked.
262 /// </param>
263 /// <returns>
264 /// <see langword="true"/> when a generated report exists
265 /// and its fingerprint matches the current supply chain;
266 /// otherwise, <see langword="false"/>.
267 /// </returns>
268 public static bool HasCurrentReport(
269 LotSizingInstance instance)
270 {
271 ArgumentNullException.ThrowIfNull(instance);
272
274 {
275 return false;
276 }
277
278 string currentFingerprint =
281 instance.SupplyChain);
282
283 return instance
285 currentFingerprint);
286 }
287
288 /// <summary>
289 /// Regenerates the standard report when it is absent,
290 /// outdated or based on another advisor or catalog
291 /// version.
292 /// </summary>
293 /// <param name="instance">
294 /// Instance to update.
295 /// </param>
296 /// <param name="recommendedScoreThreshold">
297 /// Minimum score required for a directly compatible
298 /// method to be marked as recommended.
299 /// </param>
300 /// <param name="includeIncompatibleMethods">
301 /// Value indicating whether incompatible methods must be
302 /// retained in the report.
303 /// </param>
304 /// <returns>
305 /// <see langword="true"/> when the report was regenerated;
306 /// otherwise, <see langword="false"/>.
307 /// </returns>
309 LotSizingInstance instance,
310 double recommendedScoreThreshold =
312 .DefaultRecommendedScoreThreshold,
313 bool includeIncompatibleMethods = true)
314 {
315 ArgumentNullException.ThrowIfNull(instance);
316
317 ValidateRecommendedScoreThreshold(
318 recommendedScoreThreshold);
319
322
323 bool reportIsCurrent =
324 HasCurrentReport(instance) &&
325 NearlyEqual(
326 currentReport.RecommendedScoreThreshold,
327 recommendedScoreThreshold) &&
328 string.Equals(
329 currentReport.AdvisorVersion,
331 StringComparison.Ordinal) &&
332 string.Equals(
333 currentReport.CatalogName,
336 StringComparison.Ordinal) &&
337 string.Equals(
338 currentReport.CatalogVersion,
341 StringComparison.Ordinal);
342
343 if (reportIsCurrent)
344 {
345 return false;
346 }
347
349 instance,
350 recommendedScoreThreshold,
351 includeIncompatibleMethods);
352
353 return true;
354 }
355
356 /// <summary>
357 /// Clears a stored report when it does not match the
358 /// current supply-chain fingerprint.
359 /// </summary>
360 /// <param name="instance">
361 /// Instance whose report must be checked.
362 /// </param>
363 /// <returns>
364 /// <see langword="true"/> when an outdated report was
365 /// cleared; otherwise, <see langword="false"/>.
366 /// </returns>
367 public static bool ClearOutdatedReport(
368 LotSizingInstance instance)
369 {
370 ArgumentNullException.ThrowIfNull(instance);
371
373 HasCurrentReport(instance))
374 {
375 return false;
376 }
377
379 instance.Touch();
380
381 return true;
382 }
383
384 private static bool NearlyEqual(
385 double left,
386 double right)
387 {
388 return Math.Abs(left - right) <= 1e-12;
389 }
390
391 private static void ValidateRecommendedScoreThreshold(
392 double recommendedScoreThreshold)
393 {
394 if (!double.IsFinite(recommendedScoreThreshold) ||
395 recommendedScoreThreshold < 0.0 ||
396 recommendedScoreThreshold > 1.0)
397 {
398 throw new ArgumentOutOfRangeException(
399 nameof(recommendedScoreThreshold),
400 recommendedScoreThreshold,
401 "The recommended-score threshold must be " +
402 "finite and lie between zero and one.");
403 }
404 }
405}
Creates lot-sizing problem instances and initializes their automatically derived metadata.
static string ComputeSupplyChainFingerprint(SupplyChain supplyChain)
Calculates a SHA-256 fingerprint of a supply-chain object using its compact XML serialization.
Creates, applies and refreshes solution-method recommendation reports for lot-sizing instances.
static SolutionMethodRecommendationReport CreateStandardReport(LotSizingInstance instance, double recommendedScoreThreshold=SolutionMethodAdvisor .DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Creates a recommendation report using the standard solution-method catalog.
static SolutionMethodRecommendationReport CreateReport(LotSizingInstance instance, SolutionMethodCatalog catalog, double recommendedScoreThreshold=SolutionMethodAdvisor .DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Creates a recommendation report using a supplied solution-method catalog.
static SolutionMethodRecommendationReport CreateAndApplyStandardReport(LotSizingInstance instance, double recommendedScoreThreshold=SolutionMethodAdvisor .DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Creates a report from the standard catalog and stores it in the instance.
static SolutionMethodRecommendationReport CreateAndApplyReport(LotSizingInstance instance, SolutionMethodCatalog catalog, double recommendedScoreThreshold=SolutionMethodAdvisor .DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Creates a report from a supplied catalog and stores it in the instance.
static bool RefreshStandardReportIfRequired(LotSizingInstance instance, double recommendedScoreThreshold=SolutionMethodAdvisor .DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Regenerates the standard report when it is absent, outdated or based on another advisor or catalog ve...
static void ApplyReport(LotSizingInstance instance, SolutionMethodRecommendationReport report)
Stores an existing recommendation report in an instance after checking its fingerprint.
static bool HasCurrentReport(LotSizingInstance instance)
Determines whether the report stored in an instance is current for its supply-chain data.
static bool ClearOutdatedReport(LotSizingInstance instance)
Clears a stored report when it does not match the current supply-chain fingerprint.
Adds explicit closed-loop return streams to a lot-sizing instance without changing the historical Sup...
SolutionMethodRecommendationReport SolutionMethodRecommendationReport
Gets or sets the solution-method recommendation report generated for this problem instance.
void ClearSolutionMethodRecommendationReport()
Clears the complete solution-method recommendation report.
bool HasSolutionMethodRecommendationReport
Gets a value indicating whether a recommendation report has been generated or contains recommendation...
void Touch()
Updates the last-modification date of the instance.
bool SolutionMethodRecommendationsMatchFingerprint(string currentSupplyChainFingerprint)
Determines whether the recommendation report was generated for the supplied supply-chain fingerprint.
void ReplaceSolutionMethodRecommendationReport(SolutionMethodRecommendationReport report)
Replaces the complete solution-method recommendation report.
SupplyChain SupplyChain
Gets or sets the supply-chain model represented by the instance.
Evaluates and ranks solution methods for lot-sizing problem instances.
static IReadOnlyList< SolutionMethodRecommendation > Recommend(LotSizingInstance instance, SolutionMethodCatalog catalog, double recommendedScoreThreshold=DefaultRecommendedScoreThreshold, bool includeIncompatibleMethods=true)
Evaluates and ranks every usable method contained in a solution-method catalog.
const string CurrentVersion
Gets the current version of the solution-method advisor evaluation semantics.
Creates predefined catalogs of solution methods for lot-sizing problem instances.
const string StandardCatalogVersion
Gets the current version of the standard solution-method catalog.
const string StandardCatalogName
Gets the name of the standard solution-method catalog.
static SolutionMethodCatalog CreateStandardCatalog()
Creates the standard catalog of lot-sizing solution methods.
Represents an extensible catalog of solution methods that may be evaluated for lot-sizing problem ins...
void EnsureValid()
Validates the catalog and throws an exception when at least one error is found.
string CatalogVersion
Gets or sets the version of the catalog contents and compatibility semantics.
string CatalogName
Gets or sets the human-readable name of the catalog.
Stores the solution-method recommendations generated for one lot-sizing problem instance.
void EnsureValid()
Validates the report and throws an exception when at least one error is found.
double RecommendedScoreThreshold
Gets or sets the score threshold used to mark a directly compatible method as recommended.
string CatalogName
Gets or sets the name of the solution-method catalog used during evaluation.
string AdvisorVersion
Gets or sets the version of the advisor used to produce the recommendations.
bool MatchesSupplyChainFingerprint(string currentFingerprint)
Determines whether the report was generated for the supplied supply-chain fingerprint.
string CatalogVersion
Gets or sets the version of the solution-method catalog used during evaluation.