Issue with general constraint
進行中Hi all,
I have the following objective function to implement where Beta and Alpha are decision variables, Delta, Sigma, gamma and tolerance are parameters.

The second part of the objective function works like perfect however the first part seems to be challenging for me.
I tried to use the General constraint mechanism and tried to follow the same guidelines as in https://support.gurobi.com/hc/en-us/community/posts/360047932252-How-to-add-conditionals-in-constraints- but I failed shortly.
The error I am getting is which has to do with Unit Penalty 4 constraint:
name="Unit Penalty_4")
File "model.pxi", line 3250, in gurobipy.Model.addConstr
File "linexpr.pxi", line 461, in gurobipy.LinExpr.__sub__
TypeError: unsupported operand type(s) for *: 'int' and 'TempConstr'
My code is :
continuous_penalties_slo_u_v = {}
bin_penalties_u_v = {}
for (u, v) in n_request[1].keys():
continuous_penalties_slo_u_v[u, v] = davinci_slo.addVar()
bin_penalties_u_v[u, v] = davinci_slo.addVar(vtype=GRB.BINARY)
for (u, v) in n_request[1].keys():
sum_x_y = quicksum(n_beta_u_v_x_y[u, v, x, y] * networkLinks[2][x, y] for (x, y) in networkLinks[0])
davinci_slo.addConstr(
(bin_penalties_u_v[u, v] == 0) >> sum_x_y < n_request[1][u, v][1] ,
name="Unit Penalty_1")
davinci_slo.addConstr(
(bin_penalties_u_v[u, v] == 1) >> sum_x_y > n_request[1][u, v][1],
name="Unit Penalty_2")
davinci_slo.addConstr(
(bin_penalties_u_v[u, v] == 0) >> continuous_penalties_slo_u_v[u, v] == 0,
name="Unit Penalty_3")
davinci_slo.addConstr(
(bin_penalties_u_v[u, v] == 1) >> continuous_penalties_slo_u_v[u, v] == sum_x_y,
name="Unit Penalty_4")
cost_function = quicksum(continuous_penalties_slo_u_v[u, v]
for (u, v) in n_request[1].keys() if continuous_penalties_slo_u_v[u, v] > tolerance) * Gamma + quicksum(Alpha[u,x] * gamma_x)
Could you please help me with the modelling part of the second objective function and point me out to my error ? Thank you.
-
正式なコメント
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?. -
Hi,
Actually, the error you see does not come from the indicator constraint but from your \(\texttt{cost_function}\), since you are asking for an \(\texttt{if}\)-clause with \(\texttt{continuous_penalties_slo_u_v[u, v] > tolerance}\) where \(\texttt{continuous_penalties_slo_u_v[u, v]}\) is an optimization variable.
You should consider what you what to achieve by \(\Gamma(\texttt{if-clause})\). If you are trying to model something like \(\Gamma(1)\) if \(\beta \cdot \Delta > tolerance\) and \(\Gamma(1)\) otherwise, then it is best to introduce an additional binary variable and model the \(\texttt{if-else}\)-statement (see, e.g., this stackexchange post for how to model an \(\texttt{if-else}\)-statement).
Your can then reformulate your \(\texttt{cost_function}\) with the new variables.
Best regards,
Jaromił0 -
Dear Matthias,
The post has been deleted. I will put a working example and share it with you here to reproduce the output. Thanks Jaromil for the suggestion and reply.
All the best.
Laaziz
0
投稿コメントは受け付けていません。
コメント
4件のコメント