How to add conditionals in constraints?
回答済みI have an objective function that has a if
conditional in it. I am having trouble implementing it in Gurobi Python.
Background
There are s
suppliers and p
plants. x[s][p]
is a variable that indicates the number of items that flow from supplier-x
to plant-p
. c[s][p]
indicates the cost of supplying one item from a supplier to a center.
Additionally, there is a fixed cost t[s]
for each supplier. If a supplier supplies to any center, this fixed cost is incurred (this fixed cost does not depend on the number of items).
I want to minimize the cost using an objective function like -
The first part is easy to model like sum(x[s, p] * spc[s, p] for s in range(num_suppliers) for p in range(num_center))
.
For the second term, how can I model it? (The second part basically means that add the supplier's fixed cost only if the supplier is actually supplier anything to any plant).
Edit
This is the code I have now. Note: This does not produce the minimum value -
from gurobipy import *
supplier_capacity = [
5, 10
]
plant_demand = [
2, 4
]
num_suppliers = len(supplier_capacity)
num_plants = len(plant_demand)
t = [
100, 1
]
c = {
(0, 0): 1,
(0, 1): 4,
(1, 0): 4,
(1, 1): 2
}
x = {} # flow between each supplier to plant
m = Model()
xl = [(s, p) for s in range(num_suppliers) for p in range(num_plants)]
x = m.addVars(xl, vtype=GRB.INTEGER, lb=0, name='flow')
for s in range(num_suppliers):
m.addConstr(x.sum(s, '*') <= supplier_capacity[s])
for p in range(num_plants):
m.addConstr(x.sum('*', p) >= plant_demand[p])
m.setObjective(
(
sum(x[s, p] * c[s, p] for s in range(num_suppliers) for p in range(num_plants)) +
sum(t[s] for s in range(num_suppliers) if x.sum(s, '*') >= 0)
), GRB.MINIMIZE
)
m.update()
m.optimize()
if m.status == GRB.Status.OPTIMAL:
print('==== RESULTS ====')
print('Min Cost: {}'.format(m.ObjVal))
for v in m.getVars():
print('{} = {}'.format(v.VarName, v.X))
else:
print('Infeasible model')
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Siddharth,
You could add a binary variable Xb and a continuous variable Xc and add the general constraints:
Xb == 1 >> sum(X{s,p} >= 1)
Xb == 0 >> sum(X{s,p} == 0)
Xb == 1 >> Xc == sum(t{s})
Xb == 0 >> Xc == 0
and then add Xc to the objective function
Daniel
0 -
Hi Daniel,
I am alsi trying to add similar constraints to my optimization problem:
x = IPmod.addVars(pairs, vtype=GRB.BINARY, name="x")
y = IPmod.addVars(pairs, vtype=GRB.BINARY, name="y")IPmod.setObjective(quicksum(w[j]*y[i,j] for (i,j) in pairs), GRB.MINIMIZE)
for (i,j) in pairs:
IPmod.addConstrs((y[i,j]==1)>>(p[i]*x[i,j]+p[j]-d[j]>0), name="Unit Penalty"+str(i)+","+str(j))
IPmod.addConstrs((y[i,j]==0)>>(p[i]*x[i,j]+p[j]-d[j]<=0), name="Unit Penalty"+str(i)+","+str(j))I get the following error msg:
File "linexpr.pxi", line 434, in gurobipy.LinExpr.__richcmp__NotImplementedError
0 -
In the line
IPmod.addConstrs((y[i,j]==1)>>(p[i]*x[i,j]+p[j]-d[j]>0), name="Unit Penalty"+str(i)+","+str(j))
try ">=0" instead of ">0". (Strict inequalities are not supported.)
0
投稿コメントは受け付けていません。
コメント
4件のコメント