LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelFormulationBase.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using LotSizingDataModel.Instance;
7
9
10/// <summary>
11/// Provides a reusable base implementation for
12/// solver-independent lot-sizing formulations.
13/// </summary>
14public abstract class MathematicalModelFormulationBase :
16{
17 /// <summary>
18 /// Gets the unique formulation identifier.
19 /// </summary>
20 public abstract string FormulationId
21 {
22 get;
23 }
24
25 /// <summary>
26 /// Gets the human-readable formulation name.
27 /// </summary>
28 public abstract string Name
29 {
30 get;
31 }
32
33 /// <summary>
34 /// Gets the formulation description.
35 /// </summary>
36 public virtual string Description =>
37 string.Empty;
38
39 /// <summary>
40 /// Determines whether the formulation supports a
41 /// lot-sizing instance.
42 /// </summary>
43 /// <param name="instance">
44 /// Lot-sizing instance to evaluate.
45 /// </param>
46 /// <returns>
47 /// <see langword="true"/> when the formulation can represent
48 /// the instance; otherwise, <see langword="false"/>.
49 /// </returns>
50 public abstract bool CanBuild(
51 LotSizingInstance instance);
52
53 /// <summary>
54 /// Builds the solver-independent mathematical model.
55 /// </summary>
56 /// <param name="instance">
57 /// Lot-sizing instance to formulate.
58 /// </param>
59 /// <param name="cancellationToken">
60 /// Token used to cancel model construction.
61 /// </param>
62 /// <returns>
63 /// Task returning the complete mathematical model.
64 /// </returns>
65 /// <exception cref="ArgumentNullException">
66 /// Thrown when <paramref name="instance"/> is
67 /// <see langword="null"/>.
68 /// </exception>
69 /// <exception cref="InvalidOperationException">
70 /// Thrown when the formulation metadata is invalid, the
71 /// instance is unsupported, or the generated model is
72 /// invalid.
73 /// </exception>
74 public async ValueTask<MathematicalModel> BuildAsync(
75 LotSizingInstance instance,
76 CancellationToken cancellationToken = default)
77 {
78 ArgumentNullException.ThrowIfNull(
79 instance);
80
81 EnsureMetadataIsValid();
82
83 cancellationToken.ThrowIfCancellationRequested();
84
85 if (!CanBuild(
86 instance))
87 {
88 throw new InvalidOperationException(
89 $"Formulation '{FormulationId}' does not support " +
90 "the supplied lot-sizing instance.");
91 }
92
93 var context =
96 instance));
97
98 context.SetDescription(
100
102 instance,
103 context,
104 cancellationToken);
105
106 cancellationToken.ThrowIfCancellationRequested();
107
109 instance,
110 context,
111 cancellationToken);
112
113 cancellationToken.ThrowIfCancellationRequested();
114
116 instance,
117 context,
118 cancellationToken);
119
120 cancellationToken.ThrowIfCancellationRequested();
121
122 MathematicalModel model =
123 context.Build(
124 clone:
125 false);
126
128 instance,
129 model);
130
131 return model;
132 }
133
134 /// <summary>
135 /// Creates the mathematical model name.
136 /// </summary>
137 /// <param name="instance">
138 /// Lot-sizing instance being formulated.
139 /// </param>
140 /// <returns>
141 /// Mathematical model name.
142 /// </returns>
143 protected virtual string CreateModelName(
144 LotSizingInstance instance)
145 {
146 ArgumentNullException.ThrowIfNull(
147 instance);
148
149 return FormulationId;
150 }
151
152 /// <summary>
153 /// Builds and registers all mathematical variables.
154 /// </summary>
155 /// <param name="instance">
156 /// Lot-sizing instance being formulated.
157 /// </param>
158 /// <param name="context">
159 /// Mathematical-model build context.
160 /// </param>
161 /// <param name="cancellationToken">
162 /// Token used to cancel model construction.
163 /// </param>
164 /// <returns>
165 /// Task representing variable construction.
166 /// </returns>
167 protected abstract ValueTask BuildVariablesAsync(
168 LotSizingInstance instance,
170 CancellationToken cancellationToken);
171
172 /// <summary>
173 /// Builds the mathematical objective.
174 /// </summary>
175 /// <param name="instance">
176 /// Lot-sizing instance being formulated.
177 /// </param>
178 /// <param name="context">
179 /// Mathematical-model build context.
180 /// </param>
181 /// <param name="cancellationToken">
182 /// Token used to cancel model construction.
183 /// </param>
184 /// <returns>
185 /// Task representing objective construction.
186 /// </returns>
187 protected abstract ValueTask BuildObjectiveAsync(
188 LotSizingInstance instance,
190 CancellationToken cancellationToken);
191
192 /// <summary>
193 /// Builds all mathematical constraints.
194 /// </summary>
195 /// <param name="instance">
196 /// Lot-sizing instance being formulated.
197 /// </param>
198 /// <param name="context">
199 /// Mathematical-model build context.
200 /// </param>
201 /// <param name="cancellationToken">
202 /// Token used to cancel model construction.
203 /// </param>
204 /// <returns>
205 /// Task representing constraint construction.
206 /// </returns>
207 protected abstract ValueTask BuildConstraintsAsync(
208 LotSizingInstance instance,
210 CancellationToken cancellationToken);
211
212 /// <summary>
213 /// Performs formulation-specific validation after the model
214 /// has been built.
215 /// </summary>
216 /// <param name="instance">
217 /// Source lot-sizing instance.
218 /// </param>
219 /// <param name="model">
220 /// Generated mathematical model.
221 /// </param>
222 protected virtual void ValidateBuiltModel(
223 LotSizingInstance instance,
224 MathematicalModel model)
225 {
226 ArgumentNullException.ThrowIfNull(
227 instance);
228
229 ArgumentNullException.ThrowIfNull(
230 model);
231
232 model.EnsureValid();
233 }
234
235 private void EnsureMetadataIsValid()
236 {
237 if (string.IsNullOrWhiteSpace(
239 {
240 throw new InvalidOperationException(
241 "A formulation identifier is required.");
242 }
243
244 if (string.IsNullOrWhiteSpace(
245 Name))
246 {
247 throw new InvalidOperationException(
248 "A formulation name is required.");
249 }
250 }
251}
Provides the shared state required while constructing a solver-independent mathematical model.
Provides a reusable base implementation for solver-independent lot-sizing formulations.
virtual void ValidateBuiltModel(LotSizingInstance instance, MathematicalModel model)
Performs formulation-specific validation after the model has been built.
async ValueTask< MathematicalModel > BuildAsync(LotSizingInstance instance, CancellationToken cancellationToken=default)
Builds the solver-independent mathematical model.
ValueTask BuildConstraintsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, CancellationToken cancellationToken)
Builds all mathematical constraints.
ValueTask BuildObjectiveAsync(LotSizingInstance instance, MathematicalModelBuildContext context, CancellationToken cancellationToken)
Builds the mathematical objective.
virtual string CreateModelName(LotSizingInstance instance)
Creates the mathematical model name.
ValueTask BuildVariablesAsync(LotSizingInstance instance, MathematicalModelBuildContext context, CancellationToken cancellationToken)
Builds and registers all mathematical variables.
bool CanBuild(LotSizingInstance instance)
Determines whether the formulation supports a lot-sizing instance.
Represents a solver-independent mathematical optimization model.
void EnsureValid()
Validates the complete mathematical model.
Defines a solver-independent mathematical formulation for a lot-sizing instance.