LotSizingDataModel.Solver 2.0.1
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 var expression =
74
75 foreach (Inventory inventory
76 in instance.SupplyChain.Inventories)
77 {
79 inventory.Warehouse,
80 entry.Reference))
81 {
82 continue;
83 }
84
85 var inventoryKeyBuilder =
88 .Add(
90 inventory.ItemId);
91
93 inventoryKeyBuilder,
94 inventory.Warehouse);
95
96 string inventoryKey =
97 inventoryKeyBuilder
98 .Add(
100 period)
101 .Build();
102
103 expression.Add(
104 context.GetVariable(inventoryKey),
105 inventory.UnitCapacityConsumption?[period] ??
106 1.0);
107
108 double setupTime =
109 inventory.SetupTime?[period] ??
110 0.0;
111
112 if (setupTime > 0.0)
113 {
114 var setupKeyBuilder =
117 .Add(
119 inventory.ItemId);
120
122 setupKeyBuilder,
123 inventory.Warehouse);
124
125 string setupKey =
126 setupKeyBuilder
127 .Add(
129 period)
130 .Build();
131
132 if (context.VariableRegistry.TryGet(
133 setupKey,
134 out MathematicalVariable? setupVariable) &&
135 setupVariable is not null)
136 {
137 expression.Add(
138 setupVariable,
139 setupTime);
140 }
141 }
142 }
143
144 var additionalKeyBuilder =
147 .WarehouseAdditionalCapacity);
148
150 additionalKeyBuilder,
151 entry.Reference);
152
153 string additionalKey =
154 additionalKeyBuilder
155 .Add(
157 period)
158 .Build();
159
160 if (context.VariableRegistry.TryGet(
161 additionalKey,
162 out MathematicalVariable? additionalVariable) &&
163 additionalVariable is not null)
164 {
165 expression.Subtract(
166 additionalVariable);
167 }
168
170 context,
171 $"warehouseCapacity_w{entry.Reference.ReferenceId}" +
172 $"_t{period}",
173 expression.Build(),
174 MathematicalConstraintSense.LessThanOrEqual,
175 entry.Warehouse.CapacityConstraint[period],
176 description:
177 "Global warehouse capacity constraint.");
178 }
179 }
180
181 return ValueTask.CompletedTask;
182 }
183}
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.