我很好地掌握了如何使用LPSolve
进行线性优化问题,但是其中一个方面很困难。我想为多列的总和创建一个约束。例如,我有一个约束,不允许四个特定列中的任何一个大于3。但是,我要求四个列中的任何一个等于3。
工作示例
在此示例中,我将餐食优化为“价值”,同时将其保留在5项以下且成本为40美元以下。我也有四种不同的食物组-肉类,蔬菜,水果,淀粉-我要求每顿饭的任何一组食物不得超过四种,但任何一组食物都必须有3种食物(这是我在哪里我很沮丧)。
以下是获得最后结果的代码,除了最后一个约束:
## Choose 5 food items remaining under $40 and maximizing Value ##
## There can be no more than 3 items from the same group chosen, but **there must be 3 items from at least one group**(??) ##
library(dplyr)
library(lpSolve)
# Constraints
totalItems <- 5
totalCost <- 40
maxAllGroups <- 3
# Setup problem
food <- c('Chicken', 'Beef', 'Lamb', 'Fish', 'Pork', 'Carrot', 'Lettuce', 'Asparagus', 'Beats', 'Broccoli', 'Orange', 'Apple', 'Pear', 'Banana', 'Watermelon', 'Potato', 'Corn', 'Beans', 'Bread', 'Pasta')
group <- c('Meat', 'Meat', 'Meat', 'Meat', 'Meat', 'Veggie', 'Veggie', 'Veggie', 'Veggie', 'Veggie', 'Fruit', 'Fruit', 'Fruit', 'Fruit', 'Fruit', 'Starch', 'Starch', 'Starch', 'Starch', 'Starch')
cost <- round(runif(length(food), 1, 20), 0)
value <- round(runif(length(food), 20, 60), 0)
df <- data.frame(food, group, cost, value, stringsAsFactors = FALSE) %>%
mutate(Total = 1)
# Value to be maximized
Value <- df$value
# Create constraint vectors
ConVec_Cost <- df$cost
ConVec_Items <- df$Total
# Make `Group` dummy variables
groups <- unique(df$group)
ConVec_Groups <- data.frame(row.names = 1:nrow(df))
for(i in 1:length(groups)){
currGroup <- groups[i]
vec <- df %>%
mutate(isGroup = (group == currGroup)*1) %>%
select(isGroup)
colnames(vec) <- currGroup
ConVec_Groups <- cbind(ConVec_Groups, vec)
}
# ConVec_AnyGroupEqual3 <- ???
ConVec_All <- t(cbind(ConVec_Cost, ConVec_Items, ConVec_Groups))
# Create constraint directions
ConDir_Cost <- "<="
ConDir_Items <- "=="
ConDir_Groups <- rep("<=", ncol(ConVec_Groups))
# ConDir_AnyGroupEqual3 <- "=="
ConDir_All <- c(ConDir_Cost, ConDir_Items, ConDir_Groups)
# Create constraint values
ConVal_Cost <- totalCost
ConVal_Items <- totalItems
ConVal_Groups <- rep(maxAllGroups, ncol(ConVec_Groups))
# ConVal_AnyGroupEqual3 <- 1 #1 group should have 3
ConVal_All <- c(ConVal_Cost, ConVal_Items, ConVal_Groups)
# Solve
sol <- lpSolve::lp("max",
objective.in = Value,
const.mat = ConVec_All,
const.dir = ConDir_All,
const.rhs = ConVal_All,
all.bin = TRUE
)
# Solution
df[sol$solution == 1,]
如果我需要一个特定的食物组有3个,这很容易,但是我需要将任何一个组都定为3的事实使得这很难。有没有办法不用求助于LPSolveAPI
(我承认对此知之甚少)来做到这一点?
答案 0 :(得分:0)
我通过将某人的答案从类似问题中剖析出来来解决。基本上,您必须添加5个“虚拟行”(每个食物组一个),其对角线值为-3。然后,您必须添加另一列,其所有值均为0,但最后5行为1(刚添加到矩阵中的行)。您强制新列至少为1,这表示必须必须选择最后5行之一。
由于将选择这些行之一,并且该行在其所在的任何食物组上都添加-3,因此和您有约束条件迫使每个食物组至少为0,然后选择的-3行将强制该食物组列选择3,以便总和> = 0。
新代码
## Choose 5 food items remaining under $40 and maximizing Value ##
## There can be no more than 3 items from the same group chosen, but **there must be 3 items from at least one group**(??) ##
library(dplyr)
library(lpSolve)
# Constraints
totalItems <- 5
totalCost <- 40
minAllGroups <- 0
atLeastNFrom1 <- 3
# Setup problem
food <- c('Chicken', 'Beef', 'Lamb', 'Fish', 'Pork', 'Carrot', 'Lettuce', 'Asparagus', 'Beats', 'Broccoli', 'Orange', 'Apple', 'Pear', 'Banana', 'Watermelon', 'Potato', 'Corn', 'Beans', 'Bread', 'Pasta')
group <- c('Meat', 'Meat', 'Meat', 'Meat', 'Meat', 'Veggie', 'Veggie', 'Veggie', 'Veggie', 'Veggie', 'Fruit', 'Fruit', 'Fruit', 'Fruit', 'Fruit', 'Starch', 'Starch', 'Starch', 'Starch', 'Starch')
cost <- round(runif(length(food), 1, 20), 0)
value <- round(runif(length(food), 20, 60), 0)
df <- data.frame(food, group, cost, value, stringsAsFactors = FALSE) %>%
mutate(Total = 1)
# Create constraint vectors
ConVec_Cost <- df$cost
ConVec_Items <- df$Total
# Make `Group` dummy variables
groups <- unique(df$group)
ConVec_Groups <- data.frame(row.names = 1:nrow(df))
for(i in 1:length(groups)){
currGroup <- groups[i]
vec <- df %>%
mutate(isGroup = (group == currGroup)*1) %>%
select(isGroup)
colnames(vec) <- currGroup
ConVec_Groups <- cbind(ConVec_Groups, vec)
}
# New vector for atleast
ConVec_AtLeastN <- 0
ConVec_All <- cbind(ConVec_Cost, ConVec_Items, ConVec_Groups, ConVec_AtLeastN)
# Add the negative values to the matrix
ConVec_All <- rbind(ConVec_All, c(0,0,-atLeastNFrom1,0,0,0,1))
ConVec_All <- rbind(ConVec_All, c(0,0,0,-atLeastNFrom1,0,0,1))
ConVec_All <- rbind(ConVec_All, c(0,0,0,0,-atLeastNFrom1,0,1))
ConVec_All <- rbind(ConVec_All, c(0,0,0,0,0,-atLeastNFrom1,1))
# Create constraint directions
ConDir_Cost <- "<="
ConDir_Items <- "=="
ConDir_Groups <- rep(">=", ncol(ConVec_Groups))
ConDir_AtLeastN <- ">="
ConDir_All <- c(ConDir_Cost, ConDir_Items, ConDir_Groups, ConDir_AtLeastN)
# Create constraint values
ConVal_Cost <- totalCost
ConVal_Items <- totalItems
ConVal_Groups <- rep(minAllGroups, ncol(ConVec_Groups))
ConVal_AtLeastN <- 1
ConVal_All <- c(ConVal_Cost, ConVal_Items, ConVal_Groups, ConVal_AtLeastN)
# Value to be maximized
Value <- c(df$value, rep(0, nrow(ConVec_All) - length(df$value)))
# Solve
sol <- lpSolve::lp("max",
objective.in = Value,
const.mat = t(ConVec_All),
const.dir = ConDir_All,
const.rhs = ConVal_All,
all.bin = TRUE
)
# Solution
df[sol$solution[1:nrow(df)] == 1,]