name = global_name + str(gen) + '_pop_' + str( i) # name of directory would # be used to save .vbs and .hfss file. tabname = global_tabname + str(gen) + '_pop_' + str( i) # name of directory # would be used to save .tab file. genscript(Substrate, polygons, centroid, name + '.vbs', tabname, name + '.hfss', poly_list, poly_list_type) print('genscript is done.') ## Run all scripts to get .tab file. nameDir = inAnT.tmpDir + inAnT.Antenna_Name + '_gen_' + str( gen) + '_pop_' # file name direction of the vbs file. if gen == 1: # This case is initial part. hpr.RunPopScript(len(pop), nameDir, 0, 1, state) else: # other. hpr.RunPopScript(len(pop), nameDir, numRepro, 1, state) ################################################ ### evaluate and assign the fitness of each model. ############################################################################################3 nameDir_tab = inAnT.tmpTab + inAnT.Antenna_Name + '_gen_' + str( gen) + '_pop_' # file name directory to tab file. if gen == 1: for i in range(len(pop)): spec_nameDir_tab = nameDir_tab + str(i) + '.tab' [pop[i].fitness, pop[i].ReturnLoss, p, q] = hpr.assignFitness( spec_nameDir_tab,
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) ######################################################################################################################## ######################################################################################################################## 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] = current_chromosome[ii] + anpha * d[i][ii] if X_training[i][ii] > 1: X_training[i][ii] = 1 if X_training[i][ii] < -1: X_training[i][ii] == -1 # generate all of the scripts. print('generating ', lowinit.number_direction, ' scripts in low lelvel optimizer in generation ', state.gen) ''' # before run the next generation, all of the tab file need be deleted. filelist = [ f for f in os.listdir(lowinit.tmpTab) if f.endswith(".tab") ] for f in filelist: os.remove(os.path.join(lowinit.tmpTab, f)) # be fore run the next gen, all of the hfss file in temp need be deleted. filelist = [ f for f in os.listdir(lowinit.tmpDir) if (f.endswith(".hfss") or f.endswith(".vbs") or f.endswith(".txt"))] for f in filelist: # delete all before files. os.remove(os.path.join(lowinit.tmpDir, f))''' ######################################################## ############## insert chromosome. # create temporary population to save current new individual. #pop = [] #for i in range(lowinit.number_direction): # tree = hp.insert_chrom(ind.tree,X_training[i,:],subtree_chrom, IDlist) #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 else: anpha = anpha * lowinit.shrink # shrink the anpha maybe because the stepsize is a little big. # 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 #del saved_tree return ind
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. state.lowlevel = True global_name = lowinit.tmpDir + ant.Antenna_Name + '_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 = np.zeros(lowinit.number_sample+1) y[0] = ind.fitness # save original chromosome fitness. return_loss = [] return_loss.append(ind.ReturnLoss) # save original return loss variable. for i in range(lowinit.number_sample): return_loss.append([]) X_training = np.zeros((lowinit.number_sample+1,num)) # X_train data for i in range(num): # save original chromosome. X_training[0,i] = chromosome[i] for i in range(1,lowinit.number_sample+1): for ii in range(num): X_training[i][ii] = round(random.uniform(-1,1),4) print(X_training) # generate all of the scripts. print('generating ',lowinit.number_sample, ' scripts in low lelvel optimizer in generation ',state.gen) # before run the next generation, all of the tab file need be deleted. filelist = [ f for f in os.listdir(lowinit.tmpDir) if f.endswith(".tab") ] for f in filelist: os.remove(os.path.join(lowinit.tmpDir, f)) # be fore run the next gen, all of the hfss file in temp need be deleted. filelist = [ f for f in os.listdir(lowinit.tmpTab) if (f.endswith(".hfss") or f.endswith(".vbs") or f.endswith(".txt"))] for f in filelist: # delete all before files. os.remove(os.path.join(lowinit.tmpTab, f)) ######################################################## ############## insert chromosome. # create temporary population to save current new individual. #pop = [] #for i in range(1,lowinit.number_sample + 1): # tree = hp.insert_chrom(ind.tree,X_training[i,:],subtree_chrom, IDlist) for i in range(1,lowinit.number_sample + 1): tree = hp.insert_chrom(ind.tree,X_training[i,:],subtree_chrom, IDlist) #pop[i].tree.childs[1].valueofnode.plot() [Substrate,polygons,centroid] = hp.get_all_para_for_hfss(tree) # get necessary parameters for genscript function. name = global_name + str(state.gen) + '_pop_' + str(i) # name of directory would # be used to save .vbs and .hfss file. tabname = global_tabname + str(state.gen) + '_pop_' + 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]): raise genscript(Substrate,polygons,centroid,name + '.vbs',tabname,name + '.hfss') del tree del Substrate del polygons del centroid #raise ValueError("Finished testing") ####### # running all of the scripts. nameDir = global_name + str(state.gen) + '_pop_' # file name direction of the vbs file. hrun.RunPopScript(lowinit.number_sample,nameDir,1,len(init.PCnames),state) # getting the fitness of each for i in range(1,lowinit.number_sample+1): spec_nameDir_tab = global_tabname + str(state.gen) + '_pop_' + str(i) + '.tab' [m,n] = hrun.assignFitness(spec_nameDir_tab,state.gen) print('i ',i, '___',m) [y[i],return_loss[i]] = [m,n] np.savetxt('C:\\Opt_files\\lowlevel\\y.txt',y,delimiter = ',') np.savetxt('C:\\Opt_files\\lowlevel\\X.txt',X_training,delimiter = ',') state.lowlevel = False