LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
SafetyStockConstraintFamilyBuilder.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/// Builds inventory safety-stock constraints.
14/// </summary>
15/// <remarks>
16/// When a soft safety-stock violation variable exists, the
17/// generated constraint is:
18/// <code>
19/// inventory + violation &gt;= safetyStock.
20/// </code>
21/// Otherwise the safety-stock bound is hard:
22/// <code>
23/// inventory &gt;= safetyStock.
24/// </code>
25/// </remarks>
28{
29 /// <summary>
30 /// Gets the unique constraint-family identifier.
31 /// </summary>
32 public override string ConstraintFamilyId =>
33 "safetyStock";
34
35 /// <summary>
36 /// Determines whether safety-stock constraints are enabled.
37 /// </summary>
38 public override bool IsEnabled(
39 LotSizingInstance instance,
41 {
42 ArgumentNullException.ThrowIfNull(instance);
43 ArgumentNullException.ThrowIfNull(options);
44
45 if (!options.IncludeSafetyStock)
46 {
47 return false;
48 }
49
50 foreach (Inventory inventory in instance.SupplyChain.Inventories)
51 {
52 if (inventory.SafetyStock is not null)
53 {
54 return true;
55 }
56 }
57
58 return false;
59 }
60
61 /// <summary>
62 /// Builds safety-stock constraints for every configured
63 /// inventory and period.
64 /// </summary>
65 protected override ValueTask BuildConstraintsAsync(
66 LotSizingInstance instance,
69 CancellationToken cancellationToken)
70 {
71 foreach (Inventory inventory in instance.SupplyChain.Inventories)
72 {
73 if (inventory.SafetyStock is null)
74 {
75 continue;
76 }
77
78 for (int period = 1;
79 period <= instance.PlanningHorizon;
80 period++)
81 {
82 cancellationToken.ThrowIfCancellationRequested();
83
84 var inventoryKeyBuilder =
87 .Add(
89 inventory.ItemId);
90
92 inventoryKeyBuilder,
93 inventory.Warehouse);
94
95 string inventoryKey =
96 inventoryKeyBuilder
97 .Add(
99 period)
100 .Build();
101
102 MathematicalVariable inventoryVariable =
103 context.GetVariable(
104 inventoryKey);
105
106 var expression =
108 .Add(
109 inventoryVariable);
110
111 if (options.IncludeSafetyStockViolation &&
112 inventory.SafetyStockViolationCost is not null)
113 {
114 var violationKeyBuilder =
117 .InventorySafetyStockViolation)
118 .Add(
120 inventory.ItemId);
121
123 violationKeyBuilder,
124 inventory.Warehouse);
125
126 string violationKey =
127 violationKeyBuilder
128 .Add(
130 period)
131 .Build();
132
133 if (context.VariableRegistry.TryGet(
134 violationKey,
135 out MathematicalVariable? violationVariable) &&
136 violationVariable is not null)
137 {
138 expression.Add(
139 violationVariable);
140 }
141 }
142
144 context,
145 $"safetyStock_i{inventory.ItemId}" +
146 $"_w{inventory.Warehouse.ReferenceId}_t{period}",
147 expression.Build(),
148 MathematicalConstraintSense.GreaterThanOrEqual,
149 inventory.SafetyStock[period],
150 description:
151 "Inventory safety-stock constraint.");
152 }
153 }
154
155 return ValueTask.CompletedTask;
156 }
157}
Provides a fluent builder for linear expressions.
LinearExpressionBuilder Add(MathematicalVariable variable, double coefficient=1.0)
Adds a variable term to the expression.
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.
override string ConstraintFamilyId
Gets the unique constraint-family identifier.
override ValueTask BuildConstraintsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds safety-stock constraints for every configured inventory and period.
override bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether safety-stock constraints are enabled.
Creates canonical domain-key fragments shared by standard formulation builders and provides warehouse...
static MathematicalDomainKeyBuilder AddWarehouse(MathematicalDomainKeyBuilder builder, WarehouseReference warehouse)
Adds a warehouse reference using the generic warehouse or plant segment.
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 IncludeSafetyStock
Gets or sets whether safety-stock constraints are included.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Inventory
Identifies inventory-level 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.