2using System.Collections.Generic;
4using LotSizingDataModel.Instance;
17 private readonly Dictionary<int, MathematicalVariable>
20 private readonly Dictionary<int, MathematicalVariableValue>
23 private readonly Dictionary<string, MathematicalVariableValue>
46 LotSizingInstance instance,
82 LotSizingInstance instance,
87 ArgumentNullException.ThrowIfNull(
90 ArgumentNullException.ThrowIfNull(
93 ArgumentNullException.ThrowIfNull(
96 ArgumentNullException.ThrowIfNull(
132 _valuesByVariableId =
135 variableValue.VariableId);
138 new Dictionary<string, MathematicalVariableValue>(
139 StringComparer.OrdinalIgnoreCase);
200 if (_variablesById.TryGetValue(
207 throw new KeyNotFoundException(
208 $
"No mathematical variable exists for identifier " +
227 if (_valuesByVariableId.TryGetValue(
231 return variableValue;
234 throw new KeyNotFoundException(
235 $
"No solver value exists for mathematical variable " +
236 $
"identifier '{variableId}'.");
257 string normalizedDomainKey =
261 if (_valuesByDomainKey.TryGetValue(
265 return variableValue;
268 throw new KeyNotFoundException(
269 $
"No solver value exists for domain key " +
270 $
"'{normalizedDomainKey}'.");
290 string normalizedDomainKey =
294 return _valuesByDomainKey.TryGetValue(
316 public IReadOnlyList<MathematicalVariableValue>
319 bool includeZeroValues,
320 double zeroTolerance = 1.0e-9)
322 if (
string.IsNullOrWhiteSpace(
325 throw new ArgumentException(
326 "A domain-key category is required.",
336 throw new ArgumentOutOfRangeException(
337 nameof(zeroTolerance),
339 "Zero tolerance must be finite and non-negative.");
342 string normalizedCategory =
345 return _valuesByDomainKey
355 StringComparison.OrdinalIgnoreCase))
360 return includeZeroValues ||
369 Model.FindVariableById(
370 pair.Value.VariableId);
372 if (variable is
null)
374 throw new InvalidOperationException(
375 $
"No mathematical variable with identifier " +
376 $
"'{pair.Value.VariableId}' exists in the model.");
399 public IReadOnlyList<MathematicalVariableValue>
402 double zeroTolerance = 1.0e-9)
406 includeZeroValues:
false,
410 private void BuildIndexes()
416 if (!_variablesById.TryGetValue(
420 throw new InvalidOperationException(
421 $
"Solver result references unknown " +
422 $
"mathematical variable identifier " +
423 $
"'{variableValue.VariableId}'.");
427 !
string.IsNullOrWhiteSpace(
430 : variable.DomainKey?.Trim() ??
433 if (
string.IsNullOrWhiteSpace(
439 if (!
string.IsNullOrWhiteSpace(
440 variable.DomainKey) &&
442 variable.DomainKey.Trim(),
444 StringComparison.OrdinalIgnoreCase))
446 throw new InvalidOperationException(
447 $
"Solver-result domain key '{domainKey}' does " +
448 $
"not match model domain key " +
449 $
"'{variable.DomainKey}' for variable " +
450 $
"identifier '{variable.Id}'.");
453 variableValue.DomainKey =
456 if (!_valuesByDomainKey.TryAdd(
460 throw new InvalidOperationException(
461 $
"Domain key '{domainKey}' appears more than " +
462 "once in the mathematical solver result.");
467 private static string NormalizeDomainKey(
470 if (
string.IsNullOrWhiteSpace(
473 throw new ArgumentException(
474 "A mathematical domain key is required.",
478 return domainKey.Trim();
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.
Stores the value returned by a solver for one mathematical decision variable.
int VariableId
Gets or sets the identifier of the mathematical variable.
string DomainKey
Gets or sets the business-domain key associated with the mathematical variable.
Represents a parsed mathematical-model business-domain key.
static bool TryParse(string domainKey, out MathematicalDomainKey? result)
Attempts to parse a canonical mathematical domain key.
MathematicalSolutionMappingContext(LotSizingInstance instance, MathematicalModel model, MathematicalModelSolveResult solveResult)
Initializes a mathematical-solution mapping context.
MathematicalVariableValueNormalizer ValueNormalizer
Gets the numerical normalizer used for all mathematical variable values exposed to decision mappers.
MathematicalModel Model
Gets the solver-independent mathematical model.
LotSizingInstance Instance
Gets the source lot-sizing instance.
MathematicalSolutionMappingOptions Options
Gets the normalized mapping options used by this context.
IReadOnlyList< MathematicalVariableValue > GetNonZeroValuesByCategory(string category, double zeroTolerance=1.0e-9)
Returns all non-zero solver values whose domain-key category matches the supplied category.
MathematicalVariableValue GetValue(int variableId)
Gets a solver value by mathematical-variable identifier.
bool TryGetValue(string domainKey, out MathematicalVariableValue? variableValue)
Attempts to get a solver value by business-domain key.
MathematicalVariable GetVariable(int variableId)
Gets a mathematical variable by identifier.
MathematicalSolutionMappingContext(LotSizingInstance instance, MathematicalModel model, MathematicalModelSolveResult solveResult, MathematicalSolutionMappingOptions options)
Initializes a mathematical-solution mapping context with explicit mapping options.
MathematicalModelSolveResult SolveResult
Gets the generic mathematical-model solve result.
IReadOnlyList< MathematicalVariableValue > GetValuesByCategory(string category, bool includeZeroValues, double zeroTolerance=1.0e-9)
Returns solver values whose domain-key category matches the supplied category.
MathematicalVariableValue GetValue(string domainKey)
Gets a solver value by business-domain key.
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.
double ZeroTolerance
Gets or sets the absolute tolerance below which a solver value is considered equal to zero.
Normalizes raw mathematical-variable values before they are exposed through domain decision objects o...
const double DefaultNearIntegerTolerance
Gets the default tolerance used to clean continuous values that are numerically indistinguishable fro...
const double DefaultIntegralityTolerance
Gets the default tolerance used for integer-domain values.
Represents a solver-independent mathematical optimization model.
List< MathematicalVariable > Variables
Gets the mathematical variables.
void EnsureValid()
Validates the complete mathematical model.
Represents one decision variable in a mathematical optimization model.