
    i&                        d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ  ej                   e      Z	 	 	 	 	 	 	 	 	 d6dededededededededefdZedk(  rddlZ ej0                  d      Zej5                  dedd       ej5                  ded	d       ej5                  d edd!       ej5                  d"ed#d$gd%&       ej5                  d'ed#dd(&       ej5                  d)ed
d*       ej5                  d+edd,       ej5                  d-edd.       ej5                  d/edg d0d12       ej7                         Z ej:                  ej<                  d34        eej>                  ej@                  ejB                   eejD                        ejF                  r eejF                        ndejH                  ejJ                  ejL                  ejN                  5	      Z(yy)7zAExperiment runner for comparing baseline and contrastive methods.    N)Path)DictListAny)PipelineManager)UserSimulator)InteractionEvaluatorconfig/config_baseline.yamlconfig/config_contrastive.yaml   
   resultsmixedbaseline_configcontrastive_configpreference_weighted_configliked_featuresdisliked_featurestop_krounds
output_dirsampling_strategyc	                    |dh}|du}	t         j                  d       t         j                  d|	rdndz          t         j                  d       t        j                  |d       t	        ||	      }
t         j                  d
       	 t        |       }t         j                  d       t         j                  d       	 t        |      }t         j                  d       t         j                  d       t         j                  d       t         j                  d       t        ||
|||      }|j                         }t         j                  d       t         j                  d       t         j                  d       t        ||
|||      }|j                         }d}|	rt         j                  d       t         j                  d       t         j                  d       	 t        |      }t        ||
|||      }|j                         }t        j                  j                  |d      }t        |d      5 }t        j                  ||d       ddd       t        t        j                  j                  |d      d      5 }t        j                  |d   |d       ddd       t         j                  d|        t        j                  j                  |d      }t        j                  j                  |d      }t        |d      5 }t        j                  ||d       ddd       t        |d      5 }t        j                  ||d       ddd       t        j                  j                  |d       }t        j                  j                  |d!      }t        |d      5 }t        j                  |d   |d       ddd       t        |d      5 }t        j                  |d   |d       ddd       t         j                  d"       t         j                  d#|        t         j                  d$|        t         j                  d%|        t         j                  d&|        t         j                  d       t         j                  d'       t         j                  d       t         j                  d(       t         j                  d)|d   d*   d+       t         j                  d,|d-   d*   d+       t         j                  d.|d/   d*   d+       t         j                  d0|d1   d*   d+       t         j                  d2       t         j                  d)|d   d*   d+       t         j                  d,|d-   d*   d+       t         j                  d.|d/   d*   d+       t         j                  d0|d1   d*   d+       |rt         j                  d3       t         j                  d)|d   d*   d+       t         j                  d,|d-   d*   d+       t         j                  d.|d/   d*   d+       t         j                  d0|d1   d*   d+       t         j                  d4       t         j                  d)|d   d*   |d   d*   z
  d+       t         j                  d,|d-   d*   |d-   d*   z
  d+       t         j                  d.|d/   d*   |d/   d*   z
  d+       t         j                  d0|d1   d*   |d1   d*   z
  d+       ||d5}|||d6<   |S # t        $ r }t         j                  d| d        d}~ww xY w# t        $ r }t         j                  d| d        d}~ww xY w# 1 sw Y   .xY w# 1 sw Y   xY w# t        $ r%}t         j                  d| d       Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   #xY w# 1 sw Y    xY w)7a  Run experiment comparing baseline, contrastive, and optionally preference-weighted methods.
    
    Args:
        baseline_config: Path to baseline configuration file
        contrastive_config: Path to contrastive configuration file
        preference_weighted_config: Optional path to preference-weighted config (2025-style); if None, only run baseline vs contrastive
        liked_features: Set of features user likes (default: {"Restaurant"})
        disliked_features: Set of features user dislikes (default: None)
        top_k: Number of top patterns to recommend each round
        rounds: Number of interaction rounds
        output_dir: Directory to save results
        
    Returns:
        Dictionary with baseline, contrastive, and optionally preference_weighted results
    N
