1using System.Text.RegularExpressions;
29 "ULSAlgorithms.Solver.Gurobi";
33 "ULSAlgorithms Gurobi Adapter";
40 public override async ValueTask<SolverAvailabilityInfo>
42 CancellationToken cancellationToken =
default)
44 cancellationToken.ThrowIfCancellationRequested();
46 string executablePath =
47 ResolveExecutablePath();
49 if (
string.IsNullOrWhiteSpace(executablePath))
54 solverName:
"Gurobi Optimizer",
57 "gurobi_cl was not found. Put it on PATH, define " +
58 "GUROBI_HOME, or set ULSALGORITHMS_GUROBI_EXECUTABLE."
65 await _probe.RunAsync(
70 string versionOutput =
78 FirstMeaningfulLine(versionOutput) ??
79 $
"gurobi_cl exited with code {versionResult.ExitCode}.");
83 ParseVersion(versionOutput);
86 await _probe.RunAsync(
91 string licenseOutput =
95 LooksLikeLicenseFailure(licenseOutput))
100 solverName:
"Gurobi Optimizer",
101 solverVersion: version,
103 Path.GetDirectoryName(executablePath) ??
105 nativeLibraryPath: executablePath,
107 FirstMeaningfulLine(licenseOutput) ??
108 "Gurobi license check failed.",
111 FirstMeaningfulLine(licenseOutput) ??
112 "gurobi_cl --license did not report a usable license."
119 solverName:
"Gurobi Optimizer",
120 solverVersion: version,
122 Path.GetDirectoryName(executablePath) ??
124 nativeLibraryPath: executablePath,
126 FirstMeaningfulLine(licenseOutput) ??
127 "Gurobi license information resolved.",
130 $
"Gurobi command-line executable detected at " +
131 $
"'{executablePath}'.",
132 FirstMeaningfulLine(versionOutput) ??
133 "Gurobi version command completed successfully."
136 catch (OperationCanceledException)
140 catch (Exception exception)
150 string executablePath,
157 solverName:
"Gurobi Optimizer",
159 Path.GetDirectoryName(executablePath) ??
161 nativeLibraryPath: executablePath,
162 diagnostics: [diagnostic]);
165 private static string ResolveExecutablePath()
169 "ULSALGORITHMS_GUROBI_EXECUTABLE",
170 "LOTSIZING_GUROBI_EXECUTABLE"
174 Path.Combine(
"bin",
"gurobi_cl.exe"),
175 Path.Combine(
"bin",
"gurobi_cl")
177 [
"gurobi_cl.exe",
"gurobi_cl"],
178 EnumerateCommonWindowsCandidates());
181 private static IEnumerable<string>
182 EnumerateCommonWindowsCandidates()
184 if (!OperatingSystem.IsWindows())
191 Environment.GetFolderPath(
192 Environment.SpecialFolder.System)) ??
195 IEnumerable<string> roots;
201 .EnumerateDirectories(
204 SearchOption.TopDirectoryOnly)
207 Path.GetFileName(path),
208 StringComparer.OrdinalIgnoreCase)
216 foreach (
string root
in roots)
233 private static bool LooksLikeLicenseFailure(
240 "unable to open license",
241 "unable to retrieve license",
247 return fragments.Any(
251 StringComparison.OrdinalIgnoreCase));
254 private static string ParseVersion(
258 VersionRegex().Match(text ??
string.Empty);
261 ? match.Groups[1].Value
265 private static string? FirstMeaningfulLine(
271 StringSplitOptions.RemoveEmptyEntries |
272 StringSplitOptions.TrimEntries)
277 @"(?:Gurobi(?:\s+Optimizer)?\s+version\s+|Gurobi\s+)(\d+(?:\.\d+){1,3})",
278 RegexOptions.IgnoreCase |
279 RegexOptions.CultureInvariant)]
280 private static partial Regex VersionRegex();
GurobiSolverAdapter()
Initializes the Gurobi adapter.
override string AdapterName
Gets the adapter display name.
override SolverKind SolverKind
Gets the concrete solver targeted by this adapter.
override async ValueTask< SolverAvailabilityInfo > CheckAvailabilityAsync(CancellationToken cancellationToken=default)
Performs the adapter-specific machine discovery / load / license check.
override string AdapterId
Gets the stable adapter identifier.
static SolverAvailabilityStatus ClassifyFailure(Exception exception)
Maps an exception to a stable availability state.
OptimizationSolverAdapterBase(params SolverCapability[] capabilities)
Initializes an adapter with its supported capabilities.
Locates optional native-solver command-line executables without requiring their SDKs at compile time.
static string Resolve(IEnumerable< string > explicitExecutableEnvironmentVariables, IEnumerable< string > homeEnvironmentVariables, IEnumerable< string > relativeExecutablePaths, IEnumerable< string > pathExecutableNames, IEnumerable< string >? directCandidates=null)
Resolves an executable from explicit environment variables, installation-home hints,...
Executes short solver command-line probes and captures their output.
Captured result of a short external solver process probe.
string CombinedOutput
Gets stdout and stderr as one diagnostic string.
int ExitCode
Gets the native process exit code.
Describes solver availability and the installation selected by an adapter.
SolverAvailabilityStatus
Describes whether a solver can currently be used on the current computer.
SolverCapability
Identifies an optional capability exposed by an optimization-solver adapter.
@ LinearProgramming
Continuous linear programming.
@ LpExport
LP-format export.
@ OptimalityGapReporting
Best-bound and optimality-gap reporting.
@ MixedIntegerLinearProgramming
Mixed-integer linear programming.
@ Interruption
Cooperative interruption.
@ SearchStatistics
Search-node and iteration statistics.
SolverKind
Identifies a mathematical optimization solver that can be used by solver-backed ULS algorithms.