0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Assuming must_haves is data (not a decision variable), you may want to skip the cases where must_haves[i]==0
Yes, what you've done there seems right. Don't forget to mark you answer with a tick if it answers your question! Also FYI you can specify a binary variable type - cat='Binary' rather than integer with [0,1] bounds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.