From 80cebfe5fa961d2197ea3adc098acd93126dd763 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Thu, 21 Sep 2017 15:58:31 +0100 Subject: [PATCH] Spawn population --- src/individual.py | 13 +++++++++++++ src/main.py | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main.py diff --git a/src/individual.py b/src/individual.py index 947d3fc..c525d6a 100644 --- a/src/individual.py +++ b/src/individual.py @@ -1,4 +1,5 @@ from math import pow, sin, pi +from random import Random class Individual(object): @@ -6,9 +7,21 @@ class Individual(object): def __init__(self, x, y): self.x = x self.y = y + self.rand = Random() def fitness_function(self): x = self.x y = self.y n = 9 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() diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..123ff33 --- /dev/null +++ b/src/main.py @@ -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))