Gurobi as a function
AnsweredDear Gurobi Professional,
I currently have a problem regarding incorporation of gurobi as a function.
The intended purpose is that I can eventually extract the solution and plot it using plotly in a single script
The code:
def UPMSet(jobs,machines,workers,activities,oprttime,machining,moving,B):
from gurobipy import GRB
m = Model()
x = {}
for k in workers:
for h in machines:
for a in activities:
for j in range (0, len(jobs)+1):
for i in machines:
for b in activities:
for l in jobs:
x[k,h,a,j,i,b,l] = m.addVar(vtype=GRB.BINARY,name='x_%s_%s_%s_%s_%s_%s_%s' % (k,h,a,j,i,b,l))
However, there's an error saying:
name 'Model' is not defined
Apparently the m = Model() is not defined. I recon there's a problem in the first 2 line of the code but Im not really sure.
I can normally run the gurobi code on its own but when it comes to include it as a function it fails me.
I'm quite new to gurobi and some of my explanation might be a wrong and a bit confusing but thats the broad idea.
Could you help me sort this problem out?
Thank you.
-
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. -
Hi Akbar,
The issue here relates to programming in Python, in general, rather than being specific to Gurobi. You can find a good explanation in the YouTube video: https://www.youtube.com/watch?v=CqvZ3vGoGs0
So, "Model" is a thing which comes from the gurobipy package, so you need to tell Python this information. You can achieve this with the following alternatives:
from gurobipy import Model
m = Model()import gurobipy
m = gurobipy.Model()import gurobipy as gp
m = gp.Model()Note that the last option is the preferred style.
I also suggest moving your import statements to the top of your file, and taking advantage of our Model.addVars function:
import gurobipy as gp
from gurobipy import GRB
def UPMSet(jobs,machines,workers,activities,oprttime,machining,moving,B):
m = gp.Model()
x = m.addVars(
workers, machines, activities, len(jobs), machines, activities, jobs,
vtype="B",
)
...- Riley
0
Post is closed for comments.
Comments
2 comments