LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
StandardLotSizingVariableBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6using LotSizingDataModel.Instance;
8
10
11/// <summary>
12/// Aggregates the variable-family builders used by the standard
13/// lot-sizing formulation.
14/// </summary>
15/// <remarks>
16/// This class deliberately contains no direct knowledge of the
17/// physical supply-chain classes. Each variable family is built
18/// by a dedicated <see cref="IStandardLotSizingVariableFamilyBuilder"/>
19/// implementation. This keeps the orchestration stable while
20/// individual decision families evolve.
21/// </remarks>
24{
25 private readonly IReadOnlyList<IStandardLotSizingVariableFamilyBuilder>
26 _familyBuilders;
27
28 /// <summary>
29 /// Initializes the standard variable builder.
30 /// </summary>
31 /// <param name="familyBuilders">
32 /// Ordered variable-family builders.
33 /// </param>
34 /// <exception cref="ArgumentNullException">
35 /// Thrown when <paramref name="familyBuilders"/> is
36 /// <see langword="null"/>.
37 /// </exception>
38 /// <exception cref="InvalidOperationException">
39 /// Thrown when the collection contains a
40 /// <see langword="null"/> builder or duplicate family
41 /// identifiers.
42 /// </exception>
44 IEnumerable<IStandardLotSizingVariableFamilyBuilder> familyBuilders)
45 {
46 ArgumentNullException.ThrowIfNull(
47 familyBuilders);
48
50 familyBuilders.ToArray();
51
52 if (builders.Any(
53 builder =>
54 builder is null))
55 {
56 throw new InvalidOperationException(
57 "The standard variable-family builder collection " +
58 "cannot contain a null entry.");
59 }
60
61 string[] duplicateFamilyIds =
62 builders
63 .GroupBy(
64 builder =>
65 builder.FamilyId,
66 StringComparer.OrdinalIgnoreCase)
67 .Where(
68 group =>
69 group.Count() > 1)
70 .Select(
71 group =>
72 group.Key)
73 .ToArray();
74
75 if (duplicateFamilyIds.Length > 0)
76 {
77 throw new InvalidOperationException(
78 "Duplicate standard variable-family identifiers " +
79 "were registered: " +
80 string.Join(
81 ", ",
82 duplicateFamilyIds));
83 }
84
85 _familyBuilders =
86 builders;
87 }
88
89 /// <summary>
90 /// Gets the ordered variable-family builders.
91 /// </summary>
92 public IReadOnlyList<IStandardLotSizingVariableFamilyBuilder>
94 _familyBuilders;
95
96 /// <summary>
97 /// Creates and registers all mathematical variables required
98 /// by the standard lot-sizing formulation.
99 /// </summary>
100 /// <param name="instance">
101 /// Source lot-sizing instance.
102 /// </param>
103 /// <param name="context">
104 /// Mathematical-model build context receiving the generated
105 /// variables.
106 /// </param>
107 /// <param name="options">
108 /// Standard formulation options.
109 /// </param>
110 /// <param name="cancellationToken">
111 /// Token used to cancel variable construction.
112 /// </param>
113 /// <returns>
114 /// Task representing variable construction.
115 /// </returns>
116 /// <exception cref="ArgumentNullException">
117 /// Thrown when <paramref name="instance"/>,
118 /// <paramref name="context"/>, or
119 /// <paramref name="options"/> is
120 /// <see langword="null"/>.
121 /// </exception>
122 public async ValueTask BuildAsync(
123 LotSizingInstance instance,
126 CancellationToken cancellationToken = default)
127 {
128 ArgumentNullException.ThrowIfNull(
129 instance);
130
131 ArgumentNullException.ThrowIfNull(
132 context);
133
134 ArgumentNullException.ThrowIfNull(
135 options);
136
137 options.EnsureValid();
138
139 foreach (
141 in _familyBuilders)
142 {
143 cancellationToken.ThrowIfCancellationRequested();
144
145 if (!familyBuilder.IsEnabled(
146 instance,
147 options))
148 {
149 continue;
150 }
151
152 await familyBuilder.BuildAsync(
153 instance,
154 context,
155 options,
156 cancellationToken);
157 }
158 }
159}
160
161/// <summary>
162/// Defines one decision-variable family of the standard
163/// lot-sizing formulation.
164/// </summary>
165/// <remarks>
166/// Examples of variable families are production, setup,
167/// inventory, backlog, transport, procurement, and resource
168/// capacity variables.
169/// </remarks>
171{
172 /// <summary>
173 /// Gets the unique identifier of the variable family.
174 /// </summary>
175 string FamilyId
176 {
177 get;
178 }
179
180 /// <summary>
181 /// Determines whether this variable family is enabled for
182 /// the supplied instance and formulation options.
183 /// </summary>
184 /// <param name="instance">
185 /// Source lot-sizing instance.
186 /// </param>
187 /// <param name="options">
188 /// Standard formulation options.
189 /// </param>
190 /// <returns>
191 /// <see langword="true"/> when this family must be built;
192 /// otherwise, <see langword="false"/>.
193 /// </returns>
195 LotSizingInstance instance,
197
198 /// <summary>
199 /// Creates and registers all variables belonging to this
200 /// family.
201 /// </summary>
202 /// <param name="instance">
203 /// Source lot-sizing instance.
204 /// </param>
205 /// <param name="context">
206 /// Mathematical-model build context.
207 /// </param>
208 /// <param name="options">
209 /// Standard formulation options.
210 /// </param>
211 /// <param name="cancellationToken">
212 /// Token used to cancel family construction.
213 /// </param>
214 /// <returns>
215 /// Task representing variable-family construction.
216 /// </returns>
217 ValueTask BuildAsync(
218 LotSizingInstance instance,
221 CancellationToken cancellationToken = default);
222}
Provides the shared state required while constructing a solver-independent mathematical model.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
StandardLotSizingVariableBuilder(IEnumerable< IStandardLotSizingVariableFamilyBuilder > familyBuilders)
Initializes the standard variable builder.
IReadOnlyList< IStandardLotSizingVariableFamilyBuilder > FamilyBuilders
Gets the ordered variable-family builders.
async ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Creates and registers all mathematical variables required by the standard lot-sizing formulation.
Defines the variable-construction component used by the standard solver-independent lot-sizing formul...
Defines one decision-variable family of the standard lot-sizing formulation.
bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this variable family is enabled for the supplied instance and formulation options.
ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Creates and registers all variables belonging to this family.