Python Tutorial : Using lpSum

The instructor shows why manually adding variables breaks down as a bakery grows, then introduces PuLP's lpSum function paired with list comprehensions to build scalable objective functions. Multiply each cake's profit by its variable, sum the results, and adding new products just means extending your lists.
Chapters
Welcome back. In this lesson we will learn about LP sum. In a previous lesson, we worked through a resource scheduling example at a bakery. In that example, there were two decision variables, one for each product they sold. Bakeries often sell more than two products. What if the bakery sold six products? Our code's decision variables may look like this. Think about it. What if they sold more? Coding objective function or constraints to sum the different variables together would be nearly impossible. If our model contained hundreds or thousands of variables, we need a method that scales. In later lessons we will show how to quickly define the decision variables at scale. But for now, we assume that they are defined and we want to sum many of them together. The PULP framework provides a function that does just that. LPSUM sums a list of linear expressions. Its only input is a list of expressions to sum. Therefore, coding this objective function in pulp using the addition symbol is equivalent to defining this objective function using LP sum. Often, LP sum is used with Python's list comprehension. Going back to our working example, imagine that we have defined a list of different types of cakes at the bakery. In the code example shown here, the Python list is called cake type. Additionally, in the example there are two dictionaries. One dictionary is for the amount of profit earned from each cake. The other dictionary contains the pulp LP variables defined earlier. Now, with Python's list comprehension, we create a list of pulp LP variables multiplied by the profit for each cake. Finally, we can sum all of these variables together to define the objective function by using lpsum. This structure makes it easy to scale the number of variables. To increase the number of types of cakes, we will only need to extend our list and dictionary variables. In summary, as our model becomes more complex, we need a method of summing many variables. The PULP framework contains the LP sum function, which does just that. Using this function in combination with Python's list comprehension creates a structure that scales. In upcoming lessons, we will learn how to create pulp LP variables at scale, which will allow us to work on larger problems. Awesome. It's time for you to try your.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







