LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
StandardLotSizingVariableFamilyBuilderBase.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 common validation and variable-registration helpers
12/// for one variable family of the standard lot-sizing
13/// formulation.
14/// </summary>
15/// <remarks>
16/// Derived classes only need to implement the family identifier,
17/// the family-enablement rule, and the actual family-specific
18/// variable construction.
19/// </remarks>
22{
23 /// <summary>
24 /// Gets the unique identifier of the variable family.
25 /// </summary>
26 public abstract string FamilyId
27 {
28 get;
29 }
30
31 /// <summary>
32 /// Determines whether this variable family is enabled for
33 /// the supplied instance and formulation options.
34 /// </summary>
35 /// <param name="instance">
36 /// Source lot-sizing instance.
37 /// </param>
38 /// <param name="options">
39 /// Standard formulation options.
40 /// </param>
41 /// <returns>
42 /// <see langword="true"/> when this family must be built;
43 /// otherwise, <see langword="false"/>.
44 /// </returns>
45 public virtual bool IsEnabled(
46 LotSizingInstance instance,
48 {
49 ArgumentNullException.ThrowIfNull(
50 instance);
51
52 ArgumentNullException.ThrowIfNull(
53 options);
54
55 return true;
56 }
57
58 /// <summary>
59 /// Validates the common arguments and builds the
60 /// family-specific variables.
61 /// </summary>
62 /// <param name="instance">
63 /// Source lot-sizing instance.
64 /// </param>
65 /// <param name="context">
66 /// Mathematical-model build context.
67 /// </param>
68 /// <param name="options">
69 /// Standard formulation options.
70 /// </param>
71 /// <param name="cancellationToken">
72 /// Token used to cancel family construction.
73 /// </param>
74 /// <returns>
75 /// Task representing variable-family construction.
76 /// </returns>
77 public async ValueTask BuildAsync(
78 LotSizingInstance instance,
81 CancellationToken cancellationToken = default)
82 {
83 ArgumentNullException.ThrowIfNull(
84 instance);
85
86 ArgumentNullException.ThrowIfNull(
87 context);
88
89 ArgumentNullException.ThrowIfNull(
90 options);
91
92 if (string.IsNullOrWhiteSpace(
93 FamilyId))
94 {
95 throw new InvalidOperationException(
96 "A standard variable-family builder must expose " +
97 "a non-empty family identifier.");
98 }
99
100 options.EnsureValid();
101
102 cancellationToken.ThrowIfCancellationRequested();
103
104 await BuildFamilyAsync(
105 instance,
106 context,
107 options,
108 cancellationToken);
109 }
110
111 /// <summary>
112 /// Builds the variables belonging to the concrete family.
113 /// </summary>
114 /// <param name="instance">
115 /// Source lot-sizing instance.
116 /// </param>
117 /// <param name="context">
118 /// Mathematical-model build context.
119 /// </param>
120 /// <param name="options">
121 /// Standard formulation options.
122 /// </param>
123 /// <param name="cancellationToken">
124 /// Token used to cancel family construction.
125 /// </param>
126 /// <returns>
127 /// Task representing concrete family construction.
128 /// </returns>
129 protected abstract ValueTask BuildFamilyAsync(
130 LotSizingInstance instance,
133 CancellationToken cancellationToken);
134
135 /// <summary>
136 /// Creates and registers one continuous non-negative
137 /// variable.
138 /// </summary>
139 /// <param name="context">
140 /// Mathematical-model build context.
141 /// </param>
142 /// <param name="name">
143 /// Mathematical variable name.
144 /// </param>
145 /// <param name="domainKey">
146 /// Canonical business-domain key.
147 /// </param>
148 /// <param name="upperBound">
149 /// Variable upper bound.
150 /// </param>
151 /// <param name="description">
152 /// Optional variable description.
153 /// </param>
154 /// <returns>
155 /// Created mathematical variable.
156 /// </returns>
159 string name,
160 string domainKey,
161 double upperBound = double.PositiveInfinity,
162 string description = "")
163 {
164 ArgumentNullException.ThrowIfNull(
165 context);
166
167 return context.AddVariable(
168 name,
169 domainKey,
170 MathematicalVariableType.Continuous,
171 0.0,
172 upperBound,
173 description);
174 }
175
176 /// <summary>
177 /// Creates and registers one binary variable.
178 /// </summary>
179 /// <param name="context">
180 /// Mathematical-model build context.
181 /// </param>
182 /// <param name="name">
183 /// Mathematical variable name.
184 /// </param>
185 /// <param name="domainKey">
186 /// Canonical business-domain key.
187 /// </param>
188 /// <param name="description">
189 /// Optional variable description.
190 /// </param>
191 /// <returns>
192 /// Created mathematical variable.
193 /// </returns>
196 string name,
197 string domainKey,
198 string description = "")
199 {
200 ArgumentNullException.ThrowIfNull(
201 context);
202
203 return context.AddVariable(
204 name,
205 domainKey,
207 0.0,
208 1.0,
209 description);
210 }
211
212 /// <summary>
213 /// Determines whether a finite non-negative upper bound is
214 /// structurally equal to zero according to the formulation
215 /// options.
216 /// </summary>
217 /// <param name="upperBound">
218 /// Candidate upper bound.
219 /// </param>
220 /// <param name="options">
221 /// Standard formulation options.
222 /// </param>
223 /// <returns>
224 /// <see langword="true"/> when the variable may be omitted
225 /// as structurally zero; otherwise,
226 /// <see langword="false"/>.
227 /// </returns>
228 protected static bool IsStructurallyZero(
229 double upperBound,
231 {
232 ArgumentNullException.ThrowIfNull(
233 options);
234
236 {
237 return false;
238 }
239
240 return
241 double.IsFinite(
242 upperBound) &&
243 upperBound >= 0.0 &&
244 upperBound <=
246 }
247}
Provides the shared state required while constructing a solver-independent mathematical model.
MathematicalVariable AddVariable(string name, string domainKey, MathematicalVariableType variableType, double lowerBound=0.0, double upperBound=double.PositiveInfinity, string description="")
Creates, adds, and registers a mathematical variable.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
bool RemoveStructurallyZeroVariables
Gets or sets whether structurally zero variables are omitted.
Provides common validation and variable-registration helpers for one variable family of the standard ...
virtual bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this variable family is enabled for the supplied instance and formulation options.
static MathematicalVariable AddBinaryVariable(MathematicalModelBuildContext context, string name, string domainKey, string description="")
Creates and registers one binary variable.
static bool IsStructurallyZero(double upperBound, StandardLotSizingFormulationOptions options)
Determines whether a finite non-negative upper bound is structurally equal to zero according to the f...
static MathematicalVariable AddNonNegativeContinuousVariable(MathematicalModelBuildContext context, string name, string domainKey, double upperBound=double.PositiveInfinity, string description="")
Creates and registers one continuous non-negative variable.
ValueTask BuildFamilyAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds the variables belonging to the concrete family.
async ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Validates the common arguments and builds the family-specific variables.
Represents one decision variable in a mathematical optimization model.
Defines one decision-variable family of the standard lot-sizing formulation.
MathematicalVariableType
Identifies the domain of a mathematical decision variable.