title: How to port CPLEX linear optimization from Matlab to C++
body: <p>I am trying to port an application from Matlab to C++. This application contains a linear optimization problem of the form:</p>
  <blockquote>
  <p>min. <em>F</em> * x</p>
  <p>s.t. <strong>A</strong> * x &lt; <em>Rhs</em></p>
  <p>s.t. <em>Lb</em> &lt;= x &lt;= <em>Ub</em></p>
  </blockquote>
  <p>which needs to be solved.</p>
  <p>The matlab API of CPLEX supports writing all coefficients as vectors (<em>F</em>, <em>Rhs</em>, <em>Lb</em>, and <em>Ub</em>) or matrices (<strong>A</strong>) respectively. The C++ interface however does not. Instead the problem needs to be constructed by row or by column.</p>
  <p>Here I am stuck since I can't seem to geht the same result in Matlab and C++ even for a small toy problem.</p>
  <pre class="lang-Matlab prettyprint-override"><code>t_v_l = [1 0 0 1 0 0 1 0 0]';
  t_v_m = [0 1 0 0 1 0 0 1 0]';
  t_v_r = [0 0 1 0 0 1 0 0 1]';
  
  t_h_o = [1 1 1 0 0 0 0 0 0]';
  t_h_m = [0 0 0 1 1 1 0 0 0]';
  t_h_u = [0 0 0 0 0 0 1 1 1]';
  
  target = [0 1 0 1 1 1 0 1 0];
  
  dict = [t_v_l, t_v_m, t_v_r, t_h_o, t_h_m, t_h_u];
  
  M = size(dict, 1);
  N = size(dict, 2);
  
  F = zeros(N + M, 1);
  F(N + 1 : N + M) = ones(M,1); 
  
  Lb = sparse(zeros(N + M, 1));
  Lb(N + 1 : N + M) = 0;
  
  Ub = ones(N + M, 1);
  Ub(N + 1 : N + M) = 999;
  
  Rhs = zeros(2 * M, 1);
  Rhs(    1 :     M) = -target;
  Rhs(M + 1 : 2 * M) =  target;
  
  Lhs = -999 * ones(2 * M, 1);
  
  A = zeros(2 * M, N + M);
  A(    1 :     M,     1 : N    ) = -dict;
  A(M + 1 : 2 * M,     1 : N    ) =  dict;
  A(    1 :     M, N + 1 : N + M) = -eye(M);
  A(M + 1 : 2 * M, N + 1 : N + M) = -eye(M);
  
  plex = Cplex('myModel');
  
  plex.addCols(F, [], Lb, Ub);
  plex.addRows(Lhs, A, Rhs);
              
  plex.writeModel('ex.lp'); % Optional
  
  plex.solve();
  
  X = plex.Solution.x;
  
  fprintf(&quot;Solution:\n&quot;)
  disp(X');
  </code></pre>
  <p>Conceptually I am trying to reconstruct a binary target vector from different binary templates. The first 6 entries in the solution show which templates have to be chosen (in this case template 2 and 5). The remaining entries correspond to slack variables where the reconstruction is not entirely accurate due to overlap or noise in the target. The code above works and gives me the correct result.</p>
  <p>However, when I try to follow the examples (like Ilodiet.cpp) to produce the same results in the Concert API the optimization does not work anymore.</p>
  <pre><code>#ifndef IL_STD
  #define IL_STD
  #endif
  #include &lt;cstring&gt;
  #include &lt;ilcplex/ilocplex.h&gt;
  #include &lt;vector&gt;
  ILOSTLBEGIN
  
  void main()
  {
      // Input Data
      std::vector&lt;vector&lt;float&gt;&gt; dict =
      {
          { 1, 0, 0, 1, 0, 0, 1, 0, 0 },
          { 0, 1, 0, 0, 1, 0, 0, 1, 0 },
          { 0, 0, 1, 0, 0, 1, 0, 0, 1 },
          { 1, 1, 1, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 1, 1, 1, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 1, 1, 1 }
      };
  
      std::vector&lt;float&gt; target =
      { 0, 1, 0, 1, 1, 1, 0, 1, 0 };
  
      const int M = target.size();
      const int N = dict.size();
  
      // Build Matrix A
      std::vector&lt;std::vector&lt;float&gt;&gt; A;
      for(int r = 0; r &lt; M; ++r)
      {
          std::vector&lt;float&gt; row(N + M, 0);
          for(int c = 0; c &lt; N; ++c)
          {
              row[c] = -dict[c][r];
          }
          row[N + r] = -1;
          A.push_back(row);
      }
  
      for (int r = M; r &lt; 2 * M; ++r)
      {
          std::vector&lt;float&gt; row(N + M, 0);
          for (int c = 0; c &lt; N; ++c)
          {
              row[c] = dict[c][r - M];
          }
          row[N + r - M] = -1;
          A.push_back(row);
      }
  
      // Build Model
      IloEnv env;
      IloModel mod(env);
  
      IloNumArray F(env, N + M);
      IloNumArray Lb(env, N + M);
      IloNumArray Ub(env, N + M);
  
      for (int i = 0; i &lt; N; ++i)
      {
          F[i] = 0.f;
  
          Lb[i] = 0.f;
          Ub[i] = 1.f;
      }
  
      for (int i = N; i &lt; N + M; ++i)
      {
          Lb[i] = 0.f;
          Ub[i] = 999.f;// IloInfinity;
  
          F[i] = 1.f;
      }
  
      IloNumArray Rhs(env, 2 * M);
      IloNumArray Lhs(env, 2 * M);
  
      for (int i = 0; i &lt; M; ++i)
      {
          Rhs[i] = -target[i];
          Lhs[i] = -999.f;// -IloInfinity;
  
          Rhs[M + i] = target[i];
          Lhs[M + i] = -999.f;// -IloInfinity;
      }
  
      IloNumVarArray x(env, Lb, Ub, ILOFLOAT);
  
      IloObjective minimize = IloMinimize(env, IloScalProd(x, F));
      mod.add(IloExtractable(minimize));
  
      for (int r = 0; r &lt; A.size(); ++r) 
      {
          IloExpr expr(env);
          for (int c = 0; c &lt; A[r].size(); ++c) {
              expr += F[c] * A[r][c];
          }
          mod.add(expr &lt;= Rhs[r]);
          expr.end();
      }
  
      IloCplex cplex(mod);
      cplex.exportModel(&quot;ex.lp&quot;);
      cplex.solve();
      
      std::cout &lt;&lt; &quot;Solution status = &quot; &lt;&lt; cplex.getStatus() &lt;&lt; endl;
      std::cout &lt;&lt; &quot;Cost = &quot; &lt;&lt; cplex.getObjValue() &lt;&lt; endl;
  
      std::vector&lt;int&gt; ret;
      for (int i = 0; i &lt; N; i++) 
      {
          try 
          {
              std::cout &lt;&lt; &quot;Template i: &quot; &lt;&lt; cplex.getValue(x[i]) &lt;&lt; std::endl;
          }
          catch(...)
          {
              std::cout &lt;&lt; &quot;Template i: 0 (optimized away)&quot; &lt;&lt; std::endl;
          }
      }
  
      for (int i = N; i &lt; N + M; i++)
      {
          try
          {
              std::cout &lt;&lt; &quot;Slack i: &quot; &lt;&lt; cplex.getValue(x[i]) &lt;&lt; std::endl;
          }
          catch (...)
          {
              std::cout &lt;&lt; &quot;Slack i: 0 (optimized away)&quot; &lt;&lt; std::endl;
          }
      }
  
      env.end();
  }
  </code></pre>
  <p>The output tells me that the presolver eliminated all rows and columns and my solution is empty. The exported problem files also look completely different.
  If I take a look at the individual arrays however, they look exactly the same. So I guess I am doing something wrong while passing my information to the Concert API.</p>
  <p>Where is my mistake? Any help is appreciated!</p>
  <hr />
  <p>The code for printing the variables (since IloArrays are hard to look at in the debugger) in Matlab:</p>
  <pre><code>fprintf(&quot;F:\n&quot;);
  disp(F');
  
  fprintf(&quot;Lb:\n&quot;);
  disp(Lb');
  
  fprintf(&quot;Ub:\n&quot;);
  disp(Ub');
  
  fprintf(&quot;Lhs:\n&quot;);
  disp(Lhs');
  
  fprintf(&quot;Rhs:\n&quot;);
  disp(Rhs');
  
  fprintf(&quot;A:\n&quot;);
  disp(A);
  </code></pre>
  <p>And in C++:</p>
  <pre><code>std::cout &lt;&lt; &quot;F:&quot; &lt;&lt; std::endl;
  for (int i = 0; i &lt; N + M; ++i)
  {
      std::cout &lt;&lt; F[i] &lt;&lt; &quot;, &quot;;
  }
  
  std::cout &lt;&lt; &quot;\n\nLb:&quot; &lt;&lt; std::endl;
  for (int i = 0; i &lt; N + M; ++i)
  {
      std::cout &lt;&lt; Lb[i] &lt;&lt; &quot;, &quot;;
  }
  
  std::cout &lt;&lt; &quot;\n\nUb:&quot; &lt;&lt; std::endl;
  for (int i = 0; i &lt; N + M; ++i)
  {
      std::cout &lt;&lt; Ub[i] &lt;&lt; &quot;, &quot;;
  }
  
  std::cout &lt;&lt; &quot;\n\nLhs:&quot; &lt;&lt; std::endl;
  for (int i = 0; i &lt; 2 * M; ++i)
  {
      std::cout &lt;&lt; Lhs[i] &lt;&lt; &quot;, &quot;;
  }
  
  std::cout &lt;&lt; &quot;\n\nRhs:&quot; &lt;&lt; std::endl;
  for (int i = 0; i &lt; 2 * M; ++i)
  {
      std::cout &lt;&lt; Rhs[i] &lt;&lt; &quot;, &quot;;
  }
  
  std::cout &lt;&lt; &quot;\n\nA:&quot; &lt;&lt; std::endl;
  for (int r = 0; r &lt; 2 * M; ++r)
  {
      for(int c = 0; c &lt; N + M; ++c)
      {
          std::cout.width(2);
          std::cout.fill(' ');
          std::cout &lt;&lt; A[r][c] &lt;&lt; &quot;, &quot;;
      }
      std::cout &lt;&lt; &quot;\n&quot;;
  }
  </code></pre>