Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Quadratic objective function in Python Api

  • 1.  Quadratic objective function in Python Api

    Posted Tue April 19, 2011 04:25 PM

    Originally posted by: GURJOTDHALIWAL


    Hi,

    My problem is in using the quadratic function 'cplex.set_quadratic(). Will really appreciate, if someone tells me how to pass the argument in this function. I checked the examples provided in the Cplex documentation. But that example has only 4 variables. My problem is having much more than 4 variables and am having hard time setting the loop for it.

    Can anyone guide me on how to write the code that will generate a python list like

    [
    [0,1,1,2],
    [1,2,1,2],
    .
    .
    .
    [3,2,12,15]]

    Thanks in advance.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Quadratic objective function in Python Api

    Posted Wed April 20, 2011 12:02 PM

    Originally posted by: SystemAdmin


    Hello,

    The simplest way to create a list of lists in a loop with Python is to use code like the following

    my_list = [0] * list_length
    for i in list_length:
        # your code to define new_sublist, the ith entry of my_list, goes here
        my_list[i] = new_sublist
    


    To fill in the details of the definition of new_sublist at each iteration, I would need to know how the data for your objective function is stored.

    If your objective function is diagonal, you can specify just the diagonal values by passing a list of floats to the set_quadratic() method.

    If your objective function is not diagonal, but it is very sparse, it may be easier to use the set_quadratic_coefficients() method. This method allows the quadratic coefficients to be set one at a time or in "triplet" format, where each coefficient is specified by
    [column_index, row_index, value]
    
    This method also
    allows you to make changes to the quadratic objective without the overhead of changing the entire matrix.

    Let me know if you have any further questions.
    Philip Starhill
    CPLEX Research Engineer
    #CPLEXOptimizers
    #DecisionOptimization