LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
StandardLotSizingObjectiveTermBuilderBase.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/// objective-term family of the standard lot-sizing
13/// formulation.
14/// </summary>
17{
18 /// <summary>
19 /// Gets the unique identifier of the objective-term family.
20 /// </summary>
21 public abstract string TermFamilyId
22 {
23 get;
24 }
25
26 /// <summary>
27 /// Determines whether this objective-term family is enabled.
28 /// </summary>
29 /// <param name="instance">
30 /// Source lot-sizing instance.
31 /// </param>
32 /// <param name="options">
33 /// Standard formulation options.
34 /// </param>
35 /// <returns>
36 /// <see langword="true"/> by default.
37 /// </returns>
38 public virtual bool IsEnabled(
39 LotSizingInstance instance,
41 {
42 ArgumentNullException.ThrowIfNull(
43 instance);
44
45 ArgumentNullException.ThrowIfNull(
46 options);
47
48 return true;
49 }
50
51 /// <summary>
52 /// Validates common arguments and builds the concrete
53 /// objective-term family.
54 /// </summary>
55 /// <param name="instance">
56 /// Source lot-sizing instance.
57 /// </param>
58 /// <param name="context">
59 /// Mathematical-model build context.
60 /// </param>
61 /// <param name="expressionBuilder">
62 /// Shared objective-expression builder.
63 /// </param>
64 /// <param name="options">
65 /// Standard formulation options.
66 /// </param>
67 /// <param name="cancellationToken">
68 /// Token used to cancel term construction.
69 /// </param>
70 /// <returns>
71 /// Task representing term construction.
72 /// </returns>
73 public async ValueTask BuildAsync(
74 LotSizingInstance instance,
76 LinearExpressionBuilder expressionBuilder,
78 CancellationToken cancellationToken = default)
79 {
80 ArgumentNullException.ThrowIfNull(
81 instance);
82
83 ArgumentNullException.ThrowIfNull(
84 context);
85
86 ArgumentNullException.ThrowIfNull(
87 expressionBuilder);
88
89 ArgumentNullException.ThrowIfNull(
90 options);
91
92 if (string.IsNullOrWhiteSpace(
94 {
95 throw new InvalidOperationException(
96 "A standard objective-term builder must expose " +
97 "a non-empty term-family identifier.");
98 }
99
100 options.EnsureValid();
101
102 cancellationToken.ThrowIfCancellationRequested();
103
104 await BuildTermsAsync(
105 instance,
106 context,
107 expressionBuilder,
108 options,
109 cancellationToken);
110 }
111
112 /// <summary>
113 /// Builds the concrete objective-term family.
114 /// </summary>
115 /// <param name="instance">
116 /// Source lot-sizing instance.
117 /// </param>
118 /// <param name="context">
119 /// Mathematical-model build context.
120 /// </param>
121 /// <param name="expressionBuilder">
122 /// Shared objective-expression builder.
123 /// </param>
124 /// <param name="options">
125 /// Standard formulation options.
126 /// </param>
127 /// <param name="cancellationToken">
128 /// Token used to cancel term construction.
129 /// </param>
130 /// <returns>
131 /// Task representing term construction.
132 /// </returns>
133 protected abstract ValueTask BuildTermsAsync(
134 LotSizingInstance instance,
136 LinearExpressionBuilder expressionBuilder,
138 CancellationToken cancellationToken);
139
140 /// <summary>
141 /// Adds one variable cost term to the shared objective.
142 /// </summary>
143 /// <param name="context">
144 /// Mathematical-model build context.
145 /// </param>
146 /// <param name="expressionBuilder">
147 /// Shared objective-expression builder.
148 /// </param>
149 /// <param name="domainKey">
150 /// Canonical variable domain key.
151 /// </param>
152 /// <param name="coefficient">
153 /// Linear cost coefficient.
154 /// </param>
155 protected static void AddCostTerm(
157 LinearExpressionBuilder expressionBuilder,
158 string domainKey,
159 double coefficient)
160 {
161 ArgumentNullException.ThrowIfNull(
162 context);
163
164 ArgumentNullException.ThrowIfNull(
165 expressionBuilder);
166
167 if (string.IsNullOrWhiteSpace(
168 domainKey))
169 {
170 throw new ArgumentException(
171 "A variable domain key is required.",
172 nameof(domainKey));
173 }
174
175 if (!double.IsFinite(
176 coefficient))
177 {
178 throw new ArgumentOutOfRangeException(
179 nameof(coefficient),
180 coefficient,
181 "An objective coefficient must be finite.");
182 }
183
184 MathematicalVariable variable =
185 context.GetVariable(
186 domainKey);
187
188 expressionBuilder.Add(
189 variable,
190 coefficient);
191 }
192}
Provides a fluent builder for linear expressions.
LinearExpressionBuilder Add(MathematicalVariable variable, double coefficient=1.0)
Adds a variable term to the expression.
Provides the shared state required while constructing a solver-independent mathematical model.
MathematicalVariable GetVariable(string domainKey)
Gets a required mathematical variable from its business-domain key.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
Provides common validation and helper methods for one objective-term family of the standard lot-sizin...
ValueTask BuildTermsAsync(LotSizingInstance instance, MathematicalModelBuildContext context, LinearExpressionBuilder expressionBuilder, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds the concrete objective-term family.
async ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, LinearExpressionBuilder expressionBuilder, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Validates common arguments and builds the concrete objective-term family.
virtual bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this objective-term family is enabled.
static void AddCostTerm(MathematicalModelBuildContext context, LinearExpressionBuilder expressionBuilder, string domainKey, double coefficient)
Adds one variable cost term to the shared objective.
Represents one decision variable in a mathematical optimization model.
Defines one cost-term family of the standard lot-sizing objective function.