Where to instantiate the Solver?
Whebon opened this issue · comments
Current implementation
Currently, the end user is responsible for instantiating the Solver and passing it to an iterator:
#main.jl
solver = Solver(grammar, :Int)
iter = BFSIterator(grammar, :Int, solver=solver, max_depth=4, max_size=8)
The iterator then passes the max_depth
and max_size
parameters to the solver:
function Base.iterate(iter::TopDownIterator)
# Priority queue with number of nodes in the program
pq :: PriorityQueue{State, Union{Real, Tuple{Vararg{Real}}}} = PriorityQueue()
#TODO: these attributes should be part of the solver, not of the iterator
solver = iter.solver
solver.max_size = iter.max_size
solver.max_depth = iter.max_depth
enqueue!(pq, get_state(solver), priority_function(iter, get_grammar(solver), get_tree(solver), 0))
return _find_next_complete_tree(iter.solver, pq, iter)
end
The iterator and solver are both holding the Grammar
, but only the solver needs to store it. Since the iterator is require to hold the solver, it automatically has access to the grammar via get_grammar(solver)
.
The iterator and solver are both holding the starting symbol, but again, only one of them actually needs to hold it. The starting symbol is a way to jump start the enumeration. In the current implementation, the top down iterator does not use the starting symbol anymore, because the responsibility of jumpstarting the search has been shifted to the solver.
Side note: the solver has two constructor signatures:
"""
Solver(grammar::Grammar, sym::Symbol)
Constructs a new solver, with an initial state using starting symbol `sym`
"""
function Solver(grammar::Grammar, sym::Symbol; with_statistics=false)
#...
end
"""
Solver(grammar::Grammar, init_node::AbstractRuleNode)
Constructs a new solver, with an initial state of the provided [`AbstractRuleNode`](@ref).
"""
function Solver(grammar::Grammar, init_node::AbstractRuleNode; with_statistics=false)
#...
end
Target Implementation
The solver
should be an optional keyworded argument of the program iterator constructor.
If the solver
argument is not provided, the program iterator constructor should instantiate one.
Remove the grammar
, sym
, max_depth
and max_size
fields from the program iterator. (or ensure that the program iterator and the solver have the same values for these fields in the program iterator constructor)
Resolved in #84, will be included in the next release of HerbSearch
.