2using System.Collections.Generic;
3using System.Diagnostics;
6using System.Threading.Tasks;
7using LotSizingDataModel.Instance;
21 _decisionMapperRegistry;
36 ArgumentNullException.ThrowIfNull(
37 decisionMapperRegistry);
39 _decisionMapperRegistry =
40 decisionMapperRegistry;
69 public ValueTask<MathematicalSolutionMappingResult>
MapAsync(
70 LotSizingInstance instance,
74 CancellationToken cancellationToken =
default)
76 ArgumentNullException.ThrowIfNull(
79 ArgumentNullException.ThrowIfNull(
82 ArgumentNullException.ThrowIfNull(
85 ArgumentNullException.ThrowIfNull(
93 cancellationToken.ThrowIfCancellationRequested();
107 return new ValueTask<MathematicalSolutionMappingResult>(
109 "A solver result without a feasible " +
110 "solution cannot be mapped.",
114 ValidateCompleteness(
119 cancellationToken.ThrowIfCancellationRequested();
121 IReadOnlyCollection<string> categories =
131 _decisionMapperRegistry);
133 LotSizingDataModel.Solution.LotSizingSolution solution =
140 cancellationToken.ThrowIfCancellationRequested();
156 $
"Mathematical solution mapping processed " +
157 $
"{result.ProcessedValueCount} values across " +
158 $
"{result.ProcessedCategoryCount} decision " +
163 return new ValueTask<MathematicalSolutionMappingResult>(
166 catch (OperationCanceledException)
170 catch (Exception exception)
180 exception.ToString());
184 return new ValueTask<MathematicalSolutionMappingResult>(
189 private static IReadOnlyCollection<string> GetCategories(
194 StringComparer.OrdinalIgnoreCase);
200 if (
string.IsNullOrWhiteSpace(
206 if (MathematicalDomainKey.TryParse(
208 out MathematicalDomainKey? domainKey))
211 domainKey!.Category);
218 private void ValidateCategories(
219 IEnumerable<string> categories,
220 MathematicalSolutionMappingOptions options)
222 if (!options.RequireKnownCategories)
231 if (!_decisionMapperRegistry.Contains(
234 throw new InvalidOperationException(
235 $
"No mathematical decision mapper is " +
236 $
"registered for category '{category}'.");
241 private static void ValidateCompleteness(
242 MathematicalModel model,
243 MathematicalModelSolveResult solveResult,
244 MathematicalSolutionMappingOptions options)
246 if (!options.RequireCompleteVariableValues)
251 var returnedVariableIds =
252 solveResult.VariableValues
259 MathematicalVariable variable
262 if (!returnedVariableIds.Contains(
265 throw new InvalidOperationException(
266 $
"No solver value was returned for " +
267 $
"mathematical variable '{variable.Name}' " +
268 $
"with identifier '{variable.Id}'.");
273 private static void PopulateStatistics(
274 MathematicalSolutionMappingResult result,
275 MathematicalModelSolveResult solveResult,
276 IReadOnlyCollection<string> categories,
277 MathematicalSolutionMappingOptions options)
279 int processedValueCount =
282 int ignoredValueCount =
286 MathematicalVariableValue variableValue
291 variableValue.
Value) <=
292 options.ZeroTolerance;
295 !options.IncludeZeroValues)
302 processedValueCount++;
305 result.ProcessedValueCount =
308 result.IgnoredValueCount =
311 result.ProcessedCategoryCount =
Stores the solver result for a solver-independent mathematical model.
void EnsureValid()
Validates the mathematical-model solve result.
List< MathematicalVariableValue > VariableValues
Gets the mathematical-variable values returned by the solver.
bool HasFeasibleSolution
Gets or sets a value indicating whether the solver produced at least one feasible solution.
Stores the value returned by a solver for one mathematical decision variable.
int VariableId
Gets or sets the identifier of the mathematical variable.
double Value
Gets or sets the value returned by the solver.
string DomainKey
Gets or sets the business-domain key associated with the mathematical variable.
Stores and resolves mathematical decision mappers by business-domain category.
Maps a complete mathematical solver result to a normalized lot-sizing solution by delegating each dec...
Defines the options used when mapping a mathematical solver result back to a normalized lot-sizing so...
void EnsureValid()
Validates the mapping options.
MathematicalSolutionMappingOptions Clone()
Creates an independent copy of the mapping options.
Describes the result of mapping a mathematical solver result back to a normalized lot-sizing solution...
void EnsureValid()
Validates the mathematical-solution mapping result.
static MathematicalSolutionMappingResult Failure(string failureMessage, TimeSpan mappingDuration)
Creates a failed mathematical-solution mapping result.
void AddDiagnostic(string? diagnostic)
Adds a non-empty diagnostic message.
static MathematicalSolutionMappingResult Success(LotSizingSolution solution, TimeSpan mappingDuration)
Creates a successful mathematical-solution mapping result.
MathematicalSolutionMappingService(MathematicalDecisionMapperRegistry decisionMapperRegistry)
Initializes a mathematical-solution mapping service.
ValueTask< MathematicalSolutionMappingResult > MapAsync(LotSizingInstance instance, MathematicalModel model, MathematicalModelSolveResult solveResult, MathematicalSolutionMappingOptions options, CancellationToken cancellationToken=default)
Maps a mathematical-model solve result to a normalized lot-sizing solution.
Represents a solver-independent mathematical optimization model.
List< MathematicalVariable > Variables
Gets the mathematical variables.
void EnsureValid()
Validates the complete mathematical model.
int Id
Gets or sets the unique variable identifier within the mathematical model.
Defines a service that maps a generic mathematical solver result to a normalized lot-sizing solution.