How to specify demand in lpSolveAPI in R?

Asked

Viewed 38 times

2

I am trying to optimize profit by 4 different products, one that profits 600reais/unit other that profits 550/unit other that profits 400/unit and another that profits 300/unit. However, the demand for X1 = 50 for x2 = 50 for X3 = 80 and X4 = 120.

How I specify the individual demand of each one within lpSolveAPI??

library(lpSolveAPI)
lprec <- make.lp(0,4) #numero de linhas e de variaveis de decisao
lp.control(lprec,sense='max') #maximzar ou minimizar
set.objfn(lprec, c(600, 550, 400, 300)) # funcao objetivo
add.constraint(lprec, c(31540, 40000, 29600, 14700), "<=", 108000) #restricao 1
add.constraint(lprec, c(36290,22500,42550,7350), "<=", 110000) #restricao 2
add.constraint(lprec, c(19000, 11250,11840,16800), "<=", 60000) #restricao 3

solve(lprec) #resolver ppl
get.objective(lprec) #funcao objetivo resolvida
get.variables(lprec) #qto de cada variavel otimiza o sistema
get.constraints(lprec)
lprec

1 answer

1

The package lpSolveAPI accepts restrictions of three types, "<=", ">=" and "=". I believe that in the question the demand must be understood with maximum demand, not as fixed demand. Then the restrictions to be used will be of minority, "<="

#x1 = 50, pelo x2 = 50, pelo x3 = 80 e pelo x4 = 120
add.constraint(lprec, 1, "<=", 50, indices = 1) #restricao 4a
add.constraint(lprec, 1, "<=", 50, indices = 2) #restricao 4b
add.constraint(lprec, 1, "<=", 80, indices = 3) #restricao 4c
add.constraint(lprec, 1, "<=", 120, indices = 4) #restricao 4d

Now the problem is solved as it is in the question. The solutions will be given by get.variables(lprec).

solve(lprec) #resolver ppl
#[1] 0

get.objective(lprec) #funcao objetivo resolvida
#[1] 1948.62

get.variables(lprec) #qto de cada variavel otimiza o sistema
#[1] 2.6654343 0.5221156 0.0000000 0.2073184

Verify these solutions by multiplying the coefficients of the objective function by the vector solution.

c(600, 550, 400, 300) %*% get.variables(lprec)
#        [,1]
#[1,] 1948.62

Browser other questions tagged

You are not signed in. Login or sign up in order to post.