Ë
    iKêj"  ã                   ót   — d dl mZmZ d dlmZmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ  G d„ d«      Zy	)
é    )Úint_from_bytesÚint_to_bytes)ÚChoiceTÚchoice_permitted)ÚConjectureResultÚStatusÚ_Overrun)ÚConjectureRunner)Úbits_to_bytesÚfind_integer)ÚNO_SCOREc                   ó|   — e Zd ZdZ	 ddededededdf
d„Zdd	„Z	dede
fd
„Zede
fd„«       Zdeez  defd„Zdd„Zy)Ú	Optimiseruá  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                 óJ   — || _         || _        || _        || _        d| _        y)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        úm/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/hypothesis/internal/conjecture/optimiser.pyÚ__init__zOptimiser.__init__#   s*   € ð ˆŒØ ˆÔØˆŒØ 0ˆÔØˆÕó    c                 ó$   — | j                  «        y ©N)Ú
hill_climb©r   s    r   ÚrunzOptimiser.run4   s   € Ø�‰Õr   c                 óV   — |j                   j                  | j                  t        «      S r   )Útarget_observationsÚgetr   r   )r   r   s     r   Úscore_functionzOptimiser.score_function7   s   € Ø×'Ñ'×+Ñ+¨D¯K©K¼ÓBÐBr   c                 ó8   — | j                  | j                  «      S r   )r$   r   r   s    r   Úcurrent_scorezOptimiser.current_score:   s   € à×"Ñ" 4×#4Ñ#4Ó5Ð5r   c                 óš  — |j                   t        j                  k  ryt        |t        «      sJ ‚| j                  |«      }|| j                  k  ry|| j                  kD  r| xj                  dz  c_        || _        y|| j                  k(  sJ ‚t        |j                  «      t        | j                  j                  «      k  r|| _        yy)zlConsider 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_datazOptimiser.consider_new_data>   s¸   € ð �;‰;œŸ™Ò%ØÜ˜$Ô 0Ô1Ð1Ð1Ø×#Ñ# DÓ)ˆØ�4×%Ñ%Ò%ØØ�4×%Ñ%Ò%Ø×Ò Ñ"ÕØ $ˆDÔØØ˜×*Ñ*Ò*Ð*Ð*ô ˆt�z‰z‹?œc $×"3Ñ"3×"9Ñ"9Ó:Ò:Ø $ˆDÔØØr   c                 ó^  ‡ ‡‡— t        «       }d}t        ‰ j                  j                  «      dz
  Š‰dk\  r÷‰ j                  ‰ j
                  k  rÝ|‰ j                  ur.t        ‰ j                  j                  «      dz
  Š‰ j                  }‰|v r‰dz  ŠŒd|j                  ‰«       ‰ j                  j                  ‰   }|j                  €J ‚|j                  dvrŒ«dt        dt        fˆˆ fd„Št        ˆfd„«       t        ˆfd	„«       ‰dk\  r‰ j                  ‰ j
                  k  rŒÛyyyy)
zzThe main hill climbing loop where we actually do the work: Take
        data, and attempt to improve its score for target.Nr(   r   >   ÚbytesÚfloatÚbooleanÚintegerÚkr   c           
      óð  •— t        | «      dkD  ry‰j                  j                  ‰   }|j                  €J ‚|j                  ry|j
                  dv r2t        |j                  t        t        f«      sJ ‚|j                  | z   }né|j
                  dk(  rHt        |j                  t        «      sJ ‚t        | «      dkD  ry| dk(  rd}n§| dk(  rd}nŸ|j                  }n’|j
                  d	k(  sJ ‚t        |j                  t        «      sJ ‚t        |j                  «      }|| z   d
k  ry|| z  }t        t        |j                  «      t        |j!                  «       «      «      }t#        ||«      }t%        ||j&                  «      syt)        d«      D �]ã  }‰j                  j*                  }|d|j                   |fz   ||j                  dz   d z   }‰j,                  j/                  |d¬«      }‰j1                  |«      r y|j2                  t4        j6                  u r yt        |t8        «      sJ ‚t        |j                  «      t        ‰j                  j                  «      k(  r yt;        ‰j                  j<                  «      D ]ë  \  }	}
|
j>                  |j                  dz   k\  r �Œ|
j@                  |j                  k  rŒ?|j<                  |	   }|
jB                  |jB                  k(  rŒh|j*                  |j>                  |j@                   }‰j1                  ‰j,                  j/                  |d|j                   |z   ‰j                  j*                  |
j@                  d z   «      «      sŒê  y �Œæ y)aÇ  
                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>   r2   r4   r3   r(   éÿÿÿÿTr1   r   é   Úfull)Úextend)"Úabsr   r-   ÚindexÚ
