
    bi              	           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
 ddlmZ d ZddZdefdZ	 	 dd	Z	 dd
Z	 	 ddedededefd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

    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)Minimize)failure_solutionc           	          t         j                  j                  t        d      | j                  | j
                  D cg c]	  } |        c}z         S c c}w )zEvaluates lazy constraints.r   )problemsproblemProblemr   constraints_lazy_constraints)r   cs     ]/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/solvers/bisection.py_lower_problemr      sH    ##QK0I0I"J113"JJL L"Js   Areturnc                     t        j                         5  t        j                  dd       | j                  |       d d d        y # 1 sw Y   y xY w)Nignorez.*DPP.*)messagesolver)warningscatch_warningsfilterwarningssolve)r   r   s     r   _solver       sA    		 	 	" %*=V$% % %s   *AAc                 f    | d u xs, | j                   t        j                  t        j                  fv S )N)statuss
INFEASIBLEINFEASIBLE_INACCURATE)r   s    r   _infeasibler   '   s6    d? Jgnn121H1H1J J J    c                    ||j                         rdnd}||j                         rdnd}|j                         }|j                         }t        |      D ]  }|sr||_        t	        |       }	t        |	|       t        |	      r|}|dz  }6|	j                  t        j                  v rd}n"t        j                  d|	j                  z        |ss||_        t	        |       }	t        |	|       t        |	      rd}nF|	j                  t        j                  v r|}|dz  }t        j                  d|	j                  z        |s|s||fc S  t        j                  d      )	z Finds an interval for bisection.r         TSolver failed with status %sr   zOUnable to find suitable interval for bisection; your problem may be unbounded..)	is_nonneg	is_nonposrangevaluer   r   r   r   r   SOLUTION_PRESENTerrorSolverError)
r   tr   lowhigh	max_itersinfeasible_lowfeasible_high_lowereds
             r   _find_bisection_intervalr4   1   sb    {;;=ab|KKMqq[[]NKKMM9 AG$W-G7F#7#	1#5#55 $''2W^^CE E AG$W-G76*7#!%1#5#55q''2W^^CE E m9=@ 

./ /r   c
                    d}
d}t        |	      D ]*  }||k  sJ |||z
  |k  r|||fc S ||z   dz  }|r8||
z  dk(  r0t        d||fz         t        d||fz         t        d||fz         ||_        t        |       }t	        ||       t        |      r!|r||
z  dk(  rt        d	|z          ||      }|j                  t        j                  v r9|r"||
z  dk(  rt        d
||j                  fz         |j                  } ||      }|rt        d       t        j                  d|j                  z         t        j                  d      )z&Bisect `problem` on the parameter `t`.   N       @r   z!(iteration %d) lower bound: %0.6fz!(iteration %d) upper bound: %0.6fz"(iteration %d) query point: %0.6f r   z%(iteration %d) query was infeasible.
z'(iteration %d) query was feasible. %s)
z Aborting; the solver failed ...
r$   zMax iters hit during bisection.)r'   printr(   r   r   r   r   r   r)   solutionr*   r+   )r   r   r,   r-   r.   tighten_lowertighten_higherepsverboser/   verbose_freqsolniquery_ptr3   s                  r   _bisectrB   `   s    LD9 Ad{{ 3
 d?"$J#%q<'1,5C@A5D	AB6!XFG )wv&w1|+q0>BC)C^^q1111|+q0@'**+, -##D!(+D9:##.?A A=A@ 

=
>>r   r<   r=   r/   max_iters_interval_searchc                 
   t        | d      st        d      | j                  \  }}	}
}|rt        dt	        |       z         t	        |      }t        ||       t        |      r&|rt        d       t        t        j                        S || |rt        d       t        | |	||||      \  }}|rt        d|z         t        d|z         t        | ||	|||
||||
      \  }}}||z   dz  |_        |rt        d	||fz         |S )
a  Bisection on a one-parameter family of DCP problems.

    Bisects on a one-parameter family of DCP problems emitted by `Dqcp2Dcp`.

    Parameters
    ------
    problem : Problem
        problem emitted by Dqcp2Dcp
    solver : Solver
        solver to use for bisection
    low : float
        lower bound for bisection (optional)
    high : float
        upper bound for bisection (optional)
    eps : float
        terminate bisection when width of interval is < eps
    verbose : bool
        whether to print verbose output related to the bisection
    max_iters : int
        the maximum number of iterations to run bisection

    Returns
    -------
    A Solution object.
    _bisection_dataz3`bisect` only accepts problems emitted by Dqcp2Dcp.zr
********************************************************************************
Preparing to bisect problem

%s
zProblem is infeasible.z"Finding interval for bisection ...zinitial lower bound: %0.6fzinitial upper bound: %0.6f
r7   zBisection completed, with lower bound %0.6f and upper bound %0.7f
********************************************************************************
)hasattr
ValueErrorrE   r8   r   r   r   r   r   r   r4   rB   opt_val)r   r   r-   r.   r<   r=   r/   rC   feas_problemr,   r:   r;   lowered_feasr?   s                 r   bisectrK      s4   6 7-.NOO5<5L5L2L!]N 46DW6MN 	O ",/L
< < *+--
{dl67,Wad-FH	T*S01,t34C}nWi!OD#t $J#%DL 9 d 	 Kr   )r   N)NNNd   )ư>FrL   )NNNrM   FrL   rL   )__doc__r   cvxpy.errorr*   cvxpy.problemsr   cvxpy.settingssettingsr   cvxpy.problems.objectiver   cvxpy.reductions.solutionr   r   r   boolr   r4   rB   floatintrK    r   r   <module>rY      s      !  - 6L%JD J FJ'*,/` 03&?R Z_BE=5 =RV ==<?=r   