LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
StandardLotSizingConstraintFamilyBuilderBase.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 helper methods for one
12/// constraint family of the standard lot-sizing formulation.
13/// </summary>
16{
17 /// <summary>
18 /// Gets the unique identifier of the constraint family.
19 /// </summary>
20 public abstract string ConstraintFamilyId
21 {
22 get;
23 }
24
25 /// <summary>
26 /// Determines whether this constraint family is enabled.
27 /// </summary>
28 /// <param name="instance">
29 /// Source lot-sizing instance.
30 /// </param>
31 /// <param name="options">
32 /// Standard formulation options.
33 /// </param>
34 /// <returns>
35 /// <see langword="true"/> by default.
36 /// </returns>
37 public virtual bool IsEnabled(
38 LotSizingInstance instance,
40 {
41 ArgumentNullException.ThrowIfNull(
42 instance);
43
44 ArgumentNullException.ThrowIfNull(
45 options);
46
47 return true;
48 }
49
50 /// <summary>
51 /// Validates common arguments and builds the concrete
52 /// constraint family.
53 /// </summary>
54 /// <param name="instance">
55 /// Source lot-sizing instance.
56 /// </param>
57 /// <param name="context">
58 /// Mathematical-model build context.
59 /// </param>
60 /// <param name="options">
61 /// Standard formulation options.
62 /// </param>
63 /// <param name="cancellationToken">
64 /// Token used to cancel constraint construction.
65 /// </param>
66 /// <returns>
67 /// Task representing constraint construction.
68 /// </returns>
69 public async ValueTask BuildAsync(
70 LotSizingInstance instance,
73 CancellationToken cancellationToken = default)
74 {
75 ArgumentNullException.ThrowIfNull(
76 instance);
77
78 ArgumentNullException.ThrowIfNull(
79 context);
80
81 ArgumentNullException.ThrowIfNull(
82 options);
83
84 if (string.IsNullOrWhiteSpace(
86 {
87 throw new InvalidOperationException(
88 "A standard constraint-family builder must " +
89 "expose a non-empty family identifier.");
90 }
91
92 options.EnsureValid();
93
94 cancellationToken.ThrowIfCancellationRequested();
95
97 instance,
98 context,
99 options,
100 cancellationToken);
101 }
102
103 /// <summary>
104 /// Builds the concrete constraint family.
105 /// </summary>
106 /// <param name="instance">
107 /// Source lot-sizing instance.
108 /// </param>
109 /// <param name="context">
110 /// Mathematical-model build context.
111 /// </param>
112 /// <param name="options">
113 /// Standard formulation options.
114 /// </param>
115 /// <param name="cancellationToken">
116 /// Token used to cancel constraint construction.
117 /// </param>
118 /// <returns>
119 /// Task representing constraint construction.
120 /// </returns>
121 protected abstract ValueTask BuildConstraintsAsync(
122 LotSizingInstance instance,
125 CancellationToken cancellationToken);
126
127 /// <summary>
128 /// Resolves a required variable from its canonical domain
129 /// key.
130 /// </summary>
131 /// <param name="context">
132 /// Mathematical-model build context.
133 /// </param>
134 /// <param name="domainKey">
135 /// Canonical variable domain key.
136 /// </param>
137 /// <returns>
138 /// Registered mathematical variable.
139 /// </returns>
142 string domainKey)
143 {
144 ArgumentNullException.ThrowIfNull(
145 context);
146
147 if (string.IsNullOrWhiteSpace(
148 domainKey))
149 {
150 throw new ArgumentException(
151 "A variable domain key is required.",
152 nameof(domainKey));
153 }
154
155 return context.GetVariable(
156 domainKey);
157 }
158
159 /// <summary>
160 /// Adds one linear constraint to the mathematical model.
161 /// </summary>
162 /// <param name="context">
163 /// Mathematical-model build context.
164 /// </param>
165 /// <param name="name">
166 /// Constraint name.
167 /// </param>
168 /// <param name="expression">
169 /// Left-hand-side expression.
170 /// </param>
171 /// <param name="sense">
172 /// Constraint relational sense.
173 /// </param>
174 /// <param name="rightHandSide">
175 /// Right-hand-side value.
176 /// </param>
177 /// <param name="domainKey">
178 /// Optional business-domain key.
179 /// </param>
180 /// <param name="description">
181 /// Optional constraint description.
182 /// </param>
183 protected static void AddConstraint(
185 string name,
186 LinearExpression expression,
188 double rightHandSide,
189 string domainKey = "",
190 string description = "")
191 {
192 ArgumentNullException.ThrowIfNull(
193 context);
194
195 ArgumentNullException.ThrowIfNull(
196 expression);
197
198 context.AddConstraint(
199 name,
200 expression,
201 sense,
202 rightHandSide,
203 domainKey,
204 description);
205 }
206}
Provides the shared state required while constructing a solver-independent mathematical model.
LinearConstraint AddConstraint(string name, LinearExpression leftHandSide, MathematicalConstraintSense sense, double rightHandSide, string domainKey="", string description="")
Adds a linear constraint to the mathematical model.
MathematicalVariable GetVariable(string domainKey)
Gets a required mathematical variable from its business-domain key.
Provides common validation and helper methods for one constraint family of the standard lot-sizing fo...
async ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Validates common arguments and builds the concrete constraint family.
static MathematicalVariable GetVariable(MathematicalModelBuildContext context, string domainKey)
Resolves a required variable from its canonical domain key.
virtual bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this constraint family is enabled.
ValueTask BuildConstraintsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds the concrete constraint family.
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.
Represents a linear expression composed of variable terms and a constant value.
Represents one decision variable in a mathematical optimization model.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.