forked from wuyou33/WFLOP_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (130 loc) · 6.5 KB
/
main.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
import numpy as np
import pandas as pd
import MARS # MARS (Multivariate Adaptive Regression Splines) regression class
import WindFarmGeneticToolbox # wind farm layout optimization using genetic algorithms classes
from datetime import datetime
import os
# parameters for the genetic algorithm
elite_rate = 0.2
cross_rate = 0.6
random_rate = 0.5
mutate_rate = 0.1
# wind farm size, cells
rows = 21
cols = 21
cell_width = 77.0 * 2 # unit : m
#
N = 60 # number of wind turbines
pop_size = 100 # population size, number of inidividuals in a population
iteration = 1 # number of genetic algorithm iterations
# all data will be save in data folder
data_folder = "data"
if not os.path.exists(data_folder):
os.makedirs(data_folder)
# create an object of WindFarmGenetic
wfg = WindFarmGeneticToolbox.WindFarmGenetic(rows=rows, cols=cols, N=N, pop_size=pop_size,
iteration=iteration, cell_width=cell_width, elite_rate=elite_rate,
cross_rate=cross_rate, random_rate=random_rate, mutate_rate=mutate_rate)
# set wind distribution
# wind distribution is discrete (number of wind speeds) by (number of wind directions)
# wfg.init_4_direction_1_speed_12()
wfg.init_1_direction_1_N_speed_12()
################################################
# generate initial populations
################################################
init_pops_data_folder = "data/init_pops"
if not os.path.exists(init_pops_data_folder):
os.makedirs(init_pops_data_folder)
# n_init_pops : number of initial populations
n_init_pops = 60
for i in range(n_init_pops):
wfg.gen_init_pop()
wfg.save_init_pop("{}/init_{}.dat".format(init_pops_data_folder,i))
#############################################
# generate wind distribution surface
#############################################
wds_data_folder = "data/wds"
if not os.path.exists(wds_data_folder):
os.makedirs(wds_data_folder)
# mc : monte-carlo
n_mc_samples = 10000
# each layout is binary list and the length of the list is (rows*cols)
# 1 indicates there is a wind turbine in that cell
# 0 indicates there is no wind turbine in the cell
# in "mc_layout.dat", there are 'n_mc_samples' line and each line is a layout.
# generate 'n_mc_samples' layouts and save it in 'mc_layout.data' file
WindFarmGeneticToolbox.LayoutGridMCGenerator.gen_mc_grid(rows=rows, cols=cols, n=n_mc_samples, N=N,
lofname="{}/{}".format(wds_data_folder, "mc_layout.dat"))
# read layouts from 'mc_layout.dat' file
layouts = np.genfromtxt("{}/{}".format(wds_data_folder,"mc_layout.dat"), delimiter=" ", dtype=np.int32)
# generate dataset to build wind farm distribution surface
wfg.mc_gen_xy(rows=rows, cols=cols, layouts=layouts, n=n_mc_samples, N=N, xfname="{}/{}".format(wds_data_folder, "x.dat"),
yfname="{}/{}".format(wds_data_folder, "y.dat"))
# parameters for MARS regression method
n_variables = 2
n_points = rows * cols
n_candidate_knots = [rows, cols]
n_max_basis_functions = 100
n_max_interactions = 4
difference = 1.0e-3
x_original = pd.read_csv("{}/{}".format(wds_data_folder,"x.dat"), header=None, nrows=n_points, delim_whitespace=True)
x_original = x_original.values
y_original = pd.read_csv("{}/{}".format(wds_data_folder,"y.dat"), header=None, nrows=n_points, delim_whitespace=True)
y_original = y_original.values
mars = MARS.MARS(n_variables=n_variables, n_points=n_points, x=x_original, y=y_original,
n_candidate_knots=n_candidate_knots, n_max_basis_functions=n_max_basis_functions,
n_max_interactions=n_max_interactions, difference=difference)
mars.MARS_regress()
# save wind distribution model to 'wds.mars'
mars.save_mars_model_to_file("{}/{}".format(wds_data_folder,"wds.mars"))
# results folder
# adaptive_best_layouts_N60_9_20190422213718.dat : best layout for AGA of run index 9
# result_CGA_20190422213715.dat : run time and best eta for CGA method
results_data_folder = "data/results"
if not os.path.exists(results_data_folder):
os.makedirs(results_data_folder)
n_run_times = 1 # number of run times
# result_arr stores the best conversion efficiency of each run
result_arr = np.zeros((n_run_times, 2), dtype=np.float32)
# CGA method
CGA_results_data_folder = "{}/CGA".format(results_data_folder)
if not os.path.exists(CGA_results_data_folder):
os.makedirs(CGA_results_data_folder)
for i in range(0, n_run_times): # run times
print("run times {} ...".format(i))
wfg.load_init_pop("{}/init_{}.dat".format(init_pops_data_folder, i))
run_time, eta = wfg.conventional_genetic_alg(ind_time=i, result_folder=CGA_results_data_folder)
result_arr[i, 0] = run_time
result_arr[i, 1] = eta
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
filename = "{}/result_CGA_{}.dat".format(CGA_results_data_folder, time_stamp)
np.savetxt(filename, result_arr, fmt='%f', delimiter=" ")
# AGA method
AGA_results_data_folder = "{}/AGA".format(results_data_folder)
if not os.path.exists(AGA_results_data_folder):
os.makedirs(AGA_results_data_folder)
for i in range(0, n_run_times): # run times
print("run times {} ...".format(i))
wfg.load_init_pop("{}/init_{}.dat".format(init_pops_data_folder, i))
run_time, eta = wfg.adaptive_genetic_alg(ind_time=i, result_folder=AGA_results_data_folder)
result_arr[i, 0] = run_time
result_arr[i, 1] = eta
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
filename = "{}/result_AGA_{}.dat".format(AGA_results_data_folder, time_stamp)
np.savetxt(filename, result_arr, fmt='%f', delimiter=" ")
# SIGA method
SIGA_results_data_folder = "{}/SIGA".format(results_data_folder)
if not os.path.exists(SIGA_results_data_folder):
os.makedirs(SIGA_results_data_folder)
# wds_mars_file : wind distribution surface MARS model file
wds_mars_file = "{}/{}".format(wds_data_folder, "wds.mars")
for i in range(0, n_run_times): # run times
print("run times {} ...".format(i))
wfg.load_init_pop("{}/init_{}.dat".format(init_pops_data_folder, i))
run_time, eta = wfg.self_informed_genetic_alg(ind_time=i, result_folder=SIGA_results_data_folder,
wds_file=wds_mars_file)
result_arr[i, 0] = run_time
result_arr[i, 1] = eta
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
filename = "{}/result_self_informed_{}.dat".format(SIGA_results_data_folder, time_stamp)
np.savetxt(filename, result_arr, fmt='%f', delimiter=" ")