Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

create variables in docplex

  • 1.  create variables in docplex

    Posted Tue February 12, 2019 03:36 AM

    Originally posted by: ShaCplex


     Hi sir,

    I have been working in OPL and Java API and now trying to switch to python cplex.

    I have a dataframe named odRoutes which has many columns (head() of the dataframe is attached).

    I want to create a continuous variable for each of the rows in the dataframe OdRoutes, so I wrote;

     

    mdl = Model("LP")

    varODRoute = mdl.continuous_var_dict(valid_ODRoutes_df,name="var_route")

    by doing this I am getting the variable names as var_route_0 , var_route_1, etc

    the odRoutes dataframe has columns named 'name','productCategory' etc. I want to create variables and name it as var_route_{name+productCategory} for each of the variable.

    How can I achieve this? can you please assist me.

     

     

    regards,

    shahul


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: create variables in docplex

    Posted Tue February 12, 2019 04:35 AM

    Did you see this part of the help text for Model.continuous_var_dict:

     |          name: Used to name variables. Accepts either a string or
     |              a function. If given a string, the variable name is formed by appending the string
     |              to the string representation of the key object (if `keys` is a sequence) or the
     |              index of the variable within the range, if an integer argument is passed.
     |              If passed a function, this function is called on each key object to generate a name.
     |              The default behavior is to call :func:`str` on each key object.
     |      
     |          key_format: A format string or None. This format string describes how `keys` contribute to variable names.
     |                      The default is "_%s". For example if name is "x" and each key object is represented by a string
     |                      like "k1", "k2", ... then variables will be named "x_k1", "x_k2",...

    So you can either pass a function to name in order to create the name or try to use the key_format argument with an appropriate format string.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: create variables in docplex

    Posted Wed February 13, 2019 01:46 AM

    Originally posted by: ShaCplex


    Hi Daniel,

    Thanks for the suport.

    seems like , the assignment,

    mdl.continuous_var_dict(keys=valid_ODRoutes_df,name=lambda odr:"var_route_%s" % (odr.loc['OD Route']))  is also not working. 

    how to make this happen?

    valid_ODRoutes_df is a dataframe

     

     

    Thanks,

    Shahul


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: create variables in docplex

    Posted Thu February 14, 2019 06:41 AM

    odr in your case is the row label. So this should work:

    from docplex.mp.model import Model
    import pandas as pd

    df = pd.DataFrame({'OD Route' : ['Route1', 'Route2', 'Route3'],
                       'name'     : ['Name1',  'Name2',  'Name3'  ]})
    print(df)
    with Model() as model:
        x = model.continuous_var_dict(keys=df,
                                      name=lambda odr:"var_route_%s_%s" % (df['OD Route'][odr], df['name'][odr]))
        print(x)

     


    #CPLEXOptimizers
    #DecisionOptimization