Skip to main content

Proper use of addSOS

Open

Comments

6 comments

  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Tuples are not a valid expression to use like this.

    You need to add intermediate variables to model the gp.or_ relationship.

    Cheers, 
    David

    1
  • Dawid Rataj
    First Comment
    First Question

    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
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Hi Dawid,

    Exactly, yes!

    For \(\texttt{complementary_condition_1}\) you will need some intermediate variables as it is a more complicated OR statement.

    Cheers, 
    David

    1
  • Dawid Rataj
    First Comment
    First Question

    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
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    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 = 1e-4
    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 non-zero value. In this case either \(\texttt{q_gtw}\) or \(\texttt{psi_gtw}\) is non-zero but not both.

    Cheers, 
    David

    0
  • Dawid Rataj
    First Comment
    First Question

    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.