""" Optimization-Level Logger Hook - Results Appends trial results to the high-level optimization.log file. Hook Point: post_extraction """ from pathlib import Path from datetime import datetime from typing import Dict, Any, Optional import logging logger = logging.getLogger(__name__) def log_optimization_results(context: Dict[str, Any]) -> Optional[Dict[str, Any]]: """ Append trial results to the main optimization.log file. This hook completes the trial entry in the high-level log with: - Objective values - Constraint evaluations - Trial outcome (feasible/infeasible) Args: context: Hook context containing: - trial_number: Current trial number - extracted_results: Dict of all extracted objectives and constraints - result_path: Path to result file Returns: None (logging only) """ trial_num = context.get('trial_number', '?') extracted_results = context.get('extracted_results', {}) result_path = context.get('result_path', '') # Get the output directory from context (passed by runner) output_dir = Path(context.get('output_dir', 'optimization_results')) log_file = output_dir / 'optimization.log' if not log_file.exists(): logger.warning(f"Optimization log file not found: {log_file}") return None # Find the last line for this trial and append results with open(log_file, 'a') as f: timestamp = datetime.now().strftime('%H:%M:%S') # Extract objective and constraint values results_str = " | ".join([f"{name}={value:.3f}" for name, value in extracted_results.items()]) f.write(f"[{timestamp}] Trial {trial_num:3d} COMPLETE | {results_str}\n") return None def register_hooks(hook_manager): """ Register this plugin's hooks with the manager. This function is called automatically when the plugin is loaded. """ hook_manager.register_hook( hook_point='post_extraction', function=log_optimization_results, description='Append trial results to optimization.log', name='optimization_logger_results', priority=100 ) # Hook metadata HOOK_NAME = "optimization_logger_results" HOOK_POINT = "post_extraction" ENABLED = True PRIORITY = 100