title: Multiply two variables in an objective function using cvxr
body: <p>I want to minimize the following objective function</p>
  <p><img src="https://latex.codecogs.com/gif.latex?5x%5E2+14xy+10y%5E2-76x-108y+292" alt="objective_function" /></p>
  <p>with some constraints</p>
  <p><img src="https://latex.codecogs.com/gif.latex?%5Cbegin%7Balign*%7D%20x%20%26%20%5Cgeq%200%20%5C%5C%20y%20%26%20%5Cgeq%200%20%5C%5C%20x+2y%26%20%5Cleq10%20%5C%5C%20x+y%26%20%5Cleq6%20%5Cend%7Balign*%7D" alt="constraints" /></p>
  <p>And another user (I think it was <a href="https://stackoverflow.com/users/516548/g-grothendieck">G. Grothendieck</a>) suggested to use the <code>CVXR</code> package of R.</p>
  <p>So I followed the instructions on <a href="https://cvxr.rbind.io/cvxr_examples/cvxr_gentle-intro/" rel="nofollow noreferrer">A Gentle Introduction to CVXR</a> to make my code</p>
  <pre><code>library(CVXR)  # if necessary
  x &lt;- Variable(1)
  y &lt;- Variable(1)
  objective &lt;- Minimize(5*x^2 + 14*x*y + 10*y^2 -76*x -108*y +292)
  constraints &lt;- list(x &gt;= 0, y &gt;= 0, x + 2*y &lt;=10, x + y&lt;=6)
  prob_OF &lt;- Problem(objective, constraints)
  solution_OF &lt;- solve(prob_OF)  # and here the error occured
  
  ## Error in construct_intermediate_chain(object, candidate_solvers, gp = gp): Problem does not follow DCP rules.
  </code></pre>
  <p>On <a href="https://stackoverflow.com/questions/30774270/how-to-convert-quadratic-to-linear-program">How to convert quadratic to linear program?
  </a> I found the hint to the <a href="https://optimization.mccormick.northwestern.edu/index.php/McCormick_envelopes" rel="nofollow noreferrer">McCormick envelopes</a> that helps to solve the issue with the bilinear formular part <img src="https://latex.codecogs.com/gif.latex?14xy" alt="part_of_OF" />. Especially the <img src="https://latex.codecogs.com/gif.latex?xy" alt="part_of_OF" /> part.</p>
  <p>At the end of the answer of <a href="https://stackoverflow.com/users/3093387/josliber">josliber</a> he remarks, that all the variables should have a bound. In my constraints there is no upper bound and therefore I insert an upper bound. It's an arbitrary choice. You have to recalculate with new bounds if the solution is on the bound...</p>
  <pre><code>library(CVXR)  # if necessary
  x &lt;- Variable(1)
  y &lt;- Variable(1)
  w &lt;- Variable(1)
  objective &lt;- Minimize(5*x^2 + 14*w + 10*y^2 -76*x -108*y +292)
  constraints &lt;- list(x &gt;= 0, x &lt;= 100,
                      y &gt;= 0, y &lt;= 100,
                      x+2*y &lt;= 10, 
                      x+y &lt;= 6,
                      w &gt;= 0, w &gt;= 100*x + 100*y - 10000,  # constraints according to McCormick envelopes
                      w &lt;= 100*y, w &lt;= 100*x)  # constraints according to McCormick envelopes
  prob_OF &lt;- Problem(objective, constraints)
  solution_OF &lt;- solve(prob_OF)
  solution_OF$value
  ## -125.0667
  solution_OF$getValue(x)                  
  ## 2.933333
  solution_OF$getValue(y)
  ## 3.066667
  solution_OF$getValue(w)
  ## 1.000135e-30
  </code></pre>
  <p>Here the solution is not my expected one... When I solve the same objective function with <code>solve.QP()</code> then I get <img src="https://latex.codecogs.com/gif.latex?x%3D2" alt="result_x" /> and <img src="https://latex.codecogs.com/gif.latex?y%3D4" alt="result_y" />. For establishing the code look at my other <a href="https://stackoverflow.com/questions/67912192/usage-and-determination-of-the-arguments-of-quadprogsolve-qp">question</a>...</p>
  <p>Let's check the code:</p>
  <pre><code># Parameters of the objective funtion and the constraints
  D=matrix(c(5,7,7,10),ncol=2,byrow=TRUE)
  d=c(-78,-108)
  A=matrix(c(1,2,1,1),ncol=2,byrow=TRUE)
  b=c(10,6)
  
  # Convert the parameters to an appropriate state of solve.QP()
  Dmat=2*D
  dvec=-d
  Amat=-t(A)
  bvec=-b
  
  # load the package and run solve.QP()
  library(quadprog)
  solve.QP(Dmat,dvec,Amat,bvec,meq=0,factorized=TRUE)
  ## $solution
  ## [1] 2 4     # these are the x and y results
  ##
  ## $value
  ## -587.9768
  ##
  ## and some more results...
  </code></pre>
  <p><strong>Question</strong>:</p>
  <ul>
  <li>Why are the two results different?
  <ul>
  <li>In which of the solving alternatives I have done any mistakes? Is it possible to point them out?</li>
  </ul>
  </li>
  <li>And when I put in the x and y from the results I don't get the <code>$value</code> of the <code>solve.QP()</code> alternatives
  <ul>
  <li>When doing the math <img src="https://latex.codecogs.com/gif.latex?%5Cbegin%7Bflushleft%7D%20%5Ccolor%7Bblack%7D%5Ctext%7Bsolve.QP%5C%24value%7D%20%5Cend%7Bflushleft%7D%3D%5Cmathbf%7B-587.9768%7D%5Ccolor%7Bred%7D%5Cneq%5Ccolor%7Bblack%7D5*%5Cmathbf%7B2%7D%5E2%20+%2014*%5Cmathbf%7B2%7D*%5Cmathbf%7B4%7D%20+%2010*%5Cmathbf%7B4%7D%5E2%20-76*%5Cmathbf%7B2%7D%20-108*%5Cmathbf%7B4%7D%20+292%3D%5Cmathbf%7B0%7D" alt="formula_question" /></li>
  <li>And as you can see the results don't coincide</li>
  <li>Am I doing here an mistake?!</li>
  </ul>
  </li>
  </ul>
  <p>Many thanks in advance!</p>