Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Almost done
  • Loading branch information
Stephen committed Mar 13, 2018
1 parent 30143f5 commit aa42bad
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
55 changes: 48 additions & 7 deletions gp.jl
@@ -1,4 +1,3 @@

function GT(a,b)
"""
GT function to be used in AST
Expand All @@ -20,9 +19,10 @@ function DIV(a,b)
return a/b
end
end

#=
macro create_function(defs)
for name in eval(defs)
macro create_function(names)
for name in eval(names)
ex = quote
function $(Symbol(name))(x,v)
fs = [:+, :-, :DIV, :GT]
Expand Down Expand Up @@ -96,21 +96,62 @@ Selection: Tournament
# Define x, v as globals because I can't figure out how to get
# around creating expressions and not being able to plug
# x and v into them for the past week

# Init everything
pop_size = 500
max_iterations = 1
max_initial_depth = 6
max_depth = 17
xover_prob = 0.9
fs = [:+, :-, :DIV, :GT]
ts = [:x,:v,-1]
xs = rand(1:10,20)
vs = rand(1:10,20)
elites = 2

points = [rand(-0.75:0.05:.75,2) for i in 1:20]
println(points)
elites = 2
tau = 0.02
max_time = 5
# Create initial population
pop0 = []
for i in 1:pop_size/2
push!(pop0, create_full_tree(max_initial_depth, fs, ts))
push!(pop0, create_grow_tree(max_initial_depth, fs, ts))
end

# Init iterations, scores
iteration = 0
scores = Dict(string(i) => Float64(i) for i in 1:pop_size)
while iteration < max_iterations
# For each member in the population
for (index, member) in enumerate(pop0)
# For each x,v pair
times = []
for point in points
global x, v
x = point[1]
v = point[2]
t = 0
while (t <= max_time) && (abs(x) > 0.01 && abs(v) > 0.01)
if eval(member) >=0
u = 1
else
u = 0
end
vk = v + tau*u
xk = x + tau*(v+vk)/2
v = vk
x = xk
t = t + tau
end
push!(times,t)
end
scores[string(index)] = mean(times)
end
sorted_scores = sort(collect(scores), by=x->x[2])
println(sorted_scores)
iteration += 1

end




37 changes: 28 additions & 9 deletions test.jl
@@ -1,14 +1,33 @@
macro create_ast(fns, tms)
return esc(Expr(:call, fns[1], tms[1], tms[2]))
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()
fns = [:+, :-]
tms = [:x, :y]
tst = @create_ast(fns, tms)
println(tst)
x = 1
y = 2
eval(tst)
fs = [:+, :-, :DIV, :GT]
ts = [:x,:v,-1]

expr = create_full_tree(6, fs, ts)

println(expr.args[2].args)
end

main()


0 comments on commit aa42bad

Please sign in to comment.