Get startedGet started for free

Logical constraints

1. Logical constraints

In this lesson we will talk about logical constraints. There are times when you might want to model if A happens then B also needs to happen. To talk about logical constraints let us first discus a simple example problem.

2. Example problem

Imagine we are working on a problem for a distribution center. The customer has purchased a number of products to be shipped to them which cannot all fit on the same truck together. Now we are trying to select what products should be loaded and sent together on the first shipment, so that we do not exceed the truck's weight limit. You could model this as an IP problem, where for each product you have a binary decision variable. The variable is set to 1 if the product is selected to be added to the truck, otherwise 0. In this case, you want to select the combination of products that is most profitable. However, the total weight of the truck must be less than 20,000 lbs.

3. Code example

You might code the problem similar to this, were the variables for our product, weight, and profitability are inputs to our model. Next, we initialize the class and define our decision variables as binary. Also, to define the objective and constraint we use lpSum and list comprehension to multiply and sum the profitability and weight by their respective binary variable. Finally, we solve the model. In this example we show code to print value of the decision variables. We will discuss this more in later lessons.

4. Example result

The result of that code would suggest that, on the first truck, we ship only products D, E, and F. Now what if our example problem changed?

5. Logical constraint example 1

We now add an additional constraint. If we choose to ship product E then we can not ship product D or if we ship produce D the we can not also ship product E. This might happen if the two products are an awkward shape where both can not fit on a truck at the same time. Recall the binary variable x for product E and D it will be 1 if the model selects them to be shipped. By adding this constraint we are limiting the model to choose only one or none because shipping both will sum to a number greater than 1.

6. Code example - logical constraint example 1

Adding our new constraint, of XE plus XD is less than or equal to 1, into our over all code would look like this.

7. Logical constraint 1 example result

With the new constraint the model selected to ship product D and not E. Additionally, it also selected to ship product C. Now, what if we modified our original problem again?

8. Logical constraint example 2

We now have a different constraint. If we choose to ship product D then we must also ship product B. This might happen if the two products are designed to work together. From the view of the customer, product D is useless without product B. This time our constraint will look slightly different. By having XD less than and equal to XB, XB must also equal 1 if XD equals 1.

9. Code example - logical constraint example 2

Our new constraint looks like this, and adding it into our initial code would look like this.

10. Logical constraint 2 example result

With the new constraint the model selected to ship product D and B.

11. Other logical constraints

Here are some other logical constraints for reference.

12. Summary

In summary, we reviewed examples of logical constraints. Also, we listed a reference table of other logical constraints. They can show up in a number of different modeling situations and it is useful to be aware of them.

13. Your turn!

Let's try out what we learned.