Restaurantz<============================================================z,Starting Experiment: Baseline vs Contrastivez vs Preference-Weighted T)exist_ok)r   r   z Initializing baseline manager...z)Baseline manager initialized successfullyz'Failed to initialize baseline manager: )exc_infoz#Initializing contrastive manager...z,Contrastive manager initialized successfullyz*Failed to initialize contrastive manager: z=
============================================================zRunning Baseline Evaluation)r   r   r   zRunning Contrastive EvaluationzCRunning Preference-Weighted Evaluation (2025-style adaptive fusion)z metrics_preference_weighted.jsonw   )indentz!accuracy_preference_weighted.jsonaccuracyz%  Preference-Weighted (all metrics): z Preference-Weighted run failed: zmetrics_baseline.jsonzmetrics_contrastive.jsonzaccuracy_baseline.jsonzaccuracy_contrastive.jsonz
Results saved:z  Baseline (all metrics): z  Contrastive (all metrics): z  Baseline (accuracy only): z  Contrastive (accuracy only): zExperiment SummaryzBaseline - Final Metrics:z  Accuracy:  z.4fz  Precision: 	precisionz  Recall:    recallz  F1:        f1z
Contrastive - Final Metrics:z%
Preference-Weighted - Final Metrics:z'
Improvement (Contrastive vs Baseline):)baselinecontrastivepreference_weighted)loggerinfoosmakedirsr   r   	Exceptionerrorr	   runpathjoinopenjsondump)r   r   r   r   r   r   r   r   r   	run_third	simulatorbaseline_managerecontrastive_managerbaseline_evalbaseline_resultscontrastive_evalcontrastive_resultspreference_weighted_results
pw_managerpw_evalpw_pathfbaseline_pathcontrastive_pathbaseline_acc_pathcontrastive_acc_pathouts                               H   /home/ubuntu/codebase/yexijia/保研/colocation_mvp/experiment/runner.pyrun_experimentrI      sI   4 &*$6I
KK
KK>_hB[npqr
KK KK
T* %+I KK23*?;?@
 KK56-.@ABC KK 
KK-.
KK(+M %((* KK 
KK01
KK++ +..0"&O$YZH	P()CDJ*"3G +2++-'ggll:/QRGgs#q		5qC $bggll:/RSUXY]^		5jA1QO ZKK?yIJ
 GGLL-DEMww||J0JK	mS	!Q		"Aa0 
" 
	$		%q3 
% Z1IJ77<<
4OP		%		":.!< 
& 
"C	(A		%j11Q? 
) KK"$
KK,]O<=
KK/0@/ABC
KK./@.ABC
KK12F1GHI KK 
KK$%
KK
KK+,
KK- 0 <R @EFG
KK- 0 =b A#FGH
KK- 0 :2 >sCDE
KK- 0 6r :3?@A
KK01
KK- 3J ? CCHIJ
KK- 3K @ DSIJK
KK- 3H =b A#FGH
KK- 3D 9" =cBCD"<=m$?
$KB$OPS#TUVm$?$LR$PQT#UVWm$?$I"$Mc#RSTm$?$Eb$I##NOP
KK:;
KK- 3J ? CFVWaFbceFf fgjklm
KK- 3K @ DGWXcGdegGh hilmno
KK- 3H =b ADTU]D^_aDb bcfghi
KK- 3D 9" =@PQU@VWY@Z Z[^_`a'8K
LC".%@!"Ji  >qcBTR  A!EPTU\ $#YY  	PLL;A3?$LOO	P 
"	! 
%	$ 
&	% 
)	(s   
 \'   ] #A^ :]?2^ ^! ^ _
:_'_$_1'	]0]]	]<]77]<?^	^ ^^ 	_"__
__!$_.1_;__main__z"Run preference learning experiment)descriptionz--baseline-configz#Path to baseline configuration file)typedefaulthelpz--contrastive-configz&Path to contrastive configuration filez--preference-weighted-configzMPath to preference-weighted config (2025-style); if set, run 3-way comparisonz--liked-features+r   zFeatures user likes)rL   nargsrM   rN   z--disliked-featureszFeatures user dislikesz--top-kz#Number of top patterns to recommendz--roundszNumber of interaction roundsz--output-dirzOutput directory for resultsz--sampling-strategy)topr   
stratifieduncertaintyzWPattern sampling strategy: top (original), mixed (recommended), stratified, uncertainty)rL   rM   choicesrN   z4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformat)	r   r   r   r   r   r   r   r   r   )	r
   r   NNNr   r   r   r   ))__doc__loggingr3   r+   pathlibr   typingr   r   r   controller.managerr   experiment.simulatorr   experiment.evaluatorr	   	getLogger__name__r)   strsetintrI   argparseArgumentParserparseradd_argument
parse_argsargsbasicConfigINFOr   r   r   r   r   r   r   r   r   r        rH   <module>rm      s   G   	  " " . . 5			8	$ 9>&*!$ddd !$d 	d
 d d d d dN z$X$$1UVF
+#?\A  C
.SBbD  F
6S$k  m
*C,1  3
-CsD4  6
	QA  C

b:  <
S):  <
-CLu  w D GllE ,,22#'#B#B4../9=9O9O#d445UYjj{{??00
GC rl   