LotSizingDataModel.Solver 2.0.1
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 entry.WorkCenter.CapacityConstraint is not null);
37 }
38
39 /// <summary>Builds work-center activation-link constraints.</summary>
40 protected override ValueTask BuildConstraintsAsync(
41 LotSizingInstance instance,
44 CancellationToken cancellationToken)
45 {
46 var queries = new SupplyChainQueries(instance.SupplyChain);
47
48 foreach (var entry in StandardFormulationResourceEnumerator
49 .EnumerateWorkCenters(instance.SupplyChain))
50 {
51 if (entry.WorkCenter.FixedUsageCost is null ||
52 entry.WorkCenter.CapacityConstraint is null)
53 {
54 continue;
55 }
56
57 for (int period = 1; period <= instance.PlanningHorizon; period++)
58 {
59 cancellationToken.ThrowIfCancellationRequested();
60
61 var expression = new LinearExpressionBuilder();
62
63 foreach (ProductionRouting routing
64 in instance.SupplyChain.ProductionRoutings)
65 {
66 var reference =
67 routing.WorkCenters.FirstOrDefault(candidate =>
68 candidate.PlantId == entry.PlantId &&
69 candidate.WorkCenterId == entry.WorkCenter.Id);
70
71 if (reference is null)
72 {
73 continue;
74 }
75
76 ProductionCharacteristic characteristic =
77 queries.GetRequiredProductionCharacteristic(
78 routing.ItemId,
79 reference);
80
81 expression.Add(
82 context.GetVariable(
84 .CreateProductionKey(routing.Id, period)),
85 characteristic.UnitCapacityConsumption?[period] ?? 1.0);
86 }
87
88 string activationKey =
92 .Add(
94 entry.WorkCenter.Id)
96 .Build();
97
98 expression.Subtract(
99 context.GetVariable(activationKey),
100 entry.WorkCenter.CapacityConstraint[period]);
101
103 context,
104 $"workCenterActivationLink_p{entry.PlantId}" +
105 $"_w{entry.WorkCenter.Id}_t{period}",
106 expression.Build(),
107 MathematicalConstraintSense.LessThanOrEqual,
108 0.0,
109 description:
110 "Work-center production load requires activation.");
111 }
112 }
113
114 return ValueTask.CompletedTask;
115 }
116}
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.
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.