2using System.Collections.Generic;
3using System.Diagnostics;
6using System.Threading.Tasks;
26 _solverExecutionService;
29 _solutionMappingService;
35 _solverAdapterRegistry;
37 private readonly IReadOnlyList<SolverAvailabilityInfo>
38 _availabilityInformation;
41 _solverSelectionOptions;
67 IEnumerable<SolverAvailabilityInfo> availabilityInformation)
74 solverAdapterRegistry,
75 availabilityInformation,
120 IEnumerable<SolverAvailabilityInfo> availabilityInformation,
124 ArgumentNullException.ThrowIfNull(
127 ArgumentNullException.ThrowIfNull(
128 solverExecutionService);
130 ArgumentNullException.ThrowIfNull(
131 solutionMappingService);
133 ArgumentNullException.ThrowIfNull(
134 formulationRegistry);
136 ArgumentNullException.ThrowIfNull(
137 solverAdapterRegistry);
139 ArgumentNullException.ThrowIfNull(
140 availabilityInformation);
142 ArgumentNullException.ThrowIfNull(
143 solverSelectionOptions);
145 ArgumentNullException.ThrowIfNull(
151 _solverExecutionService =
152 solverExecutionService;
154 _solutionMappingService =
155 solutionMappingService;
157 _formulationRegistry =
160 _solverAdapterRegistry =
161 solverAdapterRegistry;
163 _availabilityInformation =
164 availabilityInformation.ToArray();
166 _solverSelectionOptions =
167 solverSelectionOptions;
191 CancellationToken cancellationToken =
default)
193 ArgumentNullException.ThrowIfNull(
198 DateTime startedAtUtc =
202 Stopwatch.StartNew();
215 cancellationToken.ThrowIfCancellationRequested();
220 RequestedFormulationId =
221 request.FormulationName ??
string.Empty,
224 string.IsNullOrWhiteSpace(
227 ValidateGeneratedModel =
230 CloneGeneratedModel =
235 await _modelBuildService.BuildAsync(
237 _formulationRegistry,
242 buildResult.
Model is
null)
246 return CreateWorkflowFailure(
255 var mathematicalRequest =
262 request.RunName ??
string.Empty,
275 mathematicalRequest.ProgressObservers.Add(
282 await _solverExecutionService.SolveAsync(
285 _solverAdapterRegistry,
286 _availabilityInformation,
287 _solverSelectionOptions,
296 "No feasible mathematical solution was " +
297 "available for normalized solution mapping.",
315 await _solutionMappingService.MapAsync(
345 finalResult.RecomputedObjectiveValue =
348 finalResult.ObjectiveDifference =
351 finalResult.ObjectiveVerificationStatus =
356 in objectiveCheck.Diagnostics)
362 if (objectiveCheck.RecomputedObjectiveValue.HasValue)
364 mappingResult.Solution.Evaluation.ObjectiveValue =
365 objectiveCheck.RecomputedObjectiveValue.Value;
367 mappingResult.Solution.Evaluation.ObjectiveValueSource =
368 "Recomputed from normalized mathematical " +
369 "decision-variable values";
371 mappingResult.Solution.Evaluation.EvaluatorName =
374 mappingResult.Solution.Evaluation.EvaluatorVersion =
377 mappingResult.Solution.Evaluation.EvaluatedAtUtc =
385 "The solver produced a feasible mathematical " +
386 "solution, but mapping to LotSizingSolution " +
392 catch (OperationCanceledException)
396 return CreateWorkflowFailure(
401 "The lot-sizing solve workflow was cancelled.",
404 catch (Exception exception)
409 CreateWorkflowFailure(
418 exception.ToString());
430 _solverExecutionService.RequestStop();
435 DateTime startedAtUtc,
439 IEnumerable<string>? diagnostics)
445 request.RunName ??
string.Empty,
451 request.FormulationName ??
string.Empty,
457 startedAtUtc + elapsed,
460 elapsed.TotalSeconds,
476 if (!
string.IsNullOrWhiteSpace(
479 result.AddDiagnostic(
486 private static void AddDiagnostics(
487 SolverRunResult target,
488 IEnumerable<string>? diagnostics)
490 if (diagnostics is
null)
499 if (!
string.IsNullOrWhiteSpace(
502 target.AddDiagnostic(
Stores solver adapters that were loaded successfully and provides lookup services by adapter identifi...
Configures how a solver adapter is selected when automatic solver selection is requested.
Stores the result of objective-value post-processing.
ObjectiveVerificationStatus Status
Gets or sets the verification status.
double? AbsoluteDifference
Gets or sets the absolute difference between the two objective values.
double? RecomputedObjectiveValue
Gets or sets the objective value recomputed from normalized mathematical-variable values.
Recomputes the objective from the exact normalized numerical representation used by the business solu...
static MathematicalObjectiveRecalculationResult Recalculate(MathematicalModel model, MathematicalModelSolveResult solveResult, MathematicalSolutionMappingOptions mappingOptions)
Recomputes and verifies the objective value.
LotSizingSolverService(MathematicalModelFormulationRegistry formulationRegistry, SolverAdapterRegistry solverAdapterRegistry, IEnumerable< SolverAvailabilityInfo > availabilityInformation)
Initializes the high-level lot-sizing solver service using the standard service implementations.
LotSizingSolverService(IMathematicalModelBuildService modelBuildService, IMathematicalModelSolverExecutionService solverExecutionService, IMathematicalSolutionMappingService solutionMappingService, MathematicalModelFormulationRegistry formulationRegistry, SolverAdapterRegistry solverAdapterRegistry, IEnumerable< SolverAvailabilityInfo > availabilityInformation, SolverSelectionOptions solverSelectionOptions, MathematicalSolutionMappingOptions mappingOptions)
Initializes the high-level lot-sizing solver service with explicitly supplied service implementations...
void RequestStop()
Requests interruption of the currently running native mathematical solver.
async ValueTask< SolverRunResult > SolveAsync(SolverRequest request, CancellationToken cancellationToken=default)
Solves a complete lot-sizing instance.
Describes a request to solve an already built, solver-independent mathematical model.
void EnsureValid()
Validates the mathematical-model solve request.
Stores the solver result for a solver-independent mathematical model.
bool HasFeasibleSolution
Gets or sets a value indicating whether the solver produced at least one feasible solution.
Selects a compatible mathematical-model solver adapter and executes an already built solver-independe...
Creates the public solver-run result from the generic mathematical-model result and the normalized so...
static SolverRunResult Create(MathematicalModelSolveResult solveResult, MathematicalSolutionMappingResult mappingResult, DateTime startedAtUtc, TimeSpan elapsed)
Creates a normalized solver-run result.
Describes a request to solve a lot-sizing instance.
string FormulationName
Gets or sets the formulation name to use.
void EnsureValid()
Validates the solver request.
IList< ISolverProgressObserver > ProgressObservers
Gets the progress observers attached to this request.
SolverKind PreferredSolver
Gets or sets the preferred solver.
LotSizingInstance? Instance
Gets or sets the lot-sizing instance to solve.
Represents the normalized result of a solver execution.
void AddDiagnostic(string message)
Adds a diagnostic message.
Creates preconfigured mathematical decision-mapper registries for the standard lot-sizing solution mo...
Defines the options used when mapping a mathematical solver result back to a normalized lot-sizing so...
Describes the result of mapping a mathematical solver result back to a normalized lot-sizing solution...
bool IsSuccessful
Gets or sets a value indicating whether mapping succeeded.
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.
Maps a generic mathematical solver result to a normalized lot-sizing solution and reports mapping dia...
Defines the high-level service used to solve a complete lot-sizing instance from a normalized solver ...
Defines a service that selects a compatible solver adapter and executes an already built solver-indep...
Defines a service that maps a generic mathematical solver result to a normalized lot-sizing solution.
SolverTerminationReason
Describes why a solver execution terminated.
SolverKind
Identifies a supported mathematical optimization solver.