Exemplo n.º 1
0
Pm = 0.01  # mutation probability(变异概率)
Results = []  # save each generation's best solution(存储每一代的最优解,N个二元组)
Fit_value = []  # individual(个体适应度)
Fit_mean = []  # average(平均适应度)
num_users = 2  # number of users
const_load = []  # former user schedule load

for u in range(num_users):

    # run genetic algorithm for one user
    Pop = geneEncoding(Species_size, Pop_size,
                       Chrom_length)  #construct ecosphere(建立生物圈)

    for i in range(Iteration):  #begin genetic algorithm(开始进行遗传算法)
        Temp_pop_start, Temp_pop_end = calobjValue(
            Pop, Chrom_length, Timelist,
            Species_size)  # calculate power supply time period(计算供电时间)
        # TODO: Redefine calcostValue function based on const_load
        Cost_list = calcostValue(
            Temp_pop_start, Temp_pop_end, u,
            const_load)  #get the electricity price(得到电费价格表)

        Best_gene, Best_Results, Best_starttime, Best_endtime = best(
            Pop, Cost_list, Temp_pop_start, Temp_pop_end
        )  #get the best gene and the corresponding power start time and end time(找到最优基因及最优解以及最优解对应的供电起始时间)
        Results.append([
            Best_Results, Best_starttime, Best_endtime
        ])  #save the best result of each generation(存储每一代的最优结果)
        selection(Pop, Cost_list)  #copy the new species(新种群开始进行复制)
        crossover(Pop, Pc)  #(对新种群进行交配)
        mutation(Pop, Pm)  #对种群进行变异
Exemplo n.º 2
0
pop_size = 1000		# 世代数
max_value = 15      # 遺伝子の最大値
chrom_length = 4		# バイナリシーケンスの長さ
pc = 0.6			# 交叉確率
pm = 0.01           # 突然変異確率
max_results = [[]]		# 世代ごとの最大解を保存
min_results = [[]]		# 世代ごとの最小解を保存
fit_value = []		# 個体適応度
fit_mean = []		# 平均適応度

# pop = [[0, 1, 0, 1] for i in range(pop_size)]
pop = geneEncoding(pop_size, chrom_length)

for i in range(pop_size):
	obj_value = calobjValue(pop, chrom_length, max_value)        # 個体評価
	max_indi1, max_indi2, max_indi3, max_fit, min_indi1, min_indi2, min_indi3, min_fit = best(pop, obj_value)
	max_results.append([max_fit, b2d(max_indi1, max_value, chrom_length), b2d(max_indi2, max_value, chrom_length), b2d(max_indi3, max_value, chrom_length)])
	min_results.append([min_fit, b2d(min_indi1, max_value, chrom_length), b2d(min_indi2, max_value, chrom_length), b2d(min_indi3, max_value, chrom_length)])
	selection(pop, obj_value)		# 新しい個体群をコピー
	crossover(pop, pc)		# 交叉
	mutation(pop, pm)       # 突然変異

max_results = max_results[1:]
max_results.sort()
min_results = min_results[1:]
min_results.sort(reverse=True)
print(max_results[-1])
print(min_results[-1])

print("max : y = %f, x1 = %f, x2 = %f, x3 = %f" % (max_results[-1][0], max_results[-1][1], max_results[-1][2], max_results[-1][3]))
Exemplo n.º 3
0
pm = 0.01  # 变异概率
results = [[]]  # 存储每一代的最优解,[[]]表示是二维数组。
fit_value = []  # 个体适应度

simuNodesIndex, nodesIndexs = readSimulationNodes(inpPath)
#产生初始种群
pop = geneEncoding(
    pop_size, chrom_length
)  #pop中的个体原始为[421, 76, 213, 632, 1011, 102, 1289, 334,.......]代表其节点索引位置80个pop形状500*80

for i in range(500):  #共迭代500代
    # 对每一代做计算,判断优劣,获取当代最好个体
    start = time.clock()
    print('开始计算第%s代的适应度' % i)
    fit_value = calobjValue(
        pop, Residualsmatrix,
        SensitivityMatrix)  # 个体评价(适应度函数),obj_value放置对应每个个体的适应度值
    end1 = time.clock()
    print('完成适应度计算耗时%s' % (end1 - start), '适应度:', fit_value)

    best_individual, best_fit = best(
        pop, fit_value)  # 第一个存储最优的解对应的整数编码,第二个存储最优基因对应的适应度,值越小越好
    # end2 = time.clock()
    # print('完成挑选最好的耗时%s'%(end2-end1))
    print('最好的', best_fit)
    print('最好的个体', geneDecode(best_individual, nodesIndexs))
    results.append([best_fit,
                    geneDecode(best_individual,
                               nodesIndexs)])  ## 存储每一代的最优解,及其最终的节点ID,值越小越好

    #  遗传算法核心,生成种群,为下一次计算适应度做准备
Exemplo n.º 4
0
x_ = x_change(-5, 5)  #x的取值范围
max_value = (x_[1] - x_[0])  # 基因中允许出现的最大值
pop_size = 500  # 种群数量
chrom_length = 10  # 染色体长度
N = 200  #迭代次数
pc = 0.6  # 交配概率
pm = 0.01  # 变异概率
results = [[]]  # 存储每一代的最优解,N个二元组
fit_value = []  # 个体适应度
fit_mean = []  # 平均适应度

# pop = [[0, 1, 0, 1, 0, 1, 0, 1, 0, 1] for i in range(pop_size)]
pop = geneEncoding(pop_size, chrom_length)

for i in range(N):
    obj_value = calobjValue(pop, chrom_length, max_value, x_[0])  # 个体评价
    fit_value = calfitValue(obj_value)  # 淘汰

    best_individual, best_fit = best(pop, fit_value)  # 第一个存储最优的解, 第二个存储最优基因
    results.append([best_fit, b2d(best_individual, max_value, chrom_length)])

    selection(pop, fit_value)  # 新种群复制
    crossover(pop, pc)  # 交配
    mutation(pop, pm)  # 变异

results = results[1:]
results.sort()
print(results[-1])
print(best_individual)
print(best_fit)
print(obj_value[1])