Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Fast read of variables and constraints in CPLEX Python API

    Posted 02/24/17 11:13 PM

    Originally posted by: SinaF


    Hi,

    I am using CPLEX Python API to solve a MIP optimization problem. I read some IBM Software Group documents to see how I can speed up reading of variables and constraints in CPLEX Python API but it is still very slow. Is there any way to make this interface faster in reading variables and constraints?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Fast read of variables and constraints in CPLEX Python API



  • 3.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 02/26/17 02:23 PM

    Originally posted by: SinaF


    Hi,

    Yes, most likely it will help but I don't know how to implement those to variables and constraints (when to add these sentences to my model).

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 02/27/17 08:45 AM

    What do you mean by "reading variables and constraints"? Do you mean reading them from a file? Or reading data from a file and creating variables and constraints from that? Could you show us a code snippets that illustrates what you are doing?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/05/17 11:51 AM

    Originally posted by: SinaF


    Hi,

    This is one of the main constraint that I have:

     

    model.linear_constraints.add(lin_expr = [cplex.SparsePair(ind = [y[i][j] for j in range(len(y[i]))], val = [1.0 for j in range(len(y[i]))]) for i in range(len(y))], senses = ["E"] * len(y), rhs = [1 for i in range(len(y))])

     

    In every optimization, I might have thousands of these constraints but it takes a lot of time to read them.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/05/17 02:14 PM

    If I understand correctly, then by "read" you mean "create"? Does y[i][j] give a variable's name or its index?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/05/17 03:05 PM

    Originally posted by: SinaF


    Yes, that is correct.

     

    y[i][j] is variable name. I read somewhere that you mentioned using index will improve the speed but I don't know how I can do that. Is there any example for this approach?


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/05/17 04:22 PM

    You can do it like this:

    1. Before creating the constraints, create a dictionary that maps variable names to indices:

    name2idx = { n : j for j, n in enumerate(model.variables.get_names()) }

    2. Then, when creating constraints, use this dictionary to map names to indices:

    model.linear_constraints.add(lin_expr = [cplex.SparsePair(ind = [name2idx[y[i][j]] for j in range(len(y[i]))],
                                                              val = [1.0 for j in range(len(y[i]))]) for i in range(len(y))],
                                 senses = ["E"] * len(y),
                                 rhs = [1 for i in range(len(y))])


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/05/17 07:44 PM

    Originally posted by: SinaF


    I am assuming that I can use this approach in writing lazy constraints as well. Am I right?


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Fast read of variables and constraints in CPLEX Python API

    Posted 03/06/17 01:30 AM

    Correct.

    The CPLEX Python API allows referencing variables, constraints, etc. by either index or name everywhere. Using indices is always faster since the underlying C implementation ultimately works with indices. Thus names must always be translated to indices one way or the other. For the CPLEX Python API it is currently fastest to do this yourself using the method I described.

    Depending on your code layout you can do even better than creating this name2idx dictionary: function Cplex.variables.add() returns the indices of the newly created variables. So unless you actually need the variable names for modeling, you could assign to y[i][j] the indices of the variables instead of the names. Then mapping becomes obsolete.


    #CPLEXOptimizers
    #DecisionOptimization