I have the below code that tries to assign the best product (a or b) to each factory (1,2,3,4 or 5).
How do I set constraints/bounds so that it only uses the integer values 0 or 1?
e.g I have set x0 = [1,0,1,1,0] and the solution for this sample data is [0,0,0,0,0]
Also, Is this even this correct approach to solve this problem? I have also tried pulp,
but it does not seem to support the dynamic emission charges, as these are calculated in aggregate not at a factory level.
import numpy as np
from scipy.optimize import minimize
data = {
'1': (1.2,),
'2': (1.0,),
'3': (1.7,),
'4': (1.8,),
'5': (1.6,)
}
unit_cost_b = 8
limit_b = 100
echarge_b = 0.004
unit_cost_a = 5
limit_a = 60
echarge_a = 0.007
def objective(x):
x_a = [data[str(i+1)][0] for i in range(len(data)) if x[i] == 0]
x_b = [data[str(i+1)][0] for i in range(len(data)) if x[i] == 1]
total_unit_cost = (len(x_a) * unit_cost_a) + (len(x_b) * unit_cost_b)
total_usage_a = np.sum(x_a)
total_usage_b = np.sum(x_b)
emission_cost_a = max(total_usage_a - (len(x_a) * limit_a), 0) * echarge_a * 31
emission_cost_b = max(total_usage_b - (len(x_b) * limit_b), 0) * echarge_b * 31
return total_unit_cost + emission_cost_a + emission_cost_b
bounds = [(0, 1)] * len(data)
eq_cons = {'type': 'eq', 'fun': lambda x: np.sum(x) - 5}
x0 = [1,0,1,1,0]
print(x0)
result = minimize(objective, x0, method='SLSQP', bounds=bounds, constraints=[eq_cons])