ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
LinearModelSolver.cs
Go to the documentation of this file.
2
4
5/// <summary>
6/// High-level solver-independent execution service for portable linear models.
7/// </summary>
8/// <remarks>
9/// Automatic selection follows the repository-wide priority:
10/// CPLEX, Gurobi, Xpress, then COIN-OR CBC.
11/// </remarks>
12public sealed class LinearModelSolver
13{
14 private readonly SolverAdapterRegistry _adapterRegistry;
15 private readonly LinearModelExecutorRegistry _executorRegistry;
16 private readonly SolverSelectionService _selectionService;
17
18 /// <summary>
19 /// Initializes the service with the built-in discovery and execution
20 /// backends.
21 /// </summary>
23 : this(
27 {
28 }
29
30 /// <summary>
31 /// Initializes the service with explicit registries. This constructor is
32 /// useful for deterministic tests and custom integrations.
33 /// </summary>
35 SolverAdapterRegistry adapterRegistry,
36 LinearModelExecutorRegistry executorRegistry,
37 SolverSelectionService selectionService)
38 {
39 _adapterRegistry =
40 adapterRegistry ??
41 throw new ArgumentNullException(nameof(adapterRegistry));
42
43 _executorRegistry =
44 executorRegistry ??
45 throw new ArgumentNullException(nameof(executorRegistry));
46
47 _selectionService =
48 selectionService ??
49 throw new ArgumentNullException(nameof(selectionService));
50 }
51
52 /// <summary>
53 /// Selects a usable solver and executes the portable model.
54 /// </summary>
55 public async ValueTask<LinearModelSolveResult> SolveAsync(
56 LinearModel model,
57 LinearModelSolveOptions? options = null,
58 CancellationToken cancellationToken = default)
59 {
60 ArgumentNullException.ThrowIfNull(model);
61
62 options ??=
64
65 options.EnsureValid();
66 cancellationToken.ThrowIfCancellationRequested();
67
68 var selectionOptions =
70 {
71 RequireExactSolverKind =
72 options.Solver != SolverKind.Automatic &&
73 !options.AllowFallbackWhenExplicit
74 };
75
76 selectionOptions.RequiredCapabilities.Add(
77 model.IsMixedInteger
78 ? SolverCapability.MixedIntegerLinearProgramming
79 : SolverCapability.LinearProgramming);
80
81 SolverSelectionResult selection =
82 await _selectionService.SelectAsync(
83 options.Solver,
84 _adapterRegistry,
85 selectionOptions,
86 cancellationToken);
87
88 if (!selection.IsSelected)
89 {
90 return new LinearModelSolveResult(
91 model.Name,
92 LinearModelSolveStatus.SolverUnavailable,
93 solver: null,
94 variableValues: null,
95 validation: null,
96 solveDuration: TimeSpan.Zero,
97 nativeStatus: string.Empty,
98 diagnostics: selection.Diagnostics);
99 }
100
102 _executorRegistry.GetRequired(
103 selection.SelectedSolver);
104
105 return await executor.SolveAsync(
106 model,
107 selection,
108 options,
109 cancellationToken);
110 }
111}
Creates the built-in concrete solver-adapter registry.
Creates the built-in CPLEX, Gurobi, Xpress and CBC model executors.
Stores concrete portable-model execution backends by solver kind.
Configures one solver-backed execution of a portable linear model.
Result of executing a solver-independent linear or mixed-integer model.
LinearModelSolver()
Initializes the service with the built-in discovery and execution backends.
LinearModelSolver(SolverAdapterRegistry adapterRegistry, LinearModelExecutorRegistry executorRegistry, SolverSelectionService selectionService)
Initializes the service with explicit registries. This constructor is useful for deterministic tests ...
async ValueTask< LinearModelSolveResult > SolveAsync(LinearModel model, LinearModelSolveOptions? options=null, CancellationToken cancellationToken=default)
Selects a usable solver and executes the portable model.
Immutable solver-independent linear or mixed-integer linear model.
Definition LinearModel.cs:7
bool IsMixedInteger
Gets whether the model contains integer or binary variables.
Stores optimization-solver adapters and provides deterministic lookup by adapter identifier and solve...
Configures automatic or explicit optimization-solver selection.
List< SolverCapability > RequiredCapabilities
Gets capabilities that the selected adapter must support.
Describes the outcome of optimization-solver selection.
IReadOnlyList< string > Diagnostics
Gets deterministic selection diagnostics.
bool IsSelected
Gets whether a usable adapter was selected.
SolverKind SelectedSolver
Gets the selected solver kind, or Unknown when none was selected.
Selects a usable optimization solver by capability, machine availability, and deterministic priority.
Executes the portable LinearModel with one concrete solver.
ValueTask< LinearModelSolveResult > SolveAsync(LinearModel model, SolverSelectionResult selection, LinearModelSolveOptions options, CancellationToken cancellationToken=default)
Executes one portable model using an already selected solver.
LinearModelSolveStatus
Describes the termination state of a solver-backed portable linear model.
SolverCapability
Identifies an optional capability exposed by an optimization-solver adapter.
@ Automatic
Select the first usable solver according to the configured priority.
Definition SolverKind.cs:20