0

I am using DOCPLEX for the first time to address an optimization problem. My objective function is built through the following sequence of operations:

    model = Model(name='pack')
    
    # Total number of columns 
    colstot = 97622

    # Create a list of binary decision variables and stores them in the model.
    vars = model.binary_var_list(colstot, name='vars')

    # Loop over rows of matrix A
    rows = 90
    # Initialize expraR
    expraR = [[0] * len(vars)] * rows
    for i in range(rows):
        # Initialize expra
        expra = 0 * len(vars)
        for j in range(N):
            if A[i] != 0:
                # Build the linear expression expra
                expra += A[i][j] * vars[j]
        # Build the linear expression expraR
        expraR[i] = model.abs(expra - floatnumber)  

    # Define objective function
    obj = model.sum(expraR)

At the end of the game, expraR is a list of 90 linear expressions, each with 97622 variables (every element of expraR is a docplex.mp.functional.AbsExpr and it looks something like this: 4.631vars_97342+4.632vars_97351+4.643vars_97360+4.663vars_97369+4.686vars_97378+4.706vars_97387+4.770vars_97395+4.831vars_97403+4.856vars_97411+4.873vars_97419).

Now, when looking at obj, this is a docplex.mp.linear.LinearExpr of size 90 (for each row of the matrix, all the 97622 columns have been summed up), and it looks like this:

docplex.mp.LinearExpr(_abs97712+_abs97715+_abs97718+_abs97721+_abs97724+_abs97727+_abs97730+_abs97733+_abs97736+_abs97739+_abs97742+_abs97745+_abs97748+_abs97751+_abs97754+_abs97757+_abs97760+_abs97763+_abs97766+_abs97769+_abs97772+_abs97775+_abs97778+_abs97781+_abs97784+_abs97787+_abs97790+_abs97793+_abs97796+_abs97799+_abs97802+_abs97805+_abs97808+_abs97811+_abs97814+_abs97817+_abs97820+_abs97823+_abs97826+_abs97829+_abs97832+_abs97835+_abs97838+_abs97841+_abs97844+_abs97847+_abs97850+_abs97853+_abs97856+_abs97859+_abs97862+_abs97865+_abs97868+_abs97871+_abs97874+_abs97877+_abs97880+_abs97883+_abs97886+_abs97889+_abs97892+_abs97895+_abs97898+_abs97901+_abs97904+_abs97907+_abs97910+_abs97913+_abs97916+_abs97919+_abs97922+_abs97925+_abs97928+_abs97931+_abs97934+_abs97937+_abs97940+_abs97943+_abs97946+_abs97949+_abs97952+_abs97955+_abs97958+_abs97961+_abs97964+_abs97967+_abs97970+_abs97973+_abs97976+_abs97979)

The coefficients of the 90 variables are disappeared! The output of the following piece of code is indeed a list of 1:

for v in obj.iter_variables():
    print(obj.get_coef(v))

How can I retrieve the coefficients of the 90 variables making up the objective function? Am I missing something?

Roland
  • 427
  • 1
  • 4
  • 15
  • `abs(x)` will be reformulated as it is non-linear. You may want to write out an LP file to see what it looks like. – Erwin Kalvelagen Jun 28 '22 at 14:27
  • Sorry @ErwinKalvelagen but I am new at Docplex. What do you mean with an LP file? Also, if obj is a docplex.mp.linear.LinearExpr, why the coefficients are not there? – Roland Jun 29 '22 at 09:47
  • It actually gives you the coefficients: 1 for each absolute value. Again: the absolute value is reformulated. You can see this by writing out the lp file. You may want to read http://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.model.html (functions `abs(e)` and `export_as_lp`). – Erwin Kalvelagen Jun 29 '22 at 13:00
  • Maybe then the solution would be to get rid of the abs value, such to have an LP problem – Roland Jun 30 '22 at 10:34

0 Answers0