Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Setting the coefficients of a two-dimensional variable in the objective function using Python API

    Posted 03/09/20 07:10 PM

    Originally posted by: story_


    Hello,

    I have just started using the Python API for CPLEX. I have a very simple question.

    I have defined a two-dimensional variable named "y" with indices "i" and "k" by using the following:

    c.variables.add(names=["y_" + str(i) + "_" + str(k) for k in range(K) for i in range(I)],
                    types=["C"] * (K * I)
    

    I want to set the coefficients of these variables in the objective function. As far as I am aware, this can be done by adding the "obj=..." argument while declaring variables. To test if I got the syntax/sizes correct, I added the argument below while declaring y_i,j (note that K ranges from 0 to 5 and I ranges from 0 to 2):

    obj=[0,0,0]*6
    

    As expected this worked since the size of "obj" is consistent with the variable size. But in the specific problem I am dealing with, each y_i,j variable has a specific coefficient & I cannot declare them using the syntax above for this reason. To declare each coefficient one by one I tried the following:

    obj=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
    

    But this does not seem to work. Could you please inform me on how I might change the objective function coefficients of two-dimensional (or multi-dimensional) variables in Python? Can I use a predefined list of lists (of the same size as y) to set the coefficients?

    I might be missing something simple with the syntax - but I couldn't spot where the problem really is. 

    Thanks!

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Setting the coefficients of a two-dimensional variable in the objective function using Python API

    Posted 03/09/20 07:24 PM

    The obj argument should be a one-dimensional list (similar to the way you are setting the types argument). For example:

    obj=[0.0] * (K * I)

    If you installed CPLEX in the standard way, then you should check out the python examples that are shipped with CPLEX (e.g., lpex1.py, mipex1.py, etc.).

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Setting the coefficients of a two-dimensional variable in the objective function using Python API

    Posted 03/09/20 09:08 PM

    Originally posted by: story_


    Thanks for your response - now it is clear why the second alternative in my original post is not working.

    And also thanks a lot for pointing the two examples out - I have checked these, but the objective function coefficients (and variables) in both cases seem to be one-dimensional.

    Is there a way I could potentially represent these sparse coefficients row by row as a workaround? The first idea that comes to my mind is to declare the y variables row by row (and to add the corresponding objective coefficients row by row while doing so) - but I am not sure if this is the most computationally efficient way of doing this.

    Many thanks.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Setting the coefficients of a two-dimensional variable in the objective function using Python API

    Posted 03/10/20 02:28 PM

    The CPLEX Python API is a lightweight wrapper around the CPLEX C Callable Library. There is no concept of two-dimensional variables at this level. So, you'll have to maintain the mapping yourself from two dimensions to one. This can be done in various ways, but Cplex.variables.add() returns an iterator containing the variable indices that were created to facilitate this.

    You will get the best performance when you add the variables in batches rather than individually. You could add the variables row-by-row as you suggested, or just append the information to a group of lists and then add them all at once at the end. You should compare the two methods and pick the solution that strikes the right balance for you between maintainability and performance.


    #CPLEXOptimizers
    #DecisionOptimization