问题描述
library(lpSolveAPI)
my.lp <- make.lp(nrow = 3,ncol = 2)
set.column(my.lp,1,c(1,2))
set.column(my.lp,2,c(3,0))
set.objfn(my.lp,0))
set.constr.type(my.lp,rep("<=",3))
set.rhs(my.lp,c(4,3))
set.bounds(my.lp,lower = c(-Inf,-Inf),upper = c(Inf,Inf))
> my.lp
Model name:
C1 C2
Minimize 1 0
R1 1 3 <= 4
R2 1 1 <= 2
R3 2 0 <= 3
Kind Std Std
Type Real Real
Upper Inf Inf
Lower -Inf -Inf
在my.lp
中,目标函数设置为最小化。如何将其更改为最大化?通过查看 set.objfn
的帮助页面,我不清楚。
解决方法
使用 lp.control
设置意义(最小化/最大化)。
lp.control(my.lp,sense="max")
my.lp
#Model name:
# C1 C2
#Maximize 1 0
#R1 1 3 <= 4
#R2 1 1 <= 2
#R3 2 0 <= 3
#Kind Std Std
#Type Real Real
#Upper Inf Inf
#Lower -Inf -Inf
solve(my.lp) # returns 0,success
#[1] 0
get.variables(my.lp)
#[1] 1.5 0.0
get.objective(my.lp)
#[1] 1.5