I'm setting up a linear optimization using Pulp in Python. I would like to set up a conditional constrain to the problem.
Like, I want to Maximize the profit of a factory. For the cost of the material, the first 1000 units cost $5 each, any more unit cost $3. For example, if the factory order 1100 units, total cost will be 1000*5+100*3. I have a list of material: material_list
, a dictionary of benchmark for the materials: benchmark_dic={material_a: 1000, material_b:2000 ....}
, a dictionary of the price if order loss than benchmark :price_A_dic, and also a dictionary of the price if you order more than benchark:price_B_dic.
Here is my code:
x=pulp.LpVariable.dicts('x',material_list,lowBound=0 , cat='Integer')
New_cost_dic=pd.Series(0,index=dat.index).to_dict()
for seg in material_list:
if x[seg]>benchmark_dic[seg]:
New_cost_dic[seg]=(x[seg]-benchmark_dic[seg])*price_b_dic[seg]+benchmark[seg]*price_A_dic[seg]
else:
New_cost_DIC[seg]=x[seg]*price_A_dic[seg]
I also have a similar calculation for sales. I can get a outcome from this but I don't know if I did it right. When I get a final result of how many units for each material I tried to get the total cost and total sales using the same calculation, but the profit I got by (total sales - total cost) is not equal to the Max profit I got from pulp.value(prob.objective).
How should I code for this conditional constrains or conditional function.