MathModelSolutionVarIndicesAndValuesOf(VarD1) Method
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.
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!