LotSizingDataModel.Solver 2.1.0
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
WorkCenterActivationLinkConstraintFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
5using LotSizingDataModel.Core.Querying;
6using LotSizingDataModel.Core.Relationships;
7using LotSizingDataModel.Instance;
11
13
14/// <summary>
15/// Links work-center production load to binary work-center activation.
16/// </summary>
19{
20 /// <summary>Gets the family identifier.</summary>
21 public override string ConstraintFamilyId => "workCenterActivationLink";
22
23 /// <summary>Determines whether the family is enabled.</summary>
24 public override bool IsEnabled(
25 LotSizingInstance instance,
27 {
28 ArgumentNullException.ThrowIfNull(instance);
29 ArgumentNullException.ThrowIfNull(options);
30
31 return options.IncludeResourceActivation &&
33 .EnumerateWorkCenters(instance.SupplyChain)
34 .Any(entry =>
35 entry.WorkCenter.FixedUsageCost is not null);
36 }
37
38 /// <summary>Builds work-center activation-link constraints.</summary>
39 protected override ValueTask BuildConstraintsAsync(
40 LotSizingInstance instance,
43 CancellationToken cancellationToken)
44 {
45 var queries = new SupplyChainQueries(instance.SupplyChain);
46 var estimator = new GenericProductionSetupBigMEstimator();
47
48 foreach (var entry in StandardFormulationResourceEnumerator
49 .EnumerateWorkCenters(instance.SupplyChain))
50 {
51 if (entry.WorkCenter.FixedUsageCost is null)
52 {
53 continue;
54 }
55
56 for (int period = 1; period <= instance.PlanningHorizon; period++)
57 {
58 cancellationToken.ThrowIfCancellationRequested();
59
60 var maximum = entry.WorkCenter.GetProductionCapacity(period);
61
62 var expression = new LinearExpressionBuilder();
63 var activation = context.GetVariable(new MathematicalDomainKeyBuilder(
66 .Add(MathematicalDomainKeySegment.WorkCenter, entry.WorkCenter.Id)
68 .Build());
69
70 foreach (ProductionRouting routing
71 in instance.SupplyChain.ProductionRoutings)
72 {
73 var reference =
74 routing.WorkCenters.FirstOrDefault(candidate =>
75 candidate.PlantId == entry.PlantId &&
76 candidate.WorkCenterId == entry.WorkCenter.Id);
77
78 if (reference is null)
79 {
80 continue;
81 }
82
83 ProductionCharacteristic characteristic =
84 queries.GetRequiredProductionCharacteristic(
85 routing.ItemId,
86 reference);
87
88 var production = context.GetVariable(
90 var unitLoad = characteristic.UnitCapacityConsumption?[period] ?? 1.0;
91 if (maximum.HasValue && unitLoad > 0)
92 {
93 expression.Add(production, unitLoad);
94 }
95 else
96 {
97 var bound = estimator.Estimate(instance, routing, period, options);
98 AddConstraint(context,
99 $"workCenterActivationQuantity_p{entry.PlantId}_w{entry.WorkCenter.Id}_r{routing.Id}_t{period}",
100 new LinearExpressionBuilder().Add(production).Subtract(activation, bound.Value).Build(),
101 MathematicalConstraintSense.LessThanOrEqual, 0.0,
102 description: $"Production requires activation. Big-M={bound.Value:G17}. {bound.Source}");
103 }
104 }
105
106 if (maximum is not double limit)
107 continue;
108
109 expression.Subtract(activation, limit);
111 context,
112 $"workCenterActivationLink_p{entry.PlantId}" +
113 $"_w{entry.WorkCenter.Id}_t{period}",
114 expression.Build(),
115 MathematicalConstraintSense.LessThanOrEqual,
116 0.0,
117 description:
118 "Work-center production load requires activation.");
119 }
120 }
121
122 return ValueTask.CompletedTask;
123 }
124}
Provides a fluent builder for linear expressions.
LinearExpression Build(bool clone=true)
Builds and validates the linear 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.
Enumerates physical resources together with the canonical references required by the mathematical for...
static IEnumerable<(int PlantId, WorkCenter WorkCenter)> EnumerateWorkCenters(SupplyChain supplyChain)
Enumerates every work center with its containing plant identifier.
Creates canonical variable domain keys used by the standard formulation constraint builders.
static string CreateProductionKey(int routingId, int period)
Creates a production decision key.
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.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string WorkCenterActivation
Identifies work-center activation 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.