
    bi                     J    d Z ddlZddlmZ ddlmZ ddlmZ  G d de      Z	y)a,  
Copyright, the CVXPY authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)
Constraint)cvxtypes)scopesc                        e Zd ZdZddeddf fdZdefdZd Zddedefd	Z	ddedefd
Z
defdZed        Zedefd       Zed        Zed        Z xZS )	FiniteSeta  
    Constrain each entry of an Expression to take a value in a given set of real numbers.

    Parameters
    ----------
    expre : Expression
        The given expression to be constrained. This Expression must be affine.
        If ``expre`` has multiple elements, then the constraint is applied separately to
        each element. I.e., after solving a problem with this constraint, we should have:

        .. code-block:: python

            for e in expre.flatten():
                print(e.value in vec) # => True

    vec : Union[Expression, np.ndarray, set]
        The finite collection of values to which each entry of ``expre``
        is to be constrained.

    ineq_form : bool
        Controls how this constraint is canonicalized into mixed integer linear
        constraints.

        If True, then we use a formulation with ``vec.size - 1`` inequality constraints,
        one equality constraint, and ``vec.size - 1`` binary variables for each element
        of ``expre``.

        If False, then we use a formulation with ``vec.size`` binary variables and two
        equality constraints for each element of ``expre``.

        Defaults to False. The case ``ineq_form=True`` may speed up some mixed-integer
        solvers that use simple branch and bound methods.
    N	ineq_formreturnc                 p   t        j                         }t        |t              rt	        |      }|j                  |      j                  d      }|j                         s*|j                         sd|j                  z  }t        |      || _        || _        || _        t        t        | C  ||g|       y )NForderze
            Provided Expression must be affine or log-log affine, but had curvature %s.
            )r   
expression
isinstancesetlistcast_to_constflatten	is_affineis_log_log_affine	curvature
ValueErrorexprevec
_ineq_formsuperr   __init__)selfr   r   r   	constr_id
Expressionmsg	__class__s          W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/finite_set.pyr   zFiniteSet.__init__:   s    ((*
c3s)C&&s+33#3> )@)@)B//"C S/! 
#i'i@    c                 H    d| j                   d   d| j                   d   dS )Nz
FiniteSet(r   z,    ))argsr   s    r"   namezFiniteSet.nameK   s    &*iilDIIaLAAr#   c                 2    | j                   | j                  gS N)r   idr(   s    r"   get_datazFiniteSet.get_dataN   s    ))r#   dppc                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)zX
        A FiniteSet constraint is DCP if the constrained expression is affine.
        r   N)r   	dpp_scoper'   r   )r   r.   s     r"   is_dcpzFiniteSet.is_dcpQ   sX     !!# 0yy|--/0 0yy|%%''0 0s   AA$c                    | j                   j                         ry| j                   j                  }|rRt        j                         5  | j
                  j                         xr t        j                  |dkD        cd d d        S | j
                  j                         xr t        j                  |dkD        S # 1 sw Y   y xY w)NFr   )	r   
parametersvaluer   r0   r   r   npall)r   r.   vec_vals      r"   is_dgpzFiniteSet.is_dgpZ   s    88 ((..!!# Nzz335M"&&1:MN N :://1IbffWq[6IIN Ns   4B::Cc                 "    | j                         S r+   )r1   r(   s    r"   is_dqcpzFiniteSet.is_dqcpg   s    {{}r#   c                 .    | j                   j                  S r+   )r   sizer(   s    r"   r<   zFiniteSet.sizej   s    zzr#   c                     | j                   S )z
        Choose between two constraining methodologies, use ``ineq_form=False`` while
        working with ``Parameter`` types.
        )r   r(   s    r"   r   zFiniteSet.ineq_formn   s     r#   c                 .    | j                   j                  S r+   )r   shaper(   s    r"   r?   zFiniteSet.shapev   s    zzr#   c           	      4   t        j                  | j                  j                        j	                  d      }| j
                  j                  }|D cg c]-  }t        j                  t        j                  ||z
              / }}t        |      }|S c c}w )z`
        The residual of the constraint.

        Returns
        -------
        float
        r   r   )	r5   arrayr   r4   r   r   minabsmax)r   expr_valr7   valresidsress         r"   residualzFiniteSet.residualz   st     88DJJ,,-55C5@((..;CDC"&&g./DD&k
 Es   2B)FN)F)__name__
__module____qualname____doc__boolr   strr)   r-   r1   r8   r:   propertyr<   r   r?   rI   __classcell__)r!   s   @r"   r   r      s     DAd At A"Bc B*($ (4 (J$ J4 J    4        r#   r   )
rM   numpyr5   cvxpy.constraints.constraintr   cvxpy.expressionsr   cvxpy.utilitiesr   r    r#   r"   <module>rW      s&     3 & "p
 pr#   