
    @iP                         S r SSKrSSKrSSKJrJrJrJrJ	r	  SSK
J
r
  \R                  " \5      r " S S5      r " S S5      rg)	z:Iteration manager for Stage4 iterative interaction engine.    N)DictAnyListOptionalTuple)datetimec                   T    \ rS rSrSrS\S\S\4S jrS\\\	4   4S jr
S	\4S
 jrSrg)IterationState   z-State object for tracking iteration progress.user_idquery
max_roundsc                 R    Xl         X l        X0l        SU l        SU l        / U l        g)zInitialize iteration state.

Args:
    user_id: User identifier
    query: Original user query
    max_rounds: Maximum number of iteration rounds
r   N)r   r   r   current_rounduser_vectorhistory)selfr   r   r   s       O   /home/ubuntu/codebase/yexijia/保研/iCoLoc/src/controller/iteration_manager.py__init__IterationState.__init__   s*     
$    
round_datac                     U R                   R                  U R                  [        R                  " 5       R                  5       S.UE5        g)zCAdd a round to history.

Args:
    round_data: Data for this round
)round	timestampN)r   appendr   r   now	isoformat)r   r   s     r   	add_roundIterationState.add_round   s@     	''!113
 
 	r   returnc                 4    U R                   U R                  :  $ )zRCheck if iteration is complete.

Returns:
    True if current_round >= max_rounds
)r   r   )r   s    r   is_completeIterationState.is_complete*   s     !!T__44r   )r   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__strintr   r   r   r   boolr#   __static_attributes__ r   r   r
   r
      s?    7 C S  
DcN 
5T 5r   r
   c                      \ rS rSrSr   SS\4S jjrS\S\S\\	R                     4S	 jr SS
\S\\\\4      S\\\4   4S jjrS
\S\\\\   4   S\	R                  4S jr  SS
\S\\\\4      S\\\4   4S jjr  SS
\S\\\\   4   S\S\\\4   4S jjr SS\S\S\S\\\\4      S\\\4   4
S jjr SS
\S\\\\4      S\\\4   4S jjrSrg)IterationManager3   z5Manager for iterative preference refinement (Stage4).Nfusion_alphac	                 d    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        g)u  Initialize iteration manager.

Args:
    miner: CoLocationMiner instance
    learner: PreferenceLearner instance
    memory: MemoryStore instance
    embedder: PatternEmbedder instance
    llm_client: Optional LLMClient for intent understanding
    intent_encoder: Optional IntentEncoder for Stage0
    intent_mapper: Optional IntentMapper for Stage0
    fusion_alpha: Weight for LLM intent vs feedback fusion (α)
