""" Simple Example: Using LLM Mode for Optimization This example demonstrates the LLM-native workflow WITHOUT requiring a JSON config file. You describe your optimization problem in natural language, and the system generates all the necessary extractors, hooks, and optimization code automatically. Phase 3.2 Integration - Task 1.3: Minimal Working Example Requirements: - Beam.prt and Beam_sim1.sim in studies/simple_beam_optimization/1_setup/model/ - Claude Code running (no API key needed) - test_env activated Author: Antoine Letarte Date: 2025-11-17 """ import subprocess import sys from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) def run_llm_optimization_example(): """ Run a simple LLM-mode optimization example. This demonstrates the complete Phase 3.2 integration: 1. Natural language request 2. LLM workflow analysis 3. Auto-generated extractors 4. Auto-generated hooks 5. Optimization with Optuna 6. Results and plots """ print("=" * 80) print("PHASE 3.2 INTEGRATION: LLM MODE EXAMPLE") print("=" * 80) print() # Natural language optimization request request = """ Minimize displacement and mass while keeping stress below 200 MPa. Design variables: - beam_half_core_thickness: 15 to 30 mm - beam_face_thickness: 15 to 30 mm Run 5 trials using TPE sampler. """ print("Natural Language Request:") print(request) print() # File paths study_dir = Path(__file__).parent.parent / "studies" / "simple_beam_optimization" prt_file = study_dir / "1_setup" / "model" / "Beam.prt" sim_file = study_dir / "1_setup" / "model" / "Beam_sim1.sim" output_dir = study_dir / "2_substudies" / "06_llm_mode_example_5trials" if not prt_file.exists(): print(f"ERROR: Part file not found: {prt_file}") print("Please ensure the simple_beam_optimization study is set up.") return False if not sim_file.exists(): print(f"ERROR: Simulation file not found: {sim_file}") return False print("Configuration:") print(f" Part file: {prt_file}") print(f" Simulation file: {sim_file}") print(f" Output directory: {output_dir}") print() # Build command - use test_env python python_exe = "c:/Users/antoi/anaconda3/envs/test_env/python.exe" cmd = [ python_exe, "optimization_engine/run_optimization.py", "--llm", request, "--prt", str(prt_file), "--sim", str(sim_file), "--output", str(output_dir.parent), "--study-name", "06_llm_mode_example_5trials", "--trials", "5" ] print("Running LLM Mode Optimization...") print("Command:") print(" ".join(cmd)) print() print("=" * 80) print() # Run the command try: result = subprocess.run(cmd, check=True) print() print("=" * 80) print("SUCCESS: LLM Mode Optimization Complete!") print("=" * 80) print() print("Results saved to:") print(f" {output_dir}") print() print("What was auto-generated:") print(" ✓ Result extractors (displacement, stress, mass)") print(" ✓ Inline calculations (safety factor, objectives)") print(" ✓ Post-processing hooks (plotting, reporting)") print(" ✓ Optuna objective function") print() print("Check the output directory for:") print(" - generated_extractors/ - Auto-generated Python extractors") print(" - generated_hooks/ - Auto-generated hook scripts") print(" - history.json - Optimization history") print(" - best_trial.json - Best design found") print(" - plots/ - Convergence and design space plots (if enabled)") print() return True except subprocess.CalledProcessError as e: print() print("=" * 80) print(f"FAILED: Optimization failed with error code {e.returncode}") print("=" * 80) print() return False except Exception as e: print() print("=" * 80) print(f"ERROR: {e}") print("=" * 80) print() import traceback traceback.print_exc() return False def main(): """Main entry point.""" print() print("This example demonstrates the LLM-native optimization workflow.") print() print("IMPORTANT: This uses Claude Code integration (no API key needed).") print("Make sure Claude Code is running and test_env is activated.") print() input("Press ENTER to continue (or Ctrl+C to cancel)...") print() success = run_llm_optimization_example() if success: print() print("=" * 80) print("EXAMPLE COMPLETED SUCCESSFULLY!") print("=" * 80) print() print("Next Steps:") print("1. Review the generated extractors in the output directory") print("2. Examine the optimization history in history.json") print("3. Check the plots/ directory for visualizations") print("4. Try modifying the natural language request and re-running") print() print("This demonstrates Phase 3.2 integration:") print(" Natural Language → LLM → Code Generation → Optimization → Results") print() else: print() print("Example failed. Please check the error messages above.") print() return success if __name__ == '__main__': success = main() sys.exit(0 if success else 1)