Created
November 12, 2021 21:20
-
-
Save WillianFuks/f8f7431e26abb5777d0c8d601ee57be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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