Skip to content

Constant class

Bases: Term

Represents a constant value in an optimization model.

The Constant class is a subclass of Term and represents a fixed value that remains constant throughout the optimization process. It is typically used to represent known values or parameters in an optimization model.

Source code in pyorlib/algebra/terms/constants/constant.py
class Constant(Term):
    """
    Represents a constant value in an optimization model.

    The `Constant` class is a subclass of `Term` and represents a fixed value that remains constant
    throughout the optimization process. It is typically used to represent known values or
    parameters in an optimization model.
    """

    __slots__ = ["_name", "_value"]

    @property
    def name(self) -> str:
        return self._name

    @property
    def lower_bound(self) -> float:
        return self._value

    @property
    def upper_bound(self) -> float:
        return self._value

    @property
    def value(self) -> float:
        return self._value

    @property
    def raw(self) -> Any:
        return self.value

    def __init__(self, name: str, value_type: ValueType, value: float):
        """
        Initializes a new instance of the Constant class.
        :param name: A string representing the name of the constant.
        :param value_type: A ValueType enumeration representing the type of value the constant can assume.
        :param value: A float representing the value of the constant.
        """
        # Calls the super init method.
        super().__init__(term_type=TermType.CONSTANT, value_type=value_type)

        # Applies validations
        if not name:
            raise TermException("Constant terms must have a name.")
        if value is None:
            raise TermException("Constant terms must have a value.")
        if value >= inf or value <= -inf:
            raise TermException("Constant terms value cannot be greater than or equal to [+/-]infinity.")
        if value_type == ValueType.BINARY and not ValueTypeValidator.is_binary(num=value):
            raise TermException("The value of a binary constant must be 0 or 1.")
        if value_type == ValueType.INTEGER and not ValueTypeValidator.is_integer(num=value):
            raise TermException("The value of an integer constant must be a valid integer.")

        # Instance attributes
        self._name: str = name
        """ The name of the constant. """

        self._value: float = value
        """ The internal value of the constant. """

    def get_pretty_string(self, float_precision: int = 6) -> str:  # pragma: no cover
        default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
        return "".join(
            [
                f"Name: {debug}{self.name}{default} | ",
                f"Type: {debug}{self.term_type.name.capitalize()}{default} | ",
                f"Value type: {debug}{self.value_type.name.capitalize()}{default} | ",
                f"Val:{debug} ",
                "{0:.{prec}g} ".format(self.value, prec=float_precision),
                f"{default}",
            ]
        )

Attributes

term_type property

term_type: TermType

Retrieves the type of the term.

RETURNS DESCRIPTION
TermType

A TermType enumeration.

value_type property

value_type: ValueType

Retrieves the type of the term's value.

RETURNS DESCRIPTION
ValueType

A ValueType enumeration

is_variable property

is_variable: bool

Determines whether the term is a variable or not.

RETURNS DESCRIPTION
bool

True if the term is a variable, False otherwise.

is_constant property

is_constant: bool

Determines whether the term is a constant or not.

RETURNS DESCRIPTION
bool

True if the term is a constant, False otherwise.

name property

name: str

lower_bound property

lower_bound: float

upper_bound property

upper_bound: float

value property

value: float

raw property

raw: Any

Functions

__add__

__add__(other: Any) -> Element

Addition operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be added.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the addition.

Source code in pyorlib/algebra/element.py
def __add__(self, other: Any) -> "Element":
    """
    Addition operation.
    :param other: The `number` or `Element` instance to be added.
    :return: A new `Element` instance representing the addition.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw + other.raw)
    else:
        return self._build_expression(expression=self.raw + other)

__radd__

__radd__(other: Any) -> Element

Right addition operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be added.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the addition.

Source code in pyorlib/algebra/element.py
def __radd__(self, other: Any) -> "Element":
    """
    Right addition operation.
    :param other: The `number` or `Element` instance to be added.
    :return: A new `Element` instance representing the addition.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw + self.raw)
    else:
        return self._build_expression(expression=other + self.raw)

__iadd__

__iadd__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __iadd__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw + other.raw)
    else:
        return self._build_expression(expression=self.raw + other)

__sub__

__sub__(other: Any) -> Element

Subtraction operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be subtracted.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the subtraction.

Source code in pyorlib/algebra/element.py
def __sub__(self, other: Any) -> "Element":
    """
    Subtraction operation.
    :param other: The `number` or `Element` instance to be subtracted.
    :return: A new `Element` instance representing the subtraction.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw - other.raw)
    else:
        return self._build_expression(expression=self.raw - other)

__rsub__

__rsub__(other: Any) -> Element

Right subtraction operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be subtracted.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the subtraction.

Source code in pyorlib/algebra/element.py
def __rsub__(self, other: Any) -> "Element":
    """
    Right subtraction operation.
    :param other: The `number` or `Element` instance to be subtracted.
    :return: A new `Element` instance representing the subtraction.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw - self.raw)
    else:
        return self._build_expression(expression=other - self.raw)

