Skip to main content

arctan in gUrobi optimization (python)

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    Hi Chaymae,

    Currently, the \(\arctan\) function is not supported in Gurobi. The only way to add your constraint would be to use an approximation of the \(\arctan\) function. You could use the addGenConstrPWL method to construct a piecewise-linear approximation.

    import gurobipy as gp
    from gurobipy import GRB
    import math

    def f(u):
      return math.atan(u)

    m = gp.Model()

    lbz = -2
    ubz =  2
    z = m.addVar(lb=lbz, ub=ubz, vtype=GRB.CONTINUOUS, name="z")
    y = m.addVar(lb=-math.pi/2, ub=math.pi/2, vtype=GRB.CONTINUOUS, name="y")

    # Compute piecewise-linear arctan function for z
    npts = 101
    ptu = []
    ptf = []
    for i in range(npts):
      ptu.append(lbz + (ubz - lbz) * i / (npts - 1))
        ptf.append(f(ptu[i]))

    # Add constraint y = arctan(z) as piecewise-linear approximation
    m.addGenConstrPWL(z,y,ptu,ptf,"PWLarctan")

    Best regards, 
    Jaromił

    0

Post is closed for comments.