N)minerlearnermemoryembedder
llm_clientintent_encoderintent_mapperr2   )	r   r4   r5   r6   r7   r8   r9   r:   r2   s	            r   r   IterationManager.__init__6   s/     
 $,*(r   r   r   r!   c                 f   U R                   b  U R                  c  [        R                  S5        g [        R                  SUSS  S35        U R                   R	                  U5      nU(       d  [        R                  S5        gUR                  S/ 5      nU(       a  [        U5      S:X  a<  [        R                  S	5        [        R                  S
UR                  5        35        g[        R                  S[        U5       S35        [        R                  SU 35        [        R                  S5        U R                  R                  U5      nUc  [        R                  S5        g[        R                  S[        U5       S[        R                  R                  U5      S S35        U R                  R                  X%U5        [        R                  S5        [        R                  S[        U5       S35        U$ ! [          a!  n[        R                  SU 3SS9   SnAgSnAff = f)u   Initialize user vector from LLM intent understanding (Stage0).

This method implements the complete flow:
1. User ambiguous query → LLM intent understanding
2. Extract structured intent (business, pattern_preference, risk factors)
3. Validate patterns against dataset POI types
4. Map patterns to embeddings (feature selection)
5. Compute initial user vector u_llm

Args:
    query: User query text
    user_id: User identifier
    
Returns:
    Initial user preference vector, or None if Stage0 not available
Nz<Stage4: Stage0 not available, skipping intent initializationz1Stage4: Starting intent understanding for query: d   z...z=Stage4: Failed to parse intent from query - LLM returned Nonepattern_preferencer   z4Stage4: No pattern_preference found in parsed intentzStage4: Intent result keys: z+Stage4: Successfully parsed intent - found z preferred patternszStage4: Preferred patterns: zEStage4: Mapping intent patterns to user vector (feature selection)...z*Stage4: Failed to convert intent to vectorz1Stage4: Generated initial user vector u_llm (dim=z, norm=.4f)z#Stage4: Saved intent data to memoryzBStage4: Successfully initialized user vector from LLM intent (dim=z6Stage4: Failed to initialize user vector from intent: Texc_info)r9   r:   loggerinfoparsewarninggetlendebugkeys	to_vectornplinalgnormr6   save_intent	Exception)r   r   r   intent_resultr>   u_llmes          r   init_user_vector!IterationManager.init_user_vectorN   s   " &$*<*<*DKKVW(	KKKERVSVK=X[\] !//55e<M ^_ "/!2!23G!L%-?)@A)EUV;M<N<N<P;QRSKKEcJ\F]E^^qrsKK67I6JKL KK_`&&00?E}KLKKKCPUJ<W^_a_h_h_m_mns_tux^yyz{| KK##GMBKK=>KK\]`af]g\hhijkL 	NNSTUSVWbfNg	s-   AH A#H *BH -BH 
H0H++H0statemining_paramsc                    U=R                   S-  sl         [        R                  SUR                    SUR                   35        Uc  SSSS.nU R                  R                  UR                  S	S5      UR                  S
S5      UR                  SS5      S9n[        R                  SUR                    S[        U5       S35        USS nU H-  nSU;  d  M  U R                  R                  US   5      nXeS'   M/     SnUR                  b;  U R                  R                  XAR                  5      n[        R                  S5        OJU R                  R                  UUR                  UR                   S-
  S9nUb  [        R                  S5        Un0 n	Ub  [        [!        XG5      S SS9n
U
 VVs/ s H  u  pUPM	     nnnU
 H<  u  pSR#                  [        UR                  S/ 5      5      5      n[%        U5      X'   M>     [        R                  S[        U5       S35        O[        R                  S5        UR                  SS5      nU R                  R'                  XS9nUR                   UU[        U5      [        U5      U	UR                  b2  [%        [(        R*                  R-                  UR                  5      5      OSS.nUR/                  [        U5      [        U5      US    S!.5        U$ s  snnf )"aJ  Run one iteration round.

Algorithm:
1. Mine candidate patterns
2. Rank patterns by current user vector
3. Return ranked patterns for user feedback

Args:
    state: Current iteration state
    mining_params: Optional mining parameters (if None, uses defaults)
    
Returns:
    Dictionary containing ranked patterns and metadata
   zStage4: Starting round /N333333?   
confidencemin_participationmax_pattern_sizepriorityr_   r`   ra   Stage4: Round z mined z	 patterns   	embeddingpatternz/Stage4: Scored patterns using state user vectorr   interaction_roundz4Stage4: Scored patterns using learner's fused vectorc                     U S   $ NrY   r.   xs    r   <lambda>0IterationManager.run_one_round.<locals>.<lambda>       adr   Tkeyreverse,zStage4: Re-ranked z8Stage4: No user vector available, using original rankingmin_confidence      ?rs           )r   patternsrulestotal_patternstotal_rulessimilarity_scoresuser_vector_normr|   )ry   rz   r|   )r   rC   rD   r   r4   mine_patternsrG   rH   r7   encode_patternr   r5   _score_with_vectorscore_patternsr   sortedzipjoinfloatgenerate_rulesrL   rM   rN   r   )r   rV   rW   rw   top_patternsre   pattern_vecscoresranked_patternsr{   rankedp_scorepattern_keyrs   rx   round_results                     r   run_one_roundIterationManager.run_one_round   s     	q -e.A.A-B!EDTDTCUVW   &)$%(M ::+++//0CSI*../A1E"&&z<@ , 
 	nU%8%8$9XyYZ  } $G')"mm::79;MN'2$ $ (\\44\CTCTUFKKIK \\00"'"5"5"9 1 F
 !RT 'L)"F
 .44VTQqVO4 #!hhvaeeIr.B'CD16u!. # KK,S-A,B)LMKKRS '**+;SA

))/)Y (('!(mu:!2LQL]L]LibiinnU5F5F&G Hor
 	!(mu: ,-? @
 	 C 5s   5K<feedbackc                    UR                  S/ 5      nUR                  S/ 5      nU HZ  nUR                  S5      nU R                  R                  U5      nUc  M4  U R                  R                  UR                  U5        M\     U HZ  nUR                  S5      nU R                  R                  U5      nUc  M4  U R                  R                  UR                  U5        M\     U R                  R                  UR                  5      nU R                  R                  UR                  5      n	Sn
U	(       a/  U	R                  S5      (       a  [        R                  " U	S   5      n
U
bI  UbF  U R                  U
-  SU R                  -
  U-  -   n[        R                  SU R                   S35        O{Ub  Un[        R                  S	5        O`U
b  U
n[        R                  S
5        OEU R                  R!                  5       n[        R"                  " U5      n[        R%                  S5        Xl        [        R                  S[        R(                  R+                  U5      S S35        U$ )u   Update user vector based on feedback.

Formula: u_t = α * u_llm + (1-α) * u_feedback

Args:
    state: Current iteration state
    feedback: Dictionary with 'positive' and 'negative' pattern lists
    
Returns:
    Updated user preference vector
positivenegativerr   NrR   rY   z!Stage4: Fused user vector (alpha=r@   z2Stage4: Using feedback vector only (no LLM intent)z2Stage4: Using LLM intent vector only (no feedback)z3Stage4: No user vector available, using zero vectorz"Stage4: Updated user vector (norm=r?   )rG   splitr7   r~   r6   add_positiver   add_negativer5   compute_feedback_vectorload_intentrL   arrayr2   rC   rD    get_sentence_embedding_dimensionzerosrF   r   rM   rN   )r   rV   r   positive_patternsnegative_patternsr   pattern_listr   
u_feedbackintent_datarR   u_tdims                r   update_user_vector#IterationManager.update_user_vector   s     %LLR8$LLR8 -K&,,S1L--66|DK&((D - -K&,,S1L--66|DK&((D	 - \\99%--H
 kk--emm<;??733HH[12E !7##e+q43D3D/D
.RRCKK;D<M<M;NaPQ#CKKLMCKKLM --@@BC((3-CNNPQ  89LS8QQRST
r   c           	          U R                  X5      nUR                  US   US   US   US   US   US   UR                  5       S.$ )a  Run one iteration step: mine, rank, return results for user feedback.

This method is designed for interactive iteration where:
1. Mine and rank patterns
2. Return results for user to provide feedback
3. After feedback, call update_and_train() to update vector and train model

Args:
    state: Current iteration state
    mining_params: Optional mining parameters
    trainer: Optional PreferenceTrainer instance for training after feedback
    
Returns:
    Dictionary containing round results (patterns, rules, etc.)
rw   rx   ry   rz   r{   r|   )r   rw   rx   ry   rz   r{   r|   is_final)r   r   r#   )r   rV   rW   trainerr   s        r   run_one_iteration_step'IterationManager.run_one_iteration_step7  sh    & ))%? (($Z0!'**+;<'6!-.A!B ,-? @))+	
 		
r   training_epochsc                 6   U R                  X5      nSnUb   [        R                  SUR                   S35        UR	                  USSS9nSUUR                  S/ 5      S.n[        R                  S	UR                  S5      (       a  UR                  S/ 5      S
   OS S35        OSSS.nUb+  [        [        R                  R                  U5      5      US.$ SUS.$ ! [         a/  n[        R                  SU 3SS9  S[        U5      S.n SnANgSnAff = f)ah  Update user vector based on feedback and immediately train model.

Args:
    state: Current iteration state
    feedback: Dictionary with 'positive' and 'negative' pattern lists
    trainer: Optional PreferenceTrainer instance
    training_epochs: Number of epochs for training (default 5 for quick training)
    
Returns:
    Dictionary with training results
Nz#Stage4: Training model after round z	 feedback    )epochs
batch_size	save_pathTloss)trainedr   loss_historyz*Stage4: Model trained successfully (loss: zN/Ar@   zStage4: Training failed: rA   F)r   errorzNo trainer provided)r   reasonrv   )r|   training)r   rC   rD   r   trainrG   rP   rF   r*   r   rL   rM   rN   )	r   rV   r   r   r   updated_vectortraining_resultr   rS   s	            r   update_and_train!IterationManager.update_and_trainW  sV     00A A%BUBUAVV_`a!--*!" (   $-$+KK$;#
 Hhohshstzh{h{U[]_I`acId  BG  IH  HI  J  K !/O JXIcbiinn^&D E'
 	
il'
 	
  !:1#>N$ V#s   BC 
D)%DDKc                 H   [         R                  SU S35        [        X!U5      nU R                  X5      Ul        / nUR                  5       (       d_  U R                  XT5      nUR                  U5        [         R                  SUR                   SU S35        UR                  5       (       d  M_  U R                  XT5      nUUUUS   US   UR                  S0 5      UR                  b4  [        [        R                  R                  UR                  5      5      S
.$ S	S
.$ )a  Run iterative preference refinement.

Algorithm: Iterative Preference Refinement

Input: Q (query), K (rounds)
Output: P* (best patterns)

Initialize u0
for t=1..K:
  Generate Ct (candidate patterns)
  Rank Ct by ut
  Collect Ft (feedback)
  Update ut

Return best

Args:
    query: User query
    user_id: User identifier
    K: Number of iteration rounds
    mining_params: Optional mining parameters
    
Returns:
    Dictionary containing final results and iteration history
z)Stage4: Starting iterative refinement (K=r@   rb   rZ   z
 completedrw   rx   r{   rv   )r   total_roundsiteration_historyfinal_patternsfinal_rulesfinal_similarity_scoresr|   )rC   rD   r
   rT   r   r#   r   r   r   _final_rankrG   r   rL   rM   rN   )	r   r   r   r   rW   rV   r   r   final_results	            r   runIterationManager.run  s&   6 	?s!DE wq1 !11%A  ##%%--eCL$$\2KK.)<)<(=QqcLM ##%% ''= !2*:6'0'3'7'78KR'PLQL]L]LibiinnU5F5F&G H
 	
 ps
 	
r   c           	      b   [         R                  S5        Uc  SSSS.nU R                  R                  UR	                  SS5      UR	                  SS5      UR	                  S	S5      S9nUSS
 nU H-  nSU;  d  M  U R
                  R                  US   5      nXeS'   M/     SnUR                  b&  U R                  R                  XAR                  5      nO/U R                  R                  UUR                  UR                  S9nUn0 n	Ubm  [        [        XG5      S SS9n
U
 VVs/ s H  u  pUPM	     nnnU
 H<  u  pSR                  [        UR	                  S/ 5      5      5      n[!        U5      X'   M>     UR	                  SS5      nU R                  R#                  XS9nUUU	S.$ s  snnf )zGenerate final ranking after all iterations.

Args:
    state: Final iteration state
    mining_params: Optional mining parameters
    
Returns:
    Dictionary containing final ranked patterns and rules
z Stage4: Generating final rankingNr[   r\   r]   r^   r_   r`   ra   rc   rd   re   rf   c                     U S   $ ri   r.   rj   s    r   rl   .IterationManager._final_rank.<locals>.<lambda>  rn   r   Tro   rr   rs   rt   ru   )rw   rx   r{   )rC   rD   r4   r}   rG   r7   r~   r   r5   r   r   r   r   r   r   r   r   r   )r   rV   rW   rw   r   re   r   r   r   r{   r   r   r   r   r   rs   rx   s                    r   r   IterationManager._final_rank  s    	67 %($%(M ::+++//0CSI*../A1E"&&z<@ , 
  }#G')"mm::79;MN'2$ $
 (\\44\CTCTUF\\00"'"5"5 1 F 'L)"F
 .44VTQqVO4"!hhvaeeIr.B'CD16u!. # '**+;SA

))/)Y (!2
 	
 5s   'F+)r7   r2   r9   r:   r5   r8   r6   r4   )NNNr[   )N)NN)Nr\   )r%   r&   r'   r(   r)   r   r   r*   r   rL   ndarrayrT   r
   r   r   r   r   r   r   r+   r   r   r   r-   r.   r   r   r0   r0   3   s   ?DH48'*)$)0=c =C =HRZZ<P =@ @Dh> h$,T#s(^$<hHLSRUXhT> >$(d3i$8>=?ZZ>B JN'+
N 
.6tCH~.F
04S#X
D !%/01
n 1
"&sDI~"61
 *-1
 6:#s(^1
h 7;9
 9
s 9
s 9
#DcN39
?CCH~9
x >BB
 B
"*4S>":B
FJ3PS8nB
 B
r   r0   )r)   loggingnumpyrL   typingr   r   r   r   r   r   	getLoggerr%   rC   r
   r0   r.   r   r   <module>r      s>    @   3 3 			8	$%5 %5PT
 T
r   