LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
ProcurementDecisionMapper.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 procurement-variable values to purchase
12/// decisions in a lot-sizing solution.
13/// </summary>
14/// <remarks>
15/// Supported canonical domain-key formats are:
16/// <code>
17/// procurement|supplier=&lt;id&gt;|item=&lt;id&gt;|destinationWarehouse=&lt;id&gt;|period=&lt;index&gt;
18/// procurement|supplier=&lt;id&gt;|item=&lt;id&gt;|destinationPlant=&lt;id&gt;|period=&lt;index&gt;
19/// </code>
20/// Period numbers are one-based.
21/// </remarks>
22public sealed class ProcurementDecisionMapper :
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 purchased quantity.
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 procurement domain key.
43 /// </param>
44 /// <param name="variableValue">
45 /// Purchased quantity 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 supplierId =
66 domainKey.GetRequiredInt32(
68
69 int itemId =
70 domainKey.GetRequiredInt32(
72
73 int period =
74 domainKey.GetRequiredInt32(
76
77 WarehouseReference destinationWarehouse =
78 ResolveDestinationWarehouse(
79 domainKey);
80
81 PurchaseDecision? decision =
82 solution.PurchaseDecisions
83 .FirstOrDefault(
84 existing =>
85 existing.Matches(
86 supplierId,
87 itemId,
88 destinationWarehouse));
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 "purchase decisions are mapped.");
98 }
99
100 decision =
101 new PurchaseDecision(
102 supplierId,
103 itemId,
104 destinationWarehouse,
105 solution.PlanningHorizon);
106
107 solution.AddPurchaseDecision(
108 decision);
109 }
110
111 decision.SetPurchasedQuantity(
112 period,
113 variableValue.Value);
114 }
115
116 private static WarehouseReference ResolveDestinationWarehouse(
117 MathematicalDomainKey domainKey)
118 {
119 bool hasStandaloneWarehouse =
120 domainKey.TryGetInt32(
122 out int warehouseId);
123
124 bool hasPlantWarehouse =
125 domainKey.TryGetInt32(
127 out int plantId);
128
129 if (hasStandaloneWarehouse ==
130 hasPlantWarehouse)
131 {
132 throw new InvalidOperationException(
133 "A procurement domain key must identify exactly " +
134 "one destination warehouse using either " +
135 "'destinationWarehouse' or 'destinationPlant'.");
136 }
137
138 if (hasStandaloneWarehouse)
139 {
140 return WarehouseReference
141 .ForStandaloneWarehouse(
142 warehouseId);
143 }
144
145 return WarehouseReference
146 .ForPlantWarehouse(
147 plantId);
148 }
149}
Stores the value returned by a solver for one mathematical decision variable.
double Value
Gets or sets the value returned by the solver.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Procurement
Identifies supplier-procurement 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...
const string DestinationWarehouse
Standalone destination-warehouse identifier segment.
const string DestinationPlant
Destination-plant identifier segment.
Provides the shared state and lookup services required while mapping a mathematical solver result bac...
Maps mathematical procurement-variable values to purchase decisions in a lot-sizing solution.
override void MapValue(MathematicalSolutionMappingContext context, LotSizingSolution solution, MathematicalDomainKey domainKey, MathematicalVariableValue variableValue)
Maps one non-zero purchased quantity.
override string Category
Gets the mathematical domain-key category handled by this mapper.