Commit ·
5f74ac2
1
Parent(s): 160ceda
feat: Integrate task-specific graders into training script per hackathon rules
Browse files- Import TASK_GRADERS and get_grader_metadata from task_graders module
- Display available task graders with real-world applications at training startup
- Evaluate trained agent using task-specific grader function
- Log grader score on final test observation
- Ensures grader configuration is part of training pipeline per hackathon requirement
- train_agent.py +35 -3
train_agent.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Train an RL agent on the Energy Optimization Environment.
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import sys
|
|
@@ -19,14 +22,29 @@ sys.modules['he_demo'] = he_demo
|
|
| 19 |
sys.modules['he_demo.models'] = he_demo
|
| 20 |
|
| 21 |
from gym_wrapper import EnergyOptimizationGymEnv
|
|
|
|
| 22 |
from stable_baselines3 import PPO
|
| 23 |
from stable_baselines3.common.env_util import make_vec_env
|
| 24 |
|
| 25 |
def train_agent():
|
| 26 |
-
"""Train a PPO agent on the energy optimization environment.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
print("🚀 Training PPO Agent on Energy Optimization Environment")
|
| 29 |
print("=" * 60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Create vectorized environment for better training
|
| 32 |
def make_env():
|
|
@@ -60,19 +78,23 @@ def train_agent():
|
|
| 60 |
print("✅ Model saved as 'energy_optimization_ppo.zip'")
|
| 61 |
|
| 62 |
# Test the trained agent
|
| 63 |
-
print("\n🧪 Testing trained agent...")
|
| 64 |
test_env = EnergyOptimizationGymEnv()
|
| 65 |
obs, _ = test_env.reset()
|
| 66 |
|
| 67 |
total_reward = 0
|
| 68 |
steps = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
while steps < 50:
|
| 71 |
# Get action from trained model
|
| 72 |
action, _ = model.predict(obs, deterministic=True)
|
| 73 |
|
| 74 |
# Execute action
|
| 75 |
-
obs, reward, done, _,
|
| 76 |
|
| 77 |
total_reward += reward
|
| 78 |
steps += 1
|
|
@@ -87,6 +109,16 @@ def train_agent():
|
|
| 87 |
|
| 88 |
if done:
|
| 89 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
train_agent()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Train an RL agent on the Energy Optimization Environment.
|
| 4 |
+
|
| 5 |
+
Per hackathon requirements, this training script includes task-specific grader configuration
|
| 6 |
+
to evaluate agent performance according to the defined scoring methodology.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import sys
|
|
|
|
| 22 |
sys.modules['he_demo.models'] = he_demo
|
| 23 |
|
| 24 |
from gym_wrapper import EnergyOptimizationGymEnv
|
| 25 |
+
from task_graders import TASK_GRADERS, get_grader_metadata
|
| 26 |
from stable_baselines3 import PPO
|
| 27 |
from stable_baselines3.common.env_util import make_vec_env
|
| 28 |
|
| 29 |
def train_agent():
|
| 30 |
+
"""Train a PPO agent on the energy optimization environment.
|
| 31 |
+
|
| 32 |
+
Per hackathon requirements, this training configures task-specific graders
|
| 33 |
+
for evaluating agent performance according to defined scoring methodology.
|
| 34 |
+
"""
|
| 35 |
|
| 36 |
print("🚀 Training PPO Agent on Energy Optimization Environment")
|
| 37 |
print("=" * 60)
|
| 38 |
+
|
| 39 |
+
# ===== GRADER CONFIGURATION (Hackathon Requirement) =====
|
| 40 |
+
# Display available tasks and their grader configurations
|
| 41 |
+
print("\n📋 Available Task Graders:")
|
| 42 |
+
for task_name, task_info in TASK_GRADERS.items():
|
| 43 |
+
metadata = get_grader_metadata(task_name)
|
| 44 |
+
print(f" • {metadata['display_name']} (Difficulty {metadata['difficulty']})")
|
| 45 |
+
print(f" Targets: RAM < {metadata['target_ram']}%, Energy < {metadata['target_energy']} kWh")
|
| 46 |
+
print(f" Application: {metadata['real_world_application']}")
|
| 47 |
+
print()
|
| 48 |
|
| 49 |
# Create vectorized environment for better training
|
| 50 |
def make_env():
|
|
|
|
| 78 |
print("✅ Model saved as 'energy_optimization_ppo.zip'")
|
| 79 |
|
| 80 |
# Test the trained agent
|
| 81 |
+
print("\n🧪 Testing trained agent with grader evaluation...")
|
| 82 |
test_env = EnergyOptimizationGymEnv()
|
| 83 |
obs, _ = test_env.reset()
|
| 84 |
|
| 85 |
total_reward = 0
|
| 86 |
steps = 0
|
| 87 |
+
|
| 88 |
+
# Import grader for evaluation
|
| 89 |
+
from task_graders import get_grader
|
| 90 |
+
grader_func = get_grader("balanced_optimization") # Example grader task
|
| 91 |
|
| 92 |
while steps < 50:
|
| 93 |
# Get action from trained model
|
| 94 |
action, _ = model.predict(obs, deterministic=True)
|
| 95 |
|
| 96 |
# Execute action
|
| 97 |
+
obs, reward, done, _, info = test_env.step(action)
|
| 98 |
|
| 99 |
total_reward += reward
|
| 100 |
steps += 1
|
|
|
|
| 109 |
|
| 110 |
if done:
|
| 111 |
break
|
| 112 |
+
|
| 113 |
+
# Calculate grader score on final observation
|
| 114 |
+
if hasattr(test_env, 'env') and hasattr(test_env.env, 'observation'):
|
| 115 |
+
try:
|
| 116 |
+
final_obs = test_env.env.observation()
|
| 117 |
+
if final_obs and hasattr(final_obs, 'ram_usage'):
|
| 118 |
+
grader_score = grader_func(final_obs)
|
| 119 |
+
print(f"\n✅ Grader Score (Task: balanced_optimization): {grader_score:.3f}")
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"[DEBUG] Could not calculate grader score: {e}")
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
train_agent()
|