
    bi              	           d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
 deeeef      deeef   fdZ	 	 ddeeeef      d	edeeef   fd
Zdeeeef      defdZ	 ddeeeef      dedefdZy)a,  
Copyright 2017 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UnionN)MaximizeMinimize)	indicator
objectivesreturnc                 X     t               }t         fdt        |      D              S )zCombines objectives as a weighted sum.

    Args:
      objectives: A list of Minimize/Maximize objectives.
      weights: A vector of weights.

    Returns:
      A Minimize/Maximize objective.
    c              3   4   K   | ]  }|   |   z    y w)N ).0ir   weightss     U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/transforms/scalarize.py	<genexpr>zweighted_sum.<locals>.<genexpr>"   s     AAz!}WQZ'As   )lensumrange)r   r   num_objss   `` r   weighted_sumr      s#     :HAxAAA    
off_targetc           	      `   t        |       t        |      k(  sJ d       t        |       t        |      k(  sJ d       |t        |       t        |      k(  sJ d       |dk\  sJ d       t        |       }g }t        |      D ]  }| |   ||   |||   nd}
}	}||   dk  r| |	 |
|
 nd}
}	}|j                  d   j                         rdnd}||j                  d   ||   z
  z  }|t	        ||         |z
  z  t        j                  |      z  }|||j                  d   z  z  }|+||t        ||j                  d   z  |||   z  k  g      z  z  }|j                  |        t        |      }|j                         rt        |      S |j                         rt        |      S t        d	      )
a  
    Combines objectives with penalties within a range between target and limit.

    For nonnegative priorities, each Minimize objective i has value

        off_target*objectives[i] when objectives[i] < targets[i]

        (priorities[i]-off_target)*objectives[i] when targets[i] <= objectives[i] <= limits[i]

        +infinity when objectives[i] > limits[i]

    and each Maximize objective i has value

        off_target*objectives[i] when objectives[i] > targets[i]

        (priorities[i]-off_target)*objectives[i] when targets[i] >= objectives[i] >= limits[i]

        -infinity when objectives[i] < limits[i]

    A negative priority flips the objective sense, i.e., we 
    use -objectives[i], -targets[i], and -limits[i] with abs(priorities[i]).

    Args:
      objectives: A list of Minimize/Maximize objectives.
      priorities: The weight within the trange.
      targets: The start (end) of penalty for Minimize (Maximize)
      limits: Optional hard end (start) of penalty for Minimize (Maximize)
      off_target: Penalty outside of target.

    Returns:
      A Minimize/Maximize objective.

    Raises:
      ValueError: If the scalarized objective is neither convex nor concave.
    z/Number of objectives and priorities must match.z,Number of objectives and targets must match.Nz+Number of objectives and limits must match.r   z,The off_target argument must be nonnegative.   z3Scalarized objective is neither convex nor concave.)r   r   args	is_convexabsatomsposr   appendr   r   
is_concaver   
ValueError)r   
prioritiestargetslimitsr   r   new_objsr   objtarlimsigndeltaexprobj_exprs                  r   targets_and_prioritiesr/   %   s   T z?c*o-`/``-z?c'l*Z,ZZ*:#f+-\/\\-?JJJ?:H02H8_ "1wqz@R6!9X\#Sa=1 D3$TcCHHQK))+qchhqkGAJ./SA'*45eii6FF
388A;&&DD!$4VAY$F#GHHHD 8}H!!				!!NOOr   c                     t        |       }t        j                  t        |      D cg c]  }| |   ||   z  j                  d    c} }t        |      S c c}w )zCombines objectives as max of weighted terms.

    Args:
      objectives: A list of Minimize/Maximize objectives.
      weights: A vector of weights.

    Returns:
      A Minimize objective.
    r   )r   r   maximumr   r   r   )r   r   r   r   r-   s        r   maxr2   m   sR     :H==uXW!JqM'!*4::1=WXDD> Xs   Agammac                     t        |       }t        |      D cg c]  }| |   ||   z  j                  d    }}t        j                  |t        j
                  |      z        |z  }t        |      S c c}w )a  Combines objectives as log_sum_exp of weighted terms.


    The objective takes the form
        log(sum_{i=1}^n exp(gamma*weights[i]*objectives[i]))/gamma
    As gamma goes to 0, log_sum_exp approaches weighted_sum. As gamma goes to infinity,
    log_sum_exp approaches max.

    Args:
      objectives: A list of Minimize/Maximize objectives.
      weights: A vector of weights.
      gamma: Parameter interpolating between weighted_sum and max.

    Returns:
      A Minimize objective.
    r   )r   r   r   r   log_sum_expvstackr   )r   r   r3   r   r   termsr-   s          r   r5   r5   |   so    & :H9>xIAjmGAJ&,,Q/IEIU5<<#667=DD> Js   A3)Ngh㈵>)g      ?)__doc__typingr   r   cvxpy.atomsr   cvxpy.problems.objectiver   r   cvxpy.transformsr   r   floatr/   r2   r5   r   r   r   <module>r>      s      7 &BT%((:";< B%PXZbPbJc B$ 
FP58+,-FP
 FP 8XFPPDx123    HK58+,-?Dr   