Search code examples
pythonpyomoamplgams-mathamplpy

How to map indices of different sets in pyomo?


I have two sets in Pyomo, the first one is G=/GD1,GD2,GD3/, And the second one is N=/N1,N2,N3,...,N32,N33/. Naturally they have symbolic representation here for the sake of simplicity. I would like to map G into N, in order to define the relation between G and N as follows: /GD1.N2,GD2.N4,GD3.N20/

For example, in GAMS we use the command map(G,N) and define manually the new set. Is it possible to do the same in Pyomo? If the answer is yes, then how?

Thank you in advance...


Solution

  • As I understand it from the GAMS user guide, "mappings" are GAMS terminology for multi-dimensional sets. In Pyomo, you just need to declare and initialize the set:

    import pyomo.environ as pyo
    m = pyo.ConcreteModel()
    m.G = pyo.Set(initialize=['GD1', 'GD2', 'GD3'])
    m.N = pyo.Set(initialize=['N1', 'N2', 'N3', 'N4', 'N20'])
    m.GN = pyo.Set(within=m.G*m.N, initialize=[('GD1', 'N2'), ('GD2', 'N4'), ('GD3', 'N20')])
    

    Note that the "within" is optional: it performs error checking on the set members. Omitting it will still work, but will not enforce that m.GN elements are all in m.G * m.N