define the model as a function
回答済みHi,
I have met a problem when I use Gurobi-Python
I wrote my model as a function such that:
def VRP1():
m.gp.Model()
# add variables
x=(CUSTOMER,CUSTOMERS,VEHICLES)
y=.....
# add constraints
......
However, when I call this function from another file,
there is an error saying "CUSTOMERS" is not defined.
Could you please help me solve this problem? Can I define all my parameters and sets outside my model function?
Thanks
-
You can define your model in a function. You have to make sure that all data is available inside the function. This means that if you need the value of the parameter \(\texttt{CUSTOMERS}\), then you have to either pass it to function VRP1 as an argument, i.e.,
def VRP1(CUSTOMERS):
[...]or you read you data from a file to get access to it.
Best regards,
Jaromił0 -
Hi Jaromił,
Thanks for your response. Are there any methods that allow me to include two or three models that share the same sets?
For example, what I want such as:
CUSTOMERS = [i for i in range(1,20)]
def VRP1():
def VRP2():
Thanks!
0 -
Are there any methods that allow me to include two or three models that share the same sets?
What exactly do you mean by share the same sets?
You can write multiple functions \(\texttt{VRP1, VRP2, ...}\) and pass your \(\texttt{CUSTOMERS}\) list as argument to each one of those.
def VRP1(CUSTOMERS):
[...]
def VRP2(CUSTOMERS):
[...]
CUSTOMERS = [i for i in range(1,20)]
VRP1(CUSTOMERS)
VRP2(CUSTOMERS)Best regards,
Jaromił0
サインインしてコメントを残してください。
コメント
3件のコメント