LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
TransportSetupDecisionMapper.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 item-specific transport setup values to
12/// transport decisions in a lot-sizing solution.
13/// </summary>
14/// <remarks>
15/// Expected canonical domain-key format:
16/// <code>
17/// transportSetup|item=&lt;id&gt;|transportResource=&lt;id&gt;|originWarehouse=&lt;id&gt;|destinationWarehouse=&lt;id&gt;|period=&lt;index&gt;
18/// </code>
19/// Origin and destination may alternatively be identified by
20/// <c>originPlant</c> and <c>destinationPlant</c>.
21/// Period numbers are one-based.
22/// </remarks>
23public sealed class TransportSetupDecisionMapper :
25{
26 /// <summary>
27 /// Gets the mathematical domain-key category handled by this
28 /// mapper.
29 /// </summary>
30 public override string Category =>
32
33 /// <summary>
34 /// Maps one non-zero transport setup value.
35 /// </summary>
36 /// <param name="context">
37 /// Mathematical-solution mapping context.
38 /// </param>
39 /// <param name="solution">
40 /// Target lot-sizing solution.
41 /// </param>
42 /// <param name="domainKey">
43 /// Parsed transport-setup domain key.
44 /// </param>
45 /// <param name="variableValue">
46 /// Setup value returned by the solver.
47 /// </param>
48 protected override void MapValue(
50 LotSizingSolution solution,
51 MathematicalDomainKey domainKey,
52 MathematicalVariableValue variableValue)
53 {
54 ArgumentNullException.ThrowIfNull(
55 context);
56
57 ArgumentNullException.ThrowIfNull(
58 solution);
59
60 ArgumentNullException.ThrowIfNull(
61 domainKey);
62
63 ArgumentNullException.ThrowIfNull(
64 variableValue);
65
66 int itemId =
67 domainKey.GetRequiredInt32(
69
70 int transportResourceId =
71 domainKey.GetRequiredInt32(
73
74 int period =
75 domainKey.GetRequiredInt32(
77
78 WarehouseReference origin =
79 ResolveWarehouse(
80 domainKey,
83 "origin");
84
85 WarehouseReference destination =
86 ResolveWarehouse(
87 domainKey,
90 "destination");
91
92 TransportDecision? decision =
93 solution.TransportDecisions
94 .FirstOrDefault(
95 existing =>
96 existing.Matches(
97 itemId,
98 transportResourceId,
99 origin,
100 destination));
101
102 if (decision is null)
103 {
104 if (solution.PlanningHorizon <= 0)
105 {
106 throw new InvalidOperationException(
107 "The target lot-sizing solution must have a " +
108 "strictly positive planning horizon before " +
109 "transport decisions are mapped.");
110 }
111
112 decision =
113 new TransportDecision(
114 itemId,
115 transportResourceId,
116 origin,
117 destination,
118 solution.PlanningHorizon);
119
120 solution.AddTransportDecision(
121 decision);
122 }
123
124 decision.SetSetupActivated(
125 period,
126 variableValue.Value >
128 }
129
130 private static WarehouseReference ResolveWarehouse(
131 MathematicalDomainKey domainKey,
132 string warehouseSegment,
133 string plantSegment,
134 string role)
135 {
136 bool hasStandaloneWarehouse =
137 domainKey.TryGetInt32(
138 warehouseSegment,
139 out int warehouseId);
140
141 bool hasPlantWarehouse =
142 domainKey.TryGetInt32(
143 plantSegment,
144 out int plantId);
145
146 if (hasStandaloneWarehouse ==
147 hasPlantWarehouse)
148 {
149 throw new InvalidOperationException(
150 $"A transport-setup domain key must identify " +
151 $"exactly one {role} warehouse using either " +
152 $"'{warehouseSegment}' or '{plantSegment}'.");
153 }
154
155 if (hasStandaloneWarehouse)
156 {
157 return WarehouseReference
158 .ForStandaloneWarehouse(
159 warehouseId);
160 }
161
162 return WarehouseReference
163 .ForPlantWarehouse(
164 plantId);
165 }
166}
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 TransportSetup
Identifies transport setup decision variables.
Provides a reusable base implementation for mathematical decision mappers handling one domain-key cat...
virtual double ZeroTolerance
Gets the absolute tolerance below which a solver value is considered equal to zero.
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 TransportResource
Transport-resource identifier segment.
const string DestinationWarehouse
Standalone destination-warehouse identifier segment.
const string DestinationPlant
Destination-plant identifier segment.
const string OriginWarehouse
Standalone origin-warehouse identifier segment.
Provides the shared state and lookup services required while mapping a mathematical solver result bac...
Maps mathematical item-specific transport setup values to transport decisions in a lot-sizing solutio...
override void MapValue(MathematicalSolutionMappingContext context, LotSizingSolution solution, MathematicalDomainKey domainKey, MathematicalVariableValue variableValue)
Maps one non-zero transport setup value.
override string Category
Gets the mathematical domain-key category handled by this mapper.