
    bi                     V    d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
  G d de      Zy)	a,  
Copyright 2018 Akshay Agrawal

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.
    N)settings)
Expression)Canonicalization)DgpCanonMethodsc                   X     e Zd ZdZd
d fdZd Z fdZ	 ddedede	fdZ
 fd	Z xZS )Dgp2Dcpa,  Reduce DGP problems to DCP problems.

    This reduction takes as input a DGP problem and returns an equivalent DCP
    problem. Because every (generalized) geometric program is a DGP problem,
    this reduction can be used to convert geometric programs into convex form.

    Example
    -------

    >>> import cvxpy as cp
    >>>
    >>> x1 = cp.Variable(pos=True)
    >>> x2 = cp.Variable(pos=True)
    >>> x3 = cp.Variable(pos=True)
    >>>
    >>> monomial = 3.0 * x_1**0.4 * x_2 ** 0.2 * x_3 ** -1.4
    >>> posynomial = monomial + 2.0 * x_1 * x_2
    >>> dgp_problem = cp.Problem(cp.Minimize(posynomial), [monomial == 4.0])
    >>>
    >>> dcp2cone = cvxpy.reductions.Dcp2Cone()
    >>> assert not dcp2cone.accepts(dgp_problem)
    >>>
    >>> gp2dcp = cvxpy.reductions.Dgp2Dcp(dgp_problem)
    >>> dcp_problem = gp2dcp.reduce()
    >>>
    >>> assert dcp2cone.accepts(dcp_problem)
    >>> dcp_problem.solve()
    >>>
    >>> dgp_problem.unpack(gp2dcp.retrieve(dcp_problem.solution))
    >>> print(dgp_problem.value)
    >>> print(dgp_problem.variables())
    c                 0    t         t        |   d |       y )N)canon_methodsproblem)superr   __init__)selfr   	__class__s     [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/dgp2dcp/dgp2dcp.pyr   zDgp2Dcp.__init__9   s     	gt%D'%J    c                 "    |j                         S )z,A problem is accepted if it is DGP.
        )is_dgp)r   r   s     r   acceptszDgp2Dcp.accepts>   s     ~~r   c                     | j                  |      st        d      t               | _        t        t
        |   |      \  }}||_        ||fS )z1Converts a DGP problem to a DCP problem.
        z The supplied problem is not DGP.)r   
ValueErrorr   r
   r   r   apply_problem)r   r   equiv_probleminverse_datar   s       r   r   zDgp2Dcp.applyC   sQ     ||G$?@@,.&+GT&@&I#| 'l**r   exprargscanonicalize_paramsc                     t        |      | j                  v r | j                  t        |         ||      S |j                  |      g fS )aw  Canonicalize an expression, w.r.t. canonicalized arguments.
        
        Args:
            expr: Expression to canonicalize.
            args: Arguments to the expression.
            canonicalize_params: Should constant subtrees 
                containing parameters be canonicalized?
        
        Returns:
            canonicalized expression, constraints
        )typer
   copy)r   r   r   r   s       r   canonicalize_exprzDgp2Dcp.canonicalize_exprN   sG    " :+++14%%d4j1$==99T?B&&r   c                 >   t         t        |   ||      }|j                  t        j
                  k(  r|S |j                  j                         D ]'  \  }}t        j                  |      |j                  |<   ) t        j                  |j                        |_
        |S N)r   r   invertstatusr   SOLVER_ERRORprimal_varsitemsnpexpopt_val)r   solutionr   vidvaluer   s        r   r$   zDgp2Dcp.invertd   s    $.xF??h333O"..446 	6JC(*uH  %	6 66("2"23r   r#   )returnN)T)__name__
__module____qualname____doc__r   r   r   r   listboolr!   r$   __classcell__)r   s   @r   r   r      sI    @K
 
	+ )-	'' ' "&	', r   r   )r3   numpyr)   cvxpyr   cvxpy.expressions.expressionr   !cvxpy.reductions.canonicalizationr   'cvxpy.reductions.dgp2dcp.canonicalizersr   r    r   r   <module>r=      s*      3 > CT Tr   