LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
BacklogDecisionMapper.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 backlog-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/// backlog|distributionCenter=&lt;id&gt;|item=&lt;id&gt;|warehouse=&lt;id&gt;|period=&lt;index&gt;
18/// backlog|distributionCenter=&lt;id&gt;|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 BacklogDecisionMapper :
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 backlog level.
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 backlog domain key.
43 /// </param>
44 /// <param name="variableValue">
45 /// Backlog level 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 distributionCenterId =
66 domainKey.GetRequiredInt32(
68
69 int itemId =
70 domainKey.GetRequiredInt32(
72
73 int period =
74 domainKey.GetRequiredInt32(
76
77 WarehouseReference warehouse =
78 ResolveWarehouse(
79 domainKey);
80
81 DistributionDecision? decision =
82 solution.DistributionDecisions
83 .FirstOrDefault(
84 existing =>
85 existing.Matches(
86 distributionCenterId,
87 itemId,
88 warehouse));
89
90 if (decision is null)
91 {
92 decision =
93 new DistributionDecision(
94 distributionCenterId,
95 itemId,
96 warehouse,
97 solution.PlanningHorizon);
98
99 solution.AddDistributionDecision(
100 decision);
101 }
102
103 decision.SetBacklogLevel(
104 period,
105 variableValue.Value);
106 }
107
108 private static WarehouseReference ResolveWarehouse(
109 MathematicalDomainKey domainKey)
110 {
111 bool hasStandaloneWarehouse =
112 domainKey.TryGetInt32(
114 out int warehouseId);
115
116 bool hasPlantWarehouse =
117 domainKey.TryGetInt32(
119 out int plantId);
120
121 if (hasStandaloneWarehouse ==
122 hasPlantWarehouse)
123 {
124 throw new InvalidOperationException(
125 "A backlog domain key must identify exactly one " +
126 "warehouse using either 'warehouse' or 'plant'.");
127 }
128
129 if (hasStandaloneWarehouse)
130 {
131 return WarehouseReference
132 .ForStandaloneWarehouse(
133 warehouseId);
134 }
135
136 return WarehouseReference
137 .ForPlantWarehouse(
138 plantId);
139 }
140}
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 backlog-variable values to distribution decisions in a lot-sizing solution.
override void MapValue(MathematicalSolutionMappingContext context, LotSizingSolution solution, MathematicalDomainKey domainKey, MathematicalVariableValue variableValue)
Maps one non-zero backlog level.
override string Category
Gets the mathematical domain-key category handled by this mapper.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Backlog
Identifies backlog-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...
const string DistributionCenter
Distribution-center identifier segment.
Provides the shared state and lookup services required while mapping a mathematical solver result bac...