-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaselines.py
More file actions
280 lines (210 loc) · 10.1 KB
/
baselines.py
File metadata and controls
280 lines (210 loc) · 10.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Baseline-Agenten fuer Vergleichsevaluationen."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Optional
import numpy as np
from config_loader import Config
def _idle_action(env) -> int:
n_rho = len(env.rho_levels)
middle_power = len(env.power_levels) // 2
return middle_power * n_rho
def _encode_action(env, target_power: float, target_rho: float = 0.0) -> int:
power_idx = int(np.argmin(np.abs(env.power_levels - target_power)))
rho_idx = int(np.argmin(np.abs(env.rho_levels - target_rho)))
return power_idx * len(env.rho_levels) + rho_idx
def _soc_position(config: Config, current_soc: float) -> float:
soc_range = max(config.battery.soc_max - config.battery.soc_min, 1e-9)
return float(np.clip((current_soc - config.battery.soc_min) / soc_range, 0.0, 1.0))
def _spread_floor(env, target_soc: Optional[float] = None, reference_price: Optional[float] = None) -> float:
if not hasattr(env, "estimate_round_trip_spread_floor_eur_per_mwh"):
return 0.0
return float(
env.estimate_round_trip_spread_floor_eur_per_mwh(
delta_soc=target_soc,
reference_price=reference_price,
)
)
class BaselineAgent(ABC):
"""Abstrakte Basisklasse fuer Baseline-Agenten."""
@abstractmethod
def act(self, obs: np.ndarray, env) -> int:
pass
def reset(self):
pass
class IdleAgent(BaselineAgent):
"""Agent der nichts tut."""
def act(self, obs: np.ndarray, env) -> int:
return _idle_action(env)
class RandomAgent(BaselineAgent):
"""Agent der zufaellige Aktionen waehlt."""
def __init__(self, seed: int = 42):
self.rng = np.random.default_rng(seed)
def act(self, obs: np.ndarray, env) -> int:
return int(self.rng.integers(0, env.action_space.n))
class RuleBasedAgent(BaselineAgent):
"""Regelbasierte Strategie mit adaptivem Preisspread und Reserve-Option."""
def __init__(self, config: Config, price_history_window: int = 48, use_reserve: bool = True):
self.price_history = []
self.window = price_history_window
self.config = config
self.use_reserve = use_reserve
def reset(self):
self.price_history = []
def _reserve_level(self, env, soc_pos: float, signal_strength: float) -> float:
if not self.use_reserve or len(env.rho_levels) <= 1:
return 0.0
reserve_price = float(env.res_price[env.t])
if reserve_price < np.percentile(env.res_price, 60):
return 0.0
if not 0.3 <= soc_pos <= 0.7:
return 0.0
if abs(signal_strength) > 0.35:
return 0.0
return float(env.rho_levels[min(1, len(env.rho_levels) - 1)])
def act(self, obs: np.ndarray, env) -> int:
current_price = float(env.price[env.t])
current_soc = env.c
self.price_history.append(current_price)
if len(self.price_history) > self.window:
self.price_history.pop(0)
hist = np.asarray(self.price_history, dtype=np.float64)
avg_price = float(np.mean(hist))
price_std = float(np.std(hist)) + 1e-6
signal = (current_price - avg_price) / price_std
soc_pos = _soc_position(self.config, current_soc)
price_edge_floor = max(0.8 * price_std, 0.5 * _spread_floor(env, env.b_max, avg_price))
target_power = 0.0
if current_price <= avg_price - price_edge_floor and current_soc < self.config.battery.soc_max - 0.03:
target_power = env.b_max * min(1.0, (-signal) / 2.0) * (1.0 - soc_pos)
elif current_price >= avg_price + price_edge_floor and current_soc > self.config.battery.soc_min + 0.03:
target_power = -env.b_max * min(1.0, signal / 2.0) * soc_pos
rho = self._reserve_level(env, soc_pos, signal)
return _encode_action(env, target_power, rho)
class ThresholdAgent(BaselineAgent):
"""Schwellenwert-basierter Agent mit festen Preisgrenzen."""
def __init__(
self,
config: Config,
low_threshold: float = 40.0,
high_threshold: float = 80.0,
):
self.config = config
self.low_threshold = low_threshold
self.high_threshold = high_threshold
def act(self, obs: np.ndarray, env) -> int:
current_price = float(env.price[env.t])
current_soc = env.c
if current_price < self.low_threshold and current_soc < self.config.battery.soc_max - 0.05:
return _encode_action(env, env.b_max, 0.0)
if current_price > self.high_threshold and current_soc > self.config.battery.soc_min + 0.05:
return _encode_action(env, -env.b_max, 0.0)
return _idle_action(env)
class MovingAverageAgent(BaselineAgent):
"""Agent mit gleitendem Durchschnitt und leichter Reserve-Logik."""
def __init__(
self,
config: Config,
window: int = 24,
charge_threshold: float = 0.85,
discharge_threshold: float = 1.15,
use_reserve: bool = False,
):
self.config = config
self.window = window
self.charge_threshold = charge_threshold
self.discharge_threshold = discharge_threshold
self.use_reserve = use_reserve
self.price_history = []
def reset(self):
self.price_history = []
def act(self, obs: np.ndarray, env) -> int:
current_price = float(env.price[env.t])
current_soc = env.c
self.price_history.append(current_price)
if len(self.price_history) > self.window:
self.price_history.pop(0)
ma = float(np.mean(self.price_history))
volatility = float(np.std(self.price_history)) if len(self.price_history) > 1 else 0.0
soc_pos = _soc_position(self.config, current_soc)
spread_floor = _spread_floor(env, env.b_max, ma)
charge_band = max(ma * (1.0 - self.charge_threshold), 0.5 * spread_floor)
discharge_band = max(ma * (self.discharge_threshold - 1.0), 0.5 * spread_floor)
if current_price < ma - charge_band:
strength = np.clip((ma - charge_band - current_price) / max(volatility, 5.0), 0.0, 1.0)
target_power = env.b_max * strength * (1.0 - soc_pos)
return _encode_action(env, target_power, 0.0)
if current_price > ma + discharge_band:
strength = np.clip((current_price - ma - discharge_band) / max(volatility, 5.0), 0.0, 1.0)
target_power = -env.b_max * strength * soc_pos
return _encode_action(env, target_power, 0.0)
if self.use_reserve and len(env.rho_levels) > 1 and 0.35 <= soc_pos <= 0.65 and volatility < 8.0:
reserve_level = float(env.rho_levels[min(1, len(env.rho_levels) - 1)])
return _encode_action(env, 0.0, reserve_level)
return _idle_action(env)
class QuantileAgent(BaselineAgent):
"""Praxisnahe Heuristik auf Basis rollierender Preisquantile."""
def __init__(self, config: Config, window: int = 96):
self.config = config
self.window = window
self.price_history = []
def reset(self):
self.price_history = []
def act(self, obs: np.ndarray, env) -> int:
current_price = float(env.price[env.t])
current_soc = env.c
soc_pos = _soc_position(self.config, current_soc)
self.price_history.append(current_price)
if len(self.price_history) > self.window:
self.price_history.pop(0)
hist = np.asarray(self.price_history, dtype=np.float64)
q25, q50, q75 = np.quantile(hist, [0.25, 0.5, 0.75])
spread = max(q75 - q25, 1e-6)
rt_efficiency = max(self.config.battery.eta_charge * self.config.battery.eta_discharge, 1e-6)
spread_floor = q50 * max(0.0, 1.0 / rt_efficiency - 1.0) + _spread_floor(env, env.b_max, q50)
if current_price <= q25 and spread >= spread_floor and current_soc < self.config.battery.soc_max - 0.03:
intensity = np.clip((q25 - current_price + spread_floor) / spread, 0.0, 1.0)
return _encode_action(env, env.b_max * intensity * (1.0 - soc_pos), 0.0)
if current_price >= q75 and spread >= spread_floor and current_soc > self.config.battery.soc_min + 0.03:
intensity = np.clip((current_price - q75 + spread_floor) / spread, 0.0, 1.0)
return _encode_action(env, -env.b_max * intensity * soc_pos, 0.0)
if len(env.rho_levels) > 1 and q25 < current_price < q75 and 0.35 <= soc_pos <= 0.65:
reserve_level = float(env.rho_levels[min(1, len(env.rho_levels) - 1)])
return _encode_action(env, 0.0, reserve_level)
return _idle_action(env)
class PeakShavingAgent(BaselineAgent):
"""Heuristik fuer Peak-Shaving bzw. einfache Tagesgang-Optimierung."""
def __init__(
self,
config: Config,
peak_start: int = 9,
peak_end: int = 21,
):
self.config = config
self.peak_start = peak_start
self.peak_end = peak_end
def act(self, obs: np.ndarray, env) -> int:
if env.timestamps is not None:
hour = int(np.datetime64(env.timestamps[env.t], "h").astype(object).hour)
else:
hour = int((env.t * env.dt) % 24)
current_soc = env.c
soc_pos = _soc_position(self.config, current_soc)
if self.peak_start <= hour < self.peak_end:
if current_soc > self.config.battery.soc_min + 0.08:
return _encode_action(env, -0.6 * env.b_max * soc_pos, 0.0)
else:
if current_soc < self.config.battery.soc_max - 0.08:
return _encode_action(env, 0.6 * env.b_max * (1.0 - soc_pos), 0.0)
return _idle_action(env)
def get_all_baselines(config: Config, seed: int = 42) -> dict:
"""Erstelle alle verfuegbaren Baseline-Agenten."""
return {
"Idle": IdleAgent(),
"Random": RandomAgent(seed=seed),
"Rule-Based": RuleBasedAgent(config),
"Threshold": ThresholdAgent(config),
"MovingAvg": MovingAverageAgent(config, window=24),
"MovingAvg-Reserve": MovingAverageAgent(config, window=24, use_reserve=True),
"Quantile": QuantileAgent(config, window=96),
"PeakShaving": PeakShavingAgent(config),
}