/
runGAs.py
224 lines (177 loc) · 7.13 KB
/
runGAs.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
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
#!/usr/bin/env python3
'''
NHES optimization using genetic algorithms.
Nicholas Cooper
'''
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import differential_evolution, minimize, NonlinearConstraint
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
from geneticOpt import GA
from ScipyBaseModel import model, model_obj_only, model_con_max_T, model_con_min_T, model_con_max_ramp, get_T
from utils import get_data, results, gen_report
from default_config import config
def save_iters(x, savepath):
all_iters = np.array(x).T
df = pd.DataFrame(all_iters)
df.to_csv(savepath, index=False)
def runCustom(animate=False):
time, load = get_data(config['month'], config['year'])
# config['max_ramp_rate'] = 3000
def my_con_max_temp(gen):
inequalities = model_con_max_T(gen, time, load, config)
for i, con in enumerate(inequalities):
if con >= 0:
inequalities[i] = 0
return np.sum(inequalities)
def my_con_min_temp(gen):
inequalities = model_con_min_T(gen, time, load, config)
for i, con in enumerate(inequalities):
if con >= 0:
inequalities[i] = 0
return np.sum(inequalities)
def my_con_max_ramp(gen):
inequalities = model_con_max_ramp(gen, config)
for i, con in enumerate(inequalities):
if con >= 0:
inequalities[i] = 0
return np.sum(inequalities)
# def my_con_max_ramp(X):
# '''For custom GA.
# Max ramp up or down does not exceed 2000 MW/hr'''
# dEdt = []
# max_ramp = 2000
# for i in range(len(X)-1):
# slope = abs(X[i+1] - X[i])
# if slope > max_ramp:
# dEdt.append(slope)
# else:
# dEdt.append(0)
# return np.sum(dEdt)
populations = []
def callback(gen):
populations.append(gen)
guess = np.ones(len(time))*config['guess_coef']
bounds = [(1e3, 8e4) for i in range(len(time))]
constraints = ({'fun':my_con_max_temp, 'type':'ineq', 'scale':100},
{'fun':my_con_min_temp, 'type':'ineq', 'scale':100},
{'fun':my_con_max_ramp, 'type':'ineq', 'scale':1000})
ga = GA(model_obj_only, bounds = bounds, maxiter=100, mscale=100, tol=1e-3,
constraints=constraints, pmutate=0.5, callback=callback)
sol = ga.optimize(verbose=True)
xstar = sol[0]
nfev = ga.fevals
print(sol)
print(results(sol[0], config))
gen_report([xstar,nfev], "Custom GA", "Constrained", config, notes="lb 1e3 ub 8e4", gen_plot=True, guess=guess)
# save_iters(populations, "GA_iters3.csv")
if animate:
def update(i):
fig.clear()
plt.xlabel('Time')
plt.ylabel("Generation")
plt.plot(time, load)
plt.plot(time, populations[i])
fig = plt.figure()
plt.xlabel("Time")
plt.ylabel("Generation")
anim = animation.FuncAnimation(fig, update, frames=len(populations), interval = 500)
plt.show()
# Scipy differential_evolution
def runScipy():
time, load = get_data(config['month'], config['year'])
guess = np.ones(len(time))*config['guess_coef']
strategies = ('best1bin', 'best1exp', 'rand1exp', 'randtobest1exp', 'currenttobest1exp',
'best2exp', 'rand2exp', 'randtobest1bin', 'currenttobest1bin', 'best2bin',
'rand2bin', 'rand1bin')
def penaltyobj(gen):
return model(gen, time, load, config)[0]
bounds = [(1e3, 1e5) for i in range(len(time))]
# Try polish=False - if True then takes the best population and uses L-BFGS-B to finish
# polish = True
opt = differential_evolution(penaltyobj, bounds=bounds, polish=True, disp=True)
print(opt)
fstar = opt.fun
xstar = opt.x
nfev = opt.nfev
print(results(xstar, config))
gen_report([xstar, nfev], "Scipy GA Polished", "Penalized", config, guess=guess,
notes="lb 1e3 ub 8e4 Polished with L-BFGS-B, "+opt.message, gen_plot=True)
# polish = False
opt = differential_evolution(penaltyobj, bounds=bounds, polish=False, disp=True)
print(opt)
fstar = opt.fun
xstar = opt.x
nfev = opt.nfev
print(results(xstar, config))
gen_report([xstar, nfev], "Scipy GA", "Penalized", config, guess=guess,
notes="lb 1e3 ub 8e4, "+opt.message, gen_plot=True)
def testStrategies():
time, load = get_data(config['month'], config['year'])
guess = np.ones(len(time))*config['guess_coef']
strategies = ('best1bin', 'best1exp', 'rand1exp', 'randtobest1exp', 'currenttobest1exp',
'best2exp', 'rand2exp', 'randtobest1bin', 'currenttobest1bin', 'best2bin',
'rand2bin', 'rand1bin')
def penaltyobj(gen):
return model(gen, time, load, config)[0]
bounds = [(1e3, 1e5) for i in range(len(time))]
# Try polish=False - if True then takes the best population and uses L-BFGS-B to finish
best = {'strategy': 'none',
'results': 'none',
'besty': 1e32}
all_results = {}
maxiter = 100
print(f"Finding best over {maxiter} iterations")
for s in strategies:
print(f"{s}...")
opt = differential_evolution(penaltyobj, bounds=bounds, polish=False,
disp=False, strategy=s, maxiter=maxiter)
all_results[s] = opt
if opt.fun < best['besty']:
best['strategy'] = s
best['results'] = opt
best['besty'] = opt.fun
print(f"Best Strategy: {best['strategy']}")
print(f"Best Function Value: {best['besty']}")
print("Results:")
print(best['results'])
print("All other strategies:\n")
for key, val in all_results.items():
print(f"{key}: {val.fun}, {val.nfev}, {val.message}")
def runScipyCon():
global fevals
time, load = get_data(config['month'], config['year'])
fevals = 0
def obj(gen):
global fevals
fevals += 1
return model_obj_only(gen)
def tempCon(gen):
return np.array(get_T(gen, time, load, config))/100
def rampCon(X):
''' Max ramp up or down does not exceed 2000 MW/hr'''
dEdt = []
for i in range(len(X)-1):
slope = abs(X[i+1] - X[i])
dEdt.append(slope)
return np.array(dEdt)/1000
bounds = [(1e3, 1e5) for i in range(len(time))]
Temp_Con = NonlinearConstraint(tempCon, lb=config['tes_min_t']/100, ub=config['tes_max_t']/100)
Ramp_Con = NonlinearConstraint(rampCon, lb=0, ub=config['max_ramp_rate']/1000)
# Try polish=False - if True then takes the best population and uses L-BFGS-B to finish
opt = differential_evolution(obj, bounds=bounds, constraints={Temp_Con, Ramp_Con},
polish=False, disp=True)
print(opt)
fstar = opt.fun
xstar = opt.x
nfev = opt.nfev
print(results(xstar, config))
print("fevals:", fevals)
gen_report([xstar, nfev], "Scipy GA", "Constrained", config, notes="lb 1e3 ub 8e4, Scaled, "+opt.message, gen_plot=True)
if __name__ == "__main__":
runCustom()
runScipy()
runScipyCon()
# testStrategies()