genetic-algorithm/src/individual.py
2017-09-22 15:56:56 +01:00

30 lines
723 B
Python

from math import pow, sin, pi
from random import Random
class Individual(object):
def __init__(self):
self.rand = Random()
self.x = self.rand.uniform(-100000, 100000)
self.y = self.rand.uniform(-100000, 100000)
self.fitness = 0
def fitness_function(self):
x = self.x
y = self.y
n = 9
self.fitness = pow(15*x*y*(1-x)*(1-y)*sin(n*pi*x)*sin(n*pi*y), 2)
return self.fitness
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()