LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelSolverExecutionService.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Threading;
4using System.Threading.Tasks;
8
10
11/// <summary>
12/// Selects a compatible mathematical-model solver adapter and
13/// executes an already built solver-independent mathematical
14/// model.
15/// </summary>
18{
20 _selectionService =
21 new();
22
23 private readonly object _synchronizationRoot =
24 new();
25
26 private IMathematicalModelSolverAdapter? _activeAdapter;
27
28 /// <summary>
29 /// Selects a solver adapter and solves the mathematical
30 /// model.
31 /// </summary>
32 /// <param name="request">
33 /// Mathematical-model solve request.
34 /// </param>
35 /// <param name="requestedSolver">
36 /// Solver requested by the caller.
37 /// </param>
38 /// <param name="registry">
39 /// Registry containing loaded solver adapters.
40 /// </param>
41 /// <param name="availabilityInformation">
42 /// Availability information associated with detected
43 /// solvers.
44 /// </param>
45 /// <param name="selectionOptions">
46 /// Solver-selection options.
47 /// </param>
48 /// <param name="cancellationToken">
49 /// Token used to cancel solver selection and execution.
50 /// </param>
51 /// <returns>
52 /// Generic mathematical-model solve result.
53 /// </returns>
54 /// <exception cref="ArgumentNullException">
55 /// Thrown when one of the required arguments is
56 /// <see langword="null"/>.
57 /// </exception>
58 /// <exception cref="InvalidOperationException">
59 /// Thrown when another mathematical-model solve is already
60 /// running through this execution service.
61 /// </exception>
62 public async ValueTask<MathematicalModelSolveResult> SolveAsync(
64 SolverKind requestedSolver,
65 SolverAdapterRegistry registry,
66 IEnumerable<SolverAvailabilityInfo>
67 availabilityInformation,
68 SolverSelectionOptions selectionOptions,
69 CancellationToken cancellationToken = default)
70 {
71 ArgumentNullException.ThrowIfNull(
72 request);
73
74 ArgumentNullException.ThrowIfNull(
75 registry);
76
77 ArgumentNullException.ThrowIfNull(
78 availabilityInformation);
79
80 ArgumentNullException.ThrowIfNull(
81 selectionOptions);
82
83 request.EnsureValid();
84
85 cancellationToken.ThrowIfCancellationRequested();
86
88 await _selectionService.SelectAsync(
89 requestedSolver,
90 registry,
91 availabilityInformation,
92 selectionOptions,
93 cancellationToken);
94
95 if (!selection.IsSuccessful ||
96 selection.Adapter is null)
97 {
98 var unavailableResult =
100 {
101 RunName =
102 request.RunName,
103
104 FormulationId =
105 request.FormulationId,
106
107 SolverKind =
108 selection.SelectedSolver,
109
110 SolverName =
111 selection.AdapterName,
112
113 TerminationReason =
114 SolverTerminationReason.SolverUnavailable,
115
116 HasFeasibleSolution =
117 false,
118
119 IsOptimal =
120 false
121 };
122
123 foreach (
124 string diagnostic
125 in selection.Diagnostics)
126 {
127 unavailableResult.AddDiagnostic(
128 diagnostic);
129 }
130
131 unavailableResult.AddDiagnostic(
132 "No usable mathematical-model solver adapter " +
133 "could be selected.");
134
135 unavailableResult.EnsureValid();
136
137 return unavailableResult;
138 }
139
140 SetActiveAdapter(
141 selection.Adapter);
142
143 try
144 {
146 await selection.Adapter.SolveAsync(
147 request,
148 cancellationToken);
149
150 foreach (
151 string diagnostic
152 in selection.Diagnostics)
153 {
154 result.AddDiagnostic(
155 diagnostic);
156 }
157
158 result.EnsureValid();
159
160 return result;
161 }
162 finally
163 {
164 ClearActiveAdapter(
165 selection.Adapter);
166 }
167 }
168
169 /// <summary>
170 /// Requests interruption of the mathematical-model solve
171 /// currently executed by the selected solver adapter.
172 /// </summary>
173 /// <remarks>
174 /// Calling this method when no solver adapter is currently
175 /// running has no effect.
176 /// </remarks>
177 public void RequestStop()
178 {
180
181 lock (_synchronizationRoot)
182 {
183 adapter =
184 _activeAdapter;
185 }
186
187 if (adapter is not null)
188 {
189 ((global::LotSizingDataModel.Solver.Contracts.IMathematicalModelSolver)adapter)
190 .RequestStop();
191 }
192 }
193
194 private void SetActiveAdapter(
196 {
197 ArgumentNullException.ThrowIfNull(
198 adapter);
199
200 lock (_synchronizationRoot)
201 {
202 if (_activeAdapter is not null)
203 {
204 throw new InvalidOperationException(
205 "Another mathematical-model solve is already " +
206 "running through this execution service.");
207 }
208
209 _activeAdapter =
210 adapter;
211 }
212 }
213
214 private void ClearActiveAdapter(
216 {
217 lock (_synchronizationRoot)
218 {
219 if (ReferenceEquals(
220 _activeAdapter,
221 adapter))
222 {
223 _activeAdapter =
224 null;
225 }
226 }
227 }
228}
Stores solver adapters that were loaded successfully and provides lookup services by adapter identifi...
Configures how a solver adapter is selected when automatic solver selection is requested.
Describes a request to solve an already built, solver-independent mathematical model.
string FormulationId
Gets or sets the identifier of the formulation that generated the mathematical model.
void EnsureValid()
Validates the mathematical-model solve request.
Stores the solver result for a solver-independent mathematical model.
void AddDiagnostic(string diagnostic)
Adds a non-empty diagnostic message.
Selects a compatible mathematical-model solver adapter and executes an already built solver-independe...
void RequestStop()
Requests interruption of the mathematical-model solve currently executed by the selected solver adapt...
async ValueTask< MathematicalModelSolveResult > SolveAsync(MathematicalModelSolveRequest request, SolverKind requestedSolver, SolverAdapterRegistry registry, IEnumerable< SolverAvailabilityInfo > availabilityInformation, SolverSelectionOptions selectionOptions, CancellationToken cancellationToken=default)
Selects a solver adapter and solves the mathematical model.
Represents the result of selecting a solver adapter that can solve a solver-independent mathematical ...
SolverKind SelectedSolver
Gets or sets the solver selected by the general solver-selection service.
bool IsSuccessful
Gets a value indicating whether a usable mathematical-model solver adapter was selected.
IMathematicalModelSolverAdapter? Adapter
Gets or sets the selected live mathematical-model solver adapter.
List< string > Diagnostics
Gets the diagnostics produced during adapter selection.
Selects a solver adapter that supports direct execution of solver-independent mathematical models.
Defines a dynamically discoverable solver adapter capable of solving solver-independent mathematical ...
ValueTask< MathematicalModelSolveResult > SolveAsync(MathematicalModelSolveRequest request, CancellationToken cancellationToken=default)
Solves a solver-independent mathematical model.
Defines a service that selects a compatible solver adapter and executes an already built solver-indep...
SolverTerminationReason
Describes why a solver execution terminated.
SolverKind
Identifies a supported mathematical optimization solver.
Definition SolverKind.cs:12