title: IloExtractable : using empty handle
body: <p>I'm using Visual studio 2012 and IBM ILOG CPLEX Optimization Studio V12.6.
  When I run to the loop:</p>
  
  <pre><code>for (l=0; l&lt;num_l; ++l)
  {
      sprintf(buf,"Delta1(%d,%d,%d,%d)",k,j,l,mu);
      Delta1[k][j][l][mu].setName(buf);
      Del1_del1 = Delta1[k][j][l][mu] - delta1[k][mu];                        
      cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_del1,0));
      Del1_y = Delta1[k][j][l][mu] - (D_k[k] * y[j][l]);
      cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_y,0));
      Del1_Dydel1 = Delta1[k][j][l][mu] + D_k[k] - D_k[k] * y[j][l] + delta1[k][mu];
      cst_Del1_Dydel1.add(IloRange(env,0,Del1_y,+IloInfinity));
      for(a=0;a&lt;num_a;++a)
      {
          utility_l += beta_a[a] * r_al[a][l];
          for (b=0; b&lt;num_b; ++b)
          {
              utility_l += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
          }
      }
      sel1 += (-Delta1[k][mu][j][l]*exp(utility_l));
  }
  </code></pre>
  
  <p>I have an error shown below:</p>
  
  <p>IloExtractable: Using empty handle</p>
  
  <blockquote>
    <p>Assertion failed: (getImpl() != 0) || (ILOSTD(cerr) &lt;&lt;
    "IloExtractable" ": Using empty handle" &lt;&lt; ILOSTD(endl),
    ilo_stop_assert()), file c:\program files
    (x86)\ibm\ilog\cplex_studio_preview126\concert\include\ilconcert\iloextractable.h,
    line 267</p>
  </blockquote>
  
  <p>This is the constraint(cst_sel1) I want to add.
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;a href="https://www.codecogs.com/eqnedit.php?latex=\sum\limits_{j&amp;space;\in&amp;space;J}&amp;space;\sum\limits_{l&amp;space;\in&amp;space;L}&amp;space;\left(e^{&amp;space;u_{jkl}}&amp;space;\cdot&amp;space;\Delta_{jkl\mu}^1&amp;space;\right)&amp;space;&amp;\le&amp;space;D_k^{0}&amp;space;\cdot&amp;space;(\sum\limits_{j&amp;space;\in&amp;space;J}&amp;space;e^{&amp;space;u_{jk\mu}}&amp;space;\cdot&amp;space;y_{j\mu})\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\le&amp;space;\delta_{k\mu}^1\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\le&amp;space;D_k^{0}&amp;space;\cdot&amp;space;y_{jl}\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\ge&amp;space;-D_k^{0}&amp;space;\cdot&amp;space;(1-y_{jl})&amp;space;&amp;plus;&amp;space;\delta_{k\mu}^1" target="_blank"&gt;&lt;img src="https://latex.codecogs.com/gif.latex?\sum\limits_{j&amp;space;\in&amp;space;J}&amp;space;\sum\limits_{l&amp;space;\in&amp;space;L}&amp;space;\left(e^{&amp;space;u_{jkl}}&amp;space;\cdot&amp;space;\Delta_{jkl\mu}^1&amp;space;\right)&amp;space;&amp;\le&amp;space;D_k^{0}&amp;space;\cdot&amp;space;(\sum\limits_{j&amp;space;\in&amp;space;J}&amp;space;e^{&amp;space;u_{jk\mu}}&amp;space;\cdot&amp;space;y_{j\mu})\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\le&amp;space;\delta_{k\mu}^1\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\le&amp;space;D_k^{0}&amp;space;\cdot&amp;space;y_{jl}\\&amp;space;\Delta_{jkl\mu}^1&amp;space;&amp;\ge&amp;space;-D_k^{0}&amp;space;\cdot&amp;space;(1-y_{jl})&amp;space;&amp;plus;&amp;space;\delta_{k\mu}^1" title="\sum\limits_{j \in J} \sum\limits_{l \in L} \left(e^{ u_{jkl}} \cdot \Delta_{jkl\mu}^1 \right) &amp;\le D_k^{0} \cdot (\sum\limits_{j \in J} e^{ u_{jk\mu}} \cdot y_{j\mu})\\ \Delta_{jkl\mu}^1 &amp;\le \delta_{k\mu}^1\\ \Delta_{jkl\mu}^1 &amp;\le D_k^{0} \cdot y_{jl}\\ \Delta_{jkl\mu}^1 &amp;\ge -D_k^{0} \cdot (1-y_{jl}) + \delta_{k\mu}^1" /&gt;&lt;/a&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is the whole cpp file.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;ilcplex/ilocplex.h&gt; 
  #include &lt;math.h&gt; 
  #include &lt;iostream&gt; 
  #include &lt;fstream&gt; 
  #include &lt;sstream&gt;
  
  ILOSTLBEGIN
  
      typedef IloArray&lt;IloNumVarArray&gt; IloNumVarArray2;
      typedef IloArray&lt;IloNumVarArray2&gt; IloNumVarArray3;
      typedef IloArray&lt;IloNumVarArray3&gt; IloNumVarArray4;
      typedef IloArray&lt;IloNumArray2&gt; IloNumArray3;
      typedef IloArray&lt;IloBoolVarArray&gt; IloBoolVarArray2;
      typedef IloArray&lt;IloIntVarArray&gt; IloIntVarArray2;
      typedef IloArray&lt;IloIntVarArray2&gt; IloIntVarArray3;
      typedef IloArray&lt;IloIntVarArray3&gt; IloIntVarArray4;
      typedef IloArray&lt;IloRangeArray&gt; IloRangeArray2;
  
  
  int main(int argc, char **argv)
  {
      IloEnv env;
      char buf[300];
      try
      {
          const char* filename = "PPL_PARA.txt"; //input file
          if (argc &gt;=2) filename = argv[1];
          ifstream file(filename);
          if( !file)
          {
              cerr &lt;&lt; "No such file: " &lt;&lt; filename &lt;&lt; endl;
              throw(-1);
          }
          IloInt num_j; //number of outreach clinics;
          IloInt num_k; //number of patient groups
          IloInt num_l; //number of physicians
          IloInt num_a; //number of catagories for physician's attributes
          IloInt num_b; //number of catagories for patient's characteristics
  
          IloNum T; //working hour of physician
          IloNum alpha; //coefficient of patients' travel time for utility function
          IloNum omega1; //coefficient of 1st choice
          IloNum omega2; //coefficient of 2nd choice
          IloNum omega3; //coefficient of 3rd choice
          IloNum omega4; //coefficient of 4th choice
  
  
  
          IloInt j,k,l,mu,nu,a,b; //different index for loops
          IloNum M = 100000.0; //Big M
          IloModel mod(env);
          IloCplex cplex(mod);
  
          //reading the input file
          file &gt;&gt; num_j
              &gt;&gt; num_k
              &gt;&gt; num_l
              &gt;&gt; num_a
              &gt;&gt; num_b
              &gt;&gt; T
              &gt;&gt; alpha
              &gt;&gt; omega1
              &gt;&gt; omega2
              &gt;&gt; omega3
              &gt;&gt; omega4;
  
          IloNumArray beta_a(env, num_a); //coefficient of physician's attributes
          IloNumArray beta_b(env,num_b); //coefficient of patient's characteristics
          IloNumArray time_l(env,num_l); //diagnosis time of physician l
          IloNumArray D_k(env,num_k); //population of patient group k
  
          IloNumArray2 gamma_ab = IloNumArray2(env,num_a); //coefficient of the untility function's last term
          for(int a=0; a&lt;num_a; ++a){
              gamma_ab[a] = IloNumArray(env,num_b);
          }
          IloNumArray2 r_al = IloNumArray2(env,num_a); //charaticristics of physician
          for(int a=0; a&lt;num_a; ++a){
              r_al[a] = IloNumArray(env,num_l);
          }
          IloNumArray2 s_bk = IloNumArray2(env,num_b); //charaticristics of patient
          for(int b=0; b&lt;num_b; ++b){
              s_bk[b] = IloNumArray(env,num_k);
          }
          IloNumArray2 time_jk = IloNumArray2(env,num_j); //travel time of patient group k to outreach clinic j;
          for(int j=0; j&lt;num_j; ++j){
              time_jk[j] = IloNumArray(env,num_k);
          }
          IloNumArray2 time_jl = IloNumArray2(env,num_j); //travel time of physician l to outreach clinic j;
          for(int j=0; j&lt;num_j; ++j){
              time_jl[j] = IloNumArray(env,num_l);
          }
              for (a=0; a&lt;num_a; a++)
                  file &gt;&gt; beta_a[a];  
              for (b=0; b&lt;num_b; b++)
                  file &gt;&gt; beta_b[b];
              for (l=0; l&lt;num_l; l++)
                  file &gt;&gt; time_l[l];
              for (k=0; k&lt;num_k; k++)
                  file &gt;&gt; D_k[k];
              for (a=0; a&lt;num_a; a++)
                  for (b=0; b&lt;num_b; b++)
                      file &gt;&gt; gamma_ab[a][b];
              for (a=0; a&lt;num_a; a++)
                  for (l=0; l&lt;num_l; l++)
                      file &gt;&gt;r_al[a][l];
              for (b=0; b&lt;num_b; b++)
                  for (k=0; k&lt;num_k; k++)
                      file &gt;&gt;s_bk[b][k];
              for (j=0; j&lt;num_j; j++)
                  for (k=0; k&lt;num_k; k++)
                      file&gt;&gt;time_jk[j][k];
              for (j=0; j&lt;num_j; j++)
                  for (l=0; l&lt;num_l; l++)
                      file &gt;&gt;time_jl[j][l];
  
  
              file.close();
  
          //variables
          //Binary variable for physician l is assigned to clinic j
  
          IloBoolVarArray2 y(env,num_j);
          for (j=0; j&lt;num_j; j++)
          {
              y[j] = IloBoolVarArray(env, num_l);         
              for(l=0;l&lt;num_l;++l){
                  sprintf(buf,"y(%d,%d)",j,l);
                  y[j][l].setName(buf);
              }
          }
  
          //Integar variable for capacity of physician l
          IloNumVarArray capacity(env,num_l);
          IloExpr xpr_capacity(env);
          for (l=0;l&lt;num_l; l++)
          {
              capacity[l] = IloNumVar(env,0.0,+IloInfinity);          
              for (j=0; j&lt;num_j; j++)
                  xpr_capacity += ((T-time_jl[j][l]*y[j][l]/time_l[l]));
          }
  
          //Integar variable for population of patient group who is assigned to clinic j under 1st,2nd,3rd,4th choice
          //population of patient group k which is assigned to clinics j under 1st choices
          IloNumVarArray2 delta1(env,num_k);
          for (k=0; k&lt;num_k; k++)
          {
              delta1[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
              for(l=0;l&lt;num_l;++l){
                  sprintf(buf,"delta1(%d,%d)",k,l);
                  delta1[k][l].setName(buf);
              }
          }
  
          //population of patient group k which is assigned to clinics j under 2nd choices
          IloNumVarArray2 delta2(env,num_k);
          for (k=0; k&lt;num_k; k++)
          {
              delta2[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
              for(l=0;l&lt;num_l;++l){
                  sprintf(buf,"delta2(%d,%d)",k,l);
                  delta2[k][l].setName(buf);
              }
          }
  
          //population of patient group k which is assigned to clinics j under 3rd choices
          IloNumVarArray2 delta3(env,num_k);
          for (k=0; k&lt;num_k; k++)
          {
              delta3[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
              for(l=0;l&lt;num_l;++l){
                  sprintf(buf,"delta3(%d,%d)",k,l);
                  delta3[k][l].setName(buf);
              }
          }
  
          IloNumVarArray2 delta4(env,num_k);
          for (k=0; k&lt;num_k; k++)
          {
              delta4[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
              for(l=0;l&lt;num_l;++l){
                  sprintf(buf,"delta4(%d,%d)",k,l);
                  delta4[k][l].setName(buf);
              }
          }
  
          // define objective function and constraints
          IloObjective mod_obj = IloAdd(mod, IloMaximize(env));
  
          IloExpr obj_xpr(env);
          double utility;
          for(k=0;k&lt;num_k;++k){   
              for(l=0;l&lt;num_l;++l){
                  IloExpr Utility(env);
                  utility = 0;
                  for(j=0;j&lt;num_j;++j)
                  {
                      utility += alpha * time_jk[j][k];
                      for(a=0;a&lt;num_a;++a){
                          utility += beta_a[a] * r_al[a][l];
                          for (b=0; b&lt;num_b; ++b)
                              utility += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
                      }
                      for (b=0; b&lt;num_b; ++b)
                      {
                          utility += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                      }
                      Utility += utility * y[j][l];
                  }           
                  obj_xpr += omega1 * Utility * delta1[k][l];
                  obj_xpr += omega2 * Utility * delta2[k][l];
                  obj_xpr += omega3 * Utility * delta3[k][l];
                  obj_xpr += omega4 * Utility * delta4[k][l];
                  Utility.end();
              }
          }
          mod_obj.setExpr(obj_xpr);
          //cout &lt;&lt; obj_xpr &lt;&lt; endl;
          obj_xpr.end();
  
          //constraints
          IloRangeArray cst_y(env);
          //a physician can only pick one clinic.
          for (j=0; j&lt;num_j; ++j)
          {
              IloExpr sum_y(env);
              for(l=0; l&lt;num_l; ++l)
              {
                  sum_y += (y[j][l]);
              }   
              cst_y.add(IloRange(env,1,sum_y,1));
              sum_y.end();
          }
          mod.add(cst_y); 
  
          //
          IloRangeArray cst_sel1(env);
          IloRangeArray cst_Del1_del1(env);
          IloRangeArray cst_Del1_y(env);
          IloRangeArray cst_Del1_Dydel1(env);
          IloIntVarArray4 Delta1(env,num_l);      
          IloExpr Del1_del1(env);
          IloExpr Del1_y(env);
          IloExpr Del1_Dydel1(env);
          double utility_mu;
          double utility_l;
          utility_mu = 0;
          utility_l = 0;
          for (k=0; k&lt;num_k; ++k)
          {
              Delta1[k] = IloIntVarArray3(env, num_l);
              for (mu=0; mu&lt;num_l; ++mu)
              {               
                  Delta1[k][mu] = IloIntVarArray2(env, num_j);
                  IloExpr sel1(env);
                  for (j=0; j&lt;num_j; ++j)
                  {
                      Delta1[k][j][mu] = IloIntVarArray(env, num_l);
                      utility_mu += alpha * time_jk[j][k];
                      for(a=0;a&lt;num_a;++a)
                      {
                          utility_mu += beta_a[a] * r_al[a][mu];
                          for (b=0; b&lt;num_b; ++b)
                          {
                              utility_mu += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][mu]*time_jk[j][k]);
                          }
                      }
                      for (b=0; b&lt;num_b; ++b)
                      {
                          utility_mu += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                      }
                      utility_l += alpha * time_jk[j][k];
                      for (b=0; b&lt;num_b; ++b)
                      {
                          utility_l += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                      }
                      for (l=0; l&lt;num_l; ++l)
                      {
                          sprintf(buf,"Delta1(%d,%d,%d,%d)",k,j,l,mu);
                          Delta1[k][j][l][mu].setName(buf);
                          Del1_del1 = Delta1[k][j][l][mu] - delta1[k][mu];                        
                          cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_del1,0));
                          Del1_y = Delta1[k][j][l][mu] - (D_k[k] * y[j][l]);
                          cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_y,0));
                          Del1_Dydel1 = Delta1[k][j][l][mu] + D_k[k] - D_k[k] * y[j][l] + delta1[k][mu];
                          cst_Del1_Dydel1.add(IloRange(env,0,Del1_y,+IloInfinity));
                          for(a=0;a&lt;num_a;++a)
                          {
                              utility_l += beta_a[a] * r_al[a][l];
                              for (b=0; b&lt;num_b; ++b)
                              {
                                  utility_l += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
                              }
                          }
                          sel1 += (-Delta1[k][mu][j][l]*exp(utility_l));
                      }
                      sel1 += D_k[k]*y[j][mu]*exp(utility_mu);                    
                  }
                  cst_sel1.add(IloRange(env,-IloInfinity,sel1,0));
                  sel1.end();
              }
          }
          mod.add(cst_Del1_del1);
          mod.add(cst_sel1);  
  
          cplex.exportModel("mod.lp");
  
      }   
      catch(IloException&amp; ex)
      { //for any exceptions caught
          cerr &lt;&lt; "Error : " &lt;&lt; ex &lt;&lt; endl;
      }
      catch(...)
      {
          cerr &lt;&lt; "Error : Unknown exception caught!" &lt;&lt; endl;
      }
      env.end();
      return 0;
  }
  </code></pre>
  
  <p>Thanks.</p>