Added randround

This commit is contained in:
SimpleArt
2020-12-11 14:04:59 -05:00
parent 03c4648a24
commit 3adef48b54

View File

@ -1,5 +1,9 @@
import random import random
# Round to an integer near x with higher probability
# the closer it is to that integer.
randround = lambda x: int(x + random.random())
def _append_to_next_population(population_method): def _append_to_next_population(population_method):
"""Appends the new chromosomes to the next population. """Appends the new chromosomes to the next population.
Also modifies the input to include the mating pool. Also modifies the input to include the mating pool.
@ -161,7 +165,7 @@ class Crossover_Methods:
value = weight*value_1 + (1-weight)*value_2 value = weight*value_1 + (1-weight)*value_2
if type(value_1) == type(value_2) == int: if type(value_1) == type(value_2) == int:
value = round(value + random.uniform(-0.5, 0.5)) value = randround(value)
yield value yield value
@ -182,7 +186,7 @@ class Crossover_Methods:
value = (2-weight)*value_1 + (weight-1)*value_2 value = (2-weight)*value_1 + (weight-1)*value_2
if type(value_1) == type(value_2) == int: if type(value_1) == type(value_2) == int:
value = round(value + random.uniform(-0.5, 0.5)) value = randround(value)
yield value yield value
@ -211,7 +215,7 @@ class Crossover_Methods:
value = value_1 + (value_2-value_1) * (1-(1-x)**t)**(1/t) value = value_1 + (value_2-value_1) * (1-(1-x)**t)**(1/t)
if type(value_1) == type(value_2) == int: if type(value_1) == type(value_2) == int:
value = round(value + random.uniform(-0.5, 0.5)) value = randround(value)
yield value yield value