diff --git a/py/ga_optimized_random_generator.py b/py/ga_optimized_random_generator.py index f1083ff..a9c000e 100644 --- a/py/ga_optimized_random_generator.py +++ b/py/ga_optimized_random_generator.py @@ -5,7 +5,7 @@ class GAOptimizedRandomGenerator: """Generate even random sequences according to a predefined TL ration (Ralph, 2014)""" - def __init__(self, choices, trials=64, tl=1, pool_size=10, n=2): + def __init__(self, choices, trials=64, tl=1, pool_size=100, n=2): """ Initialize the genetic algorithm optimizer for n-back sequences. :param choices: @@ -25,13 +25,13 @@ :return: a sequence of items in "list" format. """ generation = 0 - best_parent = self.__find_best_parents(1) - while self.fitness(best_parent) > 0.1 and generation < 10000: + best_parent = self.__find_best_parents(1)[0] + while self.fitness(best_parent) > 0.1 and generation < 100000: generation += 1 self.pool = self.mutate() self.pool = self.crossover_all() - best_parent = self.__find_best_parents(1) - print(''.join(best_parent[0]), ' ', self.calculate_tl_ratio(best_parent, self.n)) + best_parent = self.__find_best_parents(1)[0] + print(best_parent, ' ', self.calculate_tl_ratio(best_parent, self.n)) return best_parent def __init_pool(self, pool_size): @@ -43,7 +43,7 @@ self.pool.clear() all_comb = it.combinations_with_replacement(self.choices, self.trials) sample = random.sample(list(all_comb), pool_size) - self.pool.extend(sample) + self.pool.extend(map(lambda sampi: ''.join(sampi), sample)) return self.pool def __find_best_parents(self, count=1): @@ -66,7 +66,7 @@ def fitness(self, seq): """ - Calculate overall fitness of a sequence (block of trials). + Calculate overall fitness of a sequence (block of trials). It's cost, so we try to minimize this cost. :param seq: :return: """ @@ -121,4 +121,4 @@ s = generator.generate() tl_ratio = generator.calculate_tl_ratio(s, 2) - print('GA-Optimized Sequence: %s' % ''.join(list(s[0])), 'with tl_ratio=%f' % tl_ratio) + print('GA-Optimized Sequence: %s' % s, 'with tl_ratio=%f' % tl_ratio)