Reputation: 533
I m curious if it is possible to define an objective function to be optimized to a specific value instead of just 'min' or 'max'. E.g., I have a function which I want to optimize to 100 having some constraints (omitted here). Objective function (to be optimized to 100):
f(x) 3.62*x1+5.19*x2
What would be the proper way to achieve my goal? Right now I can only define optimization to 'min' or 'max' which doesn't solve my goal.
The full code:
fn.obj <- c(3.62, 5.19, 7.29, 7.76, 3.82, 4.86, 4.03, 8.81, 9.14)
require(lpSolveAPI)
model <- make.lp(0,9)
lp.control(model, sense="max", verbose="full")
set.objfn(model, fn.obj)#-8333550.82)
add.constraint(model, c(70,70,-30,70,-30,-30,-30,-30,70), ">=", 0)
add.constraint(model, c(-60,-60,40,-60,40,40,40,40,-60), ">=", 0)
add.constraint(model, c(-20,-20,80,-20,-20,80,80,-20,-20), ">=", 0)
add.constraint(model, c(30,30,-70,30,30,-70,-70,30,30), ">=", 0)
add.constraint(model, c(-30,-30,-30,-30,70,-30,-30,70,-30), ">=", 0)
add.constraint(model, c(40,40,40,40,-60,40,40,-60,40), ">=", 0)
set.bounds(model, lower=c(39232,72989,90872,63238,49579,9626,158297,300931,160556), upper=c(49041,91237,113591,79048,61974,12033,197872,376164,200696))
set.type(model, 1:9,type = "integer")
res<-solve(model)
get.variables(model)
get.objective(model)
Result:
> get.variables(model)
[1] 49041 91237 113591 79048 61974 12033 197872 376164 200696
> get.objective(model)
[1] 8333551
The code works good when needs to maximize the objective function. But what if I want not to maximize but to optimize my objective function to e.g. 7000. So having the same constraints I want to find possible x1,..,x9 for my fn.obj -> 1000.
Upvotes: 3
Views: 1240
Reputation: 269471
Provided the value you specify for the objective value corresponds to a feasible solution you can do it by just adding it as a constraint. 1000 and 7000 do not appear to correspond to any feasible solutions but suppose we want the objective to equal 8000000 instead of 8333551. Then add this constraint and rerun the model.
add.constraint(model, fn.obj, "=", 8000000)
Upvotes: 2