Set values between 0.0 and 1.0 only

This commit is contained in:
George Lacey 2017-09-27 13:48:39 +01:00
parent a5a8108e99
commit 328064cb7c

View File

@ -1,4 +1,4 @@
from math import pow, sin, pi from math import pow, sin, pi, cos
from random import Random from random import Random
rand = Random() rand = Random()
@ -7,8 +7,8 @@ rand = Random()
class Individual(object): class Individual(object):
def __init__(self): def __init__(self):
self.x = rand.uniform(-100, 100) self.x = self.get_rand_param()
self.y = rand.uniform(-100, 100) self.y = self.get_rand_param()
@classmethod @classmethod
def from_params(cls, x, y): def from_params(cls, x, y):
@ -34,6 +34,10 @@ class Individual(object):
def mutate(self): def mutate(self):
if rand.randint(1, 10) % 2 == 0: if rand.randint(1, 10) % 2 == 0:
self.x = rand.random() self.x = self.get_rand_param()
else: else:
self.y = rand.random() self.y = self.get_rand_param()
@staticmethod
def get_rand_param():
return rand.uniform(0, 1)