LotSizingDataModel.Solver 2.1.0
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
WarehouseCapacityConstraintFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using LotSizingDataModel.Core.PhysicalModel;
5using LotSizingDataModel.Core.Relationships;
6using LotSizingDataModel.Instance;
10
12
13/// <summary>
14/// Builds global warehouse capacity constraints by aggregating
15/// all item-specific inventory loads stored at the warehouse.
16/// </summary>
19{
20 /// <summary>
21 /// Gets the unique constraint-family identifier.
22 /// </summary>
23 public override string ConstraintFamilyId =>
24 "warehouseCapacity";
25
26 /// <summary>
27 /// Determines whether at least one physical warehouse is
28 /// globally capacitated.
29 /// </summary>
30 public override bool IsEnabled(
31 LotSizingInstance instance,
33 {
34 ArgumentNullException.ThrowIfNull(instance);
35 ArgumentNullException.ThrowIfNull(options);
36
37 foreach (var entry in StandardFormulationResourceEnumerator
38 .EnumerateWarehouses(instance.SupplyChain))
39 {
40 if (entry.Warehouse.CapacityConstraint is not null)
41 {
42 return true;
43 }
44 }
45
46 return false;
47 }
48
49 /// <summary>
50 /// Builds global warehouse capacity constraints.
51 /// </summary>
52 protected override ValueTask BuildConstraintsAsync(
53 LotSizingInstance instance,
56 CancellationToken cancellationToken)
57 {
58 foreach (var entry in StandardFormulationResourceEnumerator
59 .EnumerateWarehouses(instance.SupplyChain))
60 {
61 if (entry.Warehouse.CapacityConstraint is null)
62 {
63 continue;
64 }
65
66 for (int period = 1;
67 period <= instance.PlanningHorizon;
68 period++)
69 {
70 cancellationToken.ThrowIfCancellationRequested();
71
72 if (entry.Warehouse.GetStorageCapacity(period) is not double maximum)
73 continue;
74
75 var expression =
77
78 foreach (Inventory inventory
79 in instance.SupplyChain.Inventories)
80 {
82 inventory.Warehouse,
83 entry.Reference))
84 {
85 continue;
86 }
87
88 var inventoryKeyBuilder =
91 .Add(
93 inventory.ItemId);
94
96 inventoryKeyBuilder,
97 inventory.Warehouse);
98
99 string inventoryKey =
100 inventoryKeyBuilder
101 .Add(
103 period)
104 .Build();
105
106 expression.Add(
107 context.GetVariable(inventoryKey),
108 inventory.UnitCapacityConsumption?[period] ??
109 1.0);
110
111 double setupTime =
112 inventory.SetupTime?[period] ??
113 0.0;
114
115 if (setupTime > 0.0)
116 {
117 var setupKeyBuilder =
120 .Add(
122 inventory.ItemId);
123
125 setupKeyBuilder,
126 inventory.Warehouse);
127
128 string setupKey =
129 setupKeyBuilder
130 .Add(
132 period)
133 .Build();
134
135 if (context.VariableRegistry.TryGet(
136 setupKey,
137 out MathematicalVariable? setupVariable) &&
138 setupVariable is not null)
139 {
140 expression.Add(
141 setupVariable,
142 setupTime);
143 }
144 }
145 }
146
147 var additionalKeyBuilder =
150 .WarehouseAdditionalCapacity);
151
153 additionalKeyBuilder,
154 entry.Reference);
155
156 string additionalKey =
157 additionalKeyBuilder
158 .Add(
160 period)
161 .Build();
162
163 if (context.VariableRegistry.TryGet(
164 additionalKey,
165 out MathematicalVariable? additionalVariable) &&
166 additionalVariable is not null)
167 {
168 expression.Subtract(
169 additionalVariable);
170 }
171
173 context,
174 $"warehouseCapacity_w{entry.Reference.ReferenceId}" +
175 $"_t{period}",
176 expression.Build(),
177 MathematicalConstraintSense.LessThanOrEqual,
178 maximum,
179 description:
180 "Global warehouse capacity constraint.");
181 }
182 }
183
184 return ValueTask.CompletedTask;
185 }
186}
Provides a fluent builder for linear expressions.
Builds stable business-domain keys for mathematical variables and constraints.
MathematicalDomainKeyBuilder Add(string name, string value)
Adds a text segment.
Provides the shared state required while constructing a solver-independent mathematical model.
Creates canonical domain-key fragments shared by standard formulation builders and provides warehouse...
static bool AreSameWarehouse(WarehouseReference left, WarehouseReference right)
Determines whether two warehouse references identify the same physical warehouse.
static MathematicalDomainKeyBuilder AddWarehouse(MathematicalDomainKeyBuilder builder, WarehouseReference warehouse)
Adds a warehouse reference using the generic warehouse or plant segment.
Enumerates physical resources together with the canonical references required by the mathematical for...
Provides common validation and helper methods for one constraint family of the standard lot-sizing fo...
static void AddConstraint(MathematicalModelBuildContext context, string name, LinearExpression expression, MathematicalConstraintSense sense, double rightHandSide, string domainKey="", string description="")
Adds one linear constraint to the mathematical model.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
Builds global warehouse capacity constraints by aggregating all item-specific inventory loads stored ...
override bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether at least one physical warehouse is globally capacitated.
override ValueTask BuildConstraintsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds global warehouse capacity constraints.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Inventory
Identifies inventory-level decision variables.
const string InventorySetup
Identifies inventory setup decision variables.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...
Represents one decision variable in a mathematical optimization model.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.