Comparing variables with constants when adding an objective function
AnsweredI want to set the following objective function:
and it contains the condition that
where
is the decision variable.
My code is as follows
dep_1 = gp.quicksum((k.hk * h[k.no, t] for k in orders for t in T if k.rk <=t <= tao[k.no]))
Is there any way to solve this problem?
Thanks a lot for any help.
Yu
-
Official comment
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. -
Hi Yu,
it is difficult to help you based on the information you provided. What kind of difficulty are you dealing with? Also, a minimal reproducible example would be helpful.
Best regards
Jonasz0 -
This is my objective function:

My code is as follows:
m = gp.Model("MCIkRP1")
K = [k.no for k in orders]
P = [p.no for p in paths]
T = [t for t in range(1, 60)]
x = m.addVars(K, P, vtype=GRB.INTEGER, name='x')
z = m.addVars(K, P, vtype=GRB.BINARY, name='z')
h = m.addVars(K, T, vtype=GRB.INTEGER, name='h')
tao = m.addVars(K, vtype=GRB.INTEGER, name='tao')
b = m.addVar(vtype=GRB.BINARY, name='b')
tr_1 = gp.quicksum(x[k.no, p.no] * p.price for k in orders for p in orders_paths[k.no - 1])
c_1 = carbon_rate_shipper * gp.quicksum(x[k.no, p.no] * p.emission for k in orders for p in orders_paths[k.no - 1])
dep_1 = gp.quicksum((k.hk * h[k.no, t] for k in orders for t in T if k.rk <= t <= tao[k.no]))
m.setObjective(tr_1 + c_1 + dep_1, GRB.MINIMIZE)As shown above, I had a problem writing the third part of the objective function.
if k.rk <= t <= tao[k.no]
t is a constant, and tao is a decision variable,and I want to determine their magnitude, which doesn't seem to work in gurobi.
it gives me the following error
GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
I know that such a problem can be solved by indicating constraints when adding constraints, but how should I solve it when adding objective functions?
Best regards,
Yu0 -
As shown above, I had a problem writing the third part of the objective function.
if k.rk <= t <= tao[k.no]
t is a constant, and tao is a decision variable,and I want to determine their magnitude, which doesn't seem to work in gurobi.
it gives me the following error
GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)I know that such a problem can be solved by indicating constraints when adding constraints, but how should I solve it when adding objective functions?
You have to model your variable \(\tau\) as an index of a different variable. This can be done and has been discussed in the Community Post use a decision variable as an index.
Best regards,
Jaromił0 -
Hi Jaromił,
I failed to understand the Community Post you recommended to me(use a decision variable as an index).
Perhaps my description was not clear and I would like to recount my problem in the hope that you can give some guidance.
I want to add the following formula to my objective function:\[\sum\limits_{k \in K} {\sum\limits_{t = {r^k}}^{{\tau ^k}} {{\varphi ^k} \cdot h_t^k} } \].
I'm not sure how I should handle this part, since \[{\tau ^k}\] is one of the decision variables:\[\sum\limits_{t = {r^k}}^{{\tau ^k}} {} \].
I tried to write this part of the code like this:
gp.quicksum((k.hk * h[k.no, t] for k in orders for t in T if k.rk <= t <= tao[k.no]))
But I get this error message:
GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
Best regards,
Yu
0 -
Hi Yu,
Thank your for clarifying. I will describe one way to model what you are looking for. There may be more efficient ways to model it but it should give you an idea of how it can be done. To simplify the notation I will model
\[\begin{align*}
\sum_{i=1}^{\tau} x_i &= 1\\
x_i &\geq 0 \,\, i=1,\dots,10\\
\tau \in &\{1,2,\dots,\tau_{max}=10\}
\end{align*}\]To model the above, you can introduce additional binary variables to force subsequent \(x_i\) values to \(0\).
\[\begin{align*}
\sum_{i=1}^{\tau_{max}} b_i &= \tau\\
b_{i+1} &\leq b_i \,\, i=1,\dots,9\\
\sum_{i=0}^{\tau_{max}} x_i \cdot b_i &= 1\\
x_i &\geq 0 \,\, i=1,\dots,10\\
\tau \in &\{1,2,\dots,\tau_{max}=10\}\\
b_i \in &\{0,1\} \,\, i=1,\dots,10
\end{align*}\]If for example \(\tau=3\), then in the above you have \(b_1=b_2=b_3=1\), \(b_4=\dots=b_{10}=0\) and thus all terms \(x_i \cdot b_i\) with \(i\geq 4\) are \(0\).
I hope this helps.
Best regards,
Jaromił0 -
Hi Jaromił,
This method is really great, it solved my problem.
Thank you very much!Best regards,
Yu0
Post is closed for comments.


Comments
7 comments