python手撸一段简单的遗传算法

有空再来写解析
 

import random

def create_answer(numbers_set,n):
    result=[]
    for i in range(n):
        result.append(random.sample(numbers_set,10))
    return result

def error_level(new_answer,numbers_set):
    error=[]
    right_answer=sum(numbers_set)/10
    for item in new_answer:
        value=abs(right_answer-sum(item))
        if value==0:
            error.append(10)
        else:
            error.append(1/value)
    return error

def choice_selected(old_answer,numbers_set):
    result=[]
    error=error_level(old_answer,numbers_set)
    error_one=[item/sum(error) for item in error]
    for i in range(1,len(error_one)):
        error_one[i]+=error_one[i-1]
    for i in range(len(old_answer)//2):
        temp=[]
        for j in range(2):
            rand=random.uniform(0,1)
            for k in range(len(error_one)):
                if k==0:
                    if rand<error_one[k]:
                        temp.append(old_answer[k])
                else:
                    if rand>=error_one[k-1] and rand<error_one[k]:
                        temp.append(old_answer[k])
        rand=random.randint(0,6)
        temp_1=temp[0][:rand]+temp[1][rand:rand+3]+temp[0][rand+3:]
        temp_2=temp[1][:rand]+temp[0][rand:rand+3]+temp[1][rand+3:]
        result.append(temp_1)
        result.append(temp_2)
    return result

def variation(old_answer,numbers_set,pro):
    for i in range(len(old_answer)):
        rand=random.uniform(0,1)
        if rand<pro:
            rand_num=random.randint(0,9)
            old_answer[i]=old_answer[i][:rand_num]+random.sample(numbers_set,1)+old_answer[i][rand_num+1:]
    return old_answer

numbers_set=random.sample(range(0,1000),50)
middle_answer=create_answer(numbers_set,100)
first_answer=middle_answer[0]
great_answer=[]
for i in range(1000):
    middle_answer=choice_selected(middle_answer,numbers_set)
    middle_answer=variation(middle_answer,numbers_set,0.1)
    error=error_level(middle_answer,numbers_set)
    index=error.index(max(error))
    great_answer.append([middle_answer[index],error[index]])
great_answer.sort(key=lambda x:x[1],reverse=True)
print("正确答案为",sum(numbers_set)/10)
print("给出的最优解为",great_answer[0][0])
print("该和为",sum(great_answer[0][0]))
print("选择系数为",great_answer[0][1])
print("最初解的和为",sum(first_answer))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靳小锅er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值