-
Notifications
You must be signed in to change notification settings - Fork 1
/
nonlinear_simulation.py
78 lines (54 loc) · 2.52 KB
/
nonlinear_simulation.py
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
import gym
import numpy as np
from numpy import linalg as LA
import pandapower as pp
class VoltageCtrl_nonlinear(gym.Env):
def __init__(self, pp_net, vmax, vmin, v0, injection_bus, obs_dim=55, action_dim=55):
self.network = pp_net
self.injection_bus = injection_bus
self.agentnum = len(injection_bus)
self.obs_dim = obs_dim
self.action_dim = action_dim
self.v0 = v0
self.vmax = vmax
self.vmin = vmin
self.load0_p = np.copy(self.network.load['p_mw'])
self.load0_q = np.copy(self.network.load['q_mvar'])
self.gen0_p = np.copy(self.network.sgen['p_mw'])
self.gen0_q = np.copy(self.network.sgen['q_mvar'])
self.state = np.ones(self.agentnum)
def step(self, action):
"State transition dynamics: it takes in the reactive power setpoint"
"then compute the voltage magnitude at each node via solving power flow"
done = False
reward = float(-100*LA.norm(self.state-1.0)**2)
# state-transition dynamics
for i in range(len(self.injection_bus)):
self.network.sgen.at[i, 'q_mvar'] = action[i]
pp.runpp(self.network, algorithm='bfsw', init = 'dc')
self.state = self.network.res_bus.iloc[self.injection_bus].vm_pu.to_numpy()
return self.state, reward, done
def step_load_solar(self, action, load_p, load_q, gen_p, gen_q):
"State transition dynamics: it takes in the reactive power setpoint, load_p and load_q"
"and gen_p & gen_q to compute the voltage magnitude at each node via solving power flow"
done = False
reward = float(-100*LA.norm(self.state-1.0)**2)
# state-transition dynamics
self.network.load['p_mw'] = load_p
self.network.load['q_mvar'] = load_q
self.network.sgen['p_mw'] = gen_p
self.network.sgen['q_mvar'] = gen_q
for i in range(len(self.injection_bus)):
self.network.sgen.at[i, 'q_mvar'] += action[i]
pp.runpp(self.network, algorithm='bfsw', init = 'dc')
self.state = self.network.res_bus.iloc[self.injection_bus].vm_pu.to_numpy()
return self.state, reward, done
def reset(self, seed=1):
np.random.seed(seed)
self.network.sgen['p_mw'] = 0.0
self.network.sgen['q_mvar'] = 0.0
self.network.load['p_mw'] = 0.0
self.network.load['q_mvar'] = 0.0
pp.runpp(self.network, algorithm='bfsw')
self.state = self.network.res_bus.iloc[self.injection_bus].vm_pu.to_numpy()
return self.state