def main(): for trial in range(cf.get_trial()): np.random.seed(trial) results_list = [] # fitness list de_list = [] """Generate Initial Population""" for p in range(cf.get_population_size()): de_list.append(id.Individual()) """Sort Array""" de_list = sorted(de_list, key=lambda ID: ID.get_fitness()) """Find Initial Best""" BestPosition = de_list[0].get_position() # Best Solution BestFitness = fn.calculation(BestPosition, 0) """↓↓↓Main Loop↓↓↓""" for iteration in range(cf.get_iteration()): """list copy""" candidate_de_list = copy.deepcopy(de_list) """Generate New Solutions""" for i in range(len(de_list)): candidate = copy.deepcopy(de_list[i]) """select three points (a, b, c)""" a = np.random.randint(0, cf.get_population_size()) while (a == i): a = np.random.randint(0, cf.get_population_size()) b = np.random.randint(0, cf.get_population_size()) while (b == i or a == b): b = np.random.randint(0, cf.get_population_size()) c = np.random.randint(0, cf.get_population_size()) while (c == i or c == a or c == b): c = np.random.randint(0, cf.get_population_size()) """Select Random Index (R)""" R = np.random.randint(0, cf.get_dimension()) candidate.generate(a=de_list[a], b=de_list[b], c=de_list[c], R=R) candidate.set_fitness( fn.calculation(candidate.get_position(), iteration)) if candidate.get_fitness() < de_list[i].get_fitness(): candidate_de_list[i] = copy.deepcopy(candidate) """Sort Array""" de_list = sorted(candidate_de_list, key=lambda ID: ID.get_fitness()) """Rank and Find the Current Best""" if de_list[0].get_fitness() < BestFitness: BestPosition = de_list[0].get_position() BestFitness = fn.calculation(BestPosition, iteration) sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.4f" % (trial, iteration, BestFitness)) results_list.append(str(BestFitness)) results_writer.writerow(results_list)
def main(): result_list = np.zeros(cf.get_iteration()) for trial in range(cf.get_trial()): # np.random.seed(trial) np.random.seed() cs_list = [] """Generate Initial Population""" for p in range(cf.get_population_size()): cs_list.append(id.Individual()) """Sort List""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Find Initial Best""" BestPosition = cs_list[0].get_position() BestFitness = fn.calculation(cs_list[0].get_position(), 0) """↓↓↓Main Loop↓↓↓""" for iteration in range(cf.get_iteration()): """Generate New Solutions""" for i in range(len(cs_list)): cs_list[i].get_cuckoo_dynamic_adaptive(iteration) cs_list[i].set_fitness( fn.calculation(cs_list[i].get_position(), iteration)) """random choice (say j)""" j = np.random.randint(low=0, high=cf.get_population_size()) while j == i: #random id[say j] ≠ i j = np.random.randint(0, cf.get_population_size()) # for minimize problem if (cs_list[i].get_fitness() < cs_list[j].get_fitness()): cs_list[j].set_position(cs_list[i].get_position()) cs_list[j].set_fitness(cs_list[i].get_fitness()) """Sort (to Keep Best)""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Abandon Solutions (exclude the best)""" for a in range(1, len(cs_list)): r = np.random.rand() if (r < cf.get_Pa()): cs_list[a].abandon_dynamic(iteration) cs_list[a].set_fitness( fn.calculation(cs_list[a].get_position(), iteration)) """Sort to Find the Best""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) if cs_list[0].get_fitness() < BestFitness: BestFitness = cs_list[0].get_fitness() BestPosition = cs_list[0].get_position() sys.stdout.write( "\r Trial:%3d , Iteration:%7d, BestFitness:%.10f" % (trial, iteration, BestFitness)) result_list[iteration] += BestFitness """ export avg result""" result_list /= cf.get_trial() for iteration in range(cf.get_iteration()): result.write('{} {}\n'.format(iteration, result_list[iteration]))
def main(): for trial in range(cf.get_trial()): np.random.seed(trial) results_list = [] # fitness list cs_list = [] """Generate Initial Population""" for p in range(cf.get_population_size()): cs_list.append(id.Individual()) """Sort List""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Find Initial Best""" BestPosition = cs_list[0].get_position() BestFitness = fn.calculation(cs_list[0].get_position(), 0) """↓↓↓Main Loop↓↓↓""" for iteration in range(cf.get_iteration()): """Generate New Solutions""" for i in range(len(cs_list)): cs_list[i].get_cuckoo() cs_list[i].set_fitness( fn.calculation(cs_list[i].get_position(), iteration)) """random choice (say j)""" j = np.random.randint(low=0, high=cf.get_population_size()) while j == i: # random id[say j] ≠ i j = np.random.randint(0, cf.get_population_size()) # for minimize problem if cs_list[i].get_fitness() < cs_list[j].get_fitness(): cs_list[j].set_position(cs_list[i].get_position()) cs_list[j].set_fitness(cs_list[i].get_fitness()) """Sort (to Keep Best)""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Abandon Solutions (exclude the best)""" for a in range(1, len(cs_list)): r = np.random.rand() if r < cf.get_Pa(): cs_list[a].abandon() cs_list[a].set_fitness( fn.calculation(cs_list[a].get_position(), iteration)) """Sort to Find the Best""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) if cs_list[0].get_fitness() < BestFitness: BestFitness = cs_list[0].get_fitness() BestPosition = cs_list[0].get_position() sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.4f" % (trial, iteration, BestFitness)) results_list.append(str(BestFitness)) results_writer.writerow(results_list)
def reset(self, t): self.__position = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() #位置座標 self.__velocity = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() #速度ベクトル self.__fitness = fn.calculation(self.__position, 0) #評価値
def __init__(self): self.__position = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() self.__velocity = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() self.__fitness = fn.calculation(self.__position, 0) # iteration = 0 self.__p_best_position = self.__position self.__p_best_fitness = self.__fitness
def main(): for trial in range(cf.get_trial()): np.random.seed(trial) results_list = [] # fitness list pso_list = [] """Generate Initial Population""" for p in range(cf.get_population_size()): pso_list.append(id.Individual()) """Sort Array""" pso_list = sorted(pso_list, key=lambda ID: ID.get_fitness()) """Find Initial Best""" BestPosition = pso_list[0].get_position() # Best Solution BestFitness = fn.calculation(BestPosition, 0) """↓↓↓Main Loop↓↓↓""" for iteration in range(cf.get_iteration()): """Generate New Solutions""" for i in range(len(pso_list)): """Update Position""" pso_list[i].update_position() """Calculate Fitness""" pso_list[i].set_fitness( fn.calculation(pso_list[i].get_position(), t=iteration)) """if f_x < f_(p_best) # for minimize optimization""" if (pso_list[i].get_fitness() < pso_list[i].get_p_best_fitness()): pso_list[i].set_p_best_fitness(pso_list[i].get_fitness()) pso_list[i].set_p_best_position(pso_list[i].get_position()) """if f_x < f_(g_best) # for minimize optimization""" if (pso_list[i].get_fitness() < BestFitness): BestFitness = pso_list[i].get_fitness() BestPosition = pso_list[i].get_position() """Reload Velocity""" pso_list[i].update_velocity(BestPosition) """Sort Array""" pso_list = sorted(pso_list, key=lambda ID: ID.get_fitness()) """Rank and Find the Current Best""" if pso_list[0].get_fitness() < BestFitness: BestPosition = pso_list[0].get_position() BestFitness = fn.calculation(BestPosition, iteration) sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.4f" % (trial, iteration, BestFitness)) results_list.append(str(BestFitness)) results_writer.writerow(results_list)
def __init__(self): #dim=次元数 self.__position = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() #位置座標 self.__velocity = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() #速度ベクトル self.__fitness = fn.calculation(self.__position, 0) #評価値 self.__p_best_fitness = self.__fitness #各個体の最良値 self.__p_best_position = self.__position
def main(): result_list = np.zeros(cf.get_iteration()) for trial in range(cf.get_trial()): np.random.seed(trial) pos_list = [] v_list = [] pbest_list = [] gbest = None """Generate Initial Population""" gbest = id.Individual() for p in range(cf.get_population_size()): pbest_list.append(id.Individual()) pos_list.append(id.Individual()) v_list.append(id.Individual()) """ Main Loop """ for iteration in range(cf.get_iteration()): """ Update Partical position """ for i in range(cf.get_population_size()): # move to new position v_list[i].get_velocity(pos_list[i], pbest_list[i], gbest) pos_list[i].get_newPos(v_list[i]) # update fitness fitness = fn.calculation(pos_list[i].get_position(), 0) pos_list[i].set_fitness(fitness) # update pbest & gbest if pos_list[i].get_fitness() < pbest_list[i].get_fitness(): pbest_list[i].set_position(pos_list[i].get_position()) pbest_list[i].set_fitness(pos_list[i].get_fitness()) if pos_list[i].get_fitness() < gbest.get_fitness(): gbest.set_position(pos_list[i].get_position()) gbest.set_fitness(pos_list[i].get_fitness()) # print([x.get_fitness() for x in pbest_list]) # print(gbest.get_fitness()) # sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.10f" % (trial , iteration, gbest.get_fitness())) result_list[iteration] += gbest.get_fitness() """ export avg result""" result_list /= cf.get_trial() for iteration in range(cf.get_iteration()): result.write('{} {}\n'.format(iteration, result_list[iteration]))
def run_cso(self, msg): err_ = list(msg.error) self.sum_err += (err_[5]) err = 0 if (self.status != 0): return self.status = 2 self.err = err if (abs(self.int_err) > 20): self.int_err = 0 else: self.int_err += err """Assuming initial best values""" FinalBestPosition = [-1 - 1 - 1] FinalBestFitness = 0.00 for trial in range(self.Trial): np.random.seed(trial) results_list = [] # fitness list cs_list = [] """Generate Initial Population""" for p in range(self.PopulationSize): cs_list.append(id.Individual()) """Sort List""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Find Initial Best""" TrialBestPosition = cs_list[0].get_position() TrialBestFitness = fn.calculation(self.err, self.int_err, self.prev_err, cs_list[0].get_position(), 0) """↓↓↓Main Loop↓↓↓""" for iteration in range(self.Iteration): """Generate New Solutions""" for i in range(len(cs_list)): cs_list[i].get_cuckoo() cs_list[i].set_fitness( fn.calculation(self.err, self.int_err, self.prev_err, cs_list[i].get_position(), iteration)) """random choice (say j)""" j = np.random.randint(low=0, high=self.PopulationSize) while j == i: #random id[say j] ≠ i j = np.random.randint(0, self.PopulationSize) if (cs_list[i].get_fitness() > cs_list[j].get_fitness()): cs_list[j].set_position(cs_list[i].get_position()) cs_list[j].set_fitness(cs_list[i].get_fitness()) """Sort (to Keep Best)""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) """Abandon Solutions (exclude the best)""" for a in range(1, len(cs_list)): r = np.random.rand() if (r < cf.get_Pa()): cs_list[a].abandon() cs_list[a].set_fitness( fn.calculation(self.err, self.int_err, self.prev_err, cs_list[a].get_position(), iteration)) """Sort to Find the Best""" cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) if cs_list[0].get_fitness() > TrialBestFitness: TrialBestFitness = cs_list[0].get_fitness() TrialBestPosition = cs_list[0].get_position() """sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.4f" % (trial , iteration, BestFitness))""" """results_list.append(str(TrialBestPosition)) results_writer.writerow(results_list)""" FinalPIDvals = np.array(TrialBestPosition) print(FinalPIDvals) p_ = float(FinalPIDvals[0]) i_ = float(FinalPIDvals[1]) d_ = float(FinalPIDvals[2]) self.pid_vals_[0] = p_ self.pid_vals_[1] = i_ self.pid_vals_[2] = d_ self.prev_err = err self.status = 1
def main(): for trial in range(cf.get_trial()): # seed = 試行回数 np.random.seed(trial) if os.path.exists("results" + os.sep + "position" + os.sep + str(trial)): pass else: os.mkdir("results" + os.sep + "position" + os.sep + str(trial)) results_list = [] # fitness list results_pso_list = [] results_cs_list = [] results_de_list = [] pso_list = [] # pso list cs_list = [] # cs list de_list = [] all_list = [] eval = 0 iteration = 0 BestFitness = 10000 improvement = [] """Generate Initial Population""" for p in range(int(args[7])): pso_list.append(ind.Individual()) for c in range(int(args[8])): cs_list.append(ind.Individual()) for d in range(int(args[9])): de_list.append(ind.Individual()) """Sort Array""" pso_list = sorted(pso_list, key=lambda ID: ID.get_fitness()) cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) de_list = sorted(de_list, key=lambda ID: ID.get_fitness()) for i in range(len(pso_list)): all_list.append(pso_list[i]) # print(pso_list[i].get_position()) for i in range(len(cs_list)): all_list.append(cs_list[i]) for i in range(len(de_list)): all_list.append(de_list[i]) """Find Initial Best""" PSO_Best = Best(pso_list[0].get_fitness(), pso_list[0].get_position()) CS_Best = Best(cs_list[0].get_fitness(), cs_list[0].get_position()) DE_Best = Best(de_list[0].get_fitness(), de_list[0].get_position()) """Find the Best""" if PSO_Best.get_best_fitness() < CS_Best.get_best_fitness(): if (PSO_Best.get_best_fitness() < DE_Best.get_best_fitness()): ALL_Best = Best(PSO_Best.get_best_fitness(), PSO_Best.get_best_position()) else: ALL_Best = Best(DE_Best.get_best_fitness(), DE_Best.get_best_position()) else: if (CS_Best.get_best_fitness() < DE_Best.get_best_fitness()): ALL_Best = Best(CS_Best.get_best_fitness(), CS_Best.get_best_position()) else: ALL_Best = Best(DE_Best.get_best_fitness(), DE_Best.get_best_position()) for iteration in range(cf.get_iteration()): # if iteration == 150: # BestFitness = 10000 # cf.set_Rnd(np.random.randint(0,5000)) # if(cf.get_function() == "ex21"): # if(iteration >= 0 and iteration < 3000): # if(iteration % 50 == 0): # cf.set_Radisu(cf.get_Radius() + 1) # 環境変化後リセット用 # # 記述 # # if(iteration == 5000 or iteration == 10000): # """Generate Initial Population""" # for p in range(cf.get_population_size()): # pso_list.append(ind.Individual()) # cs_list.append(ind.Individual()) # de_list.append(ind.Individual()) # 位置記録 pos = open( "results" + os.sep + "position" + os.sep + str(trial) + os.sep + str(iteration).zfill(6) + ".csv", "w") pos_writer = csv.writer(pos, lineterminator="\n") # # """Write Moved Position""" for i in range(len(all_list)): pos_writer.writerow(all_list[i].get_position()) # print(all_list[i].get_position()) # input() # 各群の解アップデート """>>>> PSO Update """ for i in range(len(pso_list)): """Update Position""" pso_list[i].update_position() """Calculate Fitness""" pso_list[i].set_fitness( fn.calculation(pso_list[i].get_position(), iteration)) """Update Personal Best # for minimize optimization""" if (pso_list[i].get_fitness() < fn.calculation( pso_list[i].get_p_best_position(), iteration)): pso_list[i].set_p_best_fitness(pso_list[i].get_fitness()) pso_list[i].set_p_best_position(pso_list[i].get_position()) """Update Global Best # for minimize optimization""" if (pso_list[i].get_fitness() < PSO_Best.get_best_fitness()): PSO_Best.set_best_fitness(pso_list[i].get_fitness()) PSO_Best.set_best_position(pso_list[i].get_position()) eval += 1 BestFitness, improvement = fitness_update( fn.calculation(pso_list[i].get_position(), iteration), BestFitness, improvement) """<<<< End PSO """ """>>>> CS Update """ for i in range(len(cs_list)): cs_list[i].get_cuckoo() cs_list[i].set_fitness( fn.calculation(cs_list[i].get_position(), iteration)) """random choice (say j)""" j = np.random.randint(low=0, high=len(cs_list)) # [say j] while (i == j): j = np.random.randint(low=0, high=len(cs_list)) # [say j] # for minimize problem if (cs_list[i].get_fitness() < fn.calculation( cs_list[j].get_position(), iteration)): cs_list[j].set_position(cs_list[i].get_position()) cs_list[j].set_fitness(cs_list[i].get_fitness()) eval += 1 BestFitness, improvement = fitness_update( fn.calculation(cs_list[j].get_position(), iteration), BestFitness, improvement) """Find the Best""" best_id = 0 best_fitness = cs_list[0].get_fitness() for b in range(len(cs_list)): F = cs_list[b].get_fitness() if (F < best_fitness): best_id = b best_fitness = F """Abandon Solutions (exclude the best)""" for a in range(0, len(cs_list)): if (a != best_id): # To Keep the Best r = np.random.rand() if (r < cf.get_Pa()): cs_list[a].abandon() cs_list[a].set_fitness( fn.calculation(cs_list[a].get_position(), iteration)) """<<<< End CS """ """>>>> DE Update""" """Generate New Solutions""" for i in range(len(de_list)): candidate = copy.deepcopy(de_list[i]) """select three points (a, b, c)""" a = np.random.randint(0, len(de_list)) while (a == i): a = np.random.randint(0, len(de_list)) b = np.random.randint(0, len(de_list)) while (b == i or a == b): b = np.random.randint(0, len(de_list)) c = np.random.randint(0, len(de_list)) while (c == i or c == a or c == b): c = np.random.randint(0, len(de_list)) """Select Random Index (R)""" R = np.random.randint(0, cf.get_dimension()) candidate.generate(a=de_list[a], b=de_list[b], c=de_list[c], R=R) """Calculate Solution""" candidate.set_fitness( fn.calculation(candidate.get_position(), iteration)) de_list[i].set_fitness( fn.calculation(de_list[i].get_position(), iteration)) if candidate.get_fitness() < de_list[i].get_fitness(): de_list[i] = copy.deepcopy(candidate) eval += 1 BestFitness, improvement = fitness_update( fn.calculation(de_list[i].get_position(), iteration), BestFitness, improvement) de_list = copy.deepcopy(de_list) """<<<< End DE """ """Sort Array""" all_list = [] pso_list = sorted(pso_list, key=lambda ID: ID.get_fitness()) cs_list = sorted(cs_list, key=lambda ID: ID.get_fitness()) de_list = sorted(de_list, key=lambda ID: ID.get_fitness()) for i in range(len(pso_list)): all_list.append(pso_list[i]) for i in range(len(cs_list)): all_list.append(cs_list[i]) for i in range(len(de_list)): all_list.append(de_list[i]) PSO_Best.set_best_fitness( fn.calculation(PSO_Best.get_best_position(), iteration)) CS_Best.set_best_fitness( fn.calculation(CS_Best.get_best_position(), iteration)) DE_Best.set_best_fitness( fn.calculation(DE_Best.get_best_position(), iteration)) ALL_Best.set_best_fitness( fn.calculation(ALL_Best.get_best_position(), iteration)) # print("rnd",cf.get_Rnd()) # print(fn.multimoving(PSO_Best.get_best_position(), cf.get_Rnd())) # print("pso", PSO_Best.get_best_fitness()) # print("cs", CS_Best.get_best_fitness()) # print("de", DE_Best.get_best_fitness()) cs_list[0].set_fitness( fn.calculation(cs_list[0].get_position(), iteration)) """Update Best""" UpdateBest(PSO_Best, pso_list[0]) UpdateBest(CS_Best, cs_list[0]) UpdateBest(DE_Best, de_list[0]) UpdateAllBest(ALL_Best, PSO_Best, CS_Best, DE_Best) # 一番いいものを挿入 # if(iteration % 100 == 0): pso_list[-1].set_position(ALL_Best.get_best_position()) cs_list[-1].set_position(ALL_Best.get_best_position()) de_list[-1].set_position(ALL_Best.get_best_position()) """>>>> Update PSO """ for i in range(len(pso_list)): """Reload Velocity""" pso_list[i].update_velocity(PSO_Best.get_best_position()) """<<<< End PSO """ # print("all_pos",ALL_Best.get_best_position()) # print(fn.multimoving(ALL_Best.get_best_position(), cf.get_Rnd())) # print("all_fit",ALL_Best.get_best_fitness()) ALL_Best.set_best_fitness( fn.calculation(ALL_Best.get_best_position(), iteration)) sys.stdout.write("\r Trial:%3d , Iteration:%7d, BestFitness:%.6f" % (trial, iteration, ALL_Best.get_best_fitness())) # print() # input() results_list.append(str(ALL_Best.get_best_fitness())) # results_pso_list.append(str(PSO_Best.get_best_fitness())) # results_cs_list.append(str(CS_Best.get_best_fitness())) # results_de_list.append(str(DE_Best.get_best_fitness())) results_writer.writerow(results_list) # pso_results_writer.writerow(results_pso_list) # cs_results_writer.writerow(results_cs_list) # de_results_writer.writerow(results_de_list) all = pd.DataFrame(improvement) all.to_csv("./results/fitness_by_evaluation/trial" + str(trial) + ".csv") """File Close""" FileClose()
def __init__(self): self.__position = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() self.__fitness = fn.calculation(0, 0, 0, self.__position, 0) # iteration = 0
def __init__(self): self.__position = np.random.rand(cf.get_dimension()) * ( cf.get_max_domain() - cf.get_min_domain()) + cf.get_min_domain() self.__fitness = fn.calculation(self.__position, 0) # iteration = 0 self.__intensity = 1 / (1 + self.__fitness) # for minimize problem