Alias: Multiple Names for a Set#

Sometimes it is necessary to have more than one name for the same set, e.g., in models that deal with the interactions of elements within the same set. In input-output models for example, each commodity may be used in the production of all other commodities and it is necessary to have two names for the set of commodities to specify the problem without ambiguity. Example:

from gamspy import Container, Set, Alias

m = Container()

i = Set(m, name = "i", records=["i" + str(i) for i in range(5)])
ip = Alias(m, name = "ip", alias_with = i)

A second name ip for the set i is established that can be used instead of the original set name i.

Note

The newly introduced set name may be used as an alternative name for the original set; the associated set will always contain the same elements as the original set.

Besides using the Alias() class directly, one can also facilitate the addAlias() method of the Container class (which internally calls Alias()):

h = Set(m, name="h", records=list(range(1, 6)))
hp = Alias(m, name="hp", alias_with=h)

It is possible to create an alias from another alias object. In this case a recursive search will be performed to find the root parent set – this is the set that will ultimately be stored as the alias_with property. We can see this behavior in the following example:

from gamspy import Container, Set, Alias

m = Container()

i = Set(m, name = "i", records=["i" + str(i) for i in range(5)])
ip = Alias(m, name = "ip", alias_with = i)
ipp = Alias(m, "ipp", ip)
In [1]: ip.alias_with.name
Out[1]: 'i'

In [2]: ipp.alias_with.name
Out[2]: 'i'

Warning

If an alias is to be used for the universal set, the gamspy.UniverseAlias() class has to be used. More about this in The Universal Set: * as Set Identifier.

Typical examples for the usage of aliases are problems where transportation costs between members of one set have to be modeled. Example:

from gamspy import Set, Alias, Parameter
import pandas as pd

m = Container()

i = Set(m, name = "i", description = "plant locations", records=[
    "palmasola", "pto-suarez", "potosi", "baranquill", "cartagena"
])

cost = pd.DataFrame(
    [
        ("pto-suarez", "palmasola",  87.22),
        ("potosi",     "palmasola",  31.25),
        ("potosi",     "pto-suarez", 55.97),
        ("baranquill", "palmasola",  89.80),
        ("baranquill", "pto-suarez", 114.56),
        ("baranquill", "potosi",     70.68),
        ("cartagena",  "palmasola",  89.80),
        ("cartagena",  "pto-suarez", 114.56),
        ("cartagena",  "potosi",     70.68),
        ("cartagena",  "baranquill", 5.00),
    ],
    columns=["from", "to", "us$ per ton"],
)

tran = Parameter(m, name="tran",
                       description = "transport cost for interplant shipments (us$ per ton)",
                       domain=[i, i], records=cost)

The parameter tran is two-dimensional and both indices are the set i. The data for the transport cost between the plants is given in this symbol; note that the transport costs are given only for one direction here, i.e. the costs from pto-suarez to palmasola are explicitly specified in the parameter while the costs in the opposite direction are not given at all:

palmasola

pto-suarez

potosi

baranquill

pto-suarez

87.22

nan

nan

nan

potosi

31.25

55.97

nan

nan

baranquill

89.8

114.56

70.68

nan

cartagena

89.8

114.56

70.68

5

The following alias statement introduces ip as another name for the set i. The parameter mui is also two-dimensional and both indices refer to the set i, but this time the alias ip is used in the second position. The parameter mui is defined to contain the transport costs from one plant location to the other, in both directions.

ip = Alias(m, name = "ip", alias_with = i)

mui = Parameter(m, name = "mui",
                      description = "transport cost: interplant shipments (us$ per ton)",
                      domain = [i, ip])

mui[i,ip] = tran[i,ip] + tran[ip,i]

Resulting in the following data for mui

palmasola

pto-suarez

potosi

baranquill

cartagena

palmasola

nan

87.22

31.25

89.8

89.8

pto-suarez

87.22

nan

55.97

114.56

114.56

potosi

31.25

55.97

nan

70.68

70.68

baranquill

89.8

114.56

70.68

nan

5

cartagena

89.8

114.56

70.68

5

nan

Note that if mui were defined without the alias, then all its entries would have been zero.