Skip to content

SingleValueParameter class

Bases: Parameter

A data class representing a single-value parameter in an optimization model.

The SingleValueParameter class is a subclass of Parameter and is used to represent parameters with a single value in an optimization model. It provides properties to access the parameter value, lower bound, and upper bound.

Source code in pyorlib/structures/parameters/single_value_parameter.py
@dataclass(frozen=True)
class SingleValueParameter(Parameter):
    """
    A data class representing a single-value parameter in an optimization model.

    The `SingleValueParameter` class is a subclass of `Parameter` and is used to represent parameters with a single
    value in an optimization model. It provides properties to access the parameter value, lower bound, and upper bound.
    """

    value: float | None = None
    """ The value of the parameter, or None if no value is specified. """

    lower_bound: float | None = None
    """ The lower bound of the parameter, or None if no lower bound is specified. """

    upper_bound: float | None = None
    """ The upper bound of the parameter, or None if no upper bound is specified. """

    def __post_init__(self) -> None:
        # Parameter Validations
        if self.value_type is None:
            raise ValueError("Parameter value type is required.")

        if self.parameter_type == ParameterType.FIXED and self.value is not None:
            if self.lower_bound is not None or self.upper_bound is not None:
                raise ValueError("Parameters with value cannot has bounds.")
            if self.value >= 1e20 or self.value <= -1e20:
                raise ValueError("Parameter value cannot be infinity.")

            # Validate value and value type
            if self.value_type == ValueType.BINARY and not ValueTypeValidator.is_binary(self.value):
                raise ValueError("Parameter value must be a valid binary number.")
            elif self.value_type == ValueType.INTEGER and not ValueTypeValidator.is_integer(self.value):
                raise ValueError("Parameter value must be a valid integer number.")
        elif (
            self.parameter_type == ParameterType.BOUNDED
            and self.lower_bound is not None
            and self.upper_bound is not None
        ):
            if self.value is not None:
                raise ValueError("Parameters with bounds cannot has a value.")
            if self.upper_bound >= 1e20 or self.lower_bound <= -1e20:
                raise ValueError("Parameter upper bound cannot be infinity.")
            if self.lower_bound > self.upper_bound:
                raise ValueError("The upper bound of the parameter must be greater than or equal to the lower bound.")

            # Validate lower and upper bound values and value type
            if self.value_type == ValueType.BINARY and (
                not ValueTypeValidator.is_binary(self.lower_bound) or not ValueTypeValidator.is_binary(self.upper_bound)
            ):
                raise ValueError("Parameter lower and upper bound values must be valid binary numbers.")
            elif self.value_type == ValueType.INTEGER and (
                not ValueTypeValidator.is_integer(self.lower_bound)
                or not ValueTypeValidator.is_integer(self.upper_bound)
            ):
                raise ValueError("Parameter lower and upper bound values must be valid integer numbers.")
        else:
            raise ValueError("Invalid parameter.")

Attributes

parameter_type instance-attribute

parameter_type: ParameterType

The type of the parameter.

value_type instance-attribute

value_type: ValueType

The value type of the parameter values.

is_bounded property

is_bounded: bool

Returns a boolean indicating whether the parameter is bounded.

RETURNS DESCRIPTION
bool

True if the parameter is bounded, False otherwise.

value class-attribute instance-attribute

value: float | None = None

The value of the parameter, or None if no value is specified.

lower_bound class-attribute instance-attribute

lower_bound: float | None = None

The lower bound of the parameter, or None if no lower bound is specified.

upper_bound class-attribute instance-attribute

upper_bound: float | None = None

The upper bound of the parameter, or None if no upper bound is specified.

Functions

__init__

__init__(parameter_type: ParameterType, value_type: ValueType, value: float | None = None, lower_bound: float | None = None, upper_bound: float | None = None) -> None