Skip to main content

How to raise the power of a variable?

Answered

Comments

4 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Could you please share a minimal reproducible example where you model the objective function and don't get the correct solution?

    Best regards, 
    Jaromił

    0
  • BISWAJIT KAR
    Gurobi-versary
    Conversationalist
    Investigator

    Hi Jaromił Najman, I have mentioned one objective and one constraint. I am sharing the code now

    m=gp.Model('Matelco')

    x1=m.addVar(vtype=GRB.CONTINUOUS, name ='x1')
    x2=m.addVar(vtype=GRB.CONTINUOUS, name ='x2')
    x3=m.addVar(vtype=GRB.CONTINUOUS, name ='x3')
    x4=m.addVar(vtype=GRB.CONTINUOUS, name ='x4')

    a=m.addVar(vtype= GRB.CONTINUOUS, name='a')
    b=m.addVar(vtype= GRB.CONTINUOUS, name='b')
    c=m.addVar(vtype= GRB.CONTINUOUS, name='c')
    d=m.addVar(vtype= GRB.CONTINUOUS, name='d')

    m.addConstr(a==x1, name =='c1')
    m.addConstr(b==x2, name =='c2')
    m.addConstr(c==x3, name =='c3')
    m.addConstr(d==x4, name =='c4')

    m.addGenConstrPow(x1, a, 4)
    m.addGenConstrPow(x2, b, 5)
    m.addGenConstrPow(x3, c, 3)
    m.addGenConstrPow(x4, d, 6)

    m.addConstr(x1+x2+x3+x4<=100, name = 'c1')
    m.setObjective(a+b+c+d, GRB.MAXIMIZE)

    m.update()
    m.optimize()
    m.printAttr('X')

    I am getting different results. The actual results are:

    x1=0
    x2=0
    x3=0
    x4=100

    But now I am getting different.

     

    Your correction would be highly appreciated.

    Thank You

    Regards,

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Biswajit,

    You could use the write method to generate an LP file and analyze your model in detail. In the LP file, you can see that what you actually modeled is

    \[\begin{align}
    \max \;&a + b + c +d\\
    x_1 &=a\\
    x_2 &=b\\
    x_3 &=c\\
    x_4 &=d\\
    a &= x_1^4\\
    b &= x_2^5\\
    c &= x_3^3\\
    d &= x_4^6\\
    100 &\geq x_1 + x_2 + x_3 + x_4\\
    x_i &\geq 0, a,b,c,d \geq 0
    \end{align}\]

    There you can see that the equality constraints

    \[\begin{align}
    x_1 &=a\\
    x_2 &=b\\
    x_3 &=c\\
    x_4 &=d\\
    \end{align}\]

    are wrong as they don't belong to the model you are trying to model. Additionally, the coefficients in your objective function are missing.

    import gurobipy as gp
    from gurobipy import GRB

    m=gp.Model('Matelco')
    m.setParam("FuncMaxVal",1e12)

    x1=m.addVar(vtype=GRB.CONTINUOUS, name ='x1')
    x2=m.addVar(vtype=GRB.CONTINUOUS, name ='x2')
    x3=m.addVar(vtype=GRB.CONTINUOUS, name ='x3')
    x4=m.addVar(vtype=GRB.CONTINUOUS, name ='x4')

    a=m.addVar(vtype=GRB.CONTINUOUS, name='a')
    b=m.addVar(vtype=GRB.CONTINUOUS, name='b')
    c=m.addVar(vtype=GRB.CONTINUOUS, name='c')
    d=m.addVar(vtype=GRB.CONTINUOUS, name='d')

    m.addGenConstrPow(x1, a, 4)
    m.addGenConstrPow(x2, b, 5)
    m.addGenConstrPow(x3, c, 3)
    m.addGenConstrPow(x4, d, 6)

    m.addConstr(x1+x2+x3+x4<=100, name = 'c5')
    m.setObjective(10*a + 8*b + 12*c + 16*d, GRB.MAXIMIZE)

    m.update()
    m.optimize()
    m.write("myLP.lp")
    m.printAttr('X')

    Note that you have to set the parameter FuncMaxVal to the maximum value that can be attained by your general constraint, which in your case equals \(100^6 = 10^12\).

    Working with such large values is not recommended as it may lead to numerical difficulties. You should try to reformulate or re-scale your model. Please refer to Guidelines for Numerical Issues for more details. Could you, use smaller values for powers, e.g., by finding a lower degree polynomial with same properties as the one you are currently using?

    Best regards, 
    Jaromił

    0
  • BISWAJIT KAR
    Gurobi-versary
    Conversationalist
    Investigator

    Hi Jaromił Najman, I am very grateful to you for the clarification that you have provided.  I am afraid that the question is an assignment problem and it cannot be changed. But I want to thank you for the clarification. I would try to solve smaller questions.

    0

Please sign in to leave a comment.