why does summing in cvxpy will occur broadcasting error

43 Views Asked by At

I having a problem of value error with my code. It keeps showing cannot broadcast dimensions (5,)(5,3). I am still learning how to use cvxpy so I cannot find the way to solve it. The problem is at "transportation_costs = cp.sum(cp.multiply(d, c) * x)"

import cvxpy as cp

# Given data
d = [80, 270, 250, 160, 180]
c = [[4, 5, 6, 8, 10],
     [6, 4, 3, 5, 8],
     [9, 7, 6, 3, 4]]
f = [1000, 1000, 1000]
M = [500, 500, 500]

# Variables
x = cp.Variable((3, 5), boolean=True)
y = cp.Variable(3, boolean=True)

# Objective
transportation_costs = cp.sum(cp.multiply(d, c) * x)
activation_costs = f @ y
cost = transportation_costs + activation_costs
# Constraints
constraints = [cp.sum(x, axis=0) == 1, 
               x <= cp.reshape(y, (3, -1)),
               cp.diag(d) @ x <= M * y]

# Problem
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve()

print(f"Optimal cost: ${prob.value:.2f}")
print("x values:")
print(x.value)
print("y values:")
print(y.value)

# Disaggregated constraints
disaggregated_constraints = [d[i]*x[j,i] <= M[j]*y[j] for i in range(5) for j in range(3)]

# Problem with Disaggregated Constraints
prob_disaggregated = cp.Problem(cp.Minimize(cost), constraints[:-1] + disaggregated_constraints)
prob_disaggregated.solve()

print(f"Optimal cost with disaggregated constraints: ${prob_disaggregated.value:.2f}")
print("x values with disaggregated constraints:")
print(x.value)
print("y values with disaggregated constraints:")
print(y.value)`
0

There are 0 best solutions below