7

Trying to add the following constraint in Gurobi/Python: Constraint

Code

N_SERVERS = 5                             #number of servers
C_SERVER = [1]*N_SERVERS
N_NODES = 3                               #number of nodes
C_NODES = [2]*N_NODES    

#create model
m = Model("mip1")

#declare variables
x = m.addVars(len(C_SERVER), vtype=GRB.BINARY, name = "x")
y = m.addVars(len(C_NODES), vtype=GRB.BINARY, name = "y")
m.update()

m.addConstrs(quicksum(x[i]*C_SERVER[i] for i in range(len(x))) + quicksum(y[j]*C_NODES[j] for j in range(len(y))) == quicksum(C_SERVER)

I get the following error: KeyError: 'Missing constraint index'. What is the reason?

PRMoureu
  • 12,817
  • 6
  • 38
  • 48
cherryp
  • 79
  • 1
  • 2
  • 16
    SOLVED. If anyone would have the same problem, I solved it by removing the "s" on m.addConstrs to m.addConstr as it will only generate one constraint. – cherryp May 28 '18 at 09:34
  • Be careful: your parenthesis may be incorrect in the constraint expression. – Greg Glockner Jun 12 '18 at 02:37

0 Answers0