__isub__

__isub__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __isub__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw - other.raw)
    else:
        return self._build_expression(expression=self.raw - other)

__mul__

__mul__(other: Any) -> Element

Multiplication operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be multiplied.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the multiplication.

Source code in pyorlib/algebra/element.py
def __mul__(self, other: Any) -> "Element":
    """
    Multiplication operation.
    :param other: The `number` or `Element` instance to be multiplied.
    :return: A new `Element` instance representing the multiplication.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw * other.raw)
    else:
        return self._build_expression(expression=self.raw * other)

__rmul__

__rmul__(other: Any) -> Element

Right multiplication operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be multiplied.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the multiplication.

Source code in pyorlib/algebra/element.py
def __rmul__(self, other: Any) -> "Element":
    """
    Right multiplication operation.
    :param other: The `number` or `Element` instance to be multiplied.
    :return: A new `Element` instance representing the multiplication.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw * self.raw)
    else:
        return self._build_expression(expression=other * self.raw)

__imul__

__imul__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __imul__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw * other.raw)
    else:
        return self._build_expression(expression=self.raw * other)

__truediv__

__truediv__(other: Any) -> Element

Division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the division.

Source code in pyorlib/algebra/element.py
def __truediv__(self, other: Any) -> "Element":
    """
    Division operation.
    :param other: The `number` or `Element` instance to be divided.
    :return: A new `Element` instance representing the division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw / other.raw)
    else:
        return self._build_expression(expression=self.raw / other)

__rtruediv__

__rtruediv__(other: Any) -> Element

Right division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the division.

Source code in pyorlib/algebra/element.py
def __rtruediv__(self, other: Any) -> "Element":
    """
    Right division operation.
    :param other: The `number` or `Element` instance to be divided.
    :return: A new `Element` instance representing the division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw / self.raw)
    else:
        return self._build_expression(expression=other / self.raw)

__itruediv__

__itruediv__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __itruediv__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw / other.raw)
    else:
        return self._build_expression(expression=self.raw / other)

__floordiv__

__floordiv__(other: Any) -> Element

Floor division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be floor divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the floor division.

Source code in pyorlib/algebra/element.py
def __floordiv__(self, other: Any) -> "Element":
    """
    Floor division operation.
    :param other: The `number` or `Element` instance to be floor divided.
    :return: A new `Element` instance representing the floor division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw // other.raw)
    else:
        return self._build_expression(expression=self.raw // other)

__rfloordiv__

__rfloordiv__(other: Any) -> Element

Right floor division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be floor divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the floor division.

Source code in pyorlib/algebra/element.py
def __rfloordiv__(self, other: Any) -> "Element":
    """
    Right floor division operation.
    :param other: The `number` or `Element` instance to be floor divided.
    :return: A new `Element` instance representing the floor division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw // self.raw)
    else:
        return self._build_expression(expression=other // self.raw)

__ifloordiv__

__ifloordiv__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __ifloordiv__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw // other.raw)
    else:
        return self._build_expression(expression=self.raw // other)

__mod__

__mod__(other: Any) -> Element

Modulo operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used for modulo.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the modulo operation.

Source code in pyorlib/algebra/element.py
def __mod__(self, other: Any) -> "Element":
    """
    Modulo operation.
    :param other: The `number` or `Element` instance to be used for modulo.
    :return: A new `Element` instance representing the modulo operation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw % other.raw)
    else:
        return self._build_expression(expression=self.raw % other)

__rmod__

__rmod__(other: Any) -> Element

Right modulo operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used for modulo.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the modulo operation.

Source code in pyorlib/algebra/element.py
def __rmod__(self, other: Any) -> "Element":
    """
    Right modulo operation.
    :param other: The `number` or `Element` instance to be used for modulo.
    :return: A new `Element` instance representing the modulo operation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw % self.raw)
    else:
        return self._build_expression(expression=other % self.raw)

__imod__

__imod__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __imod__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw % other.raw)
    else:
        return self._build_expression(expression=self.raw % other)

__pow__

__pow__(other: Any) -> Element

Exponentiation operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used as the exponent.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the exponentiation.

Source code in pyorlib/algebra/element.py
def __pow__(self, other: Any) -> "Element":
    """
    Exponentiation operation.
    :param other: The `number` or `Element` instance to be used as the exponent.
    :return: A new `Element` instance representing the exponentiation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw**other.raw)
    else:
        return self._build_expression(expression=self.raw**other)

__rpow__

__rpow__(other: Any) -> Element

Right exponentiation operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used as the base.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the exponentiation.

Source code in pyorlib/algebra/element.py
def __rpow__(self, other: Any) -> "Element":
    """
    Right exponentiation operation.
    :param other: The `number` or `Element` instance to be used as the base.
    :return: A new `Element` instance representing the exponentiation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw**self.raw)
    else:
        return self._build_expression(expression=other**self.raw)

