Skip to main content

Solving a non linear Programming

Answered

Comments

1 comment

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    Hi, 

    The Model.addGenConstrPow() general constraint API in Gurobi can be used to represent constraints in the form of \(y = x^a\) where \(y\) and \(x\) are instances of Gurobi Var objects and \(a\) is a positive constant. For example, to represent the term \((a+b-c)^{\frac{1}{2}}\), two auxiliary variables \(d\) and \(d_{sr}\) need to be defined to represent \(d = a + b - c \) and \( d_{sr} = d^{\frac{1}{2}}\), respectively.

    When using general constraint APIs, it is very important to define the variable bounds as tight as possible. See the code snippet below for an implementation of your model.

    import gurobipy as gp
    from gurobipy import GRB
    from math import sqrt

    if __name__ == "__main__":
    m = gp.Model("model")

    # Define decision variables
    a = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="a")
    b = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="b")
    c = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="c")

    # Define auxiliary variables
    # d = a + b - c
    d = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="d")
    # dsr = (a + b - c)**0.5 = d**0.5
    d_sr = m.addVar(lb=0, ub=8, vtype=GRB.CONTINUOUS, name="d_sr")

    # e = b + c - a
    e = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="e")
    # e_sr = (b + c - a)**0.5 = e**0.5
    e_sr = m.addVar(lb=0, ub=8, vtype=GRB.CONTINUOUS, name="e_sr")

    # f = a + c - b
    f = m.addVar(lb=0, ub=60, vtype=GRB.CONTINUOUS, name="f")
    # f_sr = (a + c - b)**0.5 = f**0.5
    f_sr = m.addVar(lb=0, ub=8, vtype=GRB.CONTINUOUS, name="f_sr")

    # g = e_sr * f_sr
    g = m.addVar(lb=0, ub=64, vtype=GRB.CONTINUOUS, name="g")

    # Define the objective
    m.setObjective(sqrt(15) * 0.5 * d_sr * g, sense=GRB.MAXIMIZE)

    # Define constraints
    # d = a + b - c
    m.addConstr(d == a + b - c, name="set_d")
    # d_sr = d**0.5
    m.addGenConstrPow(d, d_sr, 0.5, "set_d_square_root", "FuncPieces=2000")
    # e = b + c - a
    m.addConstr(e == b + c - a, name="set_e")
    # e_sr = e**0.5
    m.addGenConstrPow(e, e_sr, 0.5, "set_e_square_root", "FuncPieces=2000")
    # f = a + c - b
    m.addConstr(f == a + c - b, name="set_f")
    # f_sr = f**0.5
    m.addGenConstrPow(f, f_sr, 0.5, "set_f_square_root", "FuncPieces=2000")
    # g = e_sr * f_sr
    m.addQConstr(g == e_sr * f_sr, name="set_g")

    # c1: a + b + c <= 60
    m.addConstr(a + b + c <= 60, name="c1")

    # c2: a + b >= c
    m.addConstr(a + b >= c, name="c2")

    # c3: b + c >= a
    m.addConstr(b + c >= a, name="c3")

    # c4: a + c >= b
    m.addConstr(a + c >= b, name="c4")

    m.params.NonConvex = 2
    m.optimize()
    m.printAttr("X")

    Best regards,

    Maliheh

    0

Please sign in to leave a comment.