Converting equations into constraints
AnsweredThe model contains the following sets and variables:
# sets
u = ['u1', 'u2',...
g = ['g1', 'g2',...
i = ['i1', 'i2',...
k = ['k1', 'k2',...
l = ['l1', 'l2',...
v = ['v1', 'v2',...
# parameters
R_k = {'k1': 3, 'k2': 5, ...
# decision variables
Q_ug = {('u1','g1'): 15, ('u1','g2'): 42, ...
Q_gi = {('g1','i1'): 62, ('g1','i2'): 21, ...
Q_klh = {('k1','l1','h1'): 1000, ('k1','l1','h2'): 1200, ...
Q_kvh = {('k1','v1','h1'): 1500, ('k1','v1','h2'): 2100, ...
Q_hi = {('h1','i1'): 800, ('h1','i2'): 1200, ...
I have the following equations that I want to add to my model as a constraint.
(1)
(2)

I tried to convert them into constraints as follows:
# (1)
m.addConstrs(gp.quicksum(Q_ug[a, b] for a in u) == gp.quicksum(Q_gi[b, c] for c in i) for b in g)
# (2)
m.addConstrs(gp.quicksum(Q_klh[a, b, c] for b in l) + gp.quicksum(Q_kvh[a, b, c] for b in v) == gp.quicksum(Q_hi[c, b] for b in i) * R_k[a] for a in k for c in h)
Am I doing this correctly? The exercises I found were only dealing with simpler equations and less variables, so I am not sure if this is the correct way to convert them using Gurobi.
Appreciate any help! Thanks!
0
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Your constraints look ok. But you also need to define your decision variables as Gurobi variables, for example
Q_ug = m.addVars(u, g, vtype=gp.GRB.INTEGER, name="Q_ug")
You can also write a model file in the end with m.write("Test.lp") and check if the formulation looks like you expected.
0
Post is closed for comments.
Comments
2 comments