Max Function
回答済みI have an issue using max function in Gurobi. I know that np.max (np is for numpy) will not work fine.
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.
Below you can find the main lines in the code. (Please see max function in obj1 and obj2)
m=gp.Model()
muu=m.addVar(vtype='C',lb=-GRB.INFINITY, name='muu') #
A=m.addVars(n, lb=-GRB.INFINITY, vtype='C', name='A')
B=m.addVars(n, lb=-GRB.INFINITY, vtype='C', name='B')
C=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='C')
D=m.addVars(len(t), lb=-GRB.INFINITY, vtype='C', name='D')
obj1=gp.quicksum(np.max((x[i]-muu-C[i]-D[i]),0) for i in range(len(t)))
obj2=gp.quicksum(np.max(-1*(x[i]-muu-C[i]-D[i]),0) for i in range(len(t)))
m.setObjective((T*obj1+(1-T)*obj2)/len(t))
m.addConstrs( C[i]== gp.quicksum(A[k]*matrix1[i][k] for k in range (n)) for i in range (len(t)))
m.addConstrs( D[i]== gp.quicksum(B[k]*matrix2[i][k] for k in range (n)) for i in range (len(t)))
m.optimize()
-
正式なコメント
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. -
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 -
Thank you so much!
I do appreciate your time.
Hussein
0
投稿コメントは受け付けていません。
コメント
3件のコメント