LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
InventorySafetyStockViolationDecisionMapper.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using LotSizingDataModel.Core.PhysicalModel;
4using LotSizingDataModel.Solution;
5using LotSizingDataModel.Solution.Decisions;
7
9
10/// <summary>
11/// Maps mathematical safety-stock violation values to inventory
12/// decisions in a lot-sizing solution.
13/// </summary>
14/// <remarks>
15/// Supported canonical domain-key formats are:
16/// <code>
17/// inventorySafetyStockViolation|item=&lt;id&gt;|warehouse=&lt;id&gt;|period=&lt;index&gt;
18/// inventorySafetyStockViolation|item=&lt;id&gt;|plant=&lt;id&gt;|period=&lt;index&gt;
19/// </code>
20/// The first format identifies a standalone warehouse. The
21/// second identifies the warehouse attached to a plant.
22/// Period numbers are one-based.
23/// </remarks>
26{
27 /// <summary>
28 /// Gets the mathematical domain-key category handled by this
29 /// mapper.
30 /// </summary>
31 public override string Category =>
33
34 /// <summary>
35 /// Maps one non-zero safety-stock violation value.
36 /// </summary>
37 /// <param name="context">
38 /// Mathematical-solution mapping context.
39 /// </param>
40 /// <param name="solution">
41 /// Target lot-sizing solution.
42 /// </param>
43 /// <param name="domainKey">
44 /// Parsed safety-stock violation domain key.
45 /// </param>
46 /// <param name="variableValue">
47 /// Safety-stock violation returned by the solver.
48 /// </param>
49 protected override void MapValue(
51 LotSizingSolution solution,
52 MathematicalDomainKey domainKey,
53 MathematicalVariableValue variableValue)
54 {
55 ArgumentNullException.ThrowIfNull(
56 context);
57
58 ArgumentNullException.ThrowIfNull(
59 solution);
60
61 ArgumentNullException.ThrowIfNull(
62 domainKey);
63
64 ArgumentNullException.ThrowIfNull(
65 variableValue);
66
67 int itemId =
68 domainKey.GetRequiredInt32(
70
71 int period =
72 domainKey.GetRequiredInt32(
74
75 WarehouseReference warehouse =
76 ResolveWarehouse(
77 domainKey);
78
79 InventoryDecision? decision =
80 solution.InventoryDecisions
81 .FirstOrDefault(
82 existing =>
83 existing.ItemId ==
84 itemId &&
85 existing.Warehouse.Kind ==
86 warehouse.Kind &&
87 existing.Warehouse.ReferenceId ==
88 warehouse.ReferenceId);
89
90 if (decision is null)
91 {
92 if (solution.PlanningHorizon <= 0)
93 {
94 throw new InvalidOperationException(
95 "The target lot-sizing solution must have a " +
96 "strictly positive planning horizon before " +
97 "inventory decisions are mapped.");
98 }
99
100 decision =
101 new InventoryDecision(
102 itemId,
103 warehouse,
104 solution.PlanningHorizon);
105
106 solution.AddInventoryDecision(
107 decision);
108 }
109
110 decision.SetSafetyStockViolation(
111 period,
112 variableValue.Value);
113 }
114
115 private static WarehouseReference ResolveWarehouse(
116 MathematicalDomainKey domainKey)
117 {
118 bool hasStandaloneWarehouse =
119 domainKey.TryGetInt32(
121 out int warehouseId);
122
123 bool hasPlantWarehouse =
124 domainKey.TryGetInt32(
126 out int plantId);
127
128 if (hasStandaloneWarehouse ==
129 hasPlantWarehouse)
130 {
131 throw new InvalidOperationException(
132 "An inventory safety-stock violation domain key " +
133 "must identify exactly one warehouse using either " +
134 "'warehouse' or 'plant'.");
135 }
136
137 if (hasStandaloneWarehouse)
138 {
139 return WarehouseReference
140 .ForStandaloneWarehouse(
141 warehouseId);
142 }
143
144 return WarehouseReference
145 .ForPlantWarehouse(
146 plantId);
147 }
148}
Stores the value returned by a solver for one mathematical decision variable.
double Value
Gets or sets the value returned by the solver.
Maps mathematical safety-stock violation values to inventory decisions in a lot-sizing solution.
override string Category
Gets the mathematical domain-key category handled by this mapper.
override void MapValue(MathematicalSolutionMappingContext context, LotSizingSolution solution, MathematicalDomainKey domainKey, MathematicalVariableValue variableValue)
Maps one non-zero safety-stock violation value.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string InventorySafetyStockViolation
Identifies safety-stock violation decision variables.
Provides a reusable base implementation for mathematical decision mappers handling one domain-key cat...
Represents a parsed mathematical-model business-domain key.
int GetRequiredInt32(string name)
Gets a required integer segment.
bool TryGetInt32(string name, out int value)
Attempts to get an integer segment.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...
Provides the shared state and lookup services required while mapping a mathematical solver result bac...