Accessing solution information

Describes the information available in the Java API about a solution.

If a solution has been found with the solve method, you access it and then query it using a variety of methods. The objective function can be accessed by the call:


double objval = cplex.getObjValue();

The values of individual modeling variables for the solution are accessed by the methods IloCplex.getValue, for example:


double x1 = cplex.getValue(var1);

Frequently, solution values for an array of variables are needed. Rather than your having to implement a loop to query the solution values variable by variable, use the method IloCplex.getValues to do so with only one function call, like this:


double[] x = cplex.getValues(vars);

Similarly,you query slack values for the constraints in the active model by means of the methods IloCplex.getSlack or IloCplex.getSlacks .

These ideas apply to solvingand printing the solution to the diet problem as well.


IloCplex     cplex = new IloCplex();
IloNumVar[]  Buy   = new IloNumVar[nFoods];

if ( byColumn ) buildModelByColumn(cplex, data, Buy, varType);
   else buildModelByRow (cplex, data, Buy, varType);

      // Solve model
 
      if ( cplex.solve() ) { 
        System.out.println();
        System.out.println(“Solution status = “ + cplex.getStatus());
        System.out.println();
        System.out.println(“ cost = “ + cplex.getObjValue());
        for (int i = 0; i < nFoods; i++) {
          System.out.println(“ Buy” + i + “ = “ + 
                             cplex.getValue(Buy[i]));
        }
        System.out.println();
      }

These lines of code start by creating a new IloCplex object and passing it, along with the raw data in another object, either to the method buildModelByColumn or to the method buildModelByRow. The array of variables returned by it is saved as the array Buy. Then the method solve optimizes the active model and, upon success, the application prints solution information.