
    i"                     t    S SK JrJr  S SKJrJr  S SKJrJrJ	r	  S SK
Jr  S SKJrJr  S SKJr   " S S5      rg	)
    )int_from_bytesint_to_bytes)ChoiceTchoice_permitted)ConjectureResultStatus_Overrun)ConjectureRunner)bits_to_bytesfind_integer)NO_SCOREc                       \ rS rSrSr SS\S\S\S\SS4
S	 jjr	SS
 jr
S\S\4S jr\S\4S j5       rS\\-  S\4S jrSS jrSrg)	Optimiser   u  A fairly basic optimiser designed to increase the value of scores for
targeted property-based testing.

This implements a fairly naive hill climbing algorithm based on randomly
regenerating parts of the test case to attempt to improve the result. It is
not expected to produce amazing results, because it is designed to be run
in a fairly small testing budget, so it prioritises finding easy wins and
bailing out quickly if that doesn't work.

For more information about targeted property-based testing, see
Löscher, Andreas, and Konstantinos Sagonas. "Targeted property-based
testing." Proceedings of the 26th ACM SIGSOFT International Symposium on
Software Testing and Analysis. ACM, 2017.
enginedatatargetmax_improvementsreturnNc                 B    Xl         X l        X0l        X@l        SU l        g)a  Optimise ``target`` starting from ``data``. Will stop either when
we seem to have found a local maximum or when the target score has
been improved ``max_improvements`` times. This limit is in place to
deal with the fact that the target score may not be bounded above.r   N)r   current_datar   r   improvements)selfr   r   r   r   s        g/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/hypothesis/internal/conjecture/optimiser.py__init__Optimiser.__init__#   s"       0    c                 $    U R                  5         g N)
hill_climbr   s    r   runOptimiser.run4   s    r   c                 V    UR                   R                  U R                  [        5      $ r   )target_observationsgetr   r   )r   r   s     r   score_functionOptimiser.score_function7   s    ''++DKKBBr   c                 8    U R                  U R                  5      $ r   )r'   r   r!   s    r   current_scoreOptimiser.current_score:   s    ""4#4#455r   c                    UR                   [        R                  :  a  g[        U[        5      (       d   eU R                  U5      nX R                  :  a  gX R                  :  a  U =R                  S-  sl        Xl        gX R                  :X  d   e[        UR                  5      [        U R                  R                  5      ::  a  Xl        gg)zdConsider a new data object as a candidate target. If it is better
than the current one, return True.F   T)statusr   VALID
isinstancer   r'   r*   r   r   lennodes)r   r   scores      r   consider_new_dataOptimiser.consider_new_data>   s     ;;%$ 01111##D)%%%%%%" $***** tzz?c$"3"3"9"9:: $r   c                   ^ ^^ [        5       nSn[        T R                  R                  5      S-
  mTS:  Ga  T R                  T R
                  ::  a  UT R                  La.  [        T R                  R                  5      S-
  mT R                  nTU;   a  TS-  mMk  UR                  T5        T R                  R                  T   nUR                  c   eUR                  S;  a  M  S[        S[        4UU 4S jjm[        U4S j5        [        U4S	 j5        TS:  a  T R                  T R
                  ::  a  M  gggg)
zThe main hill climbing loop where we actually do the work: Take
data, and attempt to improve its score for target. select_example takes
a data object and returns an index to an example where we should focus
our efforts.Nr-   r   >   bytesfloatbooleanintegerkr   c           
      d  > [        U 5      S:  a  gTR                  R                  T   nUR                  c   eUR                  (       a  gUR
                  S;   a7  [        UR                  [        [        45      (       d   eUR                  U -   nOUR
                  S:X  aT  [        UR                  [        5      (       d   e[        U 5      S:  a  gU S:X  a  SnU S:X  a  SnU S	:X  a  UR                  nOUR
                  S
:X  d   e[        UR                  [        5      (       d   e[        UR                  5      nX0-   S	:  a  gX0-  n[        [        UR                  5      [        UR!                  5       5      5      n[#        X45      n[%        WUR&                  5      (       d  g[)        S5       GH  nTR                  R*                  nUSUR                   U4-   XaR                  S-   S -   nTR,                  R/                  USS9nTR1                  U5      (       a    gUR2                  [4        R6                  L a    g[        U[8        5      (       d   e[        UR                  5      [        TR                  R                  5      :X  a    g[;        TR                  R<                  5       H  u  pU
R>                  UR                  S-   :  a    GM$  U
R@                  UR                  ::  a  MB  UR<                  U	   nU
RB                  URB                  :X  a  Mm  UR*                  UR>                  UR@                   nTR1                  TR,                  R/                  USUR                   U-   TR                  R*                  U
R@                  S -   5      5      (       d  M      g   GM     g)aW  
Try replacing the current node in the current best test case
with a value which is "k times larger", where the exact notion
of "larger" depends on the choice_type.

Note that we use the *current* best and not the one we started with.
This helps ensure that if we luck into a good draw when making
random choices we get to keep the good bits.
i   FN>   r8   r:   r9   r-   Tr   r7      full)extend)"absr   r2   index
was_forcedtyper0   valueintr8   boolr7   r   maxr1   r   
bit_lengthr   r   constraintsrangechoicesr   cached_test_functionr4   r.   r   OVERRUNr   	enumeratespansstartendchoice_count)r;   node
new_choicevsize_rL   attempt_choicesattemptjex
ex_attemptreplacementir   s                r   attempt_replace-Optimiser.hill_climb.<locals>.attempt_replacep   s    q6E> ((..q1zz---??  99 44%djj3,????!%aJYY)+%djj$77771vz$Bw%*
Av%)
Av%)ZZ
99///%djj%8888&tzz2Auqy$FA s4::alln0MND!-a!6J'
D4D4DEE qA"//77G$**-%-(!**q."234 $
 #kk>>' ? G --g66#~~7$%g/?@@@@7==)S1B1B1H1H-II$!*4+<+<+B+B!C88tzzA~5!66TZZ/$%,]]1%5
??j.E.EE$&-ooj6F6F&X11 KK<< '$** 5"-!."&"3"3";";BFFH"E!F  $(! "D+ "L r   c                    > T" U 5      $ r    r;   r`   s    r   <lambda>&Optimiser.hill_climb.<locals>.<lambda>   s
    ?1#5r   c                    > T" U * 5      $ r   rc   rd   s    r   re   rf      s    ?A2#6r   )setr1   r   r2   r   r   addrB   rD   rF   rG   r   )r   nodes_examinedprevrT   r`   r_   s   `   @@r   r    Optimiser.hill_climbT   s)    (,!!''(1,1f**d.C.CC4,,,))//014((N"Qq!$$**1-D::))) yy HHW3 W4 W Wv 5667] 1f**d.C.CCfCfr   )r   r   r   r   r   )d   )r   N)__name__
__module____qualname____firstlineno____doc__r
   r   strrF   r   r"   r8   r'   propertyr*   r	   rG   r4   r    __static_attributes__rc   r   r   r   r      s    ( !$   	
  
"C#3 C C 6u 6 6&6&A d ,x8r   r   N)hypothesis.internal.compatr   r   %hypothesis.internal.conjecture.choicer   r   #hypothesis.internal.conjecture.datar   r   r	   %hypothesis.internal.conjecture.enginer
   )hypothesis.internal.conjecture.junkdrawerr   r   %hypothesis.internal.conjecture.paretor   r   rc   r   r   <module>r|      s)    D K R R B Q :y8 y8r   