Reputation: 13
I am trying to add alloys to steel in order to bring carbon content of steel to a certain range, at minimal cost. But one limitation is that in real life, the machine can only add a minimum of 50kgs of an alloy. So if we are adding a certain alloy, then it can be either 50/60/70 kgs etc. or 0kgs if we are not adding that particular alloy. How would I add a constraint for the same?
Thanks in advance!
Below is the function I've written:
def optimizer_pd(test):
# declare problem
prob = LpProblem("Minimize Alloy Cost",LpMinimize)
# percentage of carbon in each alloy
percs = ele_percs['carbon']
# alloy_vars is a list of all possible alloys
# constraints
prob += lpSum([percs[i] * alloy_vars[i] for i in alloys]) >= minimum_carbon
prob += lpSum([percs[i] * alloy_vars[i] for i in alloys]) <= maximum_carbon
# objective function
prob += lpSum([costs[i] * alloy_vars[i] for i in alloys])
# solving
sol = prob.solve()
# results
var_dict = {}
for var in prob.variables():
var_dict[var.name] = var.value()
return var_dict
Upvotes: 1
Views: 500
Reputation: 11883
Welcome to the site.
In the future, you'll get better answers if you present a minimum reproducible example for what you are trying to do. But, it is clear enough from what you post.
So, you will need to introduce an extra helper or "indicator" binary variable, indexed by your alloys to do this. This yes/no variable indicates the commitment to use at least the minimum amount of the alloy. (You essentially need to break your requirement into 2 variables....)
Then you will need to use a "big M" constraint on the amount to use (or just use the max value). In pseudocode:
use[alloy] ∈ {0,1}
amount[alloy] ∈ non-negative reals
min[alloy], max[alloy] are fixed min/max parameters
Minimum use constraint:
amount[alloy] >= use[alloy] * min[alloy] for each alloy
Maximum use constraint:
amount[alloy] <= use[alloy] * max[alloy] (or big M) for each alloy
Plug in a few numbers to ensure you "believe it" :)
Upvotes: 2