Spawn population

This commit is contained in:
George Lacey 2017-09-21 15:58:31 +01:00
parent 0e6d124eaa
commit 80cebfe5fa
2 changed files with 26 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from math import pow, sin, pi from math import pow, sin, pi
from random import Random
class Individual(object): class Individual(object):
@ -6,9 +7,21 @@ class Individual(object):
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
self.y = y self.y = y
self.rand = Random()
def fitness_function(self): def fitness_function(self):
x = self.x x = self.x
y = self.y y = self.y
n = 9 n = 9
return pow(15*x*y*(1-x)*(1-y)*sin(n*pi*x)*sin(n*pi*y), 2) return pow(15*x*y*(1-x)*(1-y)*sin(n*pi*x)*sin(n*pi*y), 2)
def crossover(self, spouse):
temp = self.x
self.x = spouse.x
spouse.x = temp
def mutate(self):
if self.rand.randint(1, 10) % 2 == 0:
self.x = self.rand.random()
else:
self.y = self.rand.random()

13
src/main.py Normal file
View File

@ -0,0 +1,13 @@
import argparse
from individual import Individual
args = argparse.ArgumentParser()
args.add_argument("pop", help="Population size", type=int)
args = args.parse_args()
population = []
for current in range(0, args.pop):
population.append(Individual())
print(len(population))