LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
InventoryDecisionMapper.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 inventory-variable values to inventory
12/// decisions in a lot-sizing solution.
13/// </summary>
14/// <remarks>
15/// Supported canonical domain-key formats are:
16/// <code>
17/// inventory|item=&lt;id&gt;|warehouse=&lt;id&gt;|period=&lt;index&gt;
18/// inventory|item=&lt;id&gt;|plant=&lt;id&gt;|period=&lt;index&gt;
19/// </code>
20/// Period numbers are one-based.
21/// </remarks>
22public sealed class InventoryDecisionMapper :
24{
25 /// <summary>
26 /// Gets the mathematical domain-key category handled by this
27 /// mapper.
28 /// </summary>
29 public override string Category =>
31
32 /// <summary>
33 /// Maps one non-zero inventory value.
34 /// </summary>
35 /// <param name="context">
36 /// Mathematical-solution mapping context.
37 /// </param>
38 /// <param name="solution">
39 /// Target lot-sizing solution.
40 /// </param>
41 /// <param name="domainKey">
42 /// Parsed inventory domain key.
43 /// </param>
44 /// <param name="variableValue">
45 /// Inventory value returned by the solver.
46 /// </param>
47 protected override void MapValue(
49 LotSizingSolution solution,
50 MathematicalDomainKey domainKey,
51 MathematicalVariableValue variableValue)
52 {
53 ArgumentNullException.ThrowIfNull(
54 context);
55
56 ArgumentNullException.ThrowIfNull(
57 solution);
58
59 ArgumentNullException.ThrowIfNull(
60 domainKey);
61
62 ArgumentNullException.ThrowIfNull(
63 variableValue);
64
65 int itemId =
66 domainKey.GetRequiredInt32(
68
69 int period =
70 domainKey.GetRequiredInt32(
72
73 WarehouseReference warehouse =
74 ResolveWarehouse(
75 domainKey);
76
77 InventoryDecision? decision =
78 solution.InventoryDecisions
79 .FirstOrDefault(
80 existing =>
81 existing.ItemId ==
82 itemId &&
83 existing.Warehouse.Kind ==
84 warehouse.Kind &&
85 existing.Warehouse.ReferenceId ==
86 warehouse.ReferenceId);
87
88 if (decision is null)
89 {
90 decision =
91 new InventoryDecision(
92 itemId,
93 warehouse,
94 solution.PlanningHorizon);
95
96 solution.AddInventoryDecision(
97 decision);
98 }
99
100 decision.SetLevel(
101 period,
102 variableValue.Value);
103 }
104
105 private static WarehouseReference ResolveWarehouse(
106 MathematicalDomainKey domainKey)
107 {
108 bool hasStandaloneWarehouse =
109 domainKey.TryGetInt32(
111 out int warehouseId);
112
113 bool hasPlantWarehouse =
114 domainKey.TryGetInt32(
116 out int plantId);
117
118 if (hasStandaloneWarehouse ==
119 hasPlantWarehouse)
120 {
121 throw new InvalidOperationException(
122 "An inventory domain key must identify exactly " +
123 "one warehouse using either 'warehouse' or " +
124 "'plant'.");
125 }
126
127 if (hasStandaloneWarehouse)
128 {
129 return WarehouseReference
130 .ForStandaloneWarehouse(
131 warehouseId);
132 }
133
134 return WarehouseReference
135 .ForPlantWarehouse(
136 plantId);
137 }
138}
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 inventory-variable 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 inventory value.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Inventory
Identifies inventory-level 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...