def calculate(times): population = Species_origin(Constant.population_size) next_species_result = population max_result_list = [] max_powerselection_list = [] for i in range(times): translation_result = translation( next_species_result) # translation result 也是二维数组 output_max_result = test_output_max(translation_result) max_result = output_max_result[0] maxresult_number = output_max_result[1] max_result_list.append(max_result) max_temporary = translation_result[maxresult_number] max_powerselection_list.append(max_temporary) print("time:", i, "maxresult:", max_result) newpopulation_result = newpopulation(next_species_result) fitness_result = fitness(newpopulation_result) selection_result = selection(fitness_result) next_species_result = nextspecies(newpopulation_result, selection_result) print("max_result_list:", max_result_list) perfect = max(max_result_list) print("the most max:", perfect) j = 0 while max_result_list[j] != perfect: j = j + 1 print("the most max temporary selection:", max_powerselection_list[j])
def fitness(new_population): # newpopulation是一个二维数组, 待选择的种群,包括初始种群,遗传,变异 fitness1 = [] judge_result = [] temporary = translation(new_population) # temporary是解码后的二维数组 for i in range(len(new_population)): objective_function_result = test_objective_function(temporary[i]) fitness1.append(objective_function_result) # fitness1 存储了10个染色体的函数值 print("judge_result:", judge_result) return fitness1
def calculate(times, mover, number, mode): ''' if mover == 0: # 燃气轮机 population = Species_origin(Constant.population_size) else: # 内燃机 population = SpeciesOriginICE(Constant.population_size).population ''' population = [[21, 5, 15, 35, 63, 0, 0, 0], [16, 9, 16, 69, 40, 0, 0, 0], [30, 18, 17, 77, 81, 0, 0, 0], [29, 26, 18, 32, 87, 0, 0, 0], [15, 11, 13, 80, 25, 0, 0, 0], [16, 2, 15, 80, 44, 0, 0, 0], [28, 22, 19, 56, 56, 0, 0, 0], [16, 5, 16, 60, 31, 0, 0, 0], [22, 4, 18, 79, 55, 0, 0, 0], [29, 15, 25, 45, 78, 0, 0, 0]] next_species_result = population print("the original", next_species_result) max_result_list = [] max_powerselection_list = [] for i in range(times): translation_result = translation( next_species_result) # translation result 也是二维数组 if mover == 0: output_max_result = output_max(translation_result, mode) max_result = output_max_result[0] maxresult_number = output_max_result[1] else: output_max_result = OutputMaxICE(translation_result, number, mode) max_result = output_max_result.max_profit_result maxresult_number = output_max_result.array_number max_result_list.append(max_result) max_temporary = translation_result[maxresult_number] max_powerselection_list.append(max_temporary) print("time:", i, "maxresult:", max_result) print("") newpopulation_result = newpopulation(next_species_result, mover) if mover == 0: fitness_result = fitness(newpopulation_result, mode) else: fitness_result = FitnessICE(newpopulation_result, number, mode).fitness1 selection_result = selection(fitness_result) next_species_result = nextspecies(newpopulation_result, selection_result) print("max_result_list:", max_result_list) perfect = max(max_result_list) print("the most max:", perfect) perfect_number = 0 while max_result_list[perfect_number] != perfect: perfect_number = perfect_number + 1 print("the most max temporary selection:", max_powerselection_list[perfect_number]) economy_index = EconomyIndex(max_powerselection_list[perfect_number], mover, number, mode) print("IRR:", economy_index.IRR) print("PaybackPeriod:", economy_index.PaybackPeriod) print("ReturnOnCapital:", economy_index.ReturnOnCapital) # 将数组 min_result_list (遗传算法每次最优结果)的值写入excel中 return 0
def calculate(times, mover, number, season, mode): if mover == 0: # 燃气轮机 population = Species_origin(Constant.population_size) else: # 内燃机 population = SpeciesOriginICE(Constant.population_size).population next_species_result = population print("the original", next_species_result) max_result_list = [] max_powerselection_list = [] for i in range(times): translation_result = translation( next_species_result) # translation result 也是二维数组 if mover == 0: output_max_result = output_max(translation_result, mode) max_result = output_max_result[0] maxresult_number = output_max_result[1] else: output_max_result = OutputMaxICE(translation_result, number, season, mode) max_result = output_max_result.max_profit_result maxresult_number = output_max_result.array_number max_result_list.append(max_result) max_temporary = translation_result[maxresult_number] max_powerselection_list.append(max_temporary) print("time:", i, "maxresult:", max_result) print("") newpopulation_result = newpopulation(next_species_result, mover) if mover == 0: fitness_result = fitness(newpopulation_result, mode) else: fitness_result = FitnessICE(newpopulation_result, number, season, mode).fitness1 selection_result = selection(fitness_result) next_species_result = nextspecies(newpopulation_result, selection_result) print("max_result_list:", max_result_list) perfect = max(max_result_list) print("the most max:", perfect) perfect_number = 0 while max_result_list[perfect_number] != perfect: perfect_number = perfect_number + 1 print("the most max temporary selection:", max_powerselection_list[perfect_number]) economy_index = EconomyIndex(max_powerselection_list[perfect_number], mover, number, season, mode) print("IRR:", economy_index.IRR) print("PaybackPeriod:", economy_index.PaybackPeriod) print("ReturnOnCapital:", economy_index.ReturnOnCapital) # 将数组 min_result_list (遗传算法每次最优结果)的值写入excel中 return 0
def fitness(newpopulation, mode): # newpopulation是一个二维数组, 待选择的种群,包括初始种群,遗传,变异 fitness1 = [] judge_result = [] temporary = translation(newpopulation) # temporary是解码后的二维数组 for i in range(len(newpopulation)): objective_function_result = objective_function(temporary[i], mode) if objective_function_result > 0: evaluation = 1 / objective_function_result judge_result.append(1) else: evaluation = 0 # 适应函数选择倒数,此时求倒数的最大值即可,不符合限制条件的eval为0(惩罚函数) judge_result.append(0) fitness1.append(evaluation) # fitness1 存储了10个染色体的函数值 print("judge_result:", judge_result) return fitness1
def __init__(self, newpopulation, number, mode): # newpopulation未经翻译 self.fitness1 = [] judge_result = [] temporary = translation(newpopulation) for i in range(len(newpopulation)): total_cost_result = Objective(temporary[i], number, mode) if total_cost_result.judge == 1: cost_result = total_cost_result.cost evaluation = 1 / cost_result judge_result.append(1) else: evaluation = 1 / total_cost_result.cost # 不符合限制条件的eval为0(惩罚函数) judge_result.append(0) self.fitness1.append(evaluation) # fitness1 存储了10个染色体的函数值 print("judge_result:", judge_result)
def __init__(self, newpopulation, number, season, mode): # newpopulation未经翻译 self.fitness1 = [] judge_result = [] temporary = translation(newpopulation) for i in range(len(newpopulation)): total_cost_result = TotalCostICE(temporary[i], number, season, mode) if total_cost_result.profit > 0: daily_cost = total_cost_result.daily_cost evaluation = 1 / daily_cost judge_result.append(1) else: evaluation = 0 # 不符合限制条件的eval为0(惩罚函数) judge_result.append(0) self.fitness1.append(evaluation) # fitness1 存储了10个染色体的函数值 print("judge_result:", judge_result)
def __init__(self, new_population, number, season, mode, way_pareto): translation_result = translation(new_population) if way_pareto == 0: pareto_sorting = ParetoSorting(new_population, number, season, mode) else: pareto_sorting = ReverseParetoSorting(new_population, number, season, mode) hierarchy_list = pareto_sorting.pareto_sorting_result print("hierarchy list:", hierarchy_list) self.chosen = [] # 被选中的编号 chosen_quantity = 0 i = 0 # 先把级别高的帕累托层级选入下一代,直到超出population_size while chosen_quantity + len( hierarchy_list[i]) <= Constant.population_size: chosen_quantity = chosen_quantity + len(hierarchy_list[i]) for j in range(len(hierarchy_list[i])): self.chosen.append(hierarchy_list[i][j]) i = i + 1 print("hierarchy入选部分:", self.chosen) # 计算下一代剩余位置,将此层的帕累托解写出,translation_result结果也写出 position_left = Constant.population_size - chosen_quantity if position_left != 0: translation_result_to_be_queued = [] for m in range(len(hierarchy_list[i])): translation_result_to_be_queued.append( translation_result[hierarchy_list[i][m]]) # 将此层的translation_result代入拥挤度计算中,排序 final_choosing_queue为排序结果 congestion_queue = QueuingForCertainHierarchyTranslationResult( translation_result_to_be_queued, number, season, mode) final_choosing_queue = congestion_queue.final_choosing_queue # 将final_choosing_queue的排序结果代入要排序的hierarchy_list[i]中,按position_left结果,选hierarchy_list[i]中标号 # (即为父代+遗传变异后的种群中,最终被选入下一代的染色体的标号) for m in range(position_left): serial_number = final_choosing_queue[m] self.chosen.append(hierarchy_list[i][serial_number]) print("hierarchy+congestion选择:", self.chosen) if len(hierarchy_list[0]) <= 10: self.length_of_best = len(hierarchy_list[0]) else: self.length_of_best = 10
def __init__(self, next_species_result, number, mode): translation_result = translation(next_species_result) self.objectives_result = ResultsOfObjectives(translation_result, number, mode) self.domination_result = Domination( self.objectives_result.cost_objective, self.objectives_result.emission_objective, self.objectives_result.pei_objective) pareto_sorting = Sorting(self.domination_result.be_domin) self.pareto_sorting_result = pareto_sorting.sorting self.pareto_sorting_best = [] self.pareto_sorting_best_cost_result = [] self.pareto_sorting_best_emission_result = [] self.pareto_sorting_best_pei_result = [] for j in range(len(self.pareto_sorting_result[0])): the_number = self.pareto_sorting_result[0][j] self.pareto_sorting_best.append( next_species_result[the_number]) # 帕累托最优解 self.pareto_sorting_best_cost_result.append( self.objectives_result.cost_objective[the_number]) self.pareto_sorting_best_emission_result.append( self.objectives_result.emission_objective[the_number]) self.pareto_sorting_best_pei_result.append( self.objectives_result.pei_objective[the_number])