-
Notifications
You must be signed in to change notification settings - Fork 3
/
DS_lowOptimize.py
181 lines (155 loc) · 7.13 KB
/
DS_lowOptimize.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
# thuan.bb.hust@gmail.com
import Helper as hp
import numpy as np
import os
from genscript import genscript
import copy
import random
import matplotlib.pyplot as plt
import helpRunning as hrun
import shutil
class temppp:
pass
def lowlevel_optimizer(ind,state,pop_num):
# the inputs: - IND individual
# - STATE
# - POP_NUM the specified ID of current individual which be optimized in here.
import initGlobal as init
import update_state as us
lowinit = init.lowlevel()
ant = init.AnT()
inGP = init.GP() # get the essential parameters of GP process from initGlobal.py file. note: this's global parameter.
if init.Re_trainning:
ant = init.AnT()
Sub = init.Sub()
L = init.L()
U = init.U()
lowinit = init.lowlevel()
GP = init.GP()
#path = us.load_temporary_path()
us.update_all_saved_parameters(ant, Sub, L, U, GP, lowinit)
else:
us.update_path_low(ant,lowinit)
anpha = lowinit.anpha
saved_tree = [] # save all of the current tree generated by all of the directions
for i in range(lowinit.number_direction):
saved_tree.append([])
state.lowlevel = True
global_name = lowinit.tmpDir + ant.Antenna_Name + 'lowlevel_gen_'
global_tabname = lowinit.tmpTab + ant.Antenna_Name + '_gen_'
print('running low-level optimizer for generation ',state.gen,'population ', pop_num)
chromosome, subtree_chrom, IDlist = hp.getChrom(ind) # get chromosome from tree and other atributes.
num = len(chromosome) # number of values will be optimized.
# create randomly the trainning data for ANN.
y = [] # to save all of the fitness of each direction.
for i in range(lowinit.number_direction):
y.append([])
state.current_low_fitness = y
fitness = ind.fitness # save original chromosome fitness.
return_loss = []
return_loss.append(ind.ReturnLoss) # save original return loss variable.
for i in range(lowinit.number_direction):
return_loss.append([])
X_training = np.zeros((lowinit.number_direction,num)) # X_training in this case used for direct search method.
d = X_training # init the matrix of the directions.
current_chromosome = np.zeros(num)
for i in range(num): # save original chromosome, where this point is the start point as well as the x0 point.
current_chromosome[i] = chromosome[i]
for i in range(lowinit.number_direction): # create the matrix of the direction.
for ii in range(num):
d[i][ii] = round(random.uniform(-1,1),4) # X_training in this case is the matrix direction.
#print(X_training)
########################################################################################################################
########################################################################################################################
save_fitness_state = [] # for saving the process fitness.
save_fitness_state.append(fitness)
for k in range(lowinit.number_search_step):
state.lowlevel_k = k
# now update the x_k follow each of direction.
for i in range(lowinit.number_direction):
for ii in range(num):
X_training[i][ii] = round(current_chromosome[ii] + anpha*d[i][ii],4)
if X_training[i][ii] > 1.0:
X_training[i][ii] = 1.0
if X_training[i][ii] < -1.0:
X_training[i][ii] == -1.0
# generate all of the scripts.
print('generating ',lowinit.number_direction, ' scripts in low lelvel optimizer in generation ',state.gen)
#tree_temp = hp.insert_chrom(ind.tree,X_training[2,:],subtree_chrom, IDlist)
for i in range(lowinit.number_direction):
tree = hp.insert_chrom(ind.tree,X_training[i,:],subtree_chrom, IDlist)
#if i != 2:
# if tree == tree_temp:
# raise
#pop[i].tree.childs[1].valueofnode.plot()
[Substrate,polygons,centroid,poly_list,poly_list_type] = hp.get_all_para_for_hfss(tree) # get necessary parameters for genscript function.
name = global_name + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) +'_d_' + str(i) # name of directory would
# be used to save .vbs and .hfss file.
tabname = global_tabname + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) + '_d_' + str(i) # name of directory
# would be used to save .tab file.
temppp.tree = tree
temppp.nodelist = ind.nodelist
temp,_,_ = hp.getChrom(temppp)
print(X_training[i,:])
print(".....")
print(temp)
for iiii in range(len(X_training[i,:])):
if not (X_training[i,iiii] == temp[iiii]): # this part is to test the insert and getchrom function.
raise
genscript(Substrate,polygons,centroid,name + '.vbs',tabname,name + '.hfss',poly_list,poly_list_type)
f = open(name + '.txt','w')
f.write(hp.tree2str(tree))
f.close()
saved_tree[i] = tree
del tree
del Substrate
del polygons
del centroid
#raise ValueError("Finished testing")
#######
# running all of the scripts.
nameDir = global_name + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) +'_d_' # file name direction of the vbs file.
hrun.RunPopScript(lowinit.number_direction,nameDir,0,len(init.PCnames),state)
# getting the fitness of each
for i in range(lowinit.number_direction):
spec_nameDir_tab = global_tabname + str(state.gen) + '_pop_' + str(state.population_num) + '_step_k' + str(k) + '_d_' + str(i) + '.tab'
[m,n,p,q] = hrun.assignFitness(spec_nameDir_tab,state.gen)
print('i ',i, '___',m)
[y[i],return_loss[i]] = [m,n]
if m < inGP.desired_fitness:
shutil.copy2(global_name + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) +'_d_' + str(i) + '.vbs', ant.resultsDir)
shutil.copy2(spec_nameDir_tab, ant.resultsDir)
shutil.copy2(global_name + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) +'_d_' + str(i) + '.txt', ant.resultsDir)
#hp.drawtree(pop[i].tree,ant.resultsDir + '_gen_' + str(gen) + '_pop_' + str(i))
f = open(ant.resultsDir + 'lowlevel_gen_' + str(state.gen)+ '_pop_' + str(state.population_num) + '_step_k' + str(k) +'_d_' + str(i) + '.txt','w')
#f.write(hp.tree2str(saved_tree[i]))
#f.write('#')
f.write('fitness: ' + str(m))
f.write('#')
if p:
f.write("_____exist best fitness")
if q:
f.write("___ that is better than overcome_desired")
#f.write('time: ' + str((time.time() - first_time)/60))
f.close()
###
fitness_k = min(y)
index = y.index(fitness_k)
if fitness_k < fitness:
for i in range(num):
current_chromosome[i] = X_training[index][i]
fitness = fitness_k
save_fitness_state.append(fitness)
else:
anpha = anpha*lowinit.shrink # shrink the anpha maybe because the stepsize is a little big.
save_fitness_state.append(fitness)
# np.savetxt('C:\\Opt_files\\lowlevel\\y.txt',y,delimiter = ',')
# np.savetxt('C:\\Opt_files\\lowlevel\\X.txt',X_training,delimiter = ',')
ind.fitness = fitness
ind.tree = hp.insert_chrom(ind.tree,current_chromosome,subtree_chrom, IDlist)
state.lowlevel = False
f = open(lowinit.tmpDir + 'fitness_'+str(state.gen)+'_pop_'+str(pop_num)+'.txt','w')
f.write(str(save_fitness_state))
f.close()
#del saved_tree
return ind