__ipow__

__ipow__(other: Any) -> Element
Source code in pyorlib/algebra/terms/term.py
def __ipow__(self, other: Any) -> Element:
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw**other.raw)
    else:
        return self._build_expression(expression=self.raw**other)

__neg__

__neg__() -> Element

Negation operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the negation.

Source code in pyorlib/algebra/element.py
def __neg__(self) -> "Element":
    """
    Negation operation.
    :return: A new `Element` instance representing the negation.
    """
    return self._build_expression(expression=-self.raw)

__pos__

__pos__() -> Element

Positive operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the positive value.

Source code in pyorlib/algebra/element.py
def __pos__(self) -> "Element":
    """
    Positive operation.
    :return: A new `Element` instance representing the positive value.
    """
    return self._build_expression(expression=+self.raw)

__abs__

__abs__() -> Element

Absolute value operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the absolute value.

Source code in pyorlib/algebra/element.py
def __abs__(self) -> "Element":
    """
    Absolute value operation.
    :return: A new `Element` instance representing the absolute value.
    """
    return self._build_expression(expression=abs(self.raw))

__eq__

__eq__(other: Any) -> Element

Equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __eq__(self, other: Any) -> "Element":  # type: ignore[override]
    """
    Equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw == other.raw)
    else:
        return self._build_expression(expression=self.raw == other)

__ne__

__ne__(other: Any) -> Element

Not equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __ne__(self, other: Any) -> "Element":  # type: ignore[override]
    """
    Not equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw != other.raw)
    else:
        return self._build_expression(expression=self.raw != other)

__lt__

__lt__(other: Any) -> Element

Less than comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __lt__(self, other: Any) -> "Element":
    """
    Less than comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw < other.raw)
    else:
        return self._build_expression(expression=self.raw < other)

__le__

__le__(other: Any) -> Element

Less than or equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __le__(self, other: Any) -> "Element":
    """
    Less than or equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw <= other.raw)
    else:
        return self._build_expression(expression=self.raw <= other)

__gt__

__gt__(other: Any) -> Element

Greater than comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __gt__(self, other: Any) -> "Element":
    """
    Greater than comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw > other.raw)
    else:
        return self._build_expression(expression=self.raw > other)

__ge__

__ge__(other: Any) -> Element

Greater than or equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __ge__(self, other: Any) -> "Element":
    """
    Greater than or equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw >= other.raw)
    else:
        return self._build_expression(expression=self.raw >= other)

__init__

__init__(name: str, value_type: ValueType, value: float)

Initializes a new instance of the Constant class.

PARAMETER DESCRIPTION
name

A string representing the name of the constant.

TYPE: str

value_type

A ValueType enumeration representing the type of value the constant can assume.

TYPE: ValueType

value

A float representing the value of the constant.

TYPE: float

Source code in pyorlib/algebra/terms/constants/constant.py
def __init__(self, name: str, value_type: ValueType, value: float):
    """
    Initializes a new instance of the Constant class.
    :param name: A string representing the name of the constant.
    :param value_type: A ValueType enumeration representing the type of value the constant can assume.
    :param value: A float representing the value of the constant.
    """
    # Calls the super init method.
    super().__init__(term_type=TermType.CONSTANT, value_type=value_type)

    # Applies validations
    if not name:
        raise TermException("Constant terms must have a name.")
    if value is None:
        raise TermException("Constant terms must have a value.")
    if value >= inf or value <= -inf:
        raise TermException("Constant terms value cannot be greater than or equal to [+/-]infinity.")
    if value_type == ValueType.BINARY and not ValueTypeValidator.is_binary(num=value):
        raise TermException("The value of a binary constant must be 0 or 1.")
    if value_type == ValueType.INTEGER and not ValueTypeValidator.is_integer(num=value):
        raise TermException("The value of an integer constant must be a valid integer.")

    # Instance attributes
    self._name: str = name
    """ The name of the constant. """

    self._value: float = value
    """ The internal value of the constant. """

get_pretty_string

get_pretty_string(float_precision: int = 6) -> str
Source code in pyorlib/algebra/terms/constants/constant.py
def get_pretty_string(self, float_precision: int = 6) -> str:  # pragma: no cover
    default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
    return "".join(
        [
            f"Name: {debug}{self.name}{default} | ",
            f"Type: {debug}{self.term_type.name.capitalize()}{default} | ",
            f"Value type: {debug}{self.value_type.name.capitalize()}{default} | ",
            f"Val:{debug} ",
            "{0:.{prec}g} ".format(self.value, prec=float_precision),
            f"{default}",
        ]
    )