Tyler Johnson
Tyler Johnson

Reputation: 1

Python Gurobi setting up objective function

I am using the following code to define the objective function:

objective = GBR.QUICKSUM(x[i,j] * c[i][j] for i in range(50) for j in range(50))
m.setObjective(objective)

However, it gives me an error stating that name 'GBR' is not defined.

I have imported gurobipy as *.

I had no issue with defining the variables:

for i in range(50):
  for j in range(50):

    x[(i,j)] = m.addVar(lb=0, vtype=GRB.INTEGER, name='x_'+str(i)+'_'+str(j))

and the cost function c[i][j] is a list of costs for each link X[i,j]

How should I define the objective for it to work?

Upvotes: 0

Views: 1420

Answers (2)

somedude
somedude

Reputation: 33

'GBR' is indeed not defined, try GRB

Upvotes: 2

joni
joni

Reputation: 7157

Gurobi's quicksum is a global function. That means after

from gurobipy import *
m = Model()             # Creates a Gurobi Model object.
# ... create your variables etc here

you can use it with

m.setObjective(quicksum(x[i,j] * c[i][j] for i in range(50) for j in range(50)))

By the way: you could use the addVars() method instead of the two nested for-loops to create your variables.

Upvotes: 1

Related Questions