From 5d7fdeb23f91092dae58f575fdc0403b955f428b Mon Sep 17 00:00:00 2001 From: Tilman Hinnerichs <32704542+THinnerichs@users.noreply.github.com> Date: Tue, 22 Aug 2023 14:57:24 +0200 Subject: [PATCH 1/2] add `evaluate_on_program` based on program and grammar Genetic algorithms need this functionality. For more consistency, we move this function here. --- src/interpreter.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/interpreter.jl b/src/interpreter.jl index 00ce120..ca8db90 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -64,6 +64,23 @@ function execute_on_examples(tab::SymbolTable, expr::Any, example_inputs::Vector end +""" + evaluate_on_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) + +Runs a program on the examples and returns tuples of actual desired output and the program's output +""" +function evaluate_on_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) + results = Tuple{<:Number,<:Number}[] + expression = rulenode2expr(program, grammar) + symbol_table = SymbolTable(grammar) + for example ∈ filter(e -> e isa IOExample, examples) + outcome = evaluation_function(symbol_table, expression, example.in) + push!(results, (example.out, outcome)) + end + return results +end + + """ interpret(tab::SymbolTable, ex::Expr) From 5869b1656aa2eede861485434ee66b0838d02584 Mon Sep 17 00:00:00 2001 From: Tilman Hinnerichs <32704542+THinnerichs@users.noreply.github.com> Date: Tue, 22 Aug 2023 14:58:46 +0200 Subject: [PATCH 2/2] Change name to evaluate_program --- src/interpreter.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interpreter.jl b/src/interpreter.jl index ca8db90..3f0edaa 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -65,11 +65,11 @@ end """ - evaluate_on_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) + evaluate_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) Runs a program on the examples and returns tuples of actual desired output and the program's output """ -function evaluate_on_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) +function evaluate_program(program::RuleNode, examples::Vector{<:Example}, grammar::Grammar, evaluation_function::Function) results = Tuple{<:Number,<:Number}[] expression = rulenode2expr(program, grammar) symbol_table = SymbolTable(grammar)