Skip to main content

Multi-index variable in gurobipy-pandas

Answered

Comments

3 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    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_product

    import 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
  • Koen Timmermans
    Gurobi-versary
    Conversationalist
    Curious

    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
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Koen,

    No need for the .to_list() conversion, but yes that should work.

    - Riley

    0

Please sign in to leave a comment.