简体   繁体   English

在混合整数线性规划中合并简单的if语句

[英]Incorporating simple if statement in mixed integer linear programming

I'm struggling with a mixed integer programming problem that incorporates an if statement. 我正在努力解决一个包含if语句的混合整数编程问题。 When using PuLP, I keep getting "infeasible" as the solving status, 使用PuLP时,我的解决状态总是“不可行”,

My decision variable is simply a list of binary indicators (either 0 or 1) corresponding to a series of containers and whether or not they are used (0 = not used, 1 = used). 我的决策变量只是与一系列容器相对应的二进制指示符列表(0或1)以及是否使用了它们(0 =未使用,1 =已使用)。

# Instantiate problem to be solved
prob = LpProblem('Test Problem', LpMaximize)

b = []
for id in container_names:
    max_count = 1
    b.append(LpVariable('b_{}'.format(id),
                    lowBound=0,
                    upBound=1,
                    cat='Integer'))

The objective function is simply whether the container is selected (takes on a value of 1) multiplied by points which have been pre-assigned to each container 目标函数只是简单地确定是否选择了容器(取值为1)乘以已预先分配给每个容器的点

prob += lpSum([i * j for i, j in zip(points, b)]), 'Total Points'

The first constraint is the following. 第一个约束如下。 Each container has a combination of items in it. 每个容器中都有物品的组合。 We can't exceed the inventory for any of these items. 我们不能超出任何这些物品的库存。 'container_item_dict' is a dictionary where the keys are container IDs and the values are dictionaries where keys are inventory IDs and the values are counts in the container. “ container_item_dict”是一个字典,其中的键是容器ID,值是字典,其中的键是库存ID,值是容器中的计数。 When I run with just this constraint, the algorithm works and I get good results. 当我仅在此约束条件下运行时,该算法就可以工作,并且可以获得良好的结果。

for j in inventory_names:
    prob += lpSum([b[i]*container_item_dict[container_names[i]][j] for i in container_index]) <= inventory_in_stock_dict[j]

I'm trying to add an additional constraint but can't figure it out. 我正在尝试添加其他约束,但无法弄清楚。 I have another list binary indicators named "must_haves." 我还有一个名为“ must_haves”的二进制指示符列表。 "must_haves" is the same length as "container_names" and each value corresponds to a container. “ must_haves”的长度与“ container_names”的长度相同,并且每个值对应一个容器。 If an element of "must_haves" is 1, then that container must be selected in the solution. 如果“ must_haves”的元素为1,则必须在解决方案中选择该容器。 If an element of "must_haves" is 0, the corresponding container can either be selected or not selected. 如果元素“ must_haves”为0,则可以选择或不选择相应的容器。

How do I code up this if statement constraint? 如何编写此if语句约束?

I believe this is the correct approach: 我相信这是正确的方法:

for i in container_index:
    prob += b[i] >= must_haves[i]

This way, if must_haves equals 1, the container must be selected. 这样,如果must_haves等于1,则必须选择容器。 If must_haves equals 0, the container can be selected or not. 如果must_haves等于0,则可以选择容器或不选择容器。

I did this originally and got an error. 我最初是这样做的,但出现了错误。 I now think this code is correct and it returned "infeasible" because it was simply infeasible given the amount of container items I have. 我现在认为这段代码是正确的,它返回“不可行”,因为考虑到我拥有的集装箱数量,这根本是不可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM