Skip to main content

arctan in gUrobi optimization (python)

Answered

Comments

1 comment

  • Jaromił Najman
    Gurobi Staff 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

Please sign in to leave a comment.