Orx.MathProg.Solvers Namespace

 

Classes

Cplex Mathematical model solver using IBM CPLEX Optimizer (https://www.ibm.com/de-de/analytics/cplex-optimizer).
MathModelSolution Solution of a MathModel by a solver such as [!:Cplex2] or [!:Scip2].

It provides meta information such as solution status and objectiev function value. Further, it allows to access solution values of the decision variables.

# Case 1: Feasible The following example demonstrates a feasible solution case on a trivial model.
C#
Set i = Set("i").HasElementsUntil(3); // { 0, 1, 2 }
Set j = Set("j").HasElementsUntil(4); // { 0, 1, 2, 3 }
VarD2 y = Variable("y").HasIndices(i, j).IsBinary();
Objective maxCount = maximize | sum(over(i, j), y[i, j]);
MathModel model = MathModel.New("dummy model").WithObjective(maxCount).HasNoConstraints();

Cplex solver = new();
CplexParams pars = new();
var resSolution = await solver.Solve(model, pars);
var solution = resSolution.Unwrap(); // normally, check if it is 'IsOk' before unwrapping!

// solution status currently depends on the used solver
Assert.Equal("integer optimal solution", solution.SolutionStatus);

double objFunValue = solution.ObjectiveValue.Unwrap(); // must be okay to Unwrap since status is 'integer optimal'.
Assert.Equal(12, objFunValue);

// ySoln is a map of indices to solution values;
// allows access to solution values of decision variables through indices.
// since y is of type VarD2, it has two indices for i and j.
Dictionary<(int, int), double> ySoln = solution.VarIndicesAndValuesOf(y).Unwrap();
Assert.Equal(1, y[(0, 0)];
Assert.Equal(1, y[(2, 3)];
Assert.False(ySoln.ContainsKey((4, 5)); // y(4, 5) is not a variable of the model, check elements of i and j.
# Case 2: Infeasible The following example demonstrates when a model is infeasible due to a trivially impossible constraint.
C#
Set i = Set("i").HasElementsUntil(3); // { 0, 1, 2 }
Set j = Set("j").HasElementsUntil(4); // { 0, 1, 2, 3 }
VarD2 y = Variable("y").HasIndices(i, j).IsBinary();
Objective maxCount = maximize | sum(over(i, j), y[i, j]);
Constraint impossible = sum(over(i, j), y[i, j]) >= 100;
MathModel model = MathModel.New("dummy model").WithObjective(maxCount).HasConstraints(impossible);

Scip solver = new();
ScipParams pars = new();
var resSolution = await solver.Solve(model, pars);
var solution = resSolution.Unwrap(); // normally, check if it is 'IsOk' before unwrapping!

// solution status currently depends on the used solver
Assert.Equal("infeasible", solution.SolutionStatus);

// at this point we also know that objective function value and variables' values are not available (None):
Assert.Equal(solution.ObjectiveValue.IsNone);
Assert.Equal(solution.VarIndicesAndValuesOf(y).IsNone);
# Case 3: An error occurred with the solver

An easy way to simulate this is to force a wrong path for the solver. In reality, different problems might cause the solver to fail such as out-of-memory problem.

C#
Set i = Set("i").HasElementsUntil(3); // { 0, 1, 2 }
Set j = Set("j").HasElementsUntil(4); // { 0, 1, 2, 3 }
VarD2 y = Variable("y").HasIndices(i, j).IsBinary();
Objective maxCount = maximize | sum(over(i, j), y[i, j]);
MathModel model = MathModel.New("dummy model").WithObjective(maxCount).HasNoConstraints();

Cplex solver = new("wrong path");
var resSolution = await solver.Solve(model);

Assert.True(resSolution.IsErr); // "resSolution.Unwrap()" would've thrown an exception here; better to check before unwrapping
Assert.True(resSolution.ErrorMessage().Unwrap().Contains("wrong path")); // the error message provides detailes about why the solver failed.
Scip Mathematical model solver using SCIP (https://www.scipopt.org/).

Structures

CplexParams Cplex parameters.
ScipParams Scip parameters.