MathModelSolutionVarIndicesAndValuesOf(VarD2) Method

Returns Some of a map of indices to solution values of 2-dimensional variable provided that:
  • the solution IsFeasible, and
  • the queried var belongs to the solved model.
Returns None otherwise.
C#
Set i = Set("i").HasElementsUntil(2); // { 0, 1 }
Set j = Set("j").HasElementsUntil(3); // { 0, 1, 2 }
VarD2 x = Variable("x").HasIndices(i, j).IsBinary();
Objective maxCount = maximize | sum(over(i, j), x[i, j]);
MathModel model = MathModel.New().WithObjective(maxCount).HasNoConstraints();
Cplex solver = new();
var resSolution = await solver.Solve(model);
MathModelSolution solution = resSolution.Unwrap(); // normally, check if it is 'IsOk' before unwrapping!

VarD2 y = Variable("y").HasIndices(i, j).IsBinary(); // irrelevant to the model

var maybeXSoln = solution.VarIndicesAndValuesOf(x);
Assert.True(maybeXSoln.IsSome);

Dictionary<(int, int), double> xSoln = maybeXSoln.Unwrap(); // a map from indices of x to the values in the solution
Assert.Equal(1, xSoln[(0, 0)]);
Assert.Equal(1, xSoln[(0, 1)]);
Assert.Equal(1, xSoln[(0, 2)]);
Assert.Equal(1, xSoln[(1, 0)]);
Assert.Equal(1, xSoln[(1, 1)]);
Assert.Equal(1, xSoln[(1, 2)]);
Assert.False(xSoln.ContainsKey((1, 3))); // x(1, 3) is not a part of the model and solution

var maybeYSoln = solution.VarIndicesAndValuesOf(y); // y is not a part of this solution
Assert.True(maybeYSoln.IsNone); // "maybeYSoln.Unwrap()" would've thrown!

Definition

Namespace: Orx.MathProg.Solvers
Assembly: Orx.MathProg (in Orx.MathProg.dll) Version: 1.0.0
C#
public Opt<Dictionary<(int , int ), double>> VarIndicesAndValuesOf(
	VarD2 var
)

Parameters

var  VarD2
Variable to query value of.

Return Value

OptDictionaryValueTupleInt32, Int32, Double

See Also