From ffadcd4ab62b41be3ee8455d9fda4271ea276084 Mon Sep 17 00:00:00 2001 From: Jan Karkowski Date: Tue, 19 Dec 2023 09:38:22 +0100 Subject: [PATCH] interpreter --- antlr/MyGPMiniVisitor.py | 4 ++-- config.py | 8 ++++---- data.txt | 9 +++++++++ fitness.py | 4 +++- main.py | 2 +- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/antlr/MyGPMiniVisitor.py b/antlr/MyGPMiniVisitor.py index 13c62ac..a7d0baa 100644 --- a/antlr/MyGPMiniVisitor.py +++ b/antlr/MyGPMiniVisitor.py @@ -22,9 +22,9 @@ def __init__(self, counter, input): def visitProgram(self, ctx:GPMiniParser.ProgramContext): for s in ctx.statement(): if self.counter == 0: - return self.output + return [self.output, self.counter] self.visitStatement(s) - return self.output + return [self.output, self.counter] # Visit a parse tree produced by GPMiniParser#statement. diff --git a/config.py b/config.py index 21b57e0..4fd3ab2 100644 --- a/config.py +++ b/config.py @@ -4,7 +4,7 @@ def __init__(self): self.data = 'data.txt' self.heuristic = '1.1.B' - self.max_interpreter_steps = 100 + self.max_interpreter_steps = 1000 self.min_inital_vars = 1 self.max_initial_vars = 4 @@ -16,8 +16,8 @@ def __init__(self): self.max_blocks = 5 self.max_blocks_depth = 2 - self.max_operations_depth = 3 - self.max_logic_depth = 3 + self.max_operations_depth = 2 + self.max_logic_depth = 2 # between 0 and 1 # 0 means no complexity @@ -27,7 +27,7 @@ def __init__(self): self.population = 50 self.generations = 100 - self.tournament_size = 20 + self.tournament_size = 40 self.not_prob = 30 self.evolution_prob = { diff --git a/data.txt b/data.txt index 8acb2a1..f8ad911 100644 --- a/data.txt +++ b/data.txt @@ -1,3 +1,12 @@ 1 2 3 4 5 : 9 8 7 6 1 2 3 : 90 60 +7 7 7 : 4 4 4 4 +1 2 3 4 5 : 9 8 7 6 +1 2 3 : 90 60 +7 7 7 : 4 4 4 4 +1 2 3 4 5 : 9 8 7 6 +1 2 3 : 90 60 +7 7 7 : 4 4 4 4 +1 2 3 4 5 : 9 8 7 6 +1 2 3 : 90 60 7 7 7 : 4 4 4 4 \ No newline at end of file diff --git a/fitness.py b/fitness.py index d299fb4..94294b5 100644 --- a/fitness.py +++ b/fitness.py @@ -40,7 +40,9 @@ def fitness_function(self, indiv): tree = parser.program() for i in range(len(self.input)): visitor = MyGPMiniVisitor(self.config.max_interpreter_steps, self.input[i]) - output_generated = visitor.visitProgram(tree) + tab = visitor.visitProgram(tree) + output_generated = tab[0] + fitness += tab[1] - self.config.max_interpreter_steps if 'ERROR' in output_generated: fitness -= math.inf else: diff --git a/main.py b/main.py index 753df7a..ec8e65a 100644 --- a/main.py +++ b/main.py @@ -8,7 +8,7 @@ import timeit class TinyGPGenerator: - def __init__(self, tree_factory=TreeFactory): + def __init__(self): self.config = Config() self.state = State(self.config) self.heuristic = Heuristic()