-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_bo.py
165 lines (121 loc) · 4.34 KB
/
main_bo.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
import argparse
import os
import numpy as np
from utils import *
import pandas as pd
import shutil
import glob
import subprocess
import time
#from run_dexof import *
import sys
from cfd_sim.run_dexof import *
from cfd_sim.dexof_reader_class import parse_dex_file
import GPyOpt
from subprocess import PIPE, run
import random
from numpy.random import seed
from single_myring import myring_hull_ds
# parameter edit for running the file
a=555; b=2664;c=500;r=513; # Fixed constraint
a_ext=0;c_ext=0 # for exp2 set a_ext & c_ext = 2500 ; It define allowable extension in nose_length & tail legth during optimization.
data_file_name='./data_bo/bo_LCB_design1' # data file name- change for each experiment
##
sys.dont_write_bytecode = True
cad_storage_name= './cad_sim/design_points.csv'
cfd_storage_name= './cfd_sim/design_points.csv'
src= './cad_sim/stl_repo'
dst='./cfd_sim/stl_cfd'
hull_ds= myring_hull_ds()
drag_storage=[97.137896]
def delete_dir(loc):
print('*Deleted directory:',loc)
shutil.rmtree(loc)
def copy_dir(src,dst):
print('*Copied directory from',src,'to destination:',dst)
shutil.copytree(src, dst)
def deletefiles(loc):
print('Deleted files from location:',loc)
file_loc= loc+'/*'
files = glob.glob(file_loc)
for f in files:
os.remove(f)
def copy_file(src,dst):
print('*Copied file from',src,'to destination:',dst)
shutil.copy(src, dst)
def save_design_points(x):
np.savetxt(cad_storage_name,x, delimiter=',')
np.savetxt(cfd_storage_name,x, delimiter=',')
def run_cad_cfd(x):
print('shape of x:',x.shape)
feasible=hull_ds.check_feasibility_design(a=a,b=b,c=c,r=r,n=x[0][0],theta=x[0][1],a_ext=x[0][2],c_ext=x[0][3],pieces=10)
if feasible==0:
return max(drag_storage)
save_design_points(np.array([x[0][0],x[0][1],a,b,c,r,x[0][2],x[0][3]]))
delete_dir(dst)
subprocess.call('./cad_sim/run_cad.sh')
copy_dir(src,dst)
deletefiles(src)
prev = os.path.abspath(os.getcwd()) # Save the real cwd
print('prev is',prev)
cfd_sim_path= prev+'/cfd_sim'
print('func path is:',cfd_sim_path)
os.chdir(cfd_sim_path)
result = main_run()
drag_storage.append(result)
print('****Drag drag_storage:',drag_storage)
os.chdir(prev)
return result
def run_bo(run_id=0,aquistion='EI',seeds=0):
global a_ext, c_ext
################################################
deletefiles('./cad_sim/fig_hull')
bounds = [{'name': 'n', 'type': 'continuous', 'domain': (1,50)},
{'name': 'theta', 'type': 'continuous', 'domain': (1,50)},
{'name': 'a_ext', 'type': 'continuous', 'domain': (0,a_ext)},
{'name': 'c_ext', 'type': 'continuous', 'domain': (0,c_ext)}]
print('Bound is:',bounds)
max_time = None
max_iter = 50
num_iter=10
batch= int(max_iter/num_iter)
#tolerance = 1e-8 # distance between two consecutive observations
#################################################
already_run = len(glob.glob(data_file_name))
print('file exist?:',already_run)
print('Batch is:',batch)
seed(seeds)
for i in range(num_iter):
if already_run==1:
evals = pd.read_csv(data_file_name, index_col=0, delimiter="\t")
Y = np.array([[x] for x in evals["Y"]])
X = np.array(evals.filter(regex="var*"))
myBopt2D = GPyOpt.methods.BayesianOptimization(run_cad_cfd, bounds,model_type = 'GP',X=X, Y=Y,
acquisition_type=aquistion, normalize_Y=False,
exact_feval = True)
print('In other runs run')
else:
myBopt2D = GPyOpt.methods.BayesianOptimization(f=run_cad_cfd,
domain=bounds,
model_type = 'GP',
acquisition_type=aquistion, normalize_Y=False,
exact_feval = True)
already_run=1
print('In 1st run')
print('------Running batch is:',i)
# --- Run the optimization
try:
myBopt2D.run_optimization(batch,verbosity=True)
pass
except KeyboardInterrupt:
pass
sim_data_x= myBopt2D.X;
myBopt2D.save_evaluations(data_file_name)
myBopt2D.plot_acquisition()
myBopt2D.plot_convergence()
if __name__=='__main__':
run=[1]; seeds=[17]
aqu2='LCB'
for i in range(len(run)):
run_bo(run[i],aqu2,seeds[i])
#print('In BO run')