Decision Optimization

 View Only
  • 1.  floor function in objective function

    Posted Fri November 19, 2021 03:12 PM
    HI All,
    I am trying to implement a quadratic objective function. Sample code is here

    def Compute_Cost(Xvar):
    Diff = 0
    k = 0
    for key in Demand:
    tmpData = A data frame available with 'Count' as a column
    index = tmpData.index

    sum = 0
    for i in index:
    sum = sum + (tmpData['Count'][i] * Xvar[i, k]) ---> here I want to use a floor function as math.floor(tmpData['Count'][i] * Xvar[i, k])



    Target = Demand[key]['Impression']
    Diff = Diff + (Target - sum)*(Target - sum)
    k = k + 1

    return Diff
    my variable is a continuous variable
    Xvar = mdl.continuous_var_matrix(len(Reqdata.index), len(Demand), lb=0, ub=1, name="Imp", key_format=None)

    Any idea how can I use the floor function in the Compute_Cost() function?

    Thanks in advance!!





    ------------------------------
    Shamik Chaudhuri
    ------------------------------

    #DecisionOptimization


  • 2.  RE: floor function in objective function

    Posted Mon November 22, 2021 04:31 AM
    Edited by System Fri January 20, 2023 04:20 PM
    Dear Shamik

    If you are using constraint programming, please consider using the "floor" method of the https://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.modeler.py.html API.
    If you are using mathematical programming, please adopt the  approach suggested in the answer to this question:
    https://math.stackexchange.com/questions/1862885/linear-program-with-ceiling-or-floor-functions

    I hope this helps.
    Cheers,

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 3.  RE: floor function in objective function

    Posted Mon November 22, 2021 10:10 PM
    Thanks a lot !!
    I am trying CP optimizer with integer variables.

    Thanks,
    Shamik





  • 4.  RE: floor function in objective function

    Posted Mon November 22, 2021 11:19 AM
    Dear Shamik,

    To complement what Renaud suggested, let me bring to your attention the piecewise linear fuctionality available in CPLEX. Please remember that the floor function makes your objective nonconvex (even non-smooth to be more precise). You cannot model it using conventional LP tricks. You can, however, represent your objective as a piecewise linear function (a staircase-shaped function to be more precise), using MIP machinery. CPLEX allows you to use an ad-hoc syntax to pass your function to the solver (see below for reference). Note, however, that now the solver will internally handle your problem no longer as a LP, but as a MIP.

    For further reading, please refer to our doc on piecewise linear functions: https://www.ibm.com/docs/en/icos/20.1.0?topic=do-using-piecewise-linear-functions-in-optimization-transport-example

    Best regards,

    Claudio Contardo

    ------------------------------
    Claudio Contardo
    ------------------------------



  • 5.  RE: floor function in objective function

    Posted Mon November 22, 2021 11:18 PM
    Hi Claudio,
    Thanks for the suggestion. I am trying the CP optimizer, I shall update my experience.

    Thanks
    Shamik