/ core / resonance / __pycache__ / tuner.cpython-314.pyc
tuner.cpython-314.pyc
  1  +
  2  5�fi+/��>�Rt^RIHtHt^RIHtHt^RIHtHtH	t	H
  3  t
  4  Ht^RIt^RI
HtHtHt]!RR44t]!RR	44t!R
  5  R4t]R8XEd0^RIt^RIt^R
IHt]P2P5]P6!4R4t]!]4t]!R4]!]4t]!]R4t ]!R4]!!]PDPG4RR7Fwt$t%]!R]$R]%R
24K	]!R4.t&]'!^
  6  4F*t(]PSR](2RR7t*]&PW]*4K,	^RI
H,t,],!RR7t-]'!^4F�t.]&Uu.uFp]P_VP`]-4NK!	upt1]1Uu.uF
  7  qfKVNK	upt1]2!]14^8dKU]!!]1RRR 7t3]3^,t4] Pk]4P`]1Uu.uFqP`NK	up]4]1R!7K�	]!R"4]!!]PDPG4R#R7Fwt$t%]!R]$R]%R
24K	] Pm4t7]!R$]7R%,24]!R&]7R',24]Pp!]4]!R(4R#R#uupiuupiuupi))aV
  8  Resonance Weight Tuner
  9  
 10  Learns optimal resonance weights per operator based on their
 11  selection behavior. When operators consistently choose bullets
 12  that score low on a factor, that factor's weight decreases.
 13  
 14  This implements implicit preference learning - the system adapts
 15  to each operator's actual behavior without explicit configuration.
 16  )�	dataclass�field)�datetime�	timedelta)�Optional�List�Dict�Any�TupleN)�ResonanceScoringEngine�ResonanceScore�ResonanceFactorsc�Za�]tRt^toRt]!]PR7tRt	V3Rlt
 17  RtVtR#)�SelectionEventz�
 18  Records when an operator selects a bullet from presented options.
 19  
 20  Used to learn which resonance factors matter to this operator.
 21  ��default_factoryNc�<�V^8�dQh/S[;R&S[S[,;R&S[;R&S[S[,;R&S[;R&S[S[S[S[3,,;R&#)��
selected_uuid�presented_uuids�selected_score�presented_scores�	timestamp�context)�strrrrrrr	)�format�
__classdict__s"��7/Users/rcerf/repos/Sovereign_OS/core/resonance/tuner.py�__annotate__�SelectionEvent.__annotate__si��������#�Y���#�"���>�*�*���=���d�3��8�n�
%�,���)
�__name__�
 22  __module__�__qualname__�__firstlineno__�__doc__rr�nowrr�__annotate_func__�__static_attributes__�__classdictcell__�rs@rrrs&����� ����=�I�(,�G��r rc�Ba�]tRt^#toRt]!]R7tV3RltRt	Vt
 23  R#)�FactorImportancez;
 24  Learned importance of a resonance factor for an operator.
 25  rc�p<�V^8�dQh/S[;R&S[;R&S[;R&S[S[S[S[3,,;R&#)r�factor_name�current_weight�selection_correlation�adjustment_history)r�floatrr
 26  r)rrs"�rr�FactorImportance.__annotate__#sG����
 27  �����
�!� ���U�8�U�?�3�4�R�r r!N)r"r#r$r%r&r�listr2r(r)r*r+s@rr-r-#s�����8=�T�7R���r r-c��a�]tRt^.toRtRtRtRt^dt]3V3RlRllt	Rt
 28  RV3R	lR
 29  lltV3RlRltV3R
lRlt
V3RlRltRtV3RlRltV3RlRltRtVtR#)�ResonanceWeightTunera�
 30  Learns and adjusts resonance weights based on operator behavior.
 31  
 32  The tuner observes selection events and adjusts weights to better
 33  predict what the operator will find relevant.
 34  
 35  Key principle: If an operator consistently selects bullets that
 36  score high on a particular factor, that factor becomes more important.
 37  Conversely, if they ignore high-scoring bullets, the factor loses weight.
 38  g�������?g{�G�z�?��?c�,<�V^8�dQhRS[RS[RS[/#)r�scoring_engine�operator_id�
learning_rate)rrr3)rrs"�rr�!ResonanceWeightTuner.__annotate__Fs)���-�-�.�-��-��	-r c�f�WnW nW0n.Vn/VnVP4R#)z�
 39  Initialize the weight tuner.
 40  
 41  Args:
 42      scoring_engine: The resonance scoring engine to tune
 43      operator_id: Identifier for this operator's preferences
 44      learning_rate: How quickly to adapt weights
 45  N)r:r;r<�selection_events�factor_importance�_initialize_factor_importance)�selfr:r;r<s&&&&r�__init__�ResonanceWeightTuner.__init__Fs7��-��&��*��79���?A����*�*�,r c���VPPP4F7wr\VVR\P
 46  !4V3.R7VPV&K9	R#)z;Initialize factor importance tracking from current weights.r8)r/r0r1r2N)r:�weights�itemsr-rr'r@)rB�factor�weights&  rrA�2ResonanceWeightTuner._initialize_factor_importance_sS��"�1�1�9�9�?�?�A�N�F�-=�"�%�&)�%-�\�\�^�V�$<�#=�	.�D�"�"�6�*�Br Nc
�~<�V^8�dQhRS[RS[S[,RS[RS[S[,RS[S[S[S[3,,/#)rrrrrr)rrrrrr	)rrs"�rrr=isS���"$�"$��"$��c��"$�'�	"$�
 47  �~�.�"$��$�s�C�x�.�)�
"$r c	�:�\VVVV\P!4VR7pVPP	V4\VP4VP^,8�d VPVP)RVnVPV4R#)aC
 48  Record a selection event and update weights.
 49  
 50  Args:
 51      selected_uuid: UUID of the bullet the operator selected
 52      presented_uuids: All UUIDs that were presented
 53      selected_score: Resonance score of selected bullet
 54      presented_scores: Scores of all presented bullets
 55      context: Optional context about the selection
 56  )rrrrrrN)rrr'r?�append�len�WINDOW_SIZE�_update_weights)rBrrrrr�events&&&&&& r�record_selection�%ResonanceWeightTuner.record_selectionis���$�'�+�)�-��l�l�n��

 57  ��	
���$�$�U�+��t�$�$�%��(8�(8�1�(<�<�$(�$9�$9�4�;K�;K�:K�:L�$M�D�!�	
���U�#r c� <�V^8�dQhRS[/#)rrQ)r)rrs"�rrr=�s���32�32�^�32r c�J�VPPp/pVPPP	4FYpVP
 58  Uu.uFp\
VPV4NK	ppV'd\V4\V4,MRW4&K[	/pVPPP	4F4p\
W$4pW4,p	W�,
 59  p
 60  W�P,pW�V&K6	/pVPPP4F�wrMW�V,,p\VP\VPV44pW�V&W�PV,nVPV,P"P%\&P(!4V34K�	\VP+44pV^8�d-VP4UUu/uFwppVVV,bK	pppW�PnR#uupiuuppi)a,
 61  Update resonance weights based on a selection event.
 62  
 63  The logic:
 64  - For each factor, compare the selected bullet's score to the average
 65  - If selected bullet scores above average on a factor, increase weight
 66  - If selected bullet scores below average, decrease weight
 67  - Normalize weights to sum to 1.0
 68  r8N)r�factorsr:rF�keysr�getattr�sumrNr<rG�max�
 69  MIN_WEIGHT�min�
 70  MAX_WEIGHTr@r0r2rMrr'�values)rBrQ�selected_factors�avg_factorsrH�sr^�adjustments�selected_value�	avg_value�delta�
 71  adjustment�new_weightsr0�
 72  new_weight�total�k�vs&&                rrP�$ResonanceWeightTuner._update_weights�s���!�/�/�7�7��)+���)�)�1�1�6�6�8�F�:?�:P�:P�Q�:P�Q�g�a�i�i��0�:P�F�Q�?E�#�f�+��F��";�3�K��9�
 73  )+���)�)�1�1�6�6�8�F�$�%5�>�N�#�+�I�#�.�E��!3�!3�3�J�",���9���&*�&9�&9�&A�&A�&G�&G�&I�"�F�'�f�*=�=�J��T�_�_�c�$�/�/�:�.N�O�J�",���=G�"�"�6�*�9��"�"�6�*�=�=�D�D������,�
�'J��K�&�&�(�)���1�9�4?�4E�4E�4G�H�4G�D�A�q�1�a�%�i�<�4G�K�H�'2���#��IR��BIs�H�/Hc�6<�V^8�dQhRS[S[S[3,/#�r�return�rrr3)rrs"�rrr=�s���,�,�T�#�u�*�-=�,r c	�4�\VP4^8d5VPPP	4Uu/uFqRbK	up#/pVPPP	4EFp^p^p^p^pVPVP
 74  )RF�pVPU	u.uFp	\V	PV4NK	p
 75  p	V
 76  'gK<\V
 77  4\V
 78  4^,,p\VPPV4pW�8�dV^,
pV^,
pK�V^,
pV^,
pK�	Wg,^8�dWE,
 79  Wg,,p
MRp
W�V&W�PV,nEK	V#uupiuup	i)z�
 80  Compute correlation between each factor and selection.
 81  
 82  Returns:
 83      Dict mapping factor names to selection correlation (-1 to 1)
 84  r8Ng)
rNr?r:rFrWrOrrXrV�sortedrr@r1)rB�f�correlationsrH�selection_when_high�selection_when_low�
 85  total_high�	total_lowrQra�
factor_values�medianrc�correlations&             r�compute_factor_correlations�0ResonanceWeightTuner.compute_factor_correlations�s~���t�$�$�%��)�$(�$7�$7�$?�$?�$D�$D�$F�G�$F�q�s�F�$F�G�G����)�)�1�1�6�6�8�F�"#��!"���J��I��.�.��0@�0@�/@�/A�B��EJ�E[�E[� \�E[������F�!;�E[�
� \�$���
�.�s�=�/A�Q�/F�G��!(��)=�)=�)E�)E�v�!N��!�+�'�1�,�'��!�O�J�&�!�+�&���N�I�C�"�%��)�2�G�J�Lb�c��!��#.�� �CN�"�"�6�*�@�=9�@���IH��!]s�F�,Fc�6<�V^8�dQhRS[S[S[3,/#rn)rrr	)rrs"�rrr=�s�����4��S��>�r c��VP4pRVPR\VP4RVPR\VPP4RVR\VPPP4RRR	7R
 86  ,R\VPPP4RR
7R
 87  ,/pV#)z^
 88  Get a report on current weights and their evolution.
 89  
 90  Returns:
 91      Dict with weight analysis
 92  r;�total_selectionsr<�current_weights�factor_correlations�top_factorsc��V^,#��r!��xs&r�<lambda>�8ResonanceWeightTuner.get_weight_report.<locals>.<lambda>���a��dr T��key�reverse:N�N�bottom_factorsc��V^,#r�r!r�s&rr�r�r�r �r�)
 93  r|r;rNr?r<�dictr:rFrrrG)rBrt�reports&  r�get_weight_report�&ResonanceWeightTuner.get_weight_report�s����7�7�9��
�4�+�+���D�$9�$9� :��T�/�/��t�D�$7�$7�$?�$?�@�!�<��6��#�#�+�+�1�1�3�"����	�
 94  
�f��#�#�+�+�1�1�3�"����
 95  ��"�
r c��\PP4VPnVP4R#)zReset weights to defaults.N)r�DEFAULT_WEIGHTS�copyr:rFrA�rBs&r�
reset_weights�"ResonanceWeightTuner.reset_weightss+��&<�&L�&L�&Q�&Q�&S����#��*�*�,r c�6<�V^8�dQhRS[S[S[3,/#rnrp)rrs"�rrr=s���1�1��S�%�Z� 0�1r c�@�\VPP4#)z'Export current weights for persistence.)r�r:rFr�s&r�export_weights�#ResonanceWeightTuner.export_weightss���D�'�'�/�/�0�0r c�6<�V^8�dQhRS[S[S[3,/#)rrFrp)rrs"�rrr=s���-�-�d�3��:�&6�-r c�Z�\V4VPnVP4R#)z Import weights from persistence.N)r�r:rFrA)rBrFs&&r�import_weights�#ResonanceWeightTuner.import_weightss ��&*�7�m����#��*�*�,r )r@r<r;r:r?�N)r"r#r$r%r&�DEFAULT_LEARNING_RATEr[r]rOrCrArRrPr|r�r�r�r�r)r*r+s@rr7r7.s����	� ���J��J��K� 5�	-�-�2�"$�"$�H32�32�j,�,�\��8-�
 96  1�1�-�-r r7�__main__)�GodDatabasez
test_tuner.dbz$=== Resonance Weight Tuner Test ===
 97  �rickzInitial weights:c��V^,)#r�r!r�s&rr�r�,�
 98  ���!��ur r�z  z: z.3fz4
 99  Simulating selections that favor topic_relevance...zTest bullet �test)�content�
100  blanket_id)�ResonanceContext�architecture)�
current_topicc�.�VPP#r�)rV�topic_relevance)ras&rr�r�Hs��q�y�y�7P�7Pr Tr�)rrrrz(
101  Weights after topic-focused selections:c��V^,)#r�r!r�s&rr�r�Sr�r z
102  Total selections: r�z
Top factors: r�z
103  === Test Complete ===)9r&�dataclassesrrrr�typingrrrr	r
104  �math�scoringrrr
rr-r7r"�tempfile�os�databaser��path�join�
105  gettempdir�db_path�db�print�engine�tunerrrrFrGrHrI�bullets�range�i�
create_bullet�bulletrMr�r�_�compute_resonance�uuid�scoresrN�sorted_by_topic�selectedrRr�r��remove)�bras00r�<module>r�s���	�)�(�3�3��M�M��-�-��-��S�S��S�j-�j-�\�z���
�&��g�g�l�l�8�.�.�0�/�B�G�	�W�	�B�	�
106  1�2�$�B�
'�F� ���0�E�	�
107  �� ����!5�!5�!7�_�M����
��6�(�"�V�C�L�)�*�N�
108109  A�B��G�
�2�Y���!�!�"�1�#�&��"�
110  ��	���v���*��^�<�G�
�2�Y��EL�M�W��&�*�*�1�6�6�7�;�W�M��#�5�V��!�!�V�5���v�;��?��!��-P�Z^�_��"�1�%��
���"�-�-�-3�4�V��V�V�V�4�#�#�		�	
111  ��&
112113  5�6� ����!5�!5�!7�_�M����
��6�(�"�V�C�L�)�*�N��
$�
$�
&�F�	� ��(:�!;� <�
114  =�>�	�M�&��/�0�
115  1�2��I�I�g��	�
116  #�$�C��JN��5��5s�%J�J�J�J