def createWidgets(self): self.selectLabel = ttk.Label(self, text = "Select:") self.selectLabel.grid(column = 0, row = 0, padx = 10, pady = 5) self.select = Select(self, textvariable = self.selectVar) self.select.grid(column = 1, row = 0, sticky = (E, W)) self.searchLabel = ttk.Label(self, text = "Search:") self.searchLabel.grid(column = 2, row = 0, padx = 10, pady = 5) self.search = Search(self, textvariable = self.searchVar) self.search.grid(column = 3, row = 0, sticky = (E, W)) self.filestorage = FileStorage(self) self.filetree = FileTree(self) self.filetree.grid(column = 0, row = 1, rowspan = 4, sticky = (N, S, E, W), columnspan = 4) self.scrollbar = ttk.Scrollbar(self, orient = VERTICAL, command = self.filetree.yview) self.scrollbar.grid(column = 4, row = 1, rowspan = 4, sticky = (N, S, E)) self.filetree.configure(yscrollcommand = self.scrollbar.set) self.tags = Tags(self) self.tags.grid(column = 5, row = 2, sticky = (E, W), padx = 5) self.tagsLab = ttk.Label(text = "Tags") self.tagsLab.grid(column = 5, row = 1, padx = 5, pady = 2) self.notes = Notes(self) self.notes.grid(column = 5, row = 4, sticky = (N, S, E, W), padx = 5) self.notesLab = ttk.Label(text = "Notes") self.notesLab.grid(column = 5, row = 3, padx = 5, pady = 2) self.scrollNotes = ttk.Scrollbar(self, orient = VERTICAL, command = self.notes.yview) self.scrollNotes.grid(column = 6, row = 4, sticky = (N, S, W)) self.notes.configure(yscrollcommand = self.scrollNotes.set) self.buttons = Buttons(self) self.buttons.grid(row = 5, column = 0, columnspan = 5, pady = 5, sticky = (E, W)) self.statusBar = StatusBar(self) self.statusBar.grid(row = 6, column = 0, columnspan = 5, padx = 5, pady = 5, sticky = (E, W))
def cul_single(self, person): other1 = person.columns[1] target_ron = PredModel.predFunc(person.drop(other1, axis=1), 1) true_target = person.iloc[0, 0] print('本样本真实ron损失:', true_target) print('预测模型认为的ron损失', target_ron) print( '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n' ) # 初始化随机种群 buildings = [] value_record = [] s_record = [] best_param = [i * 0 for i in range(34)] percent_record = [] # 20为种群内个体数 for i in range(32): p = random.uniform(0.01, 0.99) temp = [] for j in range(19): base_value = person.iloc[0, 2 + j] right_value = define.limit[j][1] - person.iloc[0, 2 + j] left_value = person.iloc[0, 2 + j] - define.limit[j][0] if random.randint(0, 1): value = base_value + p * right_value else: value = base_value - p * left_value # value = random.uniform(define.limit[j][0], define.limit[j][1]) temp.append(value) for j in range(19, 30): temp.append(person.iloc[0, j + 2]) buildings.append(temp) # 进化次数250次,建议不少于200次 a = time.time() for i in range(1000): # 计算每个个体的适应度函数的值 value, s_value = Fit(buildings, person).fitness() temp_min = min(value) value_record.append(temp_min) index = value.index(temp_min) temp_s = s_value[index] s_record.append(temp_s) reduce_percent = (target_ron - temp_min) / target_ron percent_record.append(reduce_percent) if reduce_percent > 0.301 and temp_s < 5: # if temp_min <= min(value_record) and temp_s < 5: # best_param = [buildings[index], temp_min, reduce_percent, temp_s, i] best_param = buildings[index] + [ temp_min, reduce_percent, temp_s, i ] break print('本轮最小RON损失:', temp_min) print('RON损失降幅:{:.1f}%'.format(reduce_percent * 100)) print('对应硫含量:', temp_s) print('================round', i, 'finished==================\n') # t用于存储最优个体的适应度,用以绘制最后的进化图像 # record用于存储每次进化最优的个体,方便最后打印最优基因型 # Select函数决定本代哪些个体可以活到下一代 buildings_new = Select(buildings, value, s_value).selection() # 活下来的个体以0.7的概率进行交配产生子代,交配概率动态降低 children = Cross(buildings_new, self.cross_p * max(1 - reduce_percent / 0.4, 0.1)).crossover() # 子代以0.3的基准概率基因突变,突变概率动态降低 buildings = Mutate( children, self.mutate_p * max(1 - reduce_percent / 0.4, 0.1)).mutation() b = time.time() print(b - a) # print('\n最佳优化参数:', best_param[0], '\nRON损失:', best_param[1], '\n损失降幅:', best_param[2], # '\n产品硫含量:', best_param[3], '\n所在轮次:', best_param[4]) print(best_param) plt.title('RON损失值') plt.grid(ls='--') plt.xlabel("进化轮次", fontsize=14) plt.ylabel("RON损失", fontsize=14) plt.plot(value_record) # plt.show() plt.title('产品硫含量') plt.grid(ls='--') plt.xlabel("进化轮次", fontsize=14) plt.ylabel("硫含量", fontsize=14) plt.plot(s_record) # plt.show() plt.title('RON损失降低比率') plt.grid(ls='--') plt.xlabel("进化轮次", fontsize=14) plt.ylabel("降损比例", fontsize=14) plt.plot(percent_record) # plt.show() resultframe = pd.DataFrame([best_param]) resultframe.to_csv('./result/opt_result.csv', mode='a', index=False, header=None)
prefer = input('请输入设计的传统性【0/1】:0为现代性强,1为传统性强: ') t = [] record = [] # 进化次数250次,建议不少于200次 for i in range(250): # 计算每个个体的适应度函数的值 value = Fit(buildings, rain, land, money, location, prefer).fitness() temp_max = max(value) # t用于存储最优个体的适应度,用以绘制最后的进化图像 t.append(temp_max) index = value.index(temp_max) # record用于存储每次进化最优的个体,方便最后打印最优基因型 record.append(buildings[index]) # Select函数决定本代哪些个体可以活到下一代 buildings_new = Select(buildings, value).selection() # 活下来的个体以0.7的概率进行交配产生子代(交叉概率0.7) children = Cross(buildings_new, 0.7).crossover() # 子代以0.02的概率基因突变 buildings = Mutate(children, 0.02).mutation() # 提取最优解的基因型 value_max = Fit(record, rain, land, money, location, prefer).fitness() temp_max = max(value_max) index = value_max.index(temp_max) # 把最优解的基因型从数字编码翻译成题设给的字母,打印输出 result = translate(record[index]) print('最优解的基因型为:', result) # 输出收敛过程及最大值的函数图像 plt.plot(t)
class GUI(Tk): "represents GUI" def __init__(self): super().__init__() self.option_add("*tearOff", FALSE) self.initialized = False self.title("Papyer") x, y = 1500, 500 self.minsize(x, y) placeWindow(self, x, y) self.options = Options(self) self["menu"] = TopMenu(self) self.protocol("WM_DELETE_WINDOW", self.closeFun) self.base = os.getcwd() self.selectVar = StringVar() self.searchVar = StringVar() self.createWidgets() self.columnconfigure(1, weight = 1) self.columnconfigure(3, weight = 1) self.columnconfigure(5, weight = 1) self.rowconfigure(4, weight = 1) self.bind("<Control-d>", lambda e: self.filetree.keepDuplicates()) self.bind("<Control-a>", lambda e: self.filetree.selectAll()) self.mainloop() def refresh(self): # do in a smarter way - check changes in the files self.filestorage.save() self.filetree.saveSettings() selected = self.filetree.selection() self.createWidgets() self.filetree.selection_set(selected) def createWidgets(self): self.selectLabel = ttk.Label(self, text = "Select:") self.selectLabel.grid(column = 0, row = 0, padx = 10, pady = 5) self.select = Select(self, textvariable = self.selectVar) self.select.grid(column = 1, row = 0, sticky = (E, W)) self.searchLabel = ttk.Label(self, text = "Search:") self.searchLabel.grid(column = 2, row = 0, padx = 10, pady = 5) self.search = Search(self, textvariable = self.searchVar) self.search.grid(column = 3, row = 0, sticky = (E, W)) self.filestorage = FileStorage(self) self.filetree = FileTree(self) self.filetree.grid(column = 0, row = 1, rowspan = 4, sticky = (N, S, E, W), columnspan = 4) self.scrollbar = ttk.Scrollbar(self, orient = VERTICAL, command = self.filetree.yview) self.scrollbar.grid(column = 4, row = 1, rowspan = 4, sticky = (N, S, E)) self.filetree.configure(yscrollcommand = self.scrollbar.set) self.tags = Tags(self) self.tags.grid(column = 5, row = 2, sticky = (E, W), padx = 5) self.tagsLab = ttk.Label(text = "Tags") self.tagsLab.grid(column = 5, row = 1, padx = 5, pady = 2) self.notes = Notes(self) self.notes.grid(column = 5, row = 4, sticky = (N, S, E, W), padx = 5) self.notesLab = ttk.Label(text = "Notes") self.notesLab.grid(column = 5, row = 3, padx = 5, pady = 2) self.scrollNotes = ttk.Scrollbar(self, orient = VERTICAL, command = self.notes.yview) self.scrollNotes.grid(column = 6, row = 4, sticky = (N, S, W)) self.notes.configure(yscrollcommand = self.scrollNotes.set) self.buttons = Buttons(self) self.buttons.grid(row = 5, column = 0, columnspan = 5, pady = 5, sticky = (E, W)) self.statusBar = StatusBar(self) self.statusBar.grid(row = 6, column = 0, columnspan = 5, padx = 5, pady = 5, sticky = (E, W)) def closeFun(self): "ask for saving files on exit" self.filetree.saveSettings() self.filestorage.save() self.options.save() self.destroy()
bestsIndividuos = [[], []] bestsIndividuosCrossing = [[], []] individuoMutation = [[], []] fits = [] fit1 = [] fit2 = [] porcent = 3 meta = 28 quantGenes = 36 quantIndividuos = 100 geracao = 300 contGeracao = 0 melhorFit1 = 0 melhorFit2 = 0 individuo = Population() selecao = Select() def convert_bin(listaBits): copylistaBits = listaBits.copy() copylistaBits.reverse() new_list = [] count = 0 decimal = 0 newlist = [] base = 0 for bit in copylistaBits: base = bit * 2 if base != 0: decimal = decimal + base**count