was_forcedÚtyper+   ÚvalueÚintr2   Úboolr1   r   Úmaxr,   r   Ú
bit_lengthr   r   ÚconstraintsÚrangeÚchoicesr   Úcached_test_functionr/   r)   r   ÚOVERRUNr   Ú	enumerateÚspansÚstartÚendÚchoice_count)r5   ÚnodeÚ
new_choiceÚvÚsizeÚ_rF   Úattempt_choicesÚattemptÚjÚexÚ
ex_attemptÚreplacementÚir   s                €€r   Úattempt_replacez-Optimiser.hill_climb.<locals>.attempt_replacen   s  ø€ ô �q“6˜E’>Ø à×(Ñ(×.Ñ.¨qÑ1�Ø—z‘zÐ-Ð-Ð-Ø—?’?Ø ð —9‘9Ð 4Ñ4Ü% d§j¡j´3¼°,Ô?Ð?Ð?Ø!%§¡¨a¡‘JØ—Y‘Y )Ò+Ü% d§j¡j´$Ô7Ð7Ð7Ü˜1“v ’zØ$Ø˜B’wØ%*™
Ø˜ašØ%)™
à%)§Z¡Z™
àŸ9™9¨Ò/Ð/Ð/Ü% d§j¡j´%Ô8Ð8Ð8Ü& t§z¡zÓ2�Aà˜1‘u˜q’yØ$Ø˜‘F�Aô œs 4§:¡:›´¸a¿l¹l»nÓ0MÓN�DÜ!-¨a°Ó!6�Jä'¨
°D×4DÑ4DÔEØ ä˜q›ó %(�AØ"×/Ñ/×7Ñ7�Gà  $§*¡*Ð-Ø%˜-ñ(à! $§*¡*¨q¡.Ð"2Ð3ñ4ð $ð
 #Ÿk™k×>Ñ>Ø'°ð ?ó �Gð ×-Ñ-¨gÔ6Ù#à—~‘~¬¯©Ñ7Ù$ä% gÔ/?Ô@Ð@Ð@Ü˜7Ÿ=™=Ó)¬S°×1BÑ1B×1HÑ1HÓ-IÒIÙ$ä!*¨4×+<Ñ+<×+BÑ+BÓ!Cò (™˜˜2ØŸ8™8 t§z¡z°A¡~Ò5Ú!ØŸ6™6 T§Z¡ZÒ/Ø$Ø%,§]¡]°1Ñ%5˜
ØŸ?™?¨j×.EÑ.EÒEØ$Ø&-§o¡o°j×6FÑ6FÈÏÉÐ&X˜Ø×1Ñ1Ø ŸK™K×<Ñ<Ø '¨¨$¯*©*Ð 5Ø"-ñ!.à"&×"3Ñ"3×";Ñ";¸B¿F¹F¸HÐ"Eñ!Fóõò $(ò!(ð+%(ðL r   c                 ó   •—  ‰| «      S r   © ©r5   rZ   s    €r   ú<lambda>z&Optimiser.hill_climb.<locals>.<lambda>É   s   ø€ ¡?°1Ó#5€ r   c                 ó   •—  ‰|  «      S r   r\   r]   s    €r   r^   z&Optimiser.hill_climb.<locals>.<lambda>Ê   s   ø€ ¡?°A°2Ó#6€ r   )Úsetr,   r   r-   r   r   Úaddr<   r>   r@   rA   r   )r   Únodes_examinedÚprevrN   rZ   rY   s   `   @@r   r   zOptimiser.hill_climbT   s#  ú€ ô ›ˆà(,ˆÜ�×!Ñ!×'Ñ'Ó(¨1Ñ,ˆØ�1Šf˜×*Ñ*¨d×.CÑ.CÒCØ˜4×,Ñ,Ñ,Ü˜×)Ñ)×/Ñ/Ó0°1Ñ4�Ø×(Ñ(�à�NÑ"Ø�Q‘�Øà×Ñ˜qÔ!Ø×$Ñ$×*Ñ*¨1Ñ-ˆDØ—:‘:Ð)Ð)Ð)ð �y‰yÐ HÑHØðW¤3ð W¬4ö Wôv Ó5Ô6ÜÓ6Ô7ð] �1Šf˜×*Ñ*¨d×.CÑ.CÔCˆfÐCˆfr   )éd   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Ústrr@   r   r    r2   r$   Úpropertyr&   r	   rA   r/   r   r\   r   r   r   r      s—   „ ñð( !$ñà ðð ðð ð	ð
 ðð 
óó"ðCÐ#3ð C¸ó Cð ð6˜uò 6ó ð6ðÐ&6¸Ñ&Að Àdó ô,v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   r\   r   r   ú<module>rq      s)   ð÷ Dß Kß RÑ RÝ Bß QÝ :÷w8ò w8r   