LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
SolverParameters.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Xml.Serialization;
5
7
8/// <summary>
9/// Defines solver-independent parameters for an optimization
10/// run.
11/// </summary>
12/// <remarks>
13/// Solver adapters translate these generic parameters to their
14/// native CPLEX, Gurobi, Xpress, or COIN-OR CBC equivalents.
15///
16/// Additional solver-specific values can be supplied through
17/// <see cref="NativeParameters"/>.
18/// </remarks>
19[Serializable]
20[XmlType(TypeName = "solverParameters")]
21public sealed class SolverParameters
22{
23 private readonly List<SolverNativeParameter>
24 _nativeParameters =
25 new();
26
27 /// <summary>
28 /// Initializes solver parameters with recommended default
29 /// values.
30 /// </summary>
32 {
34 null;
35
37 null;
38
40 null;
41
43 null;
44
46 null;
47
48 NodeLimit =
49 null;
50
52 null;
53
55 null;
56
58 null;
59
60 LogLevel =
61 SolverLogLevel.Information;
62
64 null;
65
67 null;
68
70 null;
71
73 false;
74
76 false;
77
79 string.Empty;
80
82 false;
83
85 string.Empty;
86
89 }
90
91 /// <summary>
92 /// Gets or sets the wall-clock time limit, in seconds.
93 /// </summary>
94 /// <remarks>
95 /// A <see langword="null"/> value leaves the solver default
96 /// unchanged.
97 /// </remarks>
98 [XmlElement("timeLimitSeconds")]
99 public double? TimeLimitSeconds
100 {
101 get;
102 set;
103 }
104
105 /// <summary>
106 /// Gets or sets the maximum number of solver threads.
107 /// </summary>
108 [XmlElement("threadCount")]
109 public int? ThreadCount
110 {
111 get;
112 set;
113 }
114
115 /// <summary>
116 /// Gets or sets the solver random seed.
117 /// </summary>
118 [XmlElement("randomSeed")]
119 public int? RandomSeed
120 {
121 get;
122 set;
123 }
124
125 /// <summary>
126 /// Gets or sets the relative mixed-integer optimality gap.
127 /// </summary>
128 [XmlElement("relativeMipGap")]
129 public double? RelativeMipGap
130 {
131 get;
132 set;
133 }
134
135 /// <summary>
136 /// Gets or sets the absolute mixed-integer optimality gap.
137 /// </summary>
138 [XmlElement("absoluteMipGap")]
139 public double? AbsoluteMipGap
140 {
141 get;
142 set;
143 }
144
145 /// <summary>
146 /// Gets or sets the maximum number of explored search-tree
147 /// nodes.
148 /// </summary>
149 [XmlElement("nodeLimit")]
150 public long? NodeLimit
151 {
152 get;
153 set;
154 }
155
156 /// <summary>
157 /// Gets or sets the maximum number of solver iterations.
158 /// </summary>
159 [XmlElement("iterationLimit")]
160 public long? IterationLimit
161 {
162 get;
163 set;
164 }
165
166 /// <summary>
167 /// Gets or sets the maximum number of feasible solutions to
168 /// search for.
169 /// </summary>
170 [XmlElement("solutionLimit")]
171 public int? SolutionLimit
172 {
173 get;
174 set;
175 }
176
177 /// <summary>
178 /// Gets or sets the approximate memory limit, in megabytes.
179 /// </summary>
180 [XmlElement("memoryLimitMegabytes")]
181 public double? MemoryLimitMegabytes
182 {
183 get;
184 set;
185 }
186
187 /// <summary>
188 /// Gets or sets the normalized solver log level.
189 /// </summary>
190 [XmlAttribute("logLevel")]
192 {
193 get;
194 set;
195 }
196
197 /// <summary>
198 /// Gets or sets whether solver presolve is enabled.
199 /// </summary>
200 /// <remarks>
201 /// A <see langword="null"/> value leaves the solver default
202 /// unchanged.
203 /// </remarks>
204 [XmlElement("enablePresolve")]
205 public bool? EnablePresolve
206 {
207 get;
208 set;
209 }
210
211 /// <summary>
212 /// Gets or sets whether automatic cut generation is
213 /// enabled.
214 /// </summary>
215 [XmlElement("enableCuts")]
216 public bool? EnableCuts
217 {
218 get;
219 set;
220 }
221
222 /// <summary>
223 /// Gets or sets whether primal heuristics are enabled.
224 /// </summary>
225 [XmlElement("enableHeuristics")]
226 public bool? EnableHeuristics
227 {
228 get;
229 set;
230 }
231
232 /// <summary>
233 /// Gets or sets whether deterministic execution is
234 /// requested when supported by the selected solver.
235 /// </summary>
236 [XmlAttribute("deterministicMode")]
238 {
239 get;
240 set;
241 }
242
243 /// <summary>
244 /// Gets or sets whether solver output is written to a log
245 /// file.
246 /// </summary>
247 [XmlAttribute("writeLogFile")]
248 public bool WriteLogFile
249 {
250 get;
251 set;
252 }
253
254 /// <summary>
255 /// Gets or sets the solver log file path.
256 /// </summary>
257 [XmlElement("logFilePath")]
258 public string LogFilePath
259 {
260 get;
261 set;
262 }
263
264 /// <summary>
265 /// Gets or sets whether the generated mathematical model is
266 /// exported before solving.
267 /// </summary>
268 [XmlAttribute("exportModel")]
269 public bool ExportModel
270 {
271 get;
272 set;
273 }
274
275 /// <summary>
276 /// Gets or sets the destination path used to export the
277 /// generated model.
278 /// </summary>
279 [XmlElement("exportModelPath")]
280 public string ExportModelPath
281 {
282 get;
283 set;
284 }
285
286 /// <summary>
287 /// Gets or sets the progress-history recording options.
288 /// </summary>
289 [XmlElement("progressRecording")]
291 {
292 get;
293 set;
294 }
295
296 /// <summary>
297 /// Gets solver-specific parameters that are passed directly
298 /// to the selected adapter.
299 /// </summary>
300 [XmlArray("nativeParameters")]
301 [XmlArrayItem("parameter")]
302 public List<SolverNativeParameter> NativeParameters =>
303 _nativeParameters;
304
305 /// <summary>
306 /// Validates the solver parameters.
307 /// </summary>
308 /// <exception cref="InvalidOperationException">
309 /// Thrown when one or more parameter values are invalid.
310 /// </exception>
311 public void EnsureValid()
312 {
313 ValidateFiniteStrictlyPositiveNullable(
315 nameof(TimeLimitSeconds));
316
317 ValidateStrictlyPositiveNullable(
319 nameof(ThreadCount));
320
321 ValidateFiniteNonNegativeNullable(
323 nameof(RelativeMipGap));
324
325 ValidateFiniteNonNegativeNullable(
327 nameof(AbsoluteMipGap));
328
329 ValidateNonNegativeNullable(
330 NodeLimit,
331 nameof(NodeLimit));
332
333 ValidateNonNegativeNullable(
335 nameof(IterationLimit));
336
337 ValidateStrictlyPositiveNullable(
339 nameof(SolutionLimit));
340
341 ValidateFiniteStrictlyPositiveNullable(
343 nameof(MemoryLimitMegabytes));
344
345 if (WriteLogFile &&
346 string.IsNullOrWhiteSpace(
348 {
349 throw new InvalidOperationException(
350 "A log file path is required when solver log " +
351 "file writing is enabled.");
352 }
353
354 if (ExportModel &&
355 string.IsNullOrWhiteSpace(
357 {
358 throw new InvalidOperationException(
359 "A model export path is required when model " +
360 "export is enabled.");
361 }
362
363 ArgumentNullException.ThrowIfNull(
365
366 ProgressRecording.EnsureValid();
367
368 foreach (
369 SolverNativeParameter parameter
370 in _nativeParameters)
371 {
372 if (parameter is null)
373 {
374 throw new InvalidOperationException(
375 "The native-parameter collection cannot " +
376 "contain a null entry.");
377 }
378
379 parameter.EnsureValid();
380 }
381 }
382
383 private static void
384 ValidateFiniteStrictlyPositiveNullable(
385 double? value,
386 string propertyName)
387 {
388 if (!value.HasValue)
389 {
390 return;
391 }
392
393 if (value.Value <= 0.0 ||
394 double.IsNaN(
395 value.Value) ||
396 double.IsInfinity(
397 value.Value))
398 {
399 throw new InvalidOperationException(
400 $"{propertyName} must be a finite strictly " +
401 "positive number when specified.");
402 }
403 }
404
405 private static void ValidateFiniteNonNegativeNullable(
406 double? value,
407 string propertyName)
408 {
409 if (!value.HasValue)
410 {
411 return;
412 }
413
414 if (value.Value < 0.0 ||
415 double.IsNaN(
416 value.Value) ||
417 double.IsInfinity(
418 value.Value))
419 {
420 throw new InvalidOperationException(
421 $"{propertyName} must be a finite " +
422 "non-negative number when specified.");
423 }
424 }
425
426 private static void ValidateStrictlyPositiveNullable(
427 int? value,
428 string propertyName)
429 {
430 if (value.HasValue &&
431 value.Value <= 0)
432 {
433 throw new InvalidOperationException(
434 $"{propertyName} must be strictly positive " +
435 "when specified.");
436 }
437 }
438
439 private static void ValidateNonNegativeNullable(
440 long? value,
441 string propertyName)
442 {
443 if (value.HasValue &&
444 value.Value < 0)
445 {
446 throw new InvalidOperationException(
447 $"{propertyName} cannot be negative when " +
448 "specified.");
449 }
450 }
451}
Represents a solver-specific parameter that is passed to a solver adapter without being interpreted b...
void EnsureValid()
Validates the native solver parameter.
bool WriteLogFile
Gets or sets whether solver output is written to a log file.
long? IterationLimit
Gets or sets the maximum number of solver iterations.
SolverLogLevel LogLevel
Gets or sets the normalized solver log level.
List< SolverNativeParameter > NativeParameters
Gets solver-specific parameters that are passed directly to the selected adapter.
long? NodeLimit
Gets or sets the maximum number of explored search-tree nodes.
SolverParameters()
Initializes solver parameters with recommended default values.
int? RandomSeed
Gets or sets the solver random seed.
bool ExportModel
Gets or sets whether the generated mathematical model is exported before solving.
string LogFilePath
Gets or sets the solver log file path.
double? MemoryLimitMegabytes
Gets or sets the approximate memory limit, in megabytes.
int? SolutionLimit
Gets or sets the maximum number of feasible solutions to search for.
double? RelativeMipGap
Gets or sets the relative mixed-integer optimality gap.
SolverProgressRecordingOptions ProgressRecording
Gets or sets the progress-history recording options.
bool? EnablePresolve
Gets or sets whether solver presolve is enabled.
bool DeterministicMode
Gets or sets whether deterministic execution is requested when supported by the selected solver.
int? ThreadCount
Gets or sets the maximum number of solver threads.
bool? EnableHeuristics
Gets or sets whether primal heuristics are enabled.
double? TimeLimitSeconds
Gets or sets the wall-clock time limit, in seconds.
string ExportModelPath
Gets or sets the destination path used to export the generated model.
void EnsureValid()
Validates the solver parameters.
bool? EnableCuts
Gets or sets whether automatic cut generation is enabled.
double? AbsoluteMipGap
Gets or sets the absolute mixed-integer optimality gap.
Configures how solver progress snapshots are retained during an optimization run.
SolverLogLevel
Defines the amount of information written by a solver during an optimization run.