energy-optimization-space / test_environment.py
Sushruth21's picture
refactor: revert to root-level package structure with proper imports and hacky pyproject.toml setup
433cefc
#!/usr/bin/env python3
"""
Test script for the Energy & Memory RAM Optimization Environment.
"""
import sys
import os
# Add the project root to Python path
project_root = os.path.dirname(__file__)
sys.path.insert(0, project_root)
# Import from root level modules
from models import EnergyOptimizationAction, EnergyOptimizationObservation, Task, TaskSummary
from server.he_demo_environment import EnergyOptimizationEnvironment
def test_environment():
"""Test the energy optimization environment."""
print("Testing Energy & Memory RAM Optimization Environment")
print("=" * 60)
# Create environment
env = EnergyOptimizationEnvironment()
# Test reset
print("\n1. Testing reset...")
obs = env.reset()
print(f"Initial RAM usage: {obs.ram_usage:.1f}%")
print(f"Initial energy consumption: {obs.energy_consumption:.1f} kWh")
print(f"Initial system load: {obs.system_load:.2f}")
print(f"Current task: {obs.current_task.name if obs.current_task else 'None'}")
print(f"Tasks completed: {obs.tasks_completed}")
# Test different actions
actions_to_test = [
("reduce_ram", 0.8),
("optimize_energy", 0.7),
("balance_resources", 0.6),
("monitor_system", 0.5)
]
print("\n2. Testing actions...")
for action_type, intensity in actions_to_test:
action = EnergyOptimizationAction(action_type=action_type, intensity=intensity)
obs = env.step(action)
print(f"\nAction: {action_type} (intensity: {intensity})")
print(f"RAM usage: {obs.ram_usage:.1f}%")
print(f"Energy consumption: {obs.energy_consumption:.1f} kWh")
print(f"System load: {obs.system_load:.2f}")
print(f"Reward: {obs.reward:.2f}")
print(f"Task progress: {obs.task_progress:.2f}")
print(f"Efficiency score: {obs.efficiency_score:.2f}")
print(f"Current task: {obs.current_task.name if obs.current_task else 'None'}")
print(f"Tasks completed: {obs.tasks_completed}")
if obs.done:
print("Episode completed!")
break
print("\n3. Testing task progression...")
# Reset and try to complete a task
obs = env.reset()
steps = 0
max_test_steps = 20
while not obs.done and steps < max_test_steps:
# Simple strategy: alternate between RAM reduction and energy optimization
if steps % 2 == 0:
action = EnergyOptimizationAction(action_type="reduce_ram", intensity=0.9)
else:
action = EnergyOptimizationAction(action_type="optimize_energy", intensity=0.8)
obs = env.step(action)
steps += 1
print(f"Step {steps}: RAM={obs.ram_usage:.1f}%, Energy={obs.energy_consumption:.1f}kWh, Reward={obs.reward:.2f}")
if obs.current_task and obs.task_progress >= 1.0:
print(f"Task '{obs.current_task.name}' completed!")
break
print("\nTest completed successfully!")
print(f"Final state: RAM={obs.ram_usage:.1f}%, Energy={obs.energy_consumption:.1f}kWh")
print(f"Tasks completed: {len(obs.tasks_completed)}")
print(f"Total steps: {steps}")
if __name__ == "__main__":
test_environment()