Strike
Strike

Reputation: 95

Pulp Solver constraint

I am create a LP Solver using PULP to get minimize the cost. I wanted to add an upper bound value to skip some of the results.

costs = dict(zip(Variety_items,tempdf['cost']))
Bright = dict(zip(Variety_items,tempdf['Bright']))
Colour = dict(zip(Variety_items,tempdf['colour']))
Thickness = dict(zip(Variety_items,tempdf['Thickness']))

prob = LpProblem("Problem", LpMinimize)

Variety_items_var = LpVariable.dicts("B",Variety_items,0)
prob += lpSum([costs[i]*Variety_items_var[i] for i in Variety_items])
prob += lpSum([Variety_items_var[i] for i in Variety_items]) == 1, "PercentagesSum"
prob += lpSum([Bright[i] * Variety_items_var[i] for i in Variety_items]) == 40, "C1"
prob += lpSum([Colour[i] * Variety_items_var[i] for i in Variety_items]) == 90, "C2"
prob += lpSum([Thickness[i] * Variety_items_var[i] for i in Variety_items]) <= 15, "C3"

prob.solve()

Below is the result combination with 101.40471619 as the best cost. Help required is : minimum should be 0.07 and I am getting 0.03 in the result which I would like to add as constraint

B_XXX_1 = 0.0
B_XXX_14 = 0.0
B_XXX_16 = 0.21351179
B_XXX_2 = 0.0
B_XXX_3 = 0.14404079
B_XXX_4 = 0.0
B_XXX_5 = 0.0
B_XXX_6 = 0.0
B_DX_15 = 0.6042065
B_DX_17 = 0.0
B_DX_7 = 0.0
B_DX_8 = 0.0
B_DP_11 = 0.0
B_DP_12 = 0.0
B_DP_13 = 0.038240918
B_TX_10 = 0.0
B_TX_9 = 0.0

Note : I have not attached full code. its Pandas DF to dict converted.

Upvotes: 0

Views: 473

Answers (1)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16724

Say you want x to be 0 or between the constants L and U. Introduce a binary variable d and the constraint

  d*L <= x <= d*U

(Actually two constraints).

This is sometimes called a semi-continuous variable. Some solvers and modeling tools support this directly as a variable type, but in others, like PuLP, you need to use a binary variable to model this.

Upvotes: 3

Related Questions