Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
JuliaGP/test.jl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
33 lines (26 sloc)
968 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function create_full_tree(depth, fs, ts) | |
""" | |
Creates a single AST with full depth | |
Inputs | |
depth Current depth of tree. Initially called from main() with max depth | |
fs Function Set - Array of allowed functions | |
ts Terminal Set - Array of allowed terminal values | |
Output | |
Full AST of typeof()==Expr | |
""" | |
# If we are at the bottom | |
if depth == 1 | |
# End of tree, return function with two terminal nodes | |
return Expr(:call, fs[rand(1:length(fs))], ts[rand(1:length(ts))], ts[rand(1:length(ts))]) | |
else | |
# Not end of expression, recurively go back through and create functions for each new node | |
return Expr(:call, fs[rand(1:length(fs))], create_full_tree(depth-1, fs, ts), create_full_tree(depth-1, fs, ts)) | |
end | |
end | |
function main() | |
fs = [:+, :-, :DIV, :GT] | |
ts = [:x,:v,-1] | |
expr = create_full_tree(6, fs, ts) | |
println(expr.args[2].args) | |
end | |
main() | |