From 94211ab72361cf75715fb8ef766d00f8c0fef456 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sat, 6 Jun 2015 19:39:46 -0400 Subject: [PATCH] 10x speedup; still slightly off though --- examples/life.py | 2 +- src/configuration.py | 12 ++++++------ src/ruleset.py | 8 +++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/life.py b/examples/life.py index 205ab3c..1b253fa 100644 --- a/examples/life.py +++ b/examples/life.py @@ -16,4 +16,4 @@ if __name__ == '__main__': p = cam_parser.CAMParser('B3/S23', c) c.randomize() - c.start_plot(100, p.ruleset) + c.start_plot(50, p.ruleset) diff --git a/src/configuration.py b/src/configuration.py index b5f42a2..f935ce3 100644 --- a/src/configuration.py +++ b/src/configuration.py @@ -45,14 +45,14 @@ class Neighborhood: Offsetted cells belonging in the given neighborhood must be added separately. """ + self.states = None + self.bit_indices = None + self.flat_indices = None + self.total = total self.bit_index = bit_index self.flat_index = flat_index - self.states = np.array([]) - self.bit_indices = np.array([]) - self.flat_indices = np.array([]) - def process_offsets(self, plane, offsets): """ @@ -179,9 +179,9 @@ class Configuration: """ if not vfunc(plane, neighborhood, *args): return (False, None) - elif callable(self.next_state): + try: return (True, self.next_state(plane, neighborhood, *args)) - else: + except TypeError: return (True, self.next_state) diff --git a/src/ruleset.py b/src/ruleset.py index 2899aa9..f4da395 100644 --- a/src/ruleset.py +++ b/src/ruleset.py @@ -95,10 +95,11 @@ class Ruleset: # Chunk into groups of 9 and sum all values # These summations represent the total number of active states in a given neighborhood totals = [0] * plane.N - chunks = list(map(sum, [neighboring[i:i+9] for i in range(0, len(neighboring), 9)])) + chunks = map(sum, [neighboring[i:i+9] for i in range(0, len(neighboring), 9)]) for chunk in chunks: - i_chunk = list(map(int, str(chunk).zfill(plane.N))) - totals = list(map(sum, zip(totals, i_chunk))) + i_chunk = map(int, str(chunk).zfill(plane.N)) + totals = map(sum, zip(totals, i_chunk)) + totals = list(totals) # Determine which function should be used to test success if self.method == Ruleset.Method.MATCH: @@ -111,6 +112,7 @@ class Ruleset: vfunc = lambda *args: True # Apply change to all successful configurations + for bit_index in to_update: neighborhood = c.Neighborhood(flat_index, bit_index, totals[bit_index]) success, state = config.passes(plane, neighborhood, vfunc, *args)