-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_with_real_data.py
More file actions
126 lines (103 loc) · 4.91 KB
/
example_with_real_data.py
File metadata and controls
126 lines (103 loc) · 4.91 KB
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
"""
Demand Response Agent with Real Kaggle Smart Grid Data
"""
import os
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
if not os.getenv("GROQ_API_KEY") and os.getenv("api_key"):
os.environ["GROQ_API_KEY"] = os.getenv("api_key")
from models import Thermostat, ThermostatMode, GridState, GridStatus
from dr_controller import DRController
from data_loader import SmartGridDataLoader
def run_dr_with_real_data():
"""Run Demand Response Agent with real smart grid data"""
print("\n" + "="*70)
print("DEMAND RESPONSE AGENT - REAL KAGGLE SMART GRID DATA")
print("="*70)
# Load real data
loader = SmartGridDataLoader()
loader.get_data_info()
# Get scenarios from dataset
num_scenarios = 3
scenarios = loader.get_dataset_scenarios(samples=num_scenarios)
for idx, scenario in enumerate(scenarios, 1):
print(f"\n{'='*70}")
print(f"SCENARIO {idx}: {scenario['description']}")
print(f"{'='*70}")
grid = scenario['grid']
thermostats = scenario['thermostats']
# Create FRESH controller for this scenario (not reused!)
controller = DRController()
# Register thermostats for THIS scenario only
for t in thermostats:
controller.register_thermostat(t)
# Display grid conditions
print(f"\n📊 GRID CONDITIONS:")
print(f" Status: {grid.status.value.upper()}")
print(f" Demand: {grid.demand_mw:.3f} MW")
print(f" Generation: {grid.generation_mw:.3f} MW")
print(f" Surplus: {grid.demand_surplus:.3f} MW")
print(f" Frequency: {grid.frequency_hz:.3f} Hz")
print(f" Stress Level: {grid.stress_level:.1%}")
print(f" Renewable: {grid.renewable_pct:.1f}%")
# Display thermostat pool
print(f"\n🌡️ THERMOSTAT POOL ({len(thermostats)} devices):")
total_capacity = sum(t.capacity_mw for t in thermostats)
total_flexibility = sum(t.flexibility for t in thermostats) / len(thermostats)
print(f" Total Capacity: {total_capacity:.3f} MW")
print(f" Average Flexibility: {total_flexibility:.2f}")
for t in thermostats[:3]:
print(f" • {t.device_id} ({t.location}): {t.current_temp}°C → {t.target_temp}°C")
print(f" Capacity: {t.capacity_mw:.3f} MW | Flexibility: {t.flexibility:.2f} | Mode: {t.mode.value}")
if len(thermostats) > 3:
print(f" ... and {len(thermostats) - 3} more")
# Run DR Agent
print(f"\n⚡ RUNNING DEMAND RESPONSE AGENT...")
try:
result = controller.run_dr(grid)
print(f"\n✅ AGENT ANALYSIS:")
print(f" {result['analysis']}")
print(f"\n📋 DR ACTIONS:")
print(f" Actions Generated: {len(result['actions'])}")
if result['actions']:
total_reduction = sum(a.expected_reduction_mw for a in result['actions'])
print(f" Expected Reduction: {total_reduction:.4f} MW")
print(f" Impact: {(total_reduction / grid.demand_mw * 100):.2f}% of demand" if grid.demand_mw > 0 else " Impact: N/A (no demand)")
print(f"\n Selected Devices:")
for action in result['actions'][:5]:
print(f" • {action.device_id}")
print(f" Action: {action.action}")
print(f" Target: {action.target_temp}°C")
print(f" Expected Reduction: {action.expected_reduction_mw:.4f} MW")
if len(result['actions']) > 5:
print(f" ... and {len(result['actions']) - 5} more devices")
# Apply actions
print(f"\n📌 Applying actions to thermostats...")
controller.apply_all_actions(result['actions'])
print(f" ✓ All actions applied")
else:
print(f" No actions needed - grid is stable")
except Exception as e:
print(f" ❌ Error running agent: {e}")
print(f" Make sure GROQ_API_KEY is set and dependencies are installed")
# Final summary
print(f"\n{'='*70}")
print("SUMMARY")
print(f"{'='*70}")
if controller:
summary = controller.get_summary()
print(f"\nRegistered Devices: {summary['registered_devices']}")
print(f"Recent Actions: {summary['recent_actions']}")
print(f"Total Reduction: {summary['total_reduction_mw']:.4f} MW")
print(f"\n{'='*70}\n")
if __name__ == "__main__":
try:
run_dr_with_real_data()
except KeyboardInterrupt:
print("\n\nInterrupted by user")
except Exception as e:
print(f"\n❌ Fatal Error: {e}")
import traceback
traceback.print_exc()