... cvxpy one.

There's loads of usecases in logistics where you'd like to optimise a system while being subjected to constraints. In this series of videos we'd like to highlight a tool that can handle a subset of these problems called cvxpy.


This is the new problem definition with all the constraints;

import cvxpy as cp
import pandas as pd

df = pd.read_csv("/path/to/stigler.csv")
price = df['price_cents'].values

constraint_dict = {
    'protein_g': 70, 'calcium_g': 0.8, 'iron_mg': 12, 
    'vitamin_a_iu': 5, 'calories': 2000,
    'vitamin_b1_mg': 1.8, 'vitamin_b2_mg': 2.7, 
    'niacin_mg': 18, 'vitamin_c_mg': 75

x = cp.Variable(price.shape[0])
objective = cp.Minimize(cp.sum(price*x))

constraints = [x >= 0]
for key, value in constraint_dict.items():
    constraints.append(x * df[key] >= value)

prob = cp.Problem(objective, constraints)

This is the pandas code at the bottom;

.assign(assignment = x.value)
.sort_values(['assignment'], ascending=False)

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.