def breed(self, genome1, genome2, n_child): datas = [] for n in range(n_child): data = genome1 for i in range(len(genome2.network_weights['weights'])): if random.random() <= 0.5: data.network_weights['weights'][i] = genome2.network_weights['weights'][i] for i in range(len(data.network_weights['weights'])): if random.random() <= mutation_rate: data.network_weights['weights'][i] += random.random() * mutation_range * 2 - mutation_range datas.append(data) return datas
时间: 2024-02-14 21:18:57 浏览: 34
这段代码实现了遗传算法中的交叉和变异操作,用于生成新的基因组。具体来说,breed 方法接收两个父代的基因组 genome1 和 genome2,以及一个指定的子代数量 n_child。该方法会根据交叉概率随机选择 genome1 或 genome2 中对应位置的神经网络权重,并将其组合成新的基因组 data。同时,对于每个新基因组中的权重,还有一定概率进行变异操作。
在交叉操作中,我们采用了单点交叉的方式,即对于每个权重,以 50% 的概率从 genome1 或 genome2 中选择其对应位置的权重值。
在变异操作中,我们采用了随机增减权重的方式,即对于每个权重,以 mutation_rate 的概率进行变异,变异的幅度为一个随机数,其范围为 [-mutation_range, mutation_range]。
最终,该方法会返回生成的 n_child 个新基因组列表 datas。这些新基因组将会用于下一代的遗传算法中。
相关问题
def generate_next_generation(self): nexts = [] for i in range(round(elitism*population)): if len(nexts) < population: nexts.append(self.genomes[i].network_weights) for i in range(round(random_behaviour*population)): n = self.genomes[0].network_weights for k in range(len(n['weights'])): n['weights'][k] = random_clamped() if len(nexts) < population: nexts.append(n) max_n = 0 while True: for i in range(max_n): childs = self.breed(self.genomes[i], self.genomes[max_n], n_child if n_child > 0 else 1) for c in range(len(childs)): nexts.append(childs[c].network_weights) if len(nexts) >= population: return nexts max_n += 1 if max_n >= len(self.genomes)-1: max_n = 0
这段代码实现了生成下一代基因组的方法 generate_next_generation。在遗传算法中,每一代的基因组都需要通过选择、交叉和变异等操作生成下一代。具体来说,该方法实现了以下几个步骤:
1. 对于当前代的前 elitism*population 个基因组,直接将其神经网络权重添加到下一代中,以保留当前代中得分最高的基因组。
2. 对于当前代的前 random_behaviour*population 个基因组,采用随机权重的方式生成新的基因组,并添加到下一代中。
3. 对于当前代中的其他基因组,随机选择其中两个进行交叉和变异操作,生成 n_child 个新基因组,并添加到下一代中。
4. 如果下一代中的基因组数量达到了预期的数量 population,则返回生成的基因组列表;否则,继续进行第三步操作并不断尝试生成新的基因组,直到达到预期数量为止。
需要注意的是,该方法中的 elitism、random_behaviour、n_child 等参数均为遗传算法中的超参数,需要根据实际情况进行调整。
优化这段代码df_in_grown_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) df_in_breed_ebv = pd.read_table(open(r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"), delim_whitespace=True, encoding="gb18030", header=None) # df_in_grown_Phenotype.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] # df_in_breed_Phenotype.columns = ['个体号', '115EBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] df_in_breed_ebv.columns = ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'] df_in_grown_ebv.columns = ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'] NBA_mean = np.mean(df_in_breed_ebv['活仔EBV']) NBA_std = np.std(df_in_breed_ebv['活仔EBV']) days_mean = np.mean(df_in_grown_ebv['115daysEBV']) days_std = np.std(df_in_grown_ebv['115daysEBV']) fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV']) fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV']) output = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', left_on='个体号', right_on='个体号') # output['计算长白母系指数'] = 0.3 * (NBA - NBA_mean)/NBA_std - 0.3 * (days - days_mean)/days_std - 0.3 * (fcr-fcr_mean)/fcr_std + 0.1 * (pcl-pcl_mean)/pcl_std output['计算长白母系指数'] = 0.29 * (df_in_breed_ebv['活仔EBV'] - NBA_mean)/NBA_std - 0.58 * (df_in_grown_ebv['115daysEBV']- days_mean)/days_std - 0.13 * (df_in_grown_ebv['饲料转化率EBV']-fcr_mean)/fcr_std MLI_mean = np.mean(output['计算长白母系指数']) MLI_std = np.std(output['计算长白母系指数']) output['校正长白母系指数'] = 25 * ((output['计算长白母系指数'] - MLI_mean)/MLI_std) + 100
可以优化的地方有:
1. 重复的路径可以抽象成一个变量,避免代码重复。
2. 可以使用 with 语句来处理文件读取,避免忘记关闭文件。
3. 可以在读取文件时指定列名,避免后续需要重新指定列名。
4. 可以将计算指数和计算校正指数的代码分成两个函数,提高代码的可读性和可维护性。
优化后的代码如下:
```
import pandas as pd
import numpy as np
# 定义文件路径
breed_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\繁殖性能育种值N72分组 (7).txt"
grown_file = r"C:\Users\荆晓燕\Desktop\20230515分品种计算育种值\生长性能育种值N72分组 (7).txt"
def read_file(file_path, columns):
with open(file_path, encoding="gb18030") as f:
df = pd.read_table(f, delim_whitespace=True, header=None)
df.columns = columns
return df
# 读取文件并指定列名
df_in_breed_ebv = read_file(breed_file, ['个体号', '活仔EBV', '21d窝重EBV', '断配EBV'])
df_in_grown_ebv = read_file(grown_file, ['个体号', '115daysEBV', '饲料转化率EBV', '瘦肉率EBV', '眼肌EBV', '背膘EBV'])
def calculate_index(df_in_breed_ebv, df_in_grown_ebv):
# 计算指数
NBA_mean = np.mean(df_in_breed_ebv['活仔EBV'])
NBA_std = np.std(df_in_breed_ebv['活仔EBV'])
days_mean = np.mean(df_in_grown_ebv['115daysEBV'])
days_std = np.std(df_in_grown_ebv['115daysEBV'])
fcr_mean = np.mean(df_in_grown_ebv['饲料转化率EBV'])
fcr_std = np.std(df_in_grown_ebv['饲料转化率EBV'])
df = pd.merge(df_in_grown_ebv, df_in_breed_ebv, how='inner', on='个体号')
df['计算长白母系指数'] = 0.29 * (df['活仔EBV'] - NBA_mean) / NBA_std - 0.58 * (df['115daysEBV'] - days_mean) / days_std - 0.13 * (df['饲料转化率EBV'] - fcr_mean) / fcr_std
return df
def calculate_correction_index(df):
# 计算校正指数
MLI_mean = np.mean(df['计算长白母系指数'])
MLI_std = np.std(df['计算长白母系指数'])
df['校正长白母系指数'] = 25 * ((df['计算长白母系指数'] - MLI_mean) / MLI_std) + 100
return df
# 计算指数和校正指数
df_index = calculate_index(df_in_breed_ebv, df_in_grown_ebv)
df_correction_index = calculate_correction_index(df_index)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)