-
Notifications
You must be signed in to change notification settings - Fork 4
/
bus13_opt_action.py
66 lines (56 loc) · 2.16 KB
/
bus13_opt_action.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
# Kevin Moy, 8/6/2020
# Find optimal capacitor control on OpenDSS IEEE 13-bus given a load profile
import win32com.client
import pandas as pd
import os
import numpy as np
from bus13_state_reward import *
TOTAL_ACTIONS = 4
def action_to_cap_control(action, DSSCircuit):
# Currently, only takes in IEEE 13 bus OpenDSS as input
# action: Range of [0 3] of actions from RL agent
# DSSCircuit: object of type DSSObj.ActiveCircuit (COM interface for OpenDSS Circuit)
# Execute capacitor bank control based on action, given the following action space:
if action == 0:
# Both capacitors off:
DSSCircuit.Capacitors.Name = "Cap1"
DSSCircuit.Capacitors.States = (0,)
DSSCircuit.Capacitors.Name = "Cap2"
DSSCircuit.Capacitors.States = (0,)
elif action == 1:
# Capacitor 1 on, Capacitor 2 off:
DSSCircuit.Capacitors.Name = "Cap1"
DSSCircuit.Capacitors.States = (1,)
DSSCircuit.Capacitors.Name = "Cap2"
DSSCircuit.Capacitors.States = (0,)
elif action == 2:
# Capacitor 1 off, Capacitor 2 on:
DSSCircuit.Capacitors.Name = "Cap1"
DSSCircuit.Capacitors.States = (0,)
DSSCircuit.Capacitors.Name = "Cap2"
DSSCircuit.Capacitors.States = (1,)
elif action == 3:
# Both capacitors on:
DSSCircuit.Capacitors.Name = "Cap1"
DSSCircuit.Capacitors.States = (1,)
DSSCircuit.Capacitors.Name = "Cap2"
DSSCircuit.Capacitors.States = (1,)
else:
print("Invalid action " + str(action) + ", action in range [0 3] expected")
def opt_control(DSSCircuit, DSSSolution):
# Get action with lowest reward
opt_reward = -np.Inf
opt_action = -np.Inf
for action in range(TOTAL_ACTIONS):
action_to_cap_control(action, DSSCircuit)
DSSSolution.solve()
observation = get_state(DSSCircuit)
reward = quad_reward(observation)
print("action=", action)
print('reward=', reward)
if reward > opt_reward:
opt_action = action
opt_reward = reward
print("\nopt action = ", opt_action)
print("opt reward = ", opt_reward)
return opt_action, opt_reward