From aa42badd5d7ea0ae95a8e4906f6351f352864336 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 13 Mar 2018 17:03:46 -0400 Subject: [PATCH] Almost done --- gp.jl | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- test.jl | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/gp.jl b/gp.jl index 09e16a9..34f5339 100644 --- a/gp.jl +++ b/gp.jl @@ -1,4 +1,3 @@ - function GT(a,b) """ GT function to be used in AST @@ -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] @@ -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 + + + diff --git a/test.jl b/test.jl index fd165be..c4b025a 100644 --- a/test.jl +++ b/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() + +