Source code for gamspy._symbols.alias

#
# GAMS - General Algebraic Modeling System Python API
#
# Copyright (c) 2023 GAMS Development Corp. <support@gams.com>
# Copyright (c) 2023 GAMS Software GmbH <support@gams.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from __future__ import annotations

from typing import TYPE_CHECKING

import gams.transfer as gt

import gamspy as gp
import gamspy._algebra.condition as condition
import gamspy._algebra.expression as expression
import gamspy._algebra.operable as operable
import gamspy.utils as utils
from gamspy._symbols.set import SetMixin
from gamspy._symbols.symbol import Symbol

if TYPE_CHECKING:
    from gamspy import Set, Container


[docs]class Alias(gt.Alias, operable.Operable, Symbol, SetMixin): """ Represents an Alias symbol in GAMS. https://www.gams.com/latest/docs/UG_SetDefinition.html#UG_SetDefinition_TheAliasStatementMultipleNamesForASet Parameters ---------- container : Container name : str alias_with : Set Examples -------- >>> import gamspy as gp >>> m = gp.Container() >>> i = gp.Set(m, "i") >>> j = gp.Alias(m, "j", i) """ def __new__(cls, container: Container, name: str, alias_with: Set): if not isinstance(container, gp.Container): raise TypeError( "Container must of type `Container` but found" f" {type(container)}" ) if not isinstance(name, str): raise TypeError(f"Name must of type `str` but found {type(name)}") try: symobj = container[name] if isinstance(symobj, cls): return symobj else: raise TypeError( f"Cannot overwrite symbol `{name}` in container" " because it is not an Alias object)" ) except KeyError: return object.__new__(Alias) def __init__(self, container: Container, name: str, alias_with: Set): self._is_dirty = False name = utils._reserved_check(name) super().__init__(container, name, alias_with) self._container_check(self.domain) self.where = condition.Condition(self) self.container._add_statement(self) self._current_index = 0 def __len__(self): if self.records is not None: return len(self.records.index) return 0 def __next__(self): if self._current_index < len(self): row = self.records.iloc[self._current_index] self._current_index += 1 return row self._current_index = 0 raise StopIteration def __iter__(self): return self def __le__(self, other): return expression.Expression(self, "<=", other) def __ge__(self, other): return expression.Expression(self, ">=", other)
[docs] def gamsRepr(self) -> str: """ Representation of this Alias in GAMS language. Returns ------- str """ return self.name
[docs] def getStatement(self) -> str: """ Statement of the Alias definition Returns ------- str """ return f"Alias({self.alias_with.name},{self.name});"