Skip to content

Instantly share code, notes, and snippets.

@WillianFuks
Created November 12, 2021 21:20
Show Gist options
  • Save WillianFuks/f8f7431e26abb5777d0c8d601ee57be9 to your computer and use it in GitHub Desktop.
Save WillianFuks/f8f7431e26abb5777d0c8d601ee57be9 to your computer and use it in GitHub Desktop.
import numpy as np
import cvxpy as cx
# First the data that sets the problem
container_costs = np.array([200, 300, 400, 500])
shippers_costs = np.array([100, 130])
shippers_spaces = np.array([2, 1])
# Main variable to be optimized
x_shippers = cx.Variable((4, 2), boolean=True, name='shippers')
# The constraints of the problem
constraint0 = cx.sum(x_shippers, axis=1) <= 1
constraint1 = cx.sum(x_shippers, axis=0) <= shippers_spaces
# Cost function. It uses `x_shippers` to compute the cost.
cost = cx.sum(x_shippers @ shippers_costs.T) + container_costs @ (1 - cx.sum(x_shippers, axis=1))
# Run optimization
objective = cx.Minimize(cost)
problem = cx.Problem(objective, [constraint0, constraint1])
problem.solve()
print(problem.solution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment