Alias: Multiple Names for a Set#
Sometimes it is necessary to have more than one name for the same set, for example, in models that deal with the interactions of elements within the same set. For instance, in input-output models, 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=[f"i{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. If the alias name is not provided, it will be autogenerated.
Besides using the Alias()
constructor directly, one can also use the gamspy.Container.addAlias()
method
of the Container
class (which internally calls Alias()
):
h = Set(m, name="h", records=range(1, 6))
hp = m.addAlias(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=[f"i{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 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 Container, Set, Alias, Parameter, SpecialValues
m = Container()
i = Set(m, "i", description="plant locations")
tran = Parameter(
m,
description="transport cost for interplant shipments (us$ per ton)",
domain=[i, i],
domain_forwarding=True,
records=[
("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),
],
)
tran[i, i] = SpecialValues.EPS
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; for instance, costs are specified from pto-suarez
to palmasola
but not
vice versa:
In [1]: tran.pivot(fill_value=SpecialValues.NA)
Out[1]:
pto-suarez potosi baranquill cartagena palmasola
pto-suarez -0.00 NaN NaN NaN 87.22
potosi 55.97 -0.00 NaN NaN 31.25
baranquill 114.56 70.68 -0.0 NaN 89.80
cartagena 114.56 70.68 5.0 -0.0 89.80
palmasola NaN NaN NaN NaN -0.00
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, domain=[i, ip], description="transport cost: interplant shipments (us$ per ton)")
mui[i, ip] = tran[i, ip] + tran[ip, i]
In [1]: mui.pivot()
Out[1]:
pto-suarez potosi baranquill cartagena palmasola
pto-suarez -0.00 55.97 114.56 114.56 87.22
potosi 55.97 -0.00 70.68 70.68 31.25
baranquill 114.56 70.68 -0.00 5.00 89.80
cartagena 114.56 70.68 5.00 -0.00 89.80
palmasola 87.22 31.25 89.80 89.80 -0.00
Please note that the domain in the declaration of mui
could be also [i, i]
. Only the assignment
statement to mui
requires the alias ip
to represent an assignment over all possible pairs. The assignment tran[i, i] = SpecialValues.EPS
assigns EPS
(a sticky 0) to the diagonal of the tran
parameter.