Proper use of addSOS
OpenHey all,
I am trying to model my complementary constraints, which is harder than I anticipated. I have a bilevel optimization problem, to make it one level I find KKT conditions of lower level problem and implement them as constraints to the upper level problem. Now I am trying to solve it, so:
I have my normal constraints and the KKT's, however because they are complementary conditions I would like to do something about it, it is either by Big M or SOS Type 1. I want to use SOS type 1 and I do following:
m.addSOS(GRB.SOS_TYPE1, complementary_condition_3)
psi_gtw=m.addVar(lb=0,vtype=GRB.CONTINUOUS, name="daul variable for the quantity")
complementary_condition_1= ((rho_gtw*(qg0c+qgbar)q_gtw),eta_gtw)
m.addSOS(GRB.SOS_TYPE1, complementary_condition_1)

Tuples are not a valid expression to use like this.
You need to add intermediate variables to model the gp.or_ relationship.
Cheers,
David1 
Hey David,
Thanks for the answer, but could you explain a little bit further?
Taking the easy example, the complementary condition 3, should I just do following:
m.addConstr(complementary_condition_3 == or_([q_gtw,psi_gtw]))
And once that is defined I can use SOStype1:
m.addSOS(GRB.SOS_TYPE1, complementary_condition_3)
Is that what you mean? I am really looking forward to hearing from you.
Best Regards,
Dawid Rataj
0 
Hi Dawid,
Exactly, yes!
For \(\texttt{complementary_condition_1}\) you will need some intermediate variables as it is a more complicated OR statement.
Cheers,
David1 
Hey,
The only problem is that if I do it in that fashion then following is not defined, so I am missing something before I add the constraint. I can not just run the:
m.addConstr(complementary_condition_3 == or_([q_gtw,psi_gtw]))
And once that is defined I can use SOStype1:
m.addSOS(GRB.SOS_TYPE1, complementary_condition_3)
That way complementary_condition_3 is not predefined and the code will not run. How should I define it if not as a tuple?
Best,
Dawid
0 
You can try and model your logic using binary variables.
What are you trying to model with the tuple? This is not clear, as \(\texttt{q_gtw}\) is a continuous variable, you need to add an expression that can be used in this way. For example
q_gtw_bin = m.addVar(vtype=gp.GRB.BINARY)
eps = 1e4
m.addConstr((q_gtw_bin == 1) >> (q_gtw >= 0 + eps))
m.addConstr((q_gtw_bin == 0) >> (q_gtw <= 0))
# Same for psi_gtw
psi_gtw_bin = m.addVar(vtype=gp.GRB.BINARY)
m.addConstr((psi_gtw_bin == 1) >> (psi_gtw >= 0 + eps))
m.addConstr((psi_gtw_bin == 0) >> (psi_gtw <= 0))
# Then we can model the relationship simply as
m.addConstr(psi_gtw_bin + q_gtw_bin <= 1)This enforces that the SOS1 relationship: at most one variable in the specified list is allowed to take a nonzero value. In this case either \(\texttt{q_gtw}\) or \(\texttt{psi_gtw}\) is nonzero but not both.
Cheers,
David0 
Hey,
First of all thanks for the help so far. I think it is going to be more effective if I provide more context to what I am doing.
The optimization I am trying to solve is regarding power systems. The goal of is to maximize social welfare taking into the account emissions trading (trading the surplus/shortage of emissions) and investment cost.
I have a bi level problem. Upper level is to maximize the SW, and the lower level is describing risk profile of the generator. To solve it as one optimization problem I take KKT conditions of lower level problem. The partial derivatives of Lagrangian function are resulting in equality constraints, which are easy to deal with. However, they also create complementary conditions with dual variables. See following picture:
Complementary condition is problematic since it results in multiplying variable with variable (primal * dual), which can be problematic. To avoid it I can either use BigM method, which is unfortunately very sensitive to the chosen M value.
Therefore I would like to use SOS type 1 constraints instead like:
How should I define it to make it work as in the picture above?, Again taking simple complementary conditions
q_gtw > quantity supplied (supply, reaction to demand function, primary variable)
ψ_gtw > dual variable to the quantity.
Can you please explain that to me?
Best,
Dawid
0
Please sign in to leave a comment.
Comments
6 comments