Skip to main content

How to include float exponents into objective function?

Answered

Comments

8 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 have to use the addGenConstrPow() function in order to model exponentiation in Gurobi.

    Best regards,
    Jaromił

    1
  • M
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromił,

    thank you very much for your comment. I applied it in this way:

    m.setObjective(
    sum( f * x[i] + y for i in I)
    , GRB.MINIMIZE)

    m.addGenConstrPow(((v * ( x[i] * Q[i]))  for i in I), y, 0.4, "gf", "FuncPieces=1000")

    However, I receive this error term now:

    GurobiError: Unsupported type (<class 'gurobipy.tupledict'>) for LinExpr addition argument

    Do you have an idea how to solve it?

    Thank you very much.

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    As noted in the documentation of addGenConstrPow(), this function requires two single variables as input.

    This means that you have to introduce a new auxiliary variable and an additional equality for each \(\texttt{i}\) in \(\texttt{I}\) to model the relationship \(\texttt{myAuxVar = v *(x * Q)}\) first. Then, you can use

    m.addGenConstrPow(myAuxVar, y, 0.4, "gf", "FuncPieces=1000")

    Best regards,
    Jaromił

    1
  • M
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromił,

    thank you!

    I added the constraint as above. However, I have problems with the auxiliary variable since it refers to a function. How would you add it? Furthermore, I have more than 1 million i's so I am looking for an efficient way to include this without additional equalities for each i in I. Do you have an idea how to solve it?

    x = m.addVars(I, vtype=GRB.BINARY, name="x")
    myAuxVar = m.addVars( ?? )

    Thank you!

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    Unfortunately, you will have to add 2 auxiliary variables and 1 equality constraint for each \(\texttt{i}\).

    The code would look something like this:

    x = m.addVars(I, vtype=GRB.BINARY, name="x")
    myAuxVar = m.addVars(I, vtype=GRB.CONTINUOUS, lb=-GRB.INFINITY, name="myAuxVar" )
    y = m.addVars(I, vtype=GRB.CONTINUOUS, name="y")

    for i in I:
    m.addConstr(myAuxVar[i] == v[i] * (x[i] * Q[i]), "auxConstr_"+str(i))
    m.addGenConstrPow(myAuxVar[i], y[i], 0.4, "gf_"+str(i), "FuncPieces=1000")

    m.setObjective( sum( f * x[i] + y[i] for i in I), GRB.MINIMIZE)

    I would most likely also decrease the number of pieces to 100 or even 10 for the general constraints to improve performance. However, this depends on how accurate your solution has to be.

    Best regards,
    Jaromił

    0
  • M
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jaromił,

    thank you very much for the code example. I added it to my problem and now I receive the following error message:

    GurobiError: lower bound of x variable is less than zero for POW function

    I am suprised since x is binary so that the auxiliary variable can only become 0. What would I need to adapt to avoid this error message?

    Thank you!

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    \(x^{0.4}\) is not defined for \(x<0\), small but important detail :) The above code should read

    myAuxVar = m.addVars(I, vtype=GRB.CONTINUOUS, name="myAuxVar" )

    The lower bound of all \(\texttt{myAuxVar}\)s has now the default value 0.

    Best regards,
    Jaromił

    1

Post is closed for comments.