0

I would like to optimize a simple function such as:

max = a1 * x1 + a2 * x2 + a3 * x3 

where the x's are known in advance and a1 + a2 + a3 = limit. Furthermore, I need to add a constraint where a1 = a2 = a3. Would some know how this can be implemented using lpSolveAPI? This is what I already have:

library(lpSolveAPI)
limit = 50
a <- c(1.5, 1.6, 2.5)
my.lp <- make.lp(0,3)
set.objfn(my.lp, a)
add.constraint(my.lp, 1:3, "=", limit)
lp.control(my.lp,sense='max')
my.lp
solve(my.lp)

Currently, I cannot seem to find a way to add the constraint a1 = a2 = a3 (or C1 = C2 = C3).

r2evans
  • 141,215
  • 6
  • 77
  • 149
roro
  • 1
  • The two constaints itself lead to single feasible solution `a1 = a2 = a3 = limit / 3`. You don't need to optimize anything then – Jan Kislinger Dec 22 '20 at 14:27

1 Answers1

0

You cannot add constraint with two equals. You can split then into

a1 - a2 = 0
a1 - a3 = 0

That can be done using

add.constraint(my.lp, c(1, -1, 0), "=", 0)
add.constraint(my.lp, c(1, 0, -1), "=", 0)

But this optimization doesn't make sense to me as you have only one feasible solution so there is nothing to optimize.

Edit: I've just noticed that your constraint isn't correct. What you have done is basically

x1 + 2 * x2 + 3 * x3 = limit

You need to change it to

add.constraint(my.lp, rep(1, 3), "=", limit)
Jan Kislinger
  • 1,441
  • 14
  • 26