title: Adding constraints to CPLEX not working C++ API
body: <p>After solving my model with CPLEX I would like to add further constraints to the model. I have a vector of requests <code>R</code>, with a corresponding variable <code>p_i</code> for each <code>i  in R</code> and a constraint <code>p_i = 1</code> (there are more variables and constraints which I have omitted here). After the first solve a new request appears for which I would like to create a new variable <code>p_{new_request}</code> and the corresponding constraint <code>p_{new_request} = 1</code>. I've tried this in two different ways, one way works using</p>
  <pre><code>model.add(p[rmap[new_request]] == 1); 
  </code></pre>
  <p>the other one doesn't, i.e. the constraint is not added to the model.</p>
  <pre><code>accept.add(IloRange());
  accept[rmap[new_request]] = IloRange(env,1,p[rmap[new_request]],1,name.str().c_str());
  </code></pre>
  <p>I need the second way, because I also need to remove certain constraints from the model once the request has been served, i.e.</p>
  <pre><code>accept[rmap[i]].end();
  </code></pre>
  <p>How can I do that properly?
  Help is very much appreciated!!!</p>
  <p>Here's the complete code:
  (Since <code>R = {1,2,3,4,5,6,7,8,9,10,25,26}</code>, and I cannot access <code>p[25]</code> as <code>p</code> is a vector of size 12, I have created a map <code>rmap</code>. )</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;unordered_map&gt;
  #include &lt;ilcplex/ilocplex.h&gt;
  ILOSTLBEGIN
  
  int main(){
    
    bool solved;
  
    // vector R contains all requests
    std::vector &lt;int&gt; R = {1,2,3,4,5,6,7,8,9,10,25,26};
    // use this unordered map to index variables p with requests
    std::unordered_map&lt;int,int&gt; rmap;
    int count = 0;
    for (const auto&amp; i: R)
    {
      rmap[i] = count;
      count++;
    }
  
    int new_request;
    
    // use this stringstream to create variable and constraint names
    std::stringstream name;
  
    IloEnv             env;
    IloModel     model(env);
    IloNumVarArray p(env,R.size());
    IloRangeArray accept(env,R.size());
    
    for (const auto&amp; i: R)
    {
      name &lt;&lt; &quot;p_&quot; &lt;&lt; i;
      p[rmap[i]] = IloNumVar(env, 0, 1, ILOBOOL, name.str().c_str()); 
      name.str(&quot;&quot;);
    }
  
    for (const auto&amp; i : R)
    {
      name &lt;&lt; &quot;accept_&quot; &lt;&lt; i;
      accept[rmap[i]] = IloRange(env,1,p[rmap[i]],1,name.str().c_str()); 
      name.str(&quot;&quot;); // Clean name
    }   
    model.add(accept);
    
    IloExpr expr(env);
    // Create objective function
    for (const auto&amp; i : R)
    {
        expr += p[rmap[i]]; 
    }
    IloObjective obj(env, expr, IloObjective::Minimize);
    model.add(obj);
    expr.clear();
    
    
    // Create the solver object
    IloCplex cplex(model);
    env.out() &lt;&lt; &quot;Before&quot; &lt;&lt; endl;
    env.out() &lt;&lt; model &lt;&lt; endl;
    cplex.exportModel(&quot;Before.1.lp&quot;);
    
    solved = cplex.solve();
  
    if (solved)
    {
      // If CPLEX successfully solved the model, print the results
      std::cout &lt;&lt; &quot;\n\nCplex success!\n&quot;;
      std::cout &lt;&lt; &quot;\tStatus: &quot; &lt;&lt; cplex.getStatus() &lt;&lt; &quot;\n&quot;;
      std::cout &lt;&lt; &quot;\tObjective value: &quot; &lt;&lt; cplex.getObjValue() &lt;&lt; &quot;\n&quot;;
  
      // add new request to R, create variable p and corresponding constraint
      new_request = 23;
      R.push_back(new_request);
      rmap[new_request] = R.size() - 1;
      name &lt;&lt; &quot;p_&quot; &lt;&lt; new_request;
      p.add(IloNumVar(env,0,1,ILOBOOL,name.str().c_str()));
      name.str(&quot;&quot;);
      name &lt;&lt; &quot;accept_&quot; &lt;&lt; new_request;
      
      // These to lines don't work
      accept.add(IloRange());
      accept[rmap[new_request]] = IloRange(env,1,p[rmap[new_request]],1,name.str().c_str()); 
      
      // this line works
      //model.add(p[rmap[new_request]] == 1);
      
      name.str(&quot;&quot;); // Clean name
  
      env.out() &lt;&lt; &quot;After&quot; &lt;&lt; endl;
      env.out() &lt;&lt; model &lt;&lt; endl;
      cplex.exportModel(&quot;After.1.lp&quot;);
      solved = cplex.solve();
    }
    
    expr.end();
    env.end();
  }
  </code></pre>