メインコンテンツへスキップ

Multi-index variable in gurobipy-pandas

回答済み

コメント

4件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Riley Clement
    • 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

    Hi Koen,

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

    - Riley

    0

投稿コメントは受け付けていません。