Multi-index variable in gurobipy-pandas
回答済みHi!
I am trying to define a binary decision variable \(x_{ijk}\) using gurobipy-pandas. I am working with two Pandas DataFrames: 1.) task_list, and 2.) vehicle_data. \(x_{ijk}\) is equal to 1 if vehicle \(k\) is driving from task \(i\) to task \(j\). Indices \(i\) and \(j\) therefore result from "task_list" and index \(k\) from "vehicle_data". \(i\) and \(j\) have the same range but \(k\) is much smaller.
To define x in Python, I now have the following code in place:
x = gppd.add_vars(model, ???, vtype=GRB.BINARY, name="x")
Unfortunately I don't know how to define "pandas_obj" on the position of the "???" in the line of code above. I found an example using \(\texttt{pd.MultiIndex( )}\) online, but I am not sure whether that is the way to go.
Hopefully someone can help me out here. Thank you in advance!
Best regards,
Koen Timmermans
-
正式なコメント
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 Koen,
The second argument to add_vars is the index of the variables. We want the variable to be indexed by i,j,k so we will want a multi-index. Let's say A is the set of tasks, and K is the set of vehicles. You can create the required multi-index using pandas.MultiIndex.from_productimport gurobipy as gp
from gurobipy import GRB
import gurobipy_pandas as gppd
import numpy as np
import pandas as pd
A = (1,2,3)
K = (1,2)
model = gp.Model()
x = gppd.add_vars(model, pd.MultiIndex.from_product((A,A,K), names=("i", "j", "k")), vtype=GRB.BINARY, name="x")
x is then a pandas.Series of gurobi variables:j i k
1 1 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
2 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
3 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
2 1 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
2 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
3 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
3 1 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
2 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
3 1 <gurobi.Var *Awaiting Model Update*>
2 <gurobi.Var *Awaiting Model Update*>
Name: x, dtype: object- Riley
0 -
Hi Riley,
Thank you for the quick and elaborate response!
To get to the set A and K that you mention in your example, the index columns of my Pandas DataFrames (task_list and vehicle_data) can be used right?
import pandas as pd
task_list = pd.DataFrame()
vehicle_data = pd.DataFrame()
A = task_list.index.to_list()
K = vehicle_data.index.to_list()Other than that, it is clear to me now. Thanks a lot!
Best regards,
Koen Timmermans
0 -
Hi Koen,
No need for the .to_list() conversion, but yes that should work.
- Riley
0
投稿コメントは受け付けていません。
コメント
4件のコメント