
    bi*              	           d Z ddlmZmZmZ ddlmZ ddlm	Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ 	 	 	 dd
edeee      deee      ddfdZ G d de      Zy)a,  
Copyright 2013 Steven Diamond

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

    http://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.
    )ListOptionalTupleN)sumtrace)Constant)
Expression)Variable)MaximizeMinimize)Problemprobopt_varsdont_opt_varsreturnPartialProblemc           	      @   ||t        d      |D|D cg c]  }t        |       }}| j                         D cg c]  }t        |      |vs| }}n|D|D cg c]  }t        |       }}| j                         D cg c]  }t        |      |vs| }}nL|J|H||z   D cg c]  }t        |       }}| j                         D ]  }t        |      |vst        d       |D ci c],  }t        |      t        |j                  fi |j
                  . }}| j                  j                  |      }| j                  D 	cg c]  }	|	j                  |       }
}	t        ||
      }t        ||||fi |S c c}w c c}w c c}w c c}w c c}w c c}w c c}	w )aw  Partially optimizes the given problem over the specified variables.

    Either opt_vars or dont_opt_vars must be given.
    If both are given, they must contain all the variables in the problem.

    Partial optimize is useful for two-stage optimization and graph implementations.
    For example, we can write

    .. code :: python

        x = Variable(n)
        t = Variable(n)
        abs_x = partial_optimize(Problem(Minimize(sum(t)),
                  [-t <= x, x <= t]), opt_vars=[t])

    to define the entrywise absolute value of x.

    Parameters
    ----------
    prob : Problem
        The problem to partially optimize.
    opt_vars : list, optional
        The variables to optimize over.
    dont_opt_vars : list, optional
        The variables to not optimize over.
    solver : str, optional
        The default solver to use for value and grad.
    kwargs : keywords, optional
        Additional solver specific keyword arguments.

    Returns
    -------
    Expression
        An expression representing the partial optimization.
        Convex for minimization objectives and concave for maximization objectives.
    z@partial_optimize called with neither opt_vars nor dont_opt_vars.z`If opt_vars and new_opt_vars are both specified, they must contain all variables in the problem.)
ValueErrorid	variablesr
   shape
attributes	objective	tree_copyconstraintsr   r   )r   r   r   solverkwargsvaridsid_to_new_varnew_objconnew_constrsnew_var_probs               \/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/transforms/partial_optimize.pypartial_optimizer&      s   X M1N
 	
 
	"/03r#w00#'>>#3JCr#wc7ICJJ		"*+3r#w++(,(8OBsG3<NOO		-";"*]":;3r#w;;>># 	C#wc! G 	 EMN=@ Whsyy 9),9 9 NM Nnn&&}5G"..0 ==/ 0K 07K0L,-R6RR- 1J ,O;N0s4   E=FFFFF'F,1FFc                   P    e Zd ZdZdedee   dee   ddf fdZd Zde	fd	Z
de	fd
Zde	fdZddede	fdZde	fdZde	fdZde	fdZde	fdZde	fdZde	fdZedeedf   fd       ZdefdZdee   fdZd Zdee   fdZed        Zed        Zed        Z d Z! xZ"S )r   zA partial optimization problem.

    Attributes
    ----------
    opt_vars : list
        The variables to optimize over.
    dont_opt_vars : list
        The variables to not optimize over.
    r   r   r   r   Nc                 r    || _         || _        || _        |g| _        || _        t
        t        |           y )N)r   r   r   args_solve_kwargssuperr   __init__)selfr   r   r   r   r   	__class__s         r%   r,   zPartialProblem.__init__s   s9     !*F	#nd,.    c                 H    | j                   | j                  | j                  gS )zLReturns info needed to reconstruct the expression besides the args.
        )r   r   r   r-   s    r%   get_datazPartialProblem.get_data}   s     t114;;??r/   c                 T    t        | j                  d   j                               dk(  S )Nr   )lenr)   r   r1   s    r%   is_constantzPartialProblem.is_constant   s#    499Q<))+,11r/   c                     | j                   d   j                         xr) t        | j                   d   j                        t        k(  S z"Is the expression convex?
        r   )r)   is_dcptyper   r   r1   s    r%   	is_convexzPartialProblem.is_convex   =     yy|""$ 51''(H4	5r/   c                     | j                   d   j                         xr) t        | j                   d   j                        t        k(  S )z#Is the expression concave?
        r   )r)   r8   r9   r   r   r1   s    r%   
is_concavezPartialProblem.is_concave   r;   r/   contextc                 z    |j                         dv r| j                  d   j                  |      S t        d|      )zBThe expression is a disciplined parameterized expression.
        )dcpdgpr   zUnsupported context)lowerr)   is_dppr   )r-   r>   s     r%   rC   zPartialProblem.is_dpp   s9     ==?n,99Q<&&w//2G<<r/   c                     | j                   d   j                         xr) t        | j                   d   j                        t        k(  S r7   )r)   is_dgpr9   r   r   r1   s    r%   is_log_log_convexz PartialProblem.is_log_log_convex   r;   r/   c                     | j                   d   j                         xr) t        | j                   d   j                        t        k(  S r7   )r)   rE   r9   r   r   r1   s    r%   is_log_log_concavez!PartialProblem.is_log_log_concave   r;   r/   c                 j    | j                   d   j                  j                   d   j                         S )z'Is the expression nonnegative?
        r   )r)   r   	is_nonnegr1   s    r%   rJ   zPartialProblem.is_nonneg   ,     yy|%%**1-7799r/   c                 j    | j                   d   j                  j                   d   j                         S )z'Is the expression nonpositive?
        r   )r)   r   	is_nonposr1   s    r%   rM   zPartialProblem.is_nonpos   rK   r/   c                      y)zIs the Leaf imaginary?
        F r1   s    r%   is_imagzPartialProblem.is_imag        r/   c                      y)z$Is the Leaf complex valued?
        FrO   r1   s    r%   
is_complexzPartialProblem.is_complex   rQ   r/   .c                     t               S )z=Returns the (row, col) dimensions of the expression.
        )tupler1   s    r%   r   zPartialProblem.shape   s     wr/   c                 (    d| j                   d    dS )z=Returns the string representation of the expression.
        zPartialProblem(r   ))r)   r1   s    r%   namezPartialProblem.name   s     !1a00r/   c                 <    | j                   d   j                         S )z.Returns the variables in the problem.
        r   )r)   r   r1   s    r%   r   zPartialProblem.variables        yy|%%''r/   c                 <    | j                   d   j                         S )z/Returns the parameters in the problem.
        r   )r)   
parametersr1   s    r%   r\   zPartialProblem.parameters   s     yy|&&((r/   c                 <    | j                   d   j                         S )z.Returns the constants in the problem.
        r   )r)   	constantsr1   s    r%   r^   zPartialProblem.constants   rZ   r/   c                    | j                         rt        j                  j                  |       S | j	                         D ci c]  }|j
                  |j                   }}g }| j                  D ]B  }|j                  !t        j                  j                  |       c S |||j                  k(  gz  }D t        | j                  d   j                  || j                  d   j                  z         } |j                  dd| j                  i| j                   |j                   t"        j$                  v r| j'                         | j)                         z
  }| j                  d   j                  j                  d   }| j                  d   j                  D ]f  }| j+                  ||j,                  z        }|j.                  |j0                  z  }	|	j3                         r|t5        |	      z  }Y|t7        |	      z  }h |j                  }
| j                  D ci c]  }||
|   
 }}nt        j                  j                  |       }| j	                         D ]  }||j
                     |_         |S c c}w c c}w )a-  Gives the (sub/super)gradient of the expression w.r.t. each variable.

        Matrix expressions are vectorized, so the gradient is a matrix.
        None indicates variable values unknown or outside domain.

        Returns:
            A map of variable to SciPy CSC sparse matrix or None.
        r   r   rO   )r5   ugradconstant_gradr   r   valuer   
error_gradr   r)   r   r   solver   r*   statussSOLUTION_PRESENTr:   r=   cast_to_const
dual_valueTexpr	is_scalarr   r   )r-   r   old_valsfix_varsr   signlagrconstrlagr_multiplierprodgrad_mapresults               r%   ra   zPartialProblem.grad   s   & 66''--151AB#CFFCII%BB%% 	/Cyy vv((..SCII-..		/
 tyy|--$))A,":"::<

<$++<););<;;!,,,>>#doo&77D99Q<))..q1D))A,22 ("&"4"4TF<M<M5M"N&((6;;6>>#CI%DE$K'D( yyH484F4FGSc8C=(GFGVV&&t,F>># 	)C (CI	)= C0 Hs   I3I8c                     | j                   d   j                  j                   d   }| j                   d   j                  |j                  z   S )znA list of constraints describing the closure of the region
           where the expression is finite.
        r   )r)   r   r   domain)r-   obj_exprs     r%   rx   zPartialProblem.domain	  s?     99Q<))..q1yy|''(//99r/   c                     | j                         D ci c]  }|j                  |j                   }}g }| j                  D ]#  }|j                   y|||j                  k(  gz  }% t	        | j
                  d   j                  || j
                  d   j                  z         } |j                  dd| j                  i| j                   | j                         D ]  }||j                     |_         |j                  j                  S c c}w )zoReturns the numeric value of the expression.

        Returns:
            A numpy matrix or a scalar.
        Nr   r   rO   )r   r   rc   r   r   r)   r   r   re   r   r*   	_solutionopt_val)r-   r   rn   ro   r   s        r%   rc   zPartialProblem.value  s     261AB#CFFCII%BB%% 	/Cyy SCII-..		/
 tyy|--x$))A,:R:R/RS

<$++<););<>># 	)C (CI	) ~~%%% Cs   Dc                     | j                   d   j                  j                   d   j                  \  }}| j                   d   j                  D ]  }||j                  d   z  } ||fS )zReturns the graph implementation of the object.

        Change the ids of all the opt_vars.

        Returns
        -------
            A tuple of (affine expression, [constraints]).
        r      )r)   r   canonical_formr   )r-   objconstrsconss       r%   canonicalizezPartialProblem.canonicalize)  sh     yy|--2215DDWIIaL,, 	.Dt**1--G	.W~r/   )r@   )#__name__
__module____qualname____doc__r   r   r
   r,   r2   boolr5   r:   r=   strrC   rF   rH   rJ   rM   rP   rS   propertyr   intr   rX   r   r\   r   r^   ra   rx   rc   r   __classcell__)r.   s   @r%   r   r   h   sP   //+/>/>/@D/@
2T 254 55D 5=c =d =54 55D 5:4 :
:4 :
 
D 
 uS#X  
1c 1
(4> (
)
(4> (
 3 3j : : & &,r/   )NNN)r   typingr   r   r   cvxpy.settingssettingsrg   cvxpy.utilities	utilitiesr`   cvxpy.atomsr   r   $cvxpy.expressions.constants.constantr   cvxpy.expressions.expressionr	   cvxpy.expressions.variabler
   cvxpy.problems.objectiver   r   cvxpy.problems.problemr   r&   r   rO   r/   r%   <module>r      s     ) (   " 9 3 / 7 *
 *..2	HS
HStH~&HS DN+HS HSVOZ Or/   