LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
UlsAlgorithmsExactBridge.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using ULSAlgorithms;
3using ULSAlgorithms.Abstractions;
4using ULSAlgorithms.Catalog;
5using ULSAlgorithms.Models;
6using ULSAlgorithms.Results;
7using ULSAlgorithms.Validation;
8
10
11/// <summary>
12/// Executes one explicitly selected exact ULSAlgorithms method.
13/// No method fallback is permitted.
14/// </summary>
15public sealed class UlsAlgorithmsExactBridge
16{
20 CancellationToken cancellationToken = default)
21 {
22 ArgumentNullException.ThrowIfNull(
23 problemData);
24
25 problemData.EnsureValid();
26
27 string solverId =
29 method);
30
31 UlsSolverDescriptor descriptor =
33 method);
34
35 IUlsSolver solver =
36 UlsSolverFactory.Create(
37 solverId);
38
39 if (solver.Kind !=
40 UlsSolverKind.Exact)
41 {
42 throw new InvalidOperationException(
43 $"ULSAlgorithms factory returned a non-exact solver for '{solverId}'.");
44 }
45
46 var problem =
47 new UlsProblem(
48 problemData.Demands,
49 problemData.SetupCosts,
50 problemData.UnitProductionCosts,
51 problemData.HoldingCosts);
52
53 cancellationToken.ThrowIfCancellationRequested();
54
55 var stopwatch =
56 Stopwatch.StartNew();
57
58 UlsSolveResult solveResult =
59 solver.Solve(
60 problem,
61 cancellationToken);
62
63 stopwatch.Stop();
64
65 if (!solveResult.HasSolution ||
66 solveResult.Solution is null)
67 {
68 throw new InvalidOperationException(
69 $"Exact ULSAlgorithms solver '{solverId}' returned no solution. Status={solveResult.Status}. Message={solveResult.Message}");
70 }
71
72 UlsSolution solution =
73 solveResult.Solution;
74
75 UlsSolutionValidationResult validation =
76 UlsSolutionValidator.Validate(
77 problem,
78 solution);
79
80 if (!validation.IsFeasible)
81 {
82 throw new InvalidOperationException(
83 $"ULSAlgorithms validator rejected the exact solution from '{solverId}'.");
84 }
85
86 if (solution.Horizon !=
87 problemData.Horizon)
88 {
89 throw new InvalidOperationException(
90 $"ULSAlgorithms solver '{solverId}' returned horizon {solution.Horizon}, expected {problemData.Horizon}.");
91 }
92
93 double objectiveValue =
94 solveResult.ObjectiveValue ??
95 solution.TotalCost;
96
97 double costDifference =
98 Math.Abs(
99 objectiveValue -
100 validation.RecomputedTotalCost);
101
102 double scale =
103 Math.Max(
104 1.0,
105 Math.Abs(objectiveValue));
106
107 if (costDifference >
108 1.0e-7 * scale)
109 {
110 throw new InvalidOperationException(
111 $"ULSAlgorithms exact objective mismatch for '{solverId}'.");
112 }
113
114 string solverVersion =
115 ULSAlgorithmsInfo.InformationalVersion;
116
117 if (string.IsNullOrWhiteSpace(
118 solverVersion))
119 {
120 solverVersion =
121 ULSAlgorithmsInfo.Version.ToString();
122 }
123
125 method,
126 descriptor.Id,
127 solveResult.SolverName,
128 solverVersion,
129 objectiveValue,
130 solution.ProductionQuantities.ToArray(),
131 solution.EndingInventories.ToArray(),
132 solution.SetupDecisions.ToArray(),
133 stopwatch.Elapsed);
134 }
135}
Executes one explicitly selected exact ULSAlgorithms method. No method fallback is permitted.
UlsAlgorithmsExactBridgeResult Solve(UlsAlgorithmsExactProblemData problemData, UlsAlgorithmsExactMethod method, CancellationToken cancellationToken=default)
Immutable external exact-solve evidence retained before projection back into LotSizingDataModel.
static UlsSolverDescriptor GetExactDescriptor(UlsAlgorithmsExactMethod method)
Normalized deterministic uncapacitated single-item problem passed to ULSAlgorithms.
UlsAlgorithmsExactMethod
Exact ULSAlgorithms methods deliberately exposed by the LotSizingDataModel alpha.34 bridge.