LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelBuildService.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
3using System.Threading;
4using System.Threading.Tasks;
5using LotSizingDataModel.Instance;
6
8
9/// <summary>
10/// Selects a compatible formulation and builds the associated
11/// solver-independent mathematical model.
12/// </summary>
13public sealed class MathematicalModelBuildService :
15{
17 _selectionService =
18 new();
19
20 /// <summary>
21 /// Selects a formulation and builds the mathematical model.
22 /// </summary>
23 /// <param name="instance">
24 /// Lot-sizing instance to formulate.
25 /// </param>
26 /// <param name="registry">
27 /// Registry containing available formulations.
28 /// </param>
29 /// <param name="options">
30 /// Mathematical-model build options.
31 /// </param>
32 /// <param name="cancellationToken">
33 /// Token used to cancel formulation selection and model
34 /// construction.
35 /// </param>
36 /// <returns>
37 /// Complete mathematical-model build result.
38 /// </returns>
39 public async ValueTask<MathematicalModelBuildResult> BuildAsync(
40 LotSizingInstance instance,
43 CancellationToken cancellationToken = default)
44 {
45 ArgumentNullException.ThrowIfNull(instance);
46 ArgumentNullException.ThrowIfNull(registry);
47 ArgumentNullException.ThrowIfNull(options);
48
49 MathematicalModelBuildOptions normalizedOptions =
50 options.Clone();
51
52 normalizedOptions.EnsureValid();
53
54 var stopwatch =
55 Stopwatch.StartNew();
56
58 null;
59
60 try
61 {
62 cancellationToken.ThrowIfCancellationRequested();
63
64 selection =
65 _selectionService.Select(
66 instance,
67 registry,
68 normalizedOptions.RequestedFormulationId,
69 normalizedOptions.AllowFallback);
70
71 if (!selection.IsSuccessful ||
72 selection.Formulation is null)
73 {
74 stopwatch.Stop();
75
77 selection,
78 "No compatible mathematical formulation " +
79 "could be selected.",
80 stopwatch.Elapsed);
81 }
82
83 cancellationToken.ThrowIfCancellationRequested();
84
85 var model =
86 await selection.Formulation.BuildAsync(
87 instance,
88 cancellationToken);
89
90 cancellationToken.ThrowIfCancellationRequested();
91
92 ArgumentNullException.ThrowIfNull(model);
93
94 if (normalizedOptions.ValidateGeneratedModel)
95 {
96 model.EnsureValid();
97 }
98
99 if (normalizedOptions.CloneGeneratedModel)
100 {
101 model =
102 model.Clone();
103 }
104
105 stopwatch.Stop();
106
109 selection,
110 model,
111 stopwatch.Elapsed);
112
113 result.AddDiagnostic(
114 $"Mathematical model '{model.Name}' was built " +
115 $"with {model.VariableCount} variables and " +
116 $"{model.EnabledConstraintCount} enabled " +
117 "constraints.");
118
119 return result;
120 }
121 catch (OperationCanceledException)
122 {
123 stopwatch.Stop();
124
127 selection,
128 "Mathematical model construction was " +
129 "cancelled.",
130 stopwatch.Elapsed);
131
132 result.AddDiagnostic(
133 "The cancellation token was signalled during " +
134 "formulation selection or model construction.");
135
136 return result;
137 }
138 catch (Exception exception)
139 {
140 stopwatch.Stop();
141
144 selection,
145 exception.Message,
146 stopwatch.Elapsed);
147
148 result.AddDiagnostic(
149 exception.ToString());
150
151 return result;
152 }
153 }
154}
Defines the options used to select a formulation and build a solver-independent mathematical model.
bool CloneGeneratedModel
Gets or sets a value indicating whether an independent clone of the generated model must be returned.
bool ValidateGeneratedModel
Gets or sets a value indicating whether the generated mathematical model must be validated before it ...
string RequestedFormulationId
Gets or sets the requested formulation identifier.
MathematicalModelBuildOptions Clone()
Creates an independent copy of the build options.
bool AllowFallback
Gets or sets a value indicating whether another compatible formulation may be selected when the reque...
Describes the result of building a solver-independent mathematical model from a lot-sizing instance.
void AddDiagnostic(string? diagnostic)
Adds a non-empty diagnostic message.
static MathematicalModelBuildResult Success(MathematicalModelFormulationSelectionResult selection, MathematicalModel model, TimeSpan buildDuration)
Creates a successful mathematical-model build result.
static MathematicalModelBuildResult Failure(MathematicalModelFormulationSelectionResult? selection, string failureMessage, TimeSpan buildDuration)
Creates a failed mathematical-model build result.
Selects a compatible formulation and builds the associated solver-independent mathematical model.
async ValueTask< MathematicalModelBuildResult > BuildAsync(LotSizingInstance instance, MathematicalModelFormulationRegistry registry, MathematicalModelBuildOptions options, CancellationToken cancellationToken=default)
Selects a formulation and builds the mathematical model.
Describes the result of selecting a mathematical formulation for a lot-sizing instance.
IMathematicalModelFormulation? Formulation
Gets or sets the selected formulation for the current process.
bool IsSuccessful
Gets or sets a value indicating whether a compatible formulation was selected.
Defines a service that selects a formulation and builds a solver-independent mathematical model.
ValueTask< MathematicalModel > BuildAsync(LotSizingInstance instance, CancellationToken cancellationToken=default)
Builds the solver-independent mathematical model.