LPSolve - 设置多列总和的约束

问题描述 投票:0回答:1

我对使用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(我承认知之甚少)?

r linear-programming lpsolve
1个回答
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,]
© www.soinside.com 2019 - 2024. All rights reserved.