-
Notifications
You must be signed in to change notification settings - Fork 1
/
demand_synthesizer.py
141 lines (113 loc) · 5.15 KB
/
demand_synthesizer.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
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
#%%
import wntr
import numpy as np
import pandas as pd
import matplotlib as mpl
from matplotlib import figure
import matplotlib.pyplot as plt
import timeit
import re
import pathlib
import nevergrad as ng
from get_equity import *
from network_tools_v2 import *
from IPython.display import clear_output
def synthesize_demands(n_nodes, n_days, n_components, seed, plot = False):
'''
n_nodes (int): number of nodes to generate demands for
n_days (int): how long (in days) to generate demands for (repeated units of one day)
n_components (int): how many patterns from the dataset to use when synthesizing a new pattern
'''
demand_patterns = pd.read_csv("DEMAND_PATTERNS.csv")
demand_patterns.set_index("TIME", inplace =True)
assert n_components <= len(demand_patterns.columns), " Enter a proper n_components < n demand patterns in dataset"
np.random.seed(seed)
new_demands = pd.DataFrame()
for node in range(n_nodes):
sample_patterns = demand_patterns.sample(axis = 1, n = n_components)
alphas=np.random.uniform(0.1, 1.5, n_components)
new_demand = alphas[0] * sample_patterns.iloc[:,0]
for column in np.arange(1,n_components):
new_demand = new_demand + alphas[column]* sample_patterns.iloc[:,column]
new_demand = new_demand / new_demand.mean()
new_demand.name = 'Node'+str(node+1)
shift_amount = np.random.randint(-2,3)
shifted_demand = new_demand.shift(shift_amount)
if shift_amount>0:
shifted_demand.iloc[0:shift_amount] = new_demand.iloc[-shift_amount:]
elif shift_amount < 0:
shifted_demand.iloc[shift_amount:] = new_demand.iloc[:-shift_amount]
new_demands = pd.concat([new_demands,shifted_demand], axis = 1)
new_demands = pd.concat([new_demands] * n_days, ignore_index= True)
new_demands.set_index(np.arange(1,len(new_demands)+1), inplace=True)
if plot:
fig, ax = plt.subplots()
ax.set_xlabel("Time of Day")
ax.set_xticks([1,4, 8, 12, 16, 20, 24])
ax.set_xlim(1,24)
ax.set_ylabel('Demand Multiplier')
for node in range(n_nodes):
ax.plot(new_demands.index[0:24], new_demands.iloc[0:24,node])
summed_demand = new_demands.sum(axis=1)
fig, ax = plt.subplots()
ax.plot(new_demands.index[0:24],summed_demand.iloc[0:24])
return new_demands
def synthesize_demand(n_nodes, n_components, seed, plot = False):
'''
n_nodes (int): number of nodes to generate demands for
n_components (int): how many patterns from the dataset to use when synthesizing a new pattern
'''
demand_patterns = pd.read_csv("DEMAND_PATTERNS.csv")
demand_patterns.set_index("TIME", inplace =True)
assert n_components <= len(demand_patterns.columns), " Enter a proper n_components < n demand patterns in dataset"
np.random.seed(seed)
new_demands = pd.DataFrame()
for node in range(n_nodes):
sample_patterns = demand_patterns.sample(axis = 1, n = n_components)
alphas=np.random.uniform(0.1, 1.5, n_components)
new_demand = alphas[0] * sample_patterns.iloc[:,0]
for column in np.arange(1,n_components):
new_demand = new_demand + alphas[column]* sample_patterns.iloc[:,column]
new_demand = new_demand / new_demand.mean()
new_demand.name = 'Node'+str(node+1)
shift_amount = np.random.randint(-2,3)
shifted_demand = new_demand.shift(shift_amount)
if shift_amount>0:
shifted_demand.iloc[0:shift_amount] = new_demand.iloc[-shift_amount:]
elif shift_amount < 0:
shifted_demand.iloc[shift_amount:] = new_demand.iloc[:-shift_amount]
new_demands = pd.concat([new_demands,shifted_demand], axis = 1)
return new_demands
#%%
synthesize_demands(15,7,5, 102, True)
#%% load data
# demand_patterns = pd.read_csv("DEMAND_PATTERNS.csv")
# demand_patterns.set_index("TIME", inplace =True)
#%%
# n_nodes = 20
# n_days = 7
# n_components = 5
# new_demands = pd.DataFrame()
# for node in range(n_nodes):
# sample_patterns = demand_patterns.sample(axis = 1, n = n_components)
# alphas=np.random.uniform(0.1, 1.5, n_components)
# new_demand = alphas[0] * sample_patterns.iloc[:,0]
# for column in np.arange(1,n_components):
# new_demand = new_demand + alphas[column]* sample_patterns.iloc[:,column]
# new_demand = new_demand / new_demand.mean()
# new_demand.name = str(node)
# shift_amount = np.random.randint(-2,3)
# shifted_demand = new_demand.shift(shift_amount)
# if shift_amount>0:
# shifted_demand.iloc[0:shift_amount] = new_demand.iloc[-shift_amount:]
# elif shift_amount < 0:
# shifted_demand.iloc[shift_amount:] = new_demand.iloc[:-shift_amount]
# new_demands = pd.concat([new_demands,shifted_demand], axis = 1)
# fig, ax = plt.subplots()
# for node in range(n_nodes):
# ax.plot(new_demand.index, new_demands[str(node)])
# new_demands = pd.concat([new_demands] * n_days, ignore_index= True)
# new_demands.set_index(np.arange(1,len(new_demands)+1), inplace=True)
# fig, ax = plt.subplots()
# for node in range(n_nodes):
# ax.plot(new_demands.index, new_demands[str(node)])