LotSizingDataModel.Checker 2.0.1
Reusable validation engine for structural, mathematical and objective consistency checks.
Loading...
Searching...
No Matches
LotSizingSolutionVerificationService.cs
Go to the documentation of this file.
6using LotSizingDataModel.Instance;
7using LotSizingDataModel.Instance.Results;
8using LotSizingDataModel.Solution;
9using LotSizingDataModel.Solver.Modeling;
10
12
13/// <summary>
14/// High-level facade for checking a lot-sizing solution and, when requested,
15/// recording the independent evaluation on serializable domain objects.
16/// </summary>
18{
19 private readonly ILotSizingSolutionChecker _checker;
20 private readonly ISolutionCheckResultApplier _applier;
21
22 /// <summary>
23 /// Initializes the facade with the standard checker and result applier.
24 /// </summary>
31
32 /// <summary>
33 /// Initializes the facade with explicit dependencies.
34 /// </summary>
35 /// <param name="checker">Independent lot-sizing solution checker.</param>
36 /// <param name="applier">Checker-result domain integration policy.</param>
40 {
41 _checker =
42 checker ??
43 throw new ArgumentNullException(nameof(checker));
44
45 _applier =
46 applier ??
47 throw new ArgumentNullException(nameof(applier));
48 }
49
50 /// <summary>
51 /// Checks a standalone candidate solution and optionally writes the
52 /// resulting evaluation into the candidate.
53 /// </summary>
54 /// <param name="instance">Lot-sizing instance defining the problem.</param>
55 /// <param name="solution">Candidate solution to verify.</param>
56 /// <param name="options">Optional verification configuration.</param>
57 /// <param name="cancellationToken">Cancellation token.</param>
58 /// <returns>The independent check and application results.</returns>
59 public Task<LotSizingSolutionVerificationResult> VerifyAsync(
60 LotSizingInstance instance,
61 LotSizingSolution solution,
62 SolutionVerificationOptions? options = null,
63 CancellationToken cancellationToken = default)
64 {
65 return VerifyCoreAsync(
66 instance,
67 solution,
68 knownResult: null,
69 mathematicalModel: null,
70 CloneVerificationOptions(options),
71 cancellationToken);
72 }
73
74 /// <summary>
75 /// Checks a standalone candidate solution while reusing a mathematical
76 /// model already built for the same instance.
77 /// </summary>
78 /// <param name="instance">Lot-sizing instance defining the problem.</param>
79 /// <param name="solution">Candidate solution to verify.</param>
80 /// <param name="mathematicalModel">
81 /// Prebuilt mathematical model representing <paramref name="instance"/>.
82 /// </param>
83 /// <param name="options">Optional verification configuration.</param>
84 /// <param name="cancellationToken">Cancellation token.</param>
85 /// <returns>The independent check and application results.</returns>
86 public Task<LotSizingSolutionVerificationResult> VerifyAsync(
87 LotSizingInstance instance,
88 LotSizingSolution solution,
89 MathematicalModel mathematicalModel,
90 SolutionVerificationOptions? options = null,
91 CancellationToken cancellationToken = default)
92 {
93 ArgumentNullException.ThrowIfNull(mathematicalModel);
94
95 return VerifyCoreAsync(
96 instance,
97 solution,
98 knownResult: null,
99 mathematicalModel,
100 CloneVerificationOptions(options),
101 cancellationToken);
102 }
103
104 /// <summary>
105 /// Checks the detailed solution attached to a known result and optionally
106 /// updates both the detailed solution evaluation and the known-result
107 /// verification metadata.
108 /// </summary>
109 /// <remarks>
110 /// When the known result contains a reported objective value, that value
111 /// is used as the independent external reference for the objective check.
112 /// This avoids comparing the checker result with an objective value that
113 /// may already have been recomputed and stored in the detailed solution.
114 /// </remarks>
115 /// <param name="instance">Lot-sizing instance defining the problem.</param>
116 /// <param name="knownResult">Known result whose detailed solution is checked.</param>
117 /// <param name="options">Optional verification configuration.</param>
118 /// <param name="cancellationToken">Cancellation token.</param>
119 /// <returns>The independent check and application results.</returns>
120 public Task<LotSizingSolutionVerificationResult> VerifyKnownResultAsync(
121 LotSizingInstance instance,
122 KnownResult knownResult,
123 SolutionVerificationOptions? options = null,
124 CancellationToken cancellationToken = default)
125 {
126 return VerifyKnownResultCoreAsync(
127 instance,
128 knownResult,
129 mathematicalModel: null,
130 options,
131 cancellationToken);
132 }
133
134 /// <summary>
135 /// Checks the detailed solution attached to a known result while reusing
136 /// a mathematical model already built for the same instance.
137 /// </summary>
138 /// <remarks>
139 /// When available, <c>knownResult.ReportedObjectiveValue</c> is used as
140 /// the objective value reported by the producing solver. The checker then
141 /// recomputes the objective independently from the detailed decisions and
142 /// compares the two values.
143 /// </remarks>
144 /// <param name="instance">Lot-sizing instance defining the problem.</param>
145 /// <param name="knownResult">Known result whose detailed solution is checked.</param>
146 /// <param name="mathematicalModel">
147 /// Prebuilt mathematical model representing <paramref name="instance"/>.
148 /// </param>
149 /// <param name="options">Optional verification configuration.</param>
150 /// <param name="cancellationToken">Cancellation token.</param>
151 /// <returns>The independent check and application results.</returns>
152 public Task<LotSizingSolutionVerificationResult> VerifyKnownResultAsync(
153 LotSizingInstance instance,
154 KnownResult knownResult,
155 MathematicalModel mathematicalModel,
156 SolutionVerificationOptions? options = null,
157 CancellationToken cancellationToken = default)
158 {
159 ArgumentNullException.ThrowIfNull(mathematicalModel);
160
161 return VerifyKnownResultCoreAsync(
162 instance,
163 knownResult,
164 mathematicalModel,
165 options,
166 cancellationToken);
167 }
168
169 private Task<LotSizingSolutionVerificationResult> VerifyKnownResultCoreAsync(
170 LotSizingInstance instance,
171 KnownResult knownResult,
172 MathematicalModel? mathematicalModel,
174 CancellationToken cancellationToken)
175 {
176 ArgumentNullException.ThrowIfNull(knownResult);
177
178 if (knownResult.DetailedSolution is null)
179 {
180 throw new InvalidOperationException(
181 "The known result does not contain a detailed solution " +
182 "that can be independently checked.");
183 }
184
185 SolutionVerificationOptions effectiveOptions =
186 CloneVerificationOptions(options);
187
188 if (knownResult.ReportedObjectiveValue.HasValue)
189 {
190 effectiveOptions.CheckOptions.ReportedObjectiveValueOverride =
191 knownResult.ReportedObjectiveValue.Value;
192 }
193
194 return VerifyCoreAsync(
195 instance,
196 knownResult.DetailedSolution,
197 knownResult,
198 mathematicalModel,
199 effectiveOptions,
200 cancellationToken);
201 }
202
203 private async Task<LotSizingSolutionVerificationResult> VerifyCoreAsync(
204 LotSizingInstance instance,
205 LotSizingSolution solution,
206 KnownResult? knownResult,
207 MathematicalModel? mathematicalModel,
209 CancellationToken cancellationToken)
210 {
211 ArgumentNullException.ThrowIfNull(instance);
212 ArgumentNullException.ThrowIfNull(solution);
213 ArgumentNullException.ThrowIfNull(options);
214
215 options.EnsureValid();
216
217 SolutionCheckResult checkResult =
218 mathematicalModel is null
219 ? await _checker.CheckAsync(
220 instance,
221 solution,
222 options.CheckOptions,
223 cancellationToken)
224 .ConfigureAwait(false)
225 : await _checker.CheckAsync(
226 instance,
227 solution,
228 mathematicalModel,
229 options.CheckOptions,
230 cancellationToken)
231 .ConfigureAwait(false);
232
233 cancellationToken.ThrowIfCancellationRequested();
234
235 DateTime evaluatedAtUtc =
236 DateTime.UtcNow;
237
238 SolutionCheckApplicationResult applicationResult =
239 _applier.Apply(
240 solution,
241 checkResult,
242 options,
243 evaluatedAtUtc,
244 knownResult);
245
246 return new LotSizingSolutionVerificationResult
247 {
248 CheckResult =
249 checkResult,
250 ApplicationResult =
251 applicationResult
252 };
253 }
254
255 private static SolutionVerificationOptions CloneVerificationOptions(
256 SolutionVerificationOptions? source)
257 {
258 source ??=
259 new SolutionVerificationOptions();
260
261 source.EnsureValid();
262
263 return new SolutionVerificationOptions
264 {
265 CheckOptions =
266 CloneCheckOptions(source.CheckOptions),
267 ApplyToSolutionEvaluation =
269 UpdateKnownResultFeasibility =
271 PromoteFullyVerifiedKnownResult =
273 EvaluatorName =
274 source.EvaluatorName,
275 EvaluatorVersion =
276 source.EvaluatorVersion
277 };
278 }
279
280 private static SolutionCheckOptions CloneCheckOptions(
281 SolutionCheckOptions source)
282 {
283 ArgumentNullException.ThrowIfNull(source);
284
285 return new SolutionCheckOptions
286 {
287 Level =
288 source.Level,
289 FeasibilityTolerance =
291 ZeroTolerance =
292 source.ZeroTolerance,
293 IntegralityTolerance =
295 ObjectiveAbsoluteTolerance =
297 ObjectiveRelativeTolerance =
299 ReportedObjectiveValueOverride =
301 IgnoreDisabledConstraints =
303 ContinueAfterStructuralErrors =
304 source.ContinueAfterStructuralErrors
305 };
306 }
307}
double ObjectiveRelativeTolerance
Gets or sets the relative tolerance used when comparing objective values.
bool IgnoreDisabledConstraints
Gets or sets whether disabled mathematical constraints must be ignored.
double ZeroTolerance
Gets or sets the absolute tolerance used to identify numerical zero.
double FeasibilityTolerance
Gets or sets the absolute tolerance used for constraint feasibility.
double ObjectiveAbsoluteTolerance
Gets or sets the absolute tolerance used when comparing objective values.
SolutionCheckLevel Level
Gets or sets the requested checking depth.
double IntegralityTolerance
Gets or sets the tolerance used for integer and binary values.
double? ReportedObjectiveValueOverride
Gets or sets an optional externally reported objective value that takes precedence over the value sto...
Configures the high-level verification facade and the application of checker results back to domain o...
SolutionCheckOptions CheckOptions
Gets or sets the options used by the underlying solution checker.
bool UpdateKnownResultFeasibility
Gets or sets whether an explicitly supplied known result receives the independently established feasi...
bool ApplyToSolutionEvaluation
Gets or sets whether the computed checker result is written to LotSizingSolution.Evaluation.
string EvaluatorName
Gets or sets the evaluator name written to the solution evaluation.
bool PromoteFullyVerifiedKnownResult
Gets or sets whether a known result is promoted to AutomaticallyVerified when a complete Full check s...
Task< LotSizingSolutionVerificationResult > VerifyAsync(LotSizingInstance instance, LotSizingSolution solution, MathematicalModel mathematicalModel, SolutionVerificationOptions? options=null, CancellationToken cancellationToken=default)
Checks a standalone candidate solution while reusing a mathematical model already built for the same ...
Task< LotSizingSolutionVerificationResult > VerifyKnownResultAsync(LotSizingInstance instance, KnownResult knownResult, SolutionVerificationOptions? options=null, CancellationToken cancellationToken=default)
Checks the detailed solution attached to a known result and optionally updates both the detailed solu...
Task< LotSizingSolutionVerificationResult > VerifyKnownResultAsync(LotSizingInstance instance, KnownResult knownResult, MathematicalModel mathematicalModel, SolutionVerificationOptions? options=null, CancellationToken cancellationToken=default)
Checks the detailed solution attached to a known result while reusing a mathematical model already bu...
Task< LotSizingSolutionVerificationResult > VerifyAsync(LotSizingInstance instance, LotSizingSolution solution, SolutionVerificationOptions? options=null, CancellationToken cancellationToken=default)
Checks a standalone candidate solution and optionally writes the resulting evaluation into the candid...
LotSizingSolutionVerificationService(ILotSizingSolutionChecker checker, ISolutionCheckResultApplier applier)
Initializes the facade with explicit dependencies.
LotSizingSolutionVerificationService()
Initializes the facade with the standard checker and result applier.
Describes which domain objects were changed while applying a checker result.
Applies independent checker results back to the serializable solution evaluation and to an optional k...
Orchestrates the complete, solver-independent validation of a LotSizingSolution against a LotSizingIn...
Contains the complete result of checking one lot-sizing solution.
Defines a generic, solver-independent checker for lot-sizing solutions.
Applies a solver-independent checker result to the serializable solution evaluation and,...