LotSizingDataModel.Solver 2.1.0
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
WarehouseActivationLinkConstraintFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using LotSizingDataModel.Core.Relationships;
5using LotSizingDataModel.Instance;
9
11
12/// <summary>
13/// Links aggregate warehouse inventory load to binary warehouse activation.
14/// </summary>
17{
18 /// <summary>Gets the family identifier.</summary>
19 public override string ConstraintFamilyId => "warehouseActivationLink";
20
21 /// <summary>Determines whether the family is enabled.</summary>
22 public override bool IsEnabled(
23 LotSizingInstance instance,
25 {
26 ArgumentNullException.ThrowIfNull(instance);
27 ArgumentNullException.ThrowIfNull(options);
28
29 if (!options.IncludeResourceActivation)
30 {
31 return false;
32 }
33
34 foreach (var entry in StandardFormulationResourceEnumerator
35 .EnumerateWarehouses(instance.SupplyChain))
36 {
37 if (entry.Warehouse.FixedUsageCost is not null &&
38 entry.Warehouse.CapacityConstraint is not null)
39 {
40 return true;
41 }
42 }
43
44 return false;
45 }
46
47 /// <summary>Builds warehouse activation-link constraints.</summary>
48 protected override ValueTask BuildConstraintsAsync(
49 LotSizingInstance instance,
52 CancellationToken cancellationToken)
53 {
54 foreach (var entry in StandardFormulationResourceEnumerator
55 .EnumerateWarehouses(instance.SupplyChain))
56 {
57 if (entry.Warehouse.FixedUsageCost is null ||
58 entry.Warehouse.CapacityConstraint is null)
59 {
60 continue;
61 }
62
63 for (int period = 1; period <= instance.PlanningHorizon; period++)
64 {
65 cancellationToken.ThrowIfCancellationRequested();
66
67 var maximum = entry.Warehouse.GetStorageCapacity(period)
68 ?? throw new InvalidOperationException("Warehouse activation requires a finite capacity in every period.");
69
70 var expression = new LinearExpressionBuilder();
71
72 foreach (Inventory inventory in instance.SupplyChain.Inventories)
73 {
75 inventory.Warehouse,
76 entry.Reference))
77 {
78 continue;
79 }
80
81 expression.Add(
82 context.GetVariable(
86 inventory.ItemId,
87 inventory.Warehouse,
88 period)),
89 inventory.UnitCapacityConsumption?[period] ?? 1.0);
90 }
91
92 var activationKeyBuilder =
95
97 activationKeyBuilder,
98 entry.Reference);
99
100 string activationKey =
101 activationKeyBuilder
103 .Build();
104
105 expression.Subtract(
106 context.GetVariable(activationKey),
107 maximum);
108
110 context,
111 $"warehouseActivationLink_w" +
112 $"{entry.Reference.ReferenceId}_t{period}",
113 expression.Build(),
114 MathematicalConstraintSense.LessThanOrEqual,
115 0.0,
116 description:
117 "Warehouse inventory load requires activation.");
118 }
119 }
120
121 return ValueTask.CompletedTask;
122 }
123}
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...
Creates canonical variable domain keys used by the standard formulation constraint builders.
static string CreateInventoryKey(string category, int itemId, WarehouseReference warehouse, int period)
Creates an inventory-like key for an item, warehouse, period, and decision category.
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.
bool IncludeResourceActivation
Gets or sets whether resource activations are included.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string WarehouseActivation
Identifies warehouse activation decision variables.
const string Inventory
Identifies inventory-level decision variables.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.