简体   繁体   中英

Incorporating simple if statement in mixed integer linear programming

I'm struggling with a mixed integer programming problem that incorporates an if statement. When using PuLP, I keep getting "infeasible" as the solving status,

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).

# 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

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. 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" is the same length as "container_names" and each value corresponds to a container. If an element of "must_haves" is 1, then that container must be selected in the solution. If an element of "must_haves" is 0, the corresponding container can either be selected or not selected.

How do I code up this if statement constraint?

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. If must_haves equals 0, the container can be selected or not.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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