LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
DeliveryDecisionMapper.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 delivery-variable values to distribution
12/// decisions in a lot-sizing solution.
13/// </summary>
14/// <remarks>
15/// Supported canonical domain-key formats are:
16/// <code>
17/// delivery|distributionCenter=&lt;id&gt;|item=&lt;id&gt;|warehouse=&lt;id&gt;|period=&lt;index&gt;
18/// delivery|distributionCenter=&lt;id&gt;|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>
24public sealed class DeliveryDecisionMapper :
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 delivered quantity.
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 delivery domain key.
45 /// </param>
46 /// <param name="variableValue">
47 /// Delivered quantity 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 distributionCenterId =
68 domainKey.GetRequiredInt32(
70
71 int itemId =
72 domainKey.GetRequiredInt32(
74
75 int period =
76 domainKey.GetRequiredInt32(
78
79 WarehouseReference warehouse =
80 ResolveWarehouse(
81 domainKey);
82
83 DistributionDecision? decision =
84 solution.DistributionDecisions
85 .FirstOrDefault(
86 existing =>
87 existing.Matches(
88 distributionCenterId,
89 itemId,
90 warehouse));
91
92 if (decision is null)
93 {
94 if (solution.PlanningHorizon <= 0)
95 {
96 throw new InvalidOperationException(
97 "The target lot-sizing solution must have a " +
98 "strictly positive planning horizon before " +
99 "distribution decisions are mapped.");
100 }
101
102 decision =
103 new DistributionDecision(
104 distributionCenterId,
105 itemId,
106 warehouse,
107 solution.PlanningHorizon);
108
109 solution.AddDistributionDecision(
110 decision);
111 }
112
113 decision.SetDeliveredQuantity(
114 period,
115 variableValue.Value);
116 }
117
118 private static WarehouseReference ResolveWarehouse(
119 MathematicalDomainKey domainKey)
120 {
121 bool hasStandaloneWarehouse =
122 domainKey.TryGetInt32(
124 out int warehouseId);
125
126 bool hasPlantWarehouse =
127 domainKey.TryGetInt32(
129 out int plantId);
130
131 if (hasStandaloneWarehouse ==
132 hasPlantWarehouse)
133 {
134 throw new InvalidOperationException(
135 "A delivery domain key must identify exactly one " +
136 "warehouse using either 'warehouse' or 'plant'.");
137 }
138
139 if (hasStandaloneWarehouse)
140 {
141 return WarehouseReference
142 .ForStandaloneWarehouse(
143 warehouseId);
144 }
145
146 return WarehouseReference
147 .ForPlantWarehouse(
148 plantId);
149 }
150}
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 delivery-variable values to distribution 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 delivered quantity.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Delivery
Identifies delivered-quantity 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 DistributionCenter
Distribution-center identifier segment.
Provides the shared state and lookup services required while mapping a mathematical solver result bac...