LotSizingDataModel.Solver 2.1.0
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
ProductionVariableFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
5using LotSizingDataModel.Core.Indexing;
6using LotSizingDataModel.Core.Relationships;
7using LotSizingDataModel.Instance;
10
12
13/// <summary>
14/// Builds the production-quantity variable family of the
15/// standard lot-sizing formulation.
16/// </summary>
17/// <remarks>
18/// One non-negative continuous production variable is created
19/// for every production routing and planning period.
20/// The canonical domain key is:
21/// <code>
22/// production|routing=&lt;routingId&gt;|period=&lt;period&gt;
23/// </code>
24/// Period numbers are one-based.
25/// </remarks>
28{
29 /// <summary>
30 /// Gets the unique identifier of the production variable
31 /// family.
32 /// </summary>
33 public override string FamilyId =>
35
36 /// <summary>
37 /// Determines whether production variables are required for
38 /// the supplied instance.
39 /// </summary>
40 /// <param name="instance">
41 /// Source lot-sizing instance.
42 /// </param>
43 /// <param name="options">
44 /// Standard formulation options.
45 /// </param>
46 /// <returns>
47 /// <see langword="true"/> when at least one production
48 /// routing exists; otherwise, <see langword="false"/>.
49 /// </returns>
50 public override bool IsEnabled(
51 LotSizingInstance instance,
53 {
54 ArgumentNullException.ThrowIfNull(
55 instance);
56
57 ArgumentNullException.ThrowIfNull(
58 options);
59
60 return
61 instance.SupplyChain.ProductionRoutings.Count > 0;
62 }
63
64 /// <summary>
65 /// Builds the production-quantity variables.
66 /// </summary>
67 /// <param name="instance">
68 /// Source lot-sizing instance.
69 /// </param>
70 /// <param name="context">
71 /// Mathematical-model build context.
72 /// </param>
73 /// <param name="options">
74 /// Standard formulation options.
75 /// </param>
76 /// <param name="cancellationToken">
77 /// Token used to cancel variable construction.
78 /// </param>
79 /// <returns>
80 /// Task representing production-variable construction.
81 /// </returns>
82 protected override ValueTask BuildFamilyAsync(
83 LotSizingInstance instance,
86 CancellationToken cancellationToken)
87 {
88 int planningHorizon =
89 instance.PlanningHorizon;
90
91 if (planningHorizon <= 0)
92 {
93 throw new InvalidOperationException(
94 "Production variables cannot be built for an " +
95 "instance with a non-positive planning horizon.");
96 }
97
98 var index = new SupplyChainIndex(instance.SupplyChain);
99
100 foreach (
101 ProductionRouting routing
102 in instance.SupplyChain.ProductionRoutings)
103 {
104 cancellationToken.ThrowIfCancellationRequested();
105
106 if (routing.Id <= 0)
107 {
108 throw new InvalidOperationException(
109 "Every production routing must have a " +
110 "strictly positive identifier before the " +
111 "mathematical formulation is built.");
112 }
113
114 var workCenters = routing.WorkCenters.Select(index.GetRequiredWorkCenter).ToArray();
115
116 for (
117 int period = 1;
118 period <= planningHorizon;
119 period++)
120 {
121 cancellationToken.ThrowIfCancellationRequested();
122
123 string domainKey =
126 .Add(
128 routing.Id)
129 .Add(
131 period)
132 .Build();
133
134 string variableName =
135 $"x_r{routing.Id}_t{period}";
136
137 // A closed stage forbids production even when its unit consumption is zero.
138 double upperBound = workCenters.Any(center => center.GetProductionCapacity(period) == 0)
139 ? 0 : double.PositiveInfinity;
141 context,
142 variableName,
143 domainKey,
144 upperBound,
145 $"Production quantity for routing " +
146 $"{routing.Id} in period {period}.");
147 }
148 }
149
150 return ValueTask.CompletedTask;
151 }
152}
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.
Builds the production-quantity variable family of the standard lot-sizing formulation.
override string FamilyId
Gets the unique identifier of the production variable family.
override ValueTask BuildFamilyAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds the production-quantity variables.
override bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether production variables are required for the supplied instance.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
Provides common validation and variable-registration helpers for one variable family of the standard ...
static MathematicalVariable AddNonNegativeContinuousVariable(MathematicalModelBuildContext context, string name, string domainKey, double upperBound=double.PositiveInfinity, string description="")
Creates and registers one continuous non-negative variable.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string Production
Identifies production-quantity decision variables.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...