main_agent.py (3955B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import sys import os import requests import time from dotenv import load_dotenv # Load env from root current_dir = os.path.dirname(os.path.abspath(__file__)) env_path = os.path.join(current_dir, "..", ".env") load_dotenv(env_path) current_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.append(current_dir) from sub_agents.fetching_agent import FetchingAgent from sub_agents.judge_agent import JudgeAgent from sub_agents.atmospheric_agent import AtmosphericAgent from sub_agents.water_agent import WaterAgent from sub_agents.Researcher import ResearcherAgent from sub_agents.Supervisor import SupervisorAgent SIMULATOR_ACTION_URL = os.getenv( "SIMULATOR_ACTION_URL", "http://localhost:8001/simulation/action" ) def main(): print("🚀 Initializing Demeter Orchestrator...") try: fetcher = FetchingAgent() judge = JudgeAgent() researcher = ResearcherAgent() atmos_agent = AtmosphericAgent() water_agent = WaterAgent() supervisor = SupervisorAgent(researcher_agent=researcher) print("✅ Agents Online.") except Exception as e: print(f"❌ Init Error: {e}") return while True: print("\n" + "=" * 50) print("⏱️ STARTING NEW CYCLE") print("=" * 50) crops_data = fetcher.fetch_and_process() if not crops_data: print("⚠️ No crops found. Waiting...") time.sleep(10) continue batch_actions = [] for crop_data in crops_data: fmu = crop_data["fmu"] sensor_snapshot = crop_data["sensor_snapshot"] history = crop_data["history"] image_b64 = crop_data["image_b64"] crop_id = crop_data["crop_id"] print(f"\n🌱 --- PROCESSING CROP: {crop_id} ---") time.sleep(1) judge_result = judge.review_previous_cycle(fmu, image_b64) # 🧠 BANDIT LEARNING: Update model based on previous cycle outcome if judge_result: supervisor.learn_from_outcome(fmu, judge_result) time.sleep(1) strat_name, strat_instr, action_idx = supervisor.get_strategic_goal(fmu) print(f"\n🎰 BANDIT STRATEGY: {strat_name}") crop = fmu.metadata.get("crop", "unknown") stage = fmu.metadata.get("stage", "unknown") query = f"optimal hydroponic conditions for {crop} in {stage} stage" time.sleep(1) research_context = researcher.search(query) print("\n🧠 Agents Planning...") time.sleep(1) atmos_plan = atmos_agent.reason( sensors=sensor_snapshot, research=research_context, strategy=strat_instr, history=history, image_b64=image_b64, ) time.sleep(1) water_plan = water_agent.reason( sensors=sensor_snapshot, research=research_context, strategy=strat_instr, history=history, image_b64=image_b64, ) print("\n👮 Supervisor Finalizing...") final_action = supervisor.synthesize_plan( atmos_plan, water_plan, fmu, history, strategy_info=(strat_name, strat_instr, action_idx), ) batch_actions.append({"crop_id": crop_id, "action": final_action}) print(f"\n✅ Final Action for {crop_id}: {final_action}") try: requests.post(SIMULATOR_ACTION_URL, json=batch_actions) print(f"\n✅ Batch sent to Simulator ({len(batch_actions)} actions).") except Exception as e: print(f"\n❌ Connection Error: {e}") # print("\nzzz Sleeping 2 minutes...") # time.sleep(120) if __name__ == "__main__": main() |