How to minimize the SUM of all flows xij
回答済みHi,
How do I set the objective function to minimize the SUM of all flows xij?
My model is below,
import itertools
n=["a","b","c","d","e","f","g"]
arcs=list(itertools.combinations(n,2))
#model and variables
m = gp.Model('min-cost')
# create variables for arcs
x = m.addVars(arcs, vtype=GRB.CONTINUOUS, name='x')
m.update()
m.printStats()
x
#constraints
m.addConstr(x[arcs[0]]+x[arcs[1]]+x[arcs[2]]+x[arcs[3]]+x[arcs[4]]+x[arcs[5]] == 62,"c0")
m.addConstr(-(x[arcs[0]])+x[arcs[6]]+x[arcs[7]]+x[arcs[8]]+x[arcs[9]]+x[arcs[10]] == -117,"c1")
m.addConstr(-(x[arcs[1]])-(x[arcs[6]])+x[arcs[11]]+x[arcs[12]]+x[arcs[13]]+x[arcs[14]] == 81,"c2")
m.addConstr(-(x[arcs[2]])-(x[arcs[7]])-(x[arcs[11]])+x[arcs[15]]+x[arcs[16]]+x[arcs[17]] == 145,"c3")
m.addConstr(-(x[arcs[3]])-(x[arcs[8]])-(x[arcs[12]])-(x[arcs[15]])+x[arcs[18]]+x[arcs[19]] == -125,"c4")
m.addConstr(-(x[arcs[4]])-(x[arcs[9]])-(x[arcs[13]])-(x[arcs[16]])-(x[arcs[18]])+x[arcs[20]] == 105,"c5")
m.addConstr(-(x[arcs[5]])-(x[arcs[10]])-(x[arcs[14]])-(x[arcs[17]])-(x[arcs[19]])-(x[arcs[20]]) == -148,"c6")
m.setObjective(3*gp.quicksum(x),GRB.MINIMIZE) doesn't work.
I want my objective function to minimize 3* the sum of the flows of all arcs
something like 3*sum(xij). Any help on the syntax of this?
Thank you!
0
-
正式なコメント
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 Kaiwen,
The quicksum function requires a data list as input and not just a Gurobi tupledict. Using
m.setObjective(3*gp.quicksum(x[a] for a in arcs), GRB.MINIMIZE)
should solve your issue.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント