1using System.Diagnostics;
21 public async ValueTask<LinearModelSolveResult>
SolveAsync(
25 CancellationToken cancellationToken =
default)
27 ArgumentNullException.ThrowIfNull(model);
28 ArgumentNullException.ThrowIfNull(selection);
29 ArgumentNullException.ThrowIfNull(options);
34 throw new ArgumentException(
35 "The supplied selection does not target COIN-OR CBC.",
42 if (
string.IsNullOrWhiteSpace(executable) ||
43 !File.Exists(executable))
48 "The selected CBC executable is no longer available.");
80 await _runner.RunAsync(
96 process.CombinedOutput;
98 string solutionHeader =
99 ReadFirstLine(solutionPath);
101 bool solutionExists =
102 File.Exists(solutionPath) &&
106 IReadOnlyDictionary<int, double> values =
125 !
string.IsNullOrWhiteSpace(solutionHeader)
129 $
"cbc exit code: {process.ExitCode}.",
130 !
string.IsNullOrWhiteSpace(solutionHeader)
136 catch (OperationCanceledException)
148 [
"CBC execution was cancelled by the caller."],
153 catch (Exception exception)
164 exception.GetType().Name,
190 string solutionHeader,
194 if (solutionHeader.StartsWith(
196 StringComparison.OrdinalIgnoreCase))
201 if (solutionHeader.Contains(
202 "infeasible or unbounded",
203 StringComparison.OrdinalIgnoreCase))
208 if (solutionHeader.Contains(
210 StringComparison.OrdinalIgnoreCase))
215 if (solutionHeader.Contains(
217 StringComparison.OrdinalIgnoreCase))
225 "optimal solution found",
226 StringComparison.OrdinalIgnoreCase))
235 "infeasible or unbounded",
236 StringComparison.OrdinalIgnoreCase))
243 StringComparison.OrdinalIgnoreCase))
250 StringComparison.OrdinalIgnoreCase))
256 "optimal solution found",
257 StringComparison.OrdinalIgnoreCase))
267 private static bool ContainsNoSolution(
270 return header.Contains(
271 "no integer solution",
272 StringComparison.OrdinalIgnoreCase) ||
275 StringComparison.OrdinalIgnoreCase) ||
278 StringComparison.OrdinalIgnoreCase);
281 private static string ReadFirstLine(
284 if (!File.Exists(path))
290 new StreamReader(path);
292 return reader.ReadLine() ??
296 private static LinearModelSolveResult Failure(
301 return new LinearModelSolveResult(
308 "Executable unavailable",
Runs a solver command-line process with cancellation and captured output.
Configures one solver-backed execution of a portable linear model.
bool KeepTemporaryFiles
Gets or sets whether temporary model/solution/log artifacts are retained.
Result of executing a solver-independent linear or mixed-integer model.
Parses solver text solutions using portable variable names v_<id>.
static IReadOnlyDictionary< int, double > ParseFile(string path)
Parses a text solution file.
Writes a conservative LP representation understood by all four supported execution backends.
void Write(LinearModel model, string path)
Writes the supplied model to an LP file.
Executes portable LP/MILP models through the stand-alone COIN-OR CBC executable.
static LinearModelSolveStatus MapStatus(string output, string solutionHeader, bool solutionExists, int exitCode)
Normalizes the status reported by CBC.
SolverKind SolverKind
Gets the concrete solver implemented by this executor.
async ValueTask< LinearModelSolveResult > SolveAsync(LinearModel model, SolverSelectionResult selection, LinearModelSolveOptions options, CancellationToken cancellationToken=default)
Executes one portable model using an already selected solver.
static LinearModelSolveResult BuildSolutionResult(LinearModel model, SolverSelectionResult selection, LinearModelSolveOptions options, LinearModelSolveStatus proposedStatus, IReadOnlyDictionary< int, double > returnedValues, bool hasCandidateSolution, TimeSpan duration, string nativeStatus, IEnumerable< string > diagnostics, string artifactDirectory)
static string CreateArtifactDirectory(SolverKind solverKind, LinearModelSolveOptions options)
static string LastMeaningfulLine(string text)
static void DeleteArtifactsUnlessRetained(string directory, LinearModelSolveOptions options)
static void ExportModelIfRequested(string generatedModelPath, LinearModelSolveOptions options)
Immutable solver-independent linear or mixed-integer linear model.
string Name
Gets the model name.
string NativeLibraryPath
Gets the native library or executable path, when applicable.
Serializable-style immutable snapshot of the solver selected for one solver-backed ULS execution.
Describes the outcome of optimization-solver selection.
SolverKind SelectedSolver
Gets the selected solver kind, or Unknown when none was selected.
SolverAvailabilityInfo? Availability
Gets machine-availability information for the selected solver.
Executes the portable LinearModel with one concrete solver.
LinearModelSolveStatus
Describes the termination state of a solver-backed portable linear model.
@ Unknown
No reliable status was obtained.
record ExternalSolverProcessResult(int ExitCode, string StandardOutput, string StandardError)