How to write a constraint that references the Objective in R optimization

363 views Asked by At

Here is the objective I would like to maximize in R:

AX1+BX2+CX3+DX4

The following constraints exists

0 >= S2 >= 8

0 >= S3 >= 8

0 >= S4 >= 8

0 >= S5 >= 8

Where

S2 = X1 + V

S3 = X2 + X1 + V

S4 = X3 + X2 + X1 + V

S5 = X4 + X3 + X2 + X1 + V

Basically, the constraints reference the objective.

As an example, if V = 4, X1 = 2, then S2 = 6. (therefore the constraint, 0 >= S2 >= is not violated.

How do I reference the objective (I used L_Objective function) in the constraint function?

Thanks in Advance

1

There are 1 answers

0
Noah On

Does the following not work?

library(ROI)

obj <- L_objective(c(A, B, C, D)

const.mat <- matrix(c(1, 0, 0, 0,
                      1, 1, 0, 0,
                      1, 1, 1, 0,
                      1, 1, 1, 1),
                    nrow = 4)
const <- L_constraint(rbind(const.mat, constmat),
                      dir = c(rep(">=", 4), rep("<=", 4)),
                      rhs = c(rep(0-V, 4),    rep(8-V, 4)))
op <- op(obj, const, maximum = TRUE)
out <- ROI_solve(op)

Of course, filling in the correct values for A, B, C, D, and V, which I assume you have. If the last constraint is satisfied, the others will be automatically, so it's the only one that matters.