What is supposed to happen:
Create a random population of object with attributes containing random integers, all objects will start with a chance of reproduction of 10.(line 16)
For all of those objects that have a "number" attribute higher than 4, add 10 to the chance_of_rep attribute pertaining to that object.(line 21)
Based off of the "chance_of_rep" attribute, decide whether or not to reproduce offspring. (line 36)
Every generation, age each member by 1, if the age is higher than 1, kill that member.(line 47)
What really happens:
The "chance_of_rep" attribute will top off at 50, and will not go any higher than that. Why is this? Besides that, everything else works as it should(at least I believe so, please mention anything that seems awry).
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):
for x in entities:
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"
raw_input()
By the way, this isn't homework, it's a personal project.
Well, with no thanks to oli CharlesWorth, I corrected the problem. Here's the corrected code:
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()