c++cplexgetvalue

getValue() CPLEX C++ : Using empty IloNumVar handle


I try to get the values of variables (IloNumVar) in an LP. The LP is solved successfully, but I cannot access the variables in the LP. The solver prints "Concert exception caught:Using empty IloNumVar handle." for the getValue() function.

This is my code for initializing the varialbes and accessing the variables in LP

        //define variables
        IloArray<IloArray<IloNumVarArray> > x(env, uNum);
        for(k=0; k<uNum; k++)
        {
            x[k]=IloArray<IloNumVarArray> (env, nNum);
            for(j=0; j<nNum; j++)
            {
                x[k][j]=IloNumVarArray(env, nNum, 0, 1, ILOFLOAT);
            }
        }

         ...

         //get values of variables
         for(k=0; k<uNum; k++)
         {
              for(j=0; j<nNum; j++)
              {
                   for(l=0; l<nNum; l++)
                   {
                        cout<<cplex.getValue(x[k][j][l])<<endl;
                            
                   }
              }
         }

The output is as follows.

Version identifier: 20.1.0.0 | 2020-11-10 | 9bedb6d68
Parallel mode: deterministic, using up to 32 threads for concurrent optimization:
 * Starting dual Simplex on 1 thread...
 * Starting Barrier on 29 threads...
 * Starting primal Simplex on 1 thread...
 * Starting Sifting on 1 thread...
Tried aggregator 1 time.
LP Presolve eliminated 2 rows and 0 columns.
Reduced LP has 140 rows, 44100 columns, and 128100 nonzeros.
Presolve time = 0.10 sec. (32.34 ticks)
Initializing dual steep norms . . .

Iteration log . . .
Iteration:     1   Dual objective     =           160.209490

Dual simplex solved model.

Solution status: Optimal
The objective value is 160.209
Concert exception caught:Using empty IloNumVar handle.

I found that someone asked similar question for getValue() function (getValue() cplex C++). I checked the answer and my code, and still cannot solve the issue. Please help if you have any suggestions.


Solution

  • Looks like one of the variables is not constrained, thus, not "extracted" by the cplex solver, and it cannot give the value of a variable it doesn't know. You can simply add all the variables to the model.