Python 2.7 繁殖机会极限的进化算法
应该发生什么:Python 2.7 繁殖机会极限的进化算法,python-2.7,evolutionary-algorithm,Python 2.7,Evolutionary Algorithm,应该发生什么: 创建一个属性包含随机整数的随机对象总体,所有对象的复制概率为10。(第16行) 对于“number”属性大于4的所有对象,在与该对象相关的“chance_of_rep”属性中添加10。(第21行) 根据“代表的机会”属性,决定是否繁殖后代。(第36行) 每一代,每个成员的年龄为1岁,如果年龄大于1岁,则杀死该成员。(第47行) 实际发生的情况: “代表的机会”属性将在50时结束,并且不会超过50。为什么会这样? 除此之外,其他一切都正常工作(至少我认为是这样,请提及任何看起来不对
将随机导入为r
导入操作系统
导入系统
导入时间
从子流程导入调用
#调用('color a',shell=True)
类实体(对象):
定义初始化(自身,编号):
self.number=number
self.chance=10
self.age=0
班级适应度(对象):
定义初始(自我,总体):
self.population=population
def StartPopulation(自身、pop、实体):
对于范围内的x(pop):
实体=实体(r.randint(3,5))
实体。追加(实体)
def FitnessMethod(自身、实体):
对于实体中的x:
如果x.number>=5:
x、 机会+=10
其他:
x、 机会+=0
如果x.chance>100:
x、 机会=100
其他:
x、 机会+=0
定义(自我、实体):
对于实体中的x:
如果r.randint(0100)=2:
实体。删除(x)
pop=r.randint(3,5)
实体=[]
健身=健身(pop)
健身。起始人群(流行、实体)
对于实体中的x:
打印x.number,x.chance
打印“~~~~~~~~~~~~~~\n”
原始输入()
对于范围(30)内的x:
Fitness.FitnessMethod(实体)
适合度。复制(实体)
健康、老化(实体)
对于实体中的x:
打印x.编号、x.年龄、x.机会
打印“生成\u突变\u完成”
原始输入()
顺便说一句,这不是家庭作业,这是一个个人项目。好吧,不感谢奥利·查尔斯沃思,我纠正了这个问题。 以下是更正后的代码:
import random as r
import os
import sys
import time
from subprocess import call
#call('color a', shell=True)
class Entity(object):
def __init__(self, number):
self.number = number
self.chance = 10
self.age = 0
class Fitness(object):
def __init__(self, population):
self.population = population
def StartPopulation(self, pop, entities):
for x in range(pop):
entity = Entity(r.randint(3, 5))
entities.append(entity)
def FitnessMethod(self, entities):
for x in entities:
if x.number >= 5:
x.chance += 10
else:
x.chance += 0
if x.chance > 100:
x.chance = 100
else:
x.chance += 0
def Reproduce(self,entities):
x = entities[r.randint(0, (len(entities) - 1))]
if r.randint(0, 100) < x.chance:
random_children = r.randint(1, 3)
for a in range(random_children):
entity = Entity(x.number)
entity.chance = x.chance
entities.append(entity)
x.age += 1
def Aging(self, entities):
for x in entities:
if x.age >= 2:
entities.remove(x)
pop = r.randint(3, 5)
entities = []
Fitness = Fitness(pop)
Fitness.StartPopulation(pop, entities)
for x in entities:
print x.number, x.chance
print "~~~~~~~~~~~~~\n"
raw_input()
for x in range(30):
Fitness.FitnessMethod(entities)
Fitness.Reproduce(entities)
Fitness.Aging(entities)
for x in entities:
print x.number, x.age, x.chance
print "Generation_Mutation_Complete"
print "Generation_Mutation_Complete"
raw_input()
将随机导入为r
导入操作系统
导入系统
导入时间
从子流程导入调用
#调用('color a',shell=True)
类实体(对象):
定义初始化(自身,编号):
self.number=number
self.chance=10
self.age=0
班级适应度(对象):
定义初始(自我,总体):
self.population=population
def StartPopulation(自身、pop、实体):
对于范围内的x(pop):
实体=实体(r.randint(3,5))
实体。追加(实体)
def FitnessMethod(自身、实体):
对于实体中的x:
如果x.number>=5:
x、 机会+=10
其他:
x、 机会+=0
如果x.chance>100:
x、 机会=100
其他:
x、 机会+=0
定义(自我、实体):
x=实体[r.randint(0,(len(实体)-1))]
如果r.randint(0100)=2:
实体。删除(x)
pop=r.randint(3,5)
实体=[]
健身=健身(pop)
健身。起始人群(流行、实体)
对于实体中的x:
打印x.number,x.chance
打印“~~~~~~~~~~~~~~\n”
原始输入()
对于范围(30)内的x:
Fitness.FitnessMethod(实体)
适合度。复制(实体)
健康、老化(实体)
对于实体中的x:
打印x.编号、x.年龄、x.机会
打印“生成\u突变\u完成”
打印“生成\u突变\u完成”
原始输入()
要求人们发现代码中的错误并不是特别有效。您应该使用调试器(或添加打印语句)来隔离问题,方法是跟踪程序的进度,并将其与预期发生的情况进行比较。一旦这两个问题出现分歧,你就发现了问题所在。(如果有必要的话,你应该构建一个。)哇,有点苛刻,毕竟这是一个问答网站。这是一个编程知识问答,而不是众包调试器……拜托,真的吗?你甚至都不想帮我?
import random as r
import os
import sys
import time
from subprocess import call
#call('color a', shell=True)
class Entity(object):
def __init__(self, number):
self.number = number
self.chance = 10
self.age = 0
class Fitness(object):
def __init__(self, population):
self.population = population
def StartPopulation(self, pop, entities):
for x in range(pop):
entity = Entity(r.randint(3, 5))
entities.append(entity)
def FitnessMethod(self, entities):
for x in entities:
if x.number >= 5:
x.chance += 10
else:
x.chance += 0
if x.chance > 100:
x.chance = 100
else:
x.chance += 0
def Reproduce(self,entities):
x = entities[r.randint(0, (len(entities) - 1))]
if r.randint(0, 100) < x.chance:
random_children = r.randint(1, 3)
for a in range(random_children):
entity = Entity(x.number)
entity.chance = x.chance
entities.append(entity)
x.age += 1
def Aging(self, entities):
for x in entities:
if x.age >= 2:
entities.remove(x)
pop = r.randint(3, 5)
entities = []
Fitness = Fitness(pop)
Fitness.StartPopulation(pop, entities)
for x in entities:
print x.number, x.chance
print "~~~~~~~~~~~~~\n"
raw_input()
for x in range(30):
Fitness.FitnessMethod(entities)
Fitness.Reproduce(entities)
Fitness.Aging(entities)
for x in entities:
print x.number, x.age, x.chance
print "Generation_Mutation_Complete"
print "Generation_Mutation_Complete"
raw_input()