diff --git a/generators/progressive_random.py b/generators/progressive_random.py index 9914b47..ff2f973 100644 --- a/generators/progressive_random.py +++ b/generators/progressive_random.py @@ -5,7 +5,7 @@ class SequenceGenerator: """Generate a sequence progressively according to a predefined TL ratio and an even distribution""" - def __init__(self, choices, trials, tl=4.0, n=3, targets_ratio=0.2): + def __init__(self, choices, trials, tl=4.0, n=3, targets_ratio=0.33): """Initialize the genetic algorithm optimizer for n-back sequences. :param choices: :param trials: @@ -52,7 +52,7 @@ for c in list(seq): costs[c] += (1.0 if costs.__contains__(c) else 0.0) even_ratio = self.trials / len(self.choices) - costs = {k: abs(v - even_ratio)/self.trials for k,v in costs.items()} + costs = {k: abs(v - even_ratio)/self.trials for k, v in costs.items()} return max(list(costs.values())) def cost(self, seq): @@ -63,15 +63,15 @@ :return: """ - targets, lures = self.count_targets_and_lures(seq, self.n) + targets, lures = self.count_targets_and_lures(seq) targets_ratio_cost = 1.0 - self.norm_targets_ratio_dist.pdf(targets/self.trials) - tl_ratio_cost = 1.0 - self.norm_tl_ratio_dist.pdf(self.calc_tl_ratio(seq, self.n)) + tl_ratio_cost = 1.0 - self.norm_tl_ratio_dist.pdf(self.calc_tl_ratio(seq)) even_dist_cost = 1.0 - self.norm_even_dist.pdf(self.calc_even_distribution_distance(seq)) # print(targets_ratio_cost, tl_ratio_cost, even_dist_cost) return targets_ratio_cost + tl_ratio_cost + even_dist_cost - @staticmethod - def count_targets_and_lures(seq, n: int): + def count_targets_and_lures(self, seq): + n = self.n targets = 0.0 lures = 0.0 for index in range(n, len(seq)): @@ -81,9 +81,9 @@ lures += 1.0 return targets, lures - def calc_tl_ratio(self, seq, n: int): + def calc_tl_ratio(self, seq): """Calculates the T/L ratio in a block of trials.""" - targets, lures = self.count_targets_and_lures(seq, n) + targets, lures = self.count_targets_and_lures(seq) if lures < 0.01: # avoid division by zero lures = 0.01 return targets/lures @@ -91,10 +91,14 @@ if __name__ == '__main__': - n = 3 - generator = SequenceGenerator(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], trials=128, n=n) + alphabetic_choices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] + generator = SequenceGenerator(alphabetic_choices, trials=128, n=3) sq = generator.generate() - tl_ratio = generator.calc_tl_ratio(sq, n=n) + tl_ratio = generator.calc_tl_ratio(sq) even_dist_distance = generator.calc_even_distribution_distance(sq) - print('Progressively-Optimized Sequence: targets=%d, lures=%d' % generator.count_targets_and_lures(sq, n=n), 'with tl_ratio=%f' % tl_ratio, 'and even_dist_cost=%f' % even_dist_distance) + print( + 'Progressively-Optimized Sequence: targets=%d, lures=%d' % generator.count_targets_and_lures(sq), + 'with tl_ratio=%f' % tl_ratio, + 'and even_dist_cost=%f' % even_dist_distance + )