File size: 3,687 Bytes
e00c2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)

# Mock the he_demo package for testing
import types
he_demo = types.ModuleType('he_demo')

# Import models and add to he_demo
from models import EnergyOptimizationAction, EnergyOptimizationObservation, Task, TaskSummary
he_demo.EnergyOptimizationAction = EnergyOptimizationAction
he_demo.EnergyOptimizationObservation = EnergyOptimizationObservation
he_demo.Task = Task
he_demo.TaskSummary = TaskSummary

# Add to sys.modules
sys.modules['he_demo'] = he_demo
sys.modules['he_demo.models'] = he_demo

# Now import the environment
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()