I want to minimize the following objective function
with some constraints
And another user (I think it was G. Grothendieck) suggested to use the CVXR
package of R.
So I followed the instructions on A Gentle Introduction to CVXR to make my code
library(CVXR) # if necessary
x <- Variable(1)
y <- Variable(1)
objective <- Minimize(5*x^2 + 14*x*y + 10*y^2 -76*x -108*y +292)
constraints <- list(x >= 0, y >= 0, x + 2*y <=10, x + y<=6)
prob_OF <- Problem(objective, constraints)
solution_OF <- solve(prob_OF) # and here the error occured
## Error in construct_intermediate_chain(object, candidate_solvers, gp = gp): Problem does not follow DCP rules.
On How to convert quadratic to linear program?
I found the hint to the McCormick envelopes that helps to solve the issue with the bilinear formular part . Especially the
part.
At the end of the answer of josliber 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...
library(CVXR) # if necessary
x <- Variable(1)
y <- Variable(1)
w <- Variable(1)
objective <- Minimize(5*x^2 + 14*w + 10*y^2 -76*x -108*y +292)
constraints <- list(x >= 0, x <= 100,
y >= 0, y <= 100,
x+2*y <= 10,
x+y <= 6,
w >= 0, w >= 100*x + 100*y - 10000, # constraints according to McCormick envelopes
w <= 100*y, w <= 100*x) # constraints according to McCormick envelopes
prob_OF <- Problem(objective, constraints)
solution_OF <- 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
Here the solution is not my expected one... When I solve the same objective function with solve.QP()
then I get and
. For establishing the code look at my other question...
Let's check the 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...
Question:
- Why are the two results different?
- In which of the solving alternatives I have done any mistakes? Is it possible to point them out?
- And when I put in the x and y from the results I don't get the
$value
of thesolve.QP()
alternatives- When doing the math
- And as you can see the results don't coincide
- Am I doing here an mistake?!
- When doing the math
Many thanks in advance!