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/gp_module_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.
80 lines (77 sloc)
2.16 KB
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
#= | |
Objective: Find minimum time vehicle control program | |
Terminal Set: x (position) | |
v (velocity) | |
-1 | |
Function Set: +, -, *, DIV, GT, ABS | |
Cost: Time to bring behicle to phase plane origin | |
averaged over 20 random initial conditions | |
Gen. Limit: 50 | |
Pop. Size: 500 | |
Max Initial Tree Depth: 6 | |
Init. Method: Ramped half-and-half | |
Max. Tree depth: 17 | |
X-Over Prob.: 0.9 | |
Mutation prob.: 0 | |
Number of elites: 2 | |
Selection: Tournament | |
=# | |
module GP | |
function GT(a,b) | |
""" | |
GT function to be used in AST | |
""" | |
if a > b | |
return 1 | |
else | |
return -1 | |
end | |
end | |
function DIV(a,b) | |
""" | |
Protected division used in AST | |
""" | |
if abs(b) < 10.0^-10 | |
return 1 | |
else | |
return a/b | |
end | |
end | |
@eval function create_single_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_single_full_tree(depth-1, fs, ts), create_single_full_tree(depth-1, fs, ts)) | |
end | |
end | |
end | |
function main() | |
""" | |
Main function | |
""" | |
# Define functional and terminal sets | |
fs = [:+, :-, :DIV, :GT] | |
ts = [:x, :v, -1] | |
# Create the tree | |
tst = GP.create_single_full_tree(4, fs, ts) | |
#println(typeof(tst)) | |
#println(tst) | |
#println(dump(tst)) | |
# If I comment these globals, eval cannot find x and v in the scope | |
GP.x = 1 | |
GP.v = 1 | |
#eval(tst) | |
end | |
main() |