Skip to main content

Piecewise linear approximation for log2 objective term

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    you are constructing one extremely long list \(\texttt{ys}\). However, you want to make this a list of lists dependent on the \(\texttt{a}\) and \(\texttt{b}\) indices. One way to achieve this would be

    ys = []
    for i in range(a):
    for j in range(b):
    ys.append([np.log2(1 + (c*p) / ( ((dx1[i]-dx2[j])**2 + (dy1[i]-dy2[j])**2 + (dz1[i]-dz2[j])**2 ) * (e+f) ) ) for p in xs])

    # add piecewise-linear constraint
    for i in range(a):
    for j in range(b):
    m.addGenConstrPWL(x[i, j], y[i, j], xs, ys[i*b+j], 'pwl')

    Moreover, please note that the getVars returns a single indexed list. Thus, you can access the variable values via

    for v in m.getVars():
    print('%s %g' % (v.varName, v.x))

    Best regards,
    Jaromił

    0
  • SHEIKH SALMAN HASSAN
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you Jaromil Najman

    The code is working now.

    0

Post is closed for comments.