MathModelSolutionVarIndicesAndValuesOf Method

Overload List

VarIndicesAndValuesOf(VarD1) Returns Some of a map of indices to solution values of 1-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(3); // { 0, 1, 2 }
VarD1 x = Variable("x").HasIndices(i).IsBinary();
Objective maxXs = maximize | sum(over(i), x[i]);
MathModel model = MathModel.New().WithObjective(maxXs).HasNoConstraints();
Cplex solver = new();
var resSolution = await solver.Solve(model);
MathModelSolution solution = resSolution.Unwrap(); // normally, check if it is 'IsOk' before unwrapping!

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

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

Dictionary<int, double> xSoln = maybeXSoln.Unwrap(); // a map from indices of x to the values in the solution
Assert.Equal(1, xSoln[0]);
Assert.Equal(1, xSoln[1]);
Assert.Equal(1, xSoln[2]);
Assert.False(xSoln.ContainsKey(3)); // x(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!
VarIndicesAndValuesOf(VarD2) 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!
VarIndicesAndValuesOf(VarD3) Returns Some of a map of indices to solution values of 3-dimensional variable provided that:
  • the solution IsFeasible, and
  • the queried var belongs to the solved model.
Returns None otherwise.

See VarIndicesAndValuesOf(VarD2) for a documentation corresponding to two-index variables.

See Also