Dear R users,
I would like to optimize a linear approximation of a quadratic function
using lpSolve. My code runs without any error or warning message but the
constraints that I set don't seem to work properly.
Nevertheless, I am certain that my code is somewhere wrong.
I would like to solve the following problem:
max 2x-x^2+y
subject to
2x^2 + 3y^2 <= 6
2>= x,y >= 0
I would split the [0,2] interval into 8 equal parts by defining k=9 points
(0 included) -lambdas in the following code- and observe the value of the
objective function and the constraint on these points and approximate it by
the following program:
(Sorry for the Latex type equations)
max Sum_{j=1}^{2}Sum_{k=0}^{8}f_{kj}lambda_{kj}
subject to:
Sum_{j=1}^{2}Sum_{k=0}^{8}g_{kj}lambda_{kj}+x2 = 6
Sum_{k=0}^{8}lambda_{k1} = 1
Sum_{k=0}^{8}lambda_{k2} = 1
lambda_{k1} >= 0
lambda_{k2} >= 0
x2 >= 0
library(lpSolve)
# Objective function and constraint
f1 <- function(x) 2*x-x^2
f2 <- function(y) y
g1 <- function(x) 2*x^2
g2 <- function(y) 3*y^2
# Setting objective function and constraint values
G1 <- g1(seq(0,2,by=0.25))
G2 <- g2(seq(0,2,by=0.25))
F1 <- f1(seq(0,2,by=0.25))
F2 <- f2(seq(0,2,by=0.25))
con <- c(G1,G2,1) # slack variable included
obj <- c(F1,F2)
# Defining the constraints
lambdasx2 <- diag(1,ncol=19,nrow=19)
lambda1x2 <- c(rep(1,times=9),rep(0,times=9),0)
lambda2x2 <- c(rep(0,times=9),rep(1,times=9),0)
lambdamatrix <- rbind(lambda1x2,lambda2x2,lambdasx2)
conlamb <- rbind(con,lambdamatrix)
# Defining the right-hand side
rightside <- c(6,1,1,rep(0,times=19))
# Defining the directions
direc <-
c("=","=","=",rep(">=",times=19))
# LP
lp("max",obj,conlamb,direc,rightside)$solution
# But I receive [1] 0.00 1.00 5.75 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# [12] 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# However, in the constraints the sum of the first 8 values should be 1 and
the second 8 values should be # 1
Thank you very much in advance,
Adam
--
View this message in context:
http://www.nabble.com/lpSolve-constraints-don%27t-seem-to-have-an-effect-tp25491959p25491959.html
Sent from the R help mailing list archive at Nabble.com.