How to Code a parameter in Gurobi/python interface
AnsweredI have a model with some parameters, variables, and constraints. I have a problem mainly in coding the f parameter that required in a specific set of constraints. Indeed, this shown in the image below constraint # 6 causing me the big problem in the whole code.
I tried to code the constraints but failed to code the f parameter, any idea or guide? thank you in advance......
```
n = 5
R = [i for i in range(1,n+1)]
Cr = {1: C[0:3], 2 : C[3:6],3: C[6:9], 4: C[9:12], 5:C[12:15] }
N = list(range(0,27))
# variables related to constraints # 6
for r in R:
yr = m.addVars(N,N, vtype=GRB.BINARY)
for r in R:
z = m.addVars(Cr[r],Cr[r],vtype=GRB.BINARY)
m.update()
# Constraint #6
for r in R:
m.addConstrs(((quicksum(yr[i,j] for j in N if j !=i) + quicksum(z [i,j] for j in Cr[r] if j !=i)- quicksum(yr[j,i] for j in N if j !=i)- quicksum(z [j,i] for j in Cr[r]if j !=i ) == f[i]) for i in Cr[r] ),'6')
```


0
-
Official comment
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?. -
I have not looked at the last line in detail, however note the following:
- The first for loop does not use the variable r, therefore it just creates the same 2D-array of variables over and over again.
- You do not need to call m.update() at each step of the second for loop (in fact, you don't need to call it at all)
- The last line needs to be indented, otherwise it is not valid Python syntax.
- In your minimum working example, you do not describe C nor f, so it is not possible to make sure that the implementation of eq. (6) is correct.
0
Post is closed for comments.
Comments
2 comments