如何编码。。选择Y中的最佳X选项(最小值或最大值)。。在混合整数线性规划中使用R和lpSolve?
我正在尝试解决一个与使用二进制约束进行优化相关的练习。下面是问题的描述 对于这个问题,我使用了如何编码。。选择Y中的最佳X选项(最小值或最大值)。。在混合整数线性规划中使用R和lpSolve?,r,optimization,lpsolve,R,Optimization,Lpsolve,我正在尝试解决一个与使用二进制约束进行优化相关的练习。下面是问题的描述 对于这个问题,我使用了R和lpSolveAPI——到目前为止,我成功地将问题转化为约束列表,并为问题建立了正确的目标函数,但是我的程序没有产生正确的输出,因为我放置了三个Y变量(yE、yT和yN)到我的目标函数中。我的目标函数不应包含三个尾随的0(请参见上图中目标函数的定义) 我的问题是,如何定义变量y,使它们是二进制的,并且仅用作约束的一部分(因此它们不会出现在目标函数中)? # SELECT FROM .... req
R
和lpSolveAPI
——到目前为止,我成功地将问题转化为约束列表,并为问题建立了正确的目标函数,但是我的程序没有产生正确的输出,因为我放置了三个Y
变量(yE
、yT
和yN
)到我的目标函数中。我的目标函数不应包含三个尾随的0
(请参见上图中目标函数的定义)
我的问题是,如何定义变量y
,使它们是二进制的,并且仅用作约束的一部分(因此它们不会出现在目标函数中)?
# SELECT FROM ....
require(lpSolveAPI)
# Set the decision variables
obj <- c(21, 22.5, 22.5, 24.5, 23, 25.5, 0, 0, 0)
# Set the constrains parameters
# EG,EK,TG,TK,NG,NK,yE,yT,yN
LHS <- matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0, 0,
1, 1, 0, 0, 0, 0, -425, 0, 0,
0, 0, 1, 1, 0, 0, 0, -400, 0,
0, 0, 0, 0, 1, 1, 0, 0, -750,
0, 0, 0, 0, 0, 0, 1, 1, 1), nrow=9, byrow = TRUE)
RHS <- c(425, 400, 750, 550, 450, 0, 0, 0, 2)
constranints_direction <- c("<=", "<=", "<=", ">=", ">=", "<=", "<=", "<=", "<=")
# Set 9 constraints and 9 decision variables ==> THERE SHOULD BE ONLY 6 !!!
lprec <- make.lp(nrow = 9, ncol = 9)
# Set the type of problem we are trying to solve
lp.control(lprec, sense="min")
set.type(lprec, 7:9, c("binary"))
set.objfn(lprec, obj)
add.constraint(lprec, LHS[1, ], constranints_direction[[1]], RHS[1])
add.constraint(lprec, LHS[2, ], constranints_direction[[2]], RHS[2])
add.constraint(lprec, LHS[3, ], constranints_direction[[3]], RHS[3])
add.constraint(lprec, LHS[4, ], constranints_direction[[4]], RHS[4])
add.constraint(lprec, LHS[5, ], constranints_direction[[5]], RHS[5])
add.constraint(lprec, LHS[6, ], constranints_direction[[6]], RHS[6])
add.constraint(lprec, LHS[7, ], constranints_direction[[7]], RHS[7])
add.constraint(lprec, LHS[8, ], constranints_direction[[8]], RHS[8])
add.constraint(lprec, LHS[9, ], constranints_direction[[9]], RHS[9])
# Display the LPsolve matrix
lprec
get.type(lprec)
# Solve problem
solve(lprec)
# Get the decision variables values
get.variables(lprec)
# Get the value of the objective function
get.objective(lprec)
但是,对于相同的变量分配,它必须产生22850.50。如果要运行:
obj <- c(21, 22.5, 22.5, 24.5, 23, 25.5)
x <- c(0, 425, 0, 0, 550, 25)
obj %*% x
i、 e.此分配的目标为22850。如果您要运行:
obj <- c(21, 22.5, 22.5, 24.5, 23, 25.5)
x <- c(0, 425, 0, 0, 550, 25)
obj %*% x
i、 e.这一分配的目标是22850
[,1]
[1,] 22850