我有以下使用lpSolveAPI
在R中开发的基本约束优化问题。
我想知道是否有办法知道哪种约束条件在最佳解决方案上具有约束力。在SAS中,我们可以访问特定约束的status
来获取信息
require(lpSolveAPI)
lprec <- make.lp(0, 2)
lp.control(lprec, sense="min")
set.objfn(lprec, c(9.3, 8.4))
add.constraint(lprec, c(1, 1), ">=", 50)
add.constraint(lprec, c(-.21, .3 ), ">=", 0)
add.constraint(lprec, c(-.03, .01), "<=", 0)
lprec
solve(lprec)
variables <- get.variables(lprec)
total_profit <- get.objective(lprec)
variables
total_profit
或者使用lpSolve
# using lpSolve
# install.packages("lpSolve")
require(lpSolve)
# Set the coefficients of the decision variables -> C
C <- c(9.3, 8.4)
# Create constraint martix A
A <- matrix(c(1, 1,
-.21, .3,
-.03, .01), nrow=3, byrow = TRUE)
B <- c(50, 0, 0)
constranints_direction <- c(">=", ">=", "<=")
# Find the optimal solution
optimum <- lp(direction="min",
objective.in = C,
const.mat = A,
const.dir = constranints_direction,
const.rhs = B,
all.int = F)
# Print status: 0 = success, 2 = no feasible solution
print(optimum$status)
# Display the optimum values for c and s
best_sol <- optimum$solution
names(best_sol) <- c("c", "s")
print(best_sol)
print(paste("Total cost: ", optimum$objval, sep=""))