LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
UlsAlgorithmsHeuristicBridge.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 exactly one pinned public ULSAlgorithms heuristic.
13/// </summary>
15{
18 string solverId,
19 CancellationToken cancellationToken = default)
20 {
21 ArgumentNullException.ThrowIfNull(
22 problemData);
23
24 problemData.EnsureValid();
25
26 UlsSolverDescriptor descriptor =
28 solverId);
29
30 IUlsSolver solver =
31 UlsSolverFactory.Create(
32 descriptor.Id);
33
34 if (solver.Kind !=
35 UlsSolverKind.Heuristic)
36 {
37 throw new InvalidOperationException(
38 $"ULSAlgorithms factory returned a non-heuristic solver for '{descriptor.Id}'.");
39 }
40
41 var problem =
42 new UlsProblem(
43 problemData.Demands,
44 problemData.SetupCosts,
45 problemData.UnitProductionCosts,
46 problemData.HoldingCosts);
47
48 cancellationToken.ThrowIfCancellationRequested();
49
50 var stopwatch =
51 Stopwatch.StartNew();
52
53 UlsSolveResult solveResult =
54 solver.Solve(
55 problem,
56 cancellationToken);
57
58 stopwatch.Stop();
59
60 if (solveResult.Status !=
61 UlsSolveStatus.Feasible)
62 {
63 throw new InvalidOperationException(
64 $"Pinned heuristic '{descriptor.Id}' returned status '{solveResult.Status}' instead of Feasible.");
65 }
66
67 if (!solveResult.HasSolution ||
68 solveResult.Solution is null)
69 {
70 throw new InvalidOperationException(
71 $"Pinned heuristic '{descriptor.Id}' returned no feasible solution.");
72 }
73
74 UlsSolution solution =
75 solveResult.Solution;
76
77 UlsSolutionValidationResult validation =
78 UlsSolutionValidator.Validate(
79 problem,
80 solution);
81
82 if (!validation.IsFeasible)
83 {
84 throw new InvalidOperationException(
85 $"ULSAlgorithms validator rejected heuristic '{descriptor.Id}'.");
86 }
87
88 if (solution.Horizon !=
89 problemData.Horizon)
90 {
91 throw new InvalidOperationException(
92 $"Heuristic '{descriptor.Id}' returned horizon {solution.Horizon}, expected {problemData.Horizon}.");
93 }
94
95 double objectiveValue =
96 solveResult.ObjectiveValue ??
97 solution.TotalCost;
98
99 double reportedDifference =
100 Math.Abs(
101 objectiveValue -
102 solution.TotalCost);
103
104 double validatedDifference =
105 Math.Abs(
106 objectiveValue -
107 validation.RecomputedTotalCost);
108
109 double scale =
110 Math.Max(
111 1.0,
112 Math.Abs(objectiveValue));
113
114 if (reportedDifference >
115 1.0e-7 * scale ||
116 validatedDifference >
117 1.0e-7 * scale)
118 {
119 throw new InvalidOperationException(
120 $"Heuristic '{descriptor.Id}' returned an inconsistent objective value.");
121 }
122
123 string solverVersion =
124 ULSAlgorithmsInfo.InformationalVersion;
125
126 if (string.IsNullOrWhiteSpace(
127 solverVersion))
128 {
129 solverVersion =
130 ULSAlgorithmsInfo.Version.ToString();
131 }
132
134 descriptor.Id,
135 solveResult.SolverName,
136 solverVersion,
137 solveResult.Status,
138 objectiveValue,
139 solution.SetupCost,
140 solution.ProductionCost,
141 solution.HoldingCost,
142 solution.ProductionQuantities.ToArray(),
143 solution.EndingInventories.ToArray(),
144 solution.SetupDecisions.ToArray(),
145 stopwatch.Elapsed);
146 }
147}
Normalized deterministic uncapacitated single-item problem passed to ULSAlgorithms.
Executes exactly one pinned public ULSAlgorithms heuristic.
UlsAlgorithmsHeuristicBridgeResult Solve(UlsAlgorithmsExactProblemData problemData, string solverId, CancellationToken cancellationToken=default)
Validated feasible result returned by one pinned ULSAlgorithms heuristic.