メインコンテンツへスキップ

Max Function

回答済み

コメント

3件のコメント

  • 正式なコメント
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    I learned that I should use the max_ function of gurobi and add some auxiliary variables but I am still not sure how to do it for my problem: The max function is applied on a vector in my problem.

    You have to introduce an auxiliary variable for every \(\texttt{x[i]-muu-C[i]-D[i]}\) and \(\texttt{-x[i]+muu+C[i]+D[i]}\) term and then use the addGenConstrMax method. Something like the following should work

    m=gp.Model()
    muu=m.addVar(vtype='C',lb=-GRB.INFINITY,  name='muu')  # 
    C=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='C')
    D=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='D')
    # add auxiliary variables
    auxvarpos=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='auxvarpos')
    auxvarneg=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='auxvarneg')
    maxobj1=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name="maxobj1")
    maxobj2=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name="maxobj2")
    # add auxiliary equality constraints
    m.addConstrs((auxvarpos[i] == x[i]-muu-C[i]-D[i]) for i in range(len(t)), name="auxconstrpos")
    m.addConstrs((auxvarneg[i] == -x[i]+muu+C[i]+D[i]) for i in range(len(t)), name="auxconstrneg")
    # add constraints maxobj1 = max(auxvarpos,0), maxobj2 = max(auxvarneg,0)
    m.addConstrs((maxobj1[i] == gp.max_(auxvarpos[i], constant=0) for i in range(len(t)), name="maxconstrobj1")
    m.addConstrs((maxobj2[i] == gp.max_(auxvarneg[i], constant=0) for i in range(len(t)), name="maxconstrobj2")

    obj1=gp.quicksum( maxobj1[i] for i in range(len(t)))
    obj2=gp.quicksum( maxobj2[i] for i in range(len(t)))

    Best regards, 
    Jaromił

    2
  • Hussein Sharadga
    • Gurobi-versary
    • Investigator
    • Conversationalist

    Thank you so much!

    I do appreciate your time.

     

    Hussein

    0

投稿コメントは受け付けていません。