Skip to main content

Mapping two sets and set alias

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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?.
  • Silke Horn
    Gurobi Staff Gurobi Staff

    Hi Buddi,

    Welcome to Gurobi!

    To define sets, you can use anything that Python offers. E.g. you could define your sets using

    starting_city = {"City1", "City2", "City3"}

    and

    ending_city = starting_city

    If you have two sets (or lists or dictionaries) "hours" and "years", you can create a dictionary of variables using the addVars method:

    var = model.addVars(hours, years)

    This dictionary then contains one variable for each combination of hours and years, e.g.:

     var[hour, year]

    Does this answer your questions?

    Silke

    0
  • Charitha Buddhika Heendeniya
    Gurobi-versary
    Conversationalist
    Curious

    Hi Silke,

    Thank you for your reply. Let me show you a small example. 

    In the following image, I have defined several technologies (key) and several energy flows resulting from those technologies. So it is a one to many mapping between the technologies (pp_tech) and energy flow type (pp_type). 

    Now I have several variables. Here t is the set of time-steps. 

    v_power = model.addVars(t, pp_tech, pp_type, name="v_power", lb=0)

    v_chp_electricity = model.addVars(t, name="v_chp_electricity", lb=0)

    Now I want to write a constraint  that gives me only the electrical power flows from CHP plants. So I write,

    model.addConstrs((v_chp_electricity[t] == quicksum(quicksum(v_power[t,pt,pp] for pp in pp_type if pp=='chp_e')
    for pt in pp_tech) for t in T), name='c6.1') 

    But something goes wrong here and when I print the model I get something like this.

    c6.1[0]: v_chp_electricity[0] = 0
    c6.1[1]: v_chp_electricity[1] = 0
    c6.1[2]: v_chp_electricity[2] = 0
    c6.1[3]: v_chp_electricity[3] = 0
    c6.1[4]: v_chp_electricity[4] = 0
    c6.1[5]: v_chp_electricity[5] = 0
    c6.1[6]: v_chp_electricity[6] = 0
    c6.1[7]: v_chp_electricity[7] = 0
    c6.1[8]: v_chp_electricity[8] = 0

    Funnily enough, somewhere else in the model, I see variables such as,

    v_power[0,CHP,BESS]

    v_power[0,CHP,EL_HEATER]

    so clearly the mapping has not worked as I intended. Do you see what I'm doing wrong here? Thank you very much for your support. 

    0
  • Silke Horn
    Gurobi Staff Gurobi Staff

    There are a few things I find confusing:

    • In your dictionary, the values are strings. Maybe you want them to be arrays of strings instead? Then you could also access the individual entries. E.g.:
      {'CHP': ['chp_e', 'chp_h', 'chp_hw'], 'PV_PROOF': ['pv_proof'], ...}
    • How are pp_tech and pp_type defined?
    • What are t and T? In your constraint definition, you have "for t in T", which suggests that T is the set of timesteps. In your variable definitions, however, you use t.
    0
  • Charitha Buddhika Heendeniya
    Gurobi-versary
    Conversationalist
    Curious

    Dear Silke,

    Thank you very much.

    What are t and T? - You are correct, that was a typo. I had longer variable and set names in the actual code and I replaced them with short symbols when I copied them to my question to make it more clear. So T is the set of hours and t is a member of T. 

    I managed to find another way to do what I wanted to do. 

    Here I defined the technology (pp_tech) and energy flow type (pp_type) as a tuplelist. So chp_e --> (CHP, ELEC)

    With this, I can write the constraints I wanted to write. Thank you. 

    0

Post is closed for comments.