Skip to content

QuadraticConstraint

Bases: Constraint

Representation of a quadratic constraint.

Source code in q3as/quadratic/problems/quadratic_constraint.py
class QuadraticConstraint(Constraint):
    """Representation of a quadratic constraint."""

    # Note: added, duplicating in effect that in Constraint, to avoid issues with Sphinx
    Sense = ConstraintSense

    def __init__(
        self,
        quadratic_program: Any,
        name: str,
        linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
        quadratic: Union[
            ndarray,
            spmatrix,
            List[List[float]],
            Dict[Tuple[Union[int, str], Union[int, str]], float],
        ],
        sense: ConstraintSense,
        rhs: float,
    ) -> None:
        """Constructs a quadratic constraint, consisting of a linear and a quadratic term.

        Args:
            quadratic_program: The parent quadratic program.
            name: The name of the constraint.
            linear: The coefficients specifying the linear part of the constraint.
            quadratic: The coefficients specifying the linear part of the constraint.
            sense: The sense of the constraint.
            rhs: The right-hand-side of the constraint.
        """
        super().__init__(quadratic_program, name, sense, rhs)
        self._linear = LinearExpression(quadratic_program, linear)
        self._quadratic = QuadraticExpression(quadratic_program, quadratic)

    @property
    def linear(self) -> LinearExpression:
        """Returns the linear expression corresponding to the left-hand-side of the constraint.

        Returns:
            The left-hand-side linear expression.
        """
        return self._linear

    @linear.setter
    def linear(
        self,
        linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
    ) -> None:
        """Sets the linear expression corresponding to the left-hand-side of the constraint.
        The coefficients can either be given by an array, a (sparse) 1d matrix, a list or a
        dictionary.

        Args:
            linear: The linear coefficients of the left-hand-side.
        """

        self._linear = LinearExpression(self.quadratic_program, linear)

    @property
    def quadratic(self) -> QuadraticExpression:
        """Returns the quadratic expression corresponding to the left-hand-side of the constraint.

        Returns:
            The left-hand-side quadratic expression.
        """
        return self._quadratic

    @quadratic.setter
    def quadratic(
        self,
        quadratic: Union[
            ndarray,
            spmatrix,
            List[List[float]],
            Dict[Tuple[Union[int, str], Union[int, str]], float],
        ],
    ) -> None:
        """Sets the quadratic expression corresponding to the left-hand-side of the constraint.
        The coefficients can either be given by an array, a (sparse) matrix, a list or a
        dictionary.

        Args:
            quadratic: The quadratic coefficients of the left-hand-side.
        """
        self._quadratic = QuadraticExpression(self.quadratic_program, quadratic)

    def evaluate(self, x: Union[ndarray, List, Dict[Union[int, str], float]]) -> float:
        """Evaluate the left-hand-side of the constraint.

        Args:
            x: The values of the variables to be evaluated.

        Returns:
            The left-hand-side of the constraint given the variable values.
        """
        return self.linear.evaluate(x) + self.quadratic.evaluate(x)

    def __repr__(self):
        # pylint: disable=cyclic-import
        from ..translators.prettyprint import expr2str, DEFAULT_TRUNCATE

        lhs = expr2str(
            linear=self.linear, quadratic=self.quadratic, truncate=DEFAULT_TRUNCATE
        )
        return f"<{self.__class__.__name__}: {lhs} {self.sense.label} {self.rhs} '{self.name}'>"

    def __str__(self):
        # pylint: disable=cyclic-import
        from ..translators.prettyprint import expr2str

        lhs = expr2str(linear=self.linear, quadratic=self.quadratic)
        return f"{lhs} {self.sense.label} {self.rhs} '{self.name}'"

linear: LinearExpression property writable

Returns the linear expression corresponding to the left-hand-side of the constraint.

Returns:

Type Description
LinearExpression

The left-hand-side linear expression.

quadratic: QuadraticExpression property writable

Returns the quadratic expression corresponding to the left-hand-side of the constraint.

Returns:

Type Description
QuadraticExpression

The left-hand-side quadratic expression.

__init__(quadratic_program, name, linear, quadratic, sense, rhs)

Constructs a quadratic constraint, consisting of a linear and a quadratic term.

Parameters:

Name Type Description Default
quadratic_program Any

The parent quadratic program.

required
name str

The name of the constraint.

required
linear Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]]

The coefficients specifying the linear part of the constraint.

required
quadratic Union[ndarray, spmatrix, List[List[float]], Dict[Tuple[Union[int, str], Union[int, str]], float]]

The coefficients specifying the linear part of the constraint.

required
sense ConstraintSense

The sense of the constraint.

required
rhs float

The right-hand-side of the constraint.

required
Source code in q3as/quadratic/problems/quadratic_constraint.py
def __init__(
    self,
    quadratic_program: Any,
    name: str,
    linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
    quadratic: Union[
        ndarray,
        spmatrix,
        List[List[float]],
        Dict[Tuple[Union[int, str], Union[int, str]], float],
    ],
    sense: ConstraintSense,
    rhs: float,
) -> None:
    """Constructs a quadratic constraint, consisting of a linear and a quadratic term.

    Args:
        quadratic_program: The parent quadratic program.
        name: The name of the constraint.
        linear: The coefficients specifying the linear part of the constraint.
        quadratic: The coefficients specifying the linear part of the constraint.
        sense: The sense of the constraint.
        rhs: The right-hand-side of the constraint.
    """
    super().__init__(quadratic_program, name, sense, rhs)
    self._linear = LinearExpression(quadratic_program, linear)
    self._quadratic = QuadraticExpression(quadratic_program, quadratic)

evaluate(x)

Evaluate the left-hand-side of the constraint.

Parameters:

Name Type Description Default
x Union[ndarray, List, Dict[Union[int, str], float]]

The values of the variables to be evaluated.

required

Returns:

Type Description
float

The left-hand-side of the constraint given the variable values.

Source code in q3as/quadratic/problems/quadratic_constraint.py
def evaluate(self, x: Union[ndarray, List, Dict[Union[int, str], float]]) -> float:
    """Evaluate the left-hand-side of the constraint.

    Args:
        x: The values of the variables to be evaluated.

    Returns:
        The left-hand-side of the constraint given the variable values.
    """
    return self.linear.evaluate(x) + self.quadratic.evaluate(x)