Anexia.MathematicalProgram
2.10.0
dotnet add package Anexia.MathematicalProgram --version 2.10.0
NuGet\Install-Package Anexia.MathematicalProgram -Version 2.10.0
<PackageReference Include="Anexia.MathematicalProgram" Version="2.10.0" />
<PackageVersion Include="Anexia.MathematicalProgram" Version="2.10.0" />
<PackageReference Include="Anexia.MathematicalProgram" />
paket add Anexia.MathematicalProgram --version 2.10.0
#r "nuget: Anexia.MathematicalProgram, 2.10.0"
#:package Anexia.MathematicalProgram@2.10.0
#addin nuget:?package=Anexia.MathematicalProgram&version=2.10.0
#tool nuget:?package=Anexia.MathematicalProgram&version=2.10.0
dotnet-mathematical-program
This library allows you to build and solve linear programs and integer linear programs in a very handy way.
For linear programs, either SCIP or Google's GLOP solver can be used.
For integer linear programs, SCIP, Gurobi and the Coin-OR CBC branch and cut
solver can be chosen. When the desired solver is not available, i.e., no licence for Gurobi could be found, SCIP is used
as fallback. CBC Solver is marked deprecated, SCIP should be used instead.
Installation
- Install the latest version of
Anexia.MathematicalProgrampackage via nuget
Description
This library works for any linear program (LP), integer linear program (ILP) or constraint program (CP).
Anexia.MathematicalProgram.Result
After solving the LP/ILP you get a SolverResult according to the Google.OrTools.LinearSolver.Solver.ResultStatus.
The SolverResult contains the following information:
- SolutionValues: You can read out the actual values of the variables to transform the result correctly.
- ObjectiveValue: Actual objective value. This value can be either the optimum, a deviation of the optimum
if the LP/ILP was not entirely solved, or
nullif the LP/ILP is infeasible. - IsFeasible: Information whether the LP/ILP is generally feasible.
- IsOptimal: Information whether the LP/ILP was solved to optimality.
- OptimalityGap: The deviation to the optimum calculated by
Math.Abs(bestObjectiveBound - objectiveValue) / objectiveValue). If the objective value and the best bound are zero, the gap is also set to zero. If the objective value is zero but the best bound is not, the gap is defined to be +/- infinity.
Examples for using this library
Example (Build and solve ILP)
- Feasible model: min 2x + y, s.t. x >= y, integer variables x in [1,3], y binary
- Result: x = 1, objective value = 2
var model = new OptimizationModel<IIntegerVariable<IRealScalar>, RealScalar, IRealScalar>();
var x = model.NewVariable<IntegerVariable<IRealScalar>>(new IntegralInterval(1, 3), "x");
var y = model.NewVariable<IntegerVariable<IRealScalar>>(new IntegralInterval(0, 1), "y");
var constraint = model.ConstraintBuilder()
.AddWeightedSum([x, y], [1, -1])
.Build(new RealInterval(0, double.PositiveInfinity));
model.AddConstraint(constraint);
var objFunction = model.TermsBuilder()
.AddTerm(2, x)
.AddTerm(2, y).Build()
.ToObjectiveFunction(false);
var optimizationModel = model.SetObjective(objFunction);
var result = SolverFactory.SolverFor(IlpSolverType.Scip).Solve(optimizationModel,
new SolverParameter(new EnableSolverOutput(true)),
out _);
Further detailed examples can be found in the examples folder.
Solver parameters (SolverParameter)
You can control solver behavior using the SolverParameter record in Anexia.MathematicalProgram.SolverConfiguration. Common fields:
- EnableSolverOutput: toggles solver console logs.
- TimeLimitInMilliseconds: overall time limit.
- NumberOfThreads: caps thread usage when supported by the solver.
- RelativeGap: early stopping gap (when supported by the solver).
- AdditionalSolverSpecificParameters: extra key/value pairs passed straight to the underlying solver.
- ExportModelFilePath: path to export the model (MPS or solver-specific format depending on backend).
Examples:
Use with native Gurobi API (GurobiNativeSolver):
var native = new GurobiNativeSolver();
var result = native.Solve(optimizationModel,
new SolverParameter(
new EnableSolverOutput(true),
NumberOfThreads: new NumberOfThreads(8),
RelativeGap: RelativeGap.EMinus7,
AdditionalSolverSpecificParameters: new[]
{
("MIPFocus", "1"),
("Heuristics", "0.05")
},
ExportModelFilePath: "model.mps"
)
);
Notes:
- For Gurobi parameters, see https://docs.gurobi.com/projects/optimizer/en/current/reference/parameters.html#secparameterreference
- The AdditionalSolverSpecificParameters are forwarded as-is.
- NumberOfThreads, TimeLimitInMilliseconds, and RelativeGap is mapped to the solver’s native time limit.
Warm start (MIP start) and variable hints
A feasible (or partial) start solution can be attached to a completed model with WithHint. The values are typed
like the variable intervals (IRealScalar for ILP models), so integer, binary and real scalars are accepted.
| Solver | Handling of the warm start |
|---|---|
Gurobi (native, GurobiNativeSolver) |
MIP start via the Start attribute |
| Gurobi via OR-Tools | OR-Tools solution hint, forwarded to Gurobi as MIP start |
| SCIP | OR-Tools solution hint, added as (partial) solution |
| CP-SAT | Solution hint |
| HiGHS | Ignored with a warning, see note below |
| CBC | Ignored |
var startSolution = new StartSolution<IIntegerVariable<IRealScalar>, IRealScalar>()
.Add(x, new IntegerScalar(1))
.Add(y, BinaryScalar.Zero);
var result = SolverFactory.SolverFor(IlpSolverType.GurobiNativeIntegerProgramming)
.Solve(optimizationModel.WithHint(startSolution), new SolverParameter());
// Re-solve with the previous solution as start solution:
var startSolutionFromResult = ((SolutionValues<IIntegerVariable<IRealScalar>, RealScalar, IRealScalar>)result.SolutionValues)
.ToStartSolution();
Note on HiGHS: OR-Tools (up to and including 9.14) sizes the hint arrays for HiGHS incorrectly and writes out of bounds, which crashes the process. Warm starts are therefore not forwarded to HiGHS, also when HiGHS is used as fallback.
Solver specific per-variable attributes (Gurobi's VarHintVal, VarHintPri and BranchPriority) can be attached with
WithVariableAttributes. They are applied by the native Gurobi solver only; all other solvers log and ignore them.
var attributes = new VariableAttributes<IIntegerVariable<IRealScalar>>()
.Add(x, VariableAttributeType.HintValue, 1)
.Add(x, VariableAttributeType.HintPriority, 5)
.Add(y, VariableAttributeType.BranchPriority, 10);
var result = new GurobiNativeSolver().Solve(optimizationModel.WithVariableAttributes(attributes), new SolverParameter());
Both WithHint and WithVariableAttributes throw a VariableNotInModelException when a referenced variable is not
part of the model.
Contributing
Contributions are welcomed! Read the Contributing Guide for more information.
Licensing
This project is licensed under MIT License. See LICENSE for more information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Equ (>= 2.3.0)
- Google.OrTools (>= 9.14.6206)
- Gurobi.Optimizer (>= 13.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.6)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Anexia.MathematicalProgram:
| Package | Downloads |
|---|---|
|
Anexia.MathematicalProgram.MPaaS.Client
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.10.0 | 45 | 10/28/2026 |
| 2.9.0 | 174 | 8/18/2026 |
| 2.9.0-rc2 | 146 | 7/14/2026 |
| 2.9.0-rc1 | 147 | 7/14/2026 |
| 2.8.0 | 158 | 7/10/2026 |
| 2.7.0 | 10,710 | 11/17/2025 |
| 2.6.4 | 285 | 11/7/2025 |
| 2.6.3 | 281 | 11/6/2025 |
| 2.6.2 | 278 | 11/6/2025 |
| 2.6.1 | 278 | 11/6/2025 |
| 2.6.0 | 268 | 11/5/2025 |
| 2.5.0 | 262 | 11/5/2025 |
| 2.4.0 | 1,929 | 10/2/2025 |
| 2.3.0 | 305 | 6/24/2025 |
| 2.2.0 | 5,626 | 3/26/2025 |
| 2.1.0 | 932 | 3/6/2025 |
| 2.0.1 | 307 | 3/3/2025 |
| 2.0.0 | 223 | 2/26/2025 |
| 1.0.3 | 502 | 1/22/2025 |
| 1.0.2 | 237 | 12/5/2024 |