Source code for gamspy._symbols.universe_alias
from __future__ import annotations
import os
import threading
from typing import TYPE_CHECKING
import gams.transfer as gt
from gams.core.gdx import GMS_DT_ALIAS
import gamspy as gp
import gamspy._algebra.condition as condition
import gamspy._validation as validation
from gamspy.exceptions import ValidationError
if TYPE_CHECKING:
import pandas as pd
from gamspy import Container
TEMP_ALIAS_NAME = "a" + gp.utils._get_unique_name()
TEMP_GDX_OUT_NAME = "_" + gp.utils._get_unique_name() + ".gdx"
[docs]
class UniverseAlias(gt.UniverseAlias):
"""
Represents a UniverseAlias symbol in GAMS.
Parameters
----------
container : Container
name : str
Examples
--------
>>> import gamspy as gp
>>> m = gp.Container()
>>> universe = gp.UniverseAlias(m)
>>> universe.records
Empty DataFrame
Columns: [uni]
Index: []
>>> i = gp.Set(m, "i", records=['i1', 'i2'])
>>> universe.records
uni
0 i1
1 i2
"""
@classmethod
def _constructor_bypass(cls, container: Container, name: str):
# create new symbol object
obj = UniverseAlias.__new__(cls, container, name)
# set private properties directly
obj._requires_state_check = False
obj._container = container
container._requires_state_check = True
obj._name = name
obj._modified = True
# typing
obj._gams_type = GMS_DT_ALIAS
obj._gams_subtype = 0
# add to container
container.data.update({name: obj})
# gamspy attributes
obj.where = condition.Condition(obj)
obj._latex_name = name.replace("_", r"\_")
# add statement
obj.container._add_statement(obj)
return obj
def __new__(cls, container: Container | None = None, name: str = "universe"):
if container is not None and not isinstance(container, gp.Container):
raise TypeError(
f"Container must of type `Container` but found {type(container)}"
)
if not isinstance(name, str):
raise TypeError(f"Name must of type `str` but found {type(name)}")
try:
if not container:
container = gp._ctx_managers[(os.getpid(), threading.get_native_id())]
symbol = container[name]
if isinstance(symbol, cls):
return symbol
raise TypeError(
f"Cannot overwrite symbol `{name}` in container"
" because it is not a UniverseAlias object)"
)
except KeyError:
return object.__new__(cls)
def __init__(self, container: Container | None = None, name: str = "universe"):
# check if the name is a reserved word
if name is not None:
name = validation.validate_name(name)
else:
name = container._get_symbol_name(prefix="u")
if container is None:
try:
container = gp._ctx_managers[(os.getpid(), threading.get_native_id())]
except KeyError as e:
raise ValidationError("UniverseAlias requires a container.") from e
super().__init__(container, name)
self._latex_name = self.name.replace("_", r"\_")
# allow conditions
self.where = condition.Condition(self)
self.container._add_statement(self)
self.container._synch_with_gams()
def _serialize(self) -> dict:
return {}
def _deserialize(self, info: dict) -> None: ...
def __repr__(self) -> str:
return f"UniverseAlias(name='{self.name}')"
@property
def records(self) -> pd.DataFrame:
"""
Records of the UniverseAlias
Returns
-------
list[str]
Examples
--------
>>> import gamspy as gp
>>> import numpy as np
>>> m = gp.Container()
>>> i = gp.Set(m, name="i", records=["seattle", "san-diego"])
>>> uni = gp.UniverseAlias(m)
>>> uni.records
uni
0 seattle
1 san-diego
"""
import pandas as pd
global TEMP_ALIAS_NAME
global TEMP_GDX_OUT_NAME
self.container._add_statement(f"Alias (*, {TEMP_ALIAS_NAME})")
self.container._add_statement(
f"execute_unload '{TEMP_GDX_OUT_NAME}' {TEMP_ALIAS_NAME};"
)
self.container._synch_with_gams()
gdx_handle = gp.utils._open_gdx_file(
self.container.system_directory, TEMP_GDX_OUT_NAME
)
uels: list[str] = self.container._gams2np.gdxGetUelList(gdx_handle)
gp.utils._close_gdx_handle(gdx_handle)
return pd.DataFrame(uels[1:], columns=["uni"])
[docs]
def gamsRepr(self) -> str:
"""
Representation of the UniverseAlias in GAMS language.
Returns
-------
str
Examples
--------
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.UniverseAlias(m, name="universe")
>>> i.gamsRepr()
'universe'
"""
return self.name
[docs]
def latexRepr(self) -> str:
"""
Representation of the UniverseAlias in LaTeX.
Returns
-------
str
Examples
--------
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.UniverseAlias(m, name="universe")
>>> i.latexRepr()
'universe'
"""
return self._latex_name
[docs]
def getDeclaration(self) -> str:
"""
Declaration of the UniverseAlias in GAMS
Returns
-------
str
Examples
--------
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.UniverseAlias(m, name="universe")
>>> i.getDeclaration()
'Alias(universe,*);'
"""
return f"Alias({self.name},*);"