{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "using JLD\n", "include(\"Transformation.jl\")\n", "include(\"AbstractSystem.jl\")\n", "include(\"Tree.jl\")\n", "include(\"Evaluation.jl\")\n", "include(\"BackPropogation.jl\")\n", "include(\"Facility.jl\")\n", "import Base.push!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(::fs) (generic function with 3 methods)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type Axiom\n", " tree1 :: Tree\n", " tree2 :: Tree\n", " index :: Dict\n", "end\n", "\n", "function Base.show(io :: IO, m :: Axiom)\n", " print(io, \"Axiom[\")\n", " print(io, m.tree1)\n", " print(io, \", \")\n", " print(io, m.tree2)\n", " print(io, \"]\")\n", "end\n", "\n", "function beautify(m :: Axiom)\n", " string(\"Axiom:\\n\", beautify(m.tree1, 1), \"\\n\", beautify(m.tree2, 1), \"\\n\")\n", "end\n", "\n", "Axiom(tree1 :: Tree, tree2 :: Tree) = Axiom(tree1, tree2, add!(index(tree1), index(tree2)))\n", "\n", "Axiom(skeleton1, skeleton2) = Axiom(toTree(skeleton1), toTree(skeleton2))\n", "\n", "function push!(index :: Dict, ops :: Array)\n", " n = length(ops)\n", " for i in 1:n\n", " for t in index[i]\n", " t.op = ops[i]\n", " end\n", " end\n", "end\n", "\n", "function push!(index :: Dict, ops :: Dict)\n", " for key in keys(ops)\n", " if haskey(index, key)\n", " ts = index[key]\n", " for t in ts\n", " t.op = ops[key]\n", " end\n", " end\n", " end\n", "end\n", "\n", "function push!(axiom :: Axiom, ops)\n", " push!(axiom.index, variables)\n", "end\n", "\n", "function init_axiom!(axiom :: Axiom, variables)\n", " push!(axiom.index, variables)\n", " init_tree!(axiom.tree1)\n", " init_tree!(axiom.tree2)\n", "end\n", "\n", "init_axioms! = distribute(init_axiom!)\n", "\n", "## add some loss function to deal with degenerating problem?\n", "\n", "function loss(a, b)\n", " b * (1. - a * b)\n", "end\n", "\n", "function train_axiom!(axiom :: Axiom, variables, n = 1, randomize = identity)\n", " push!(axiom.index, variables)\n", " d1 :: Array{Float64, 1} = axiom.tree1.value[:d]\n", " d2 :: Array{Float64, 1} = axiom.tree2.value[:d]\n", " v1 :: Array{Float64, 1} = axiom.tree1.value[:value]\n", " v2 :: Array{Float64, 1} = axiom.tree2.value[:value]\n", " for i in 1:n\n", " randomize(variables)\n", " push!(axiom.index, variables)\n", " eval_tree!(axiom.tree2)\n", " eval_tree!(axiom.tree1)\n", " for j in 1:length(d1)\n", " d1[j] = loss(v1[j], v2[j])\n", " d2[j] = loss(v2[j], v1[j])\n", " end\n", " bp_tree!(axiom.tree1)\n", " bp_tree!(axiom.tree2)\n", " end\n", "end\n", "\n", "train_axioms! = distribute(train_axiom!)\n", "\n", "## to prevent degeneration problem, we use anti-traing to deal with the problem.\n", "\n", "function anti_train_axiom!(axiom :: Axiom, n = 1, randomize = identity)\n", " d1 :: Array{Float64, 1} = axiom.tree1.value[:d]\n", " d2 :: Array{Float64, 1} = axiom.tree2.value[:d]\n", " v1 :: Array{Float64, 1} = axiom.tree1.value[:value]\n", " v2 :: Array{Float64, 1} = axiom.tree2.value[:value]\n", " for i in 1:n\n", " randomize(axiom.tree1)\n", " randomize(axiom.tree2)\n", " eval_tree!(axiom.tree2)\n", " eval_tree!(axiom.tree1)\n", " for j in 1:length(d1)\n", " d1[j] = - loss(v1[j], v2[j])\n", " d2[j] = - loss(v2[j], v1[j])\n", " end\n", " bp_tree!(axiom.tree1)\n", " bp_tree!(axiom.tree2)\n", " end\n", "end\n", "\n", "anti_train_axioms! = distribute(anti_train_axiom!)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "List" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "include(\"DataStructs.jl\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "23-element Array{Symbol,1}:\n", " :Binding \n", " :Bindingc \n", " :Env \n", " :Envc \n", " :Frame \n", " :Framec \n", " :Prog \n", " :Seq \n", " :Stream \n", " :UChar \n", " :Variable \n", " :add_binding\n", " :assignment \n", " :def \n", " :definition \n", " :extend \n", " :f_eval \n", " :func_call \n", " :lookup \n", " :procedure \n", " :s_eval \n", " :set \n", " :var " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@load \"infinity.jld\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "REPL(Obj(empty){Env},Obj(empty){Prog})" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## The Repl facility, note that for the program structure parsing, we rely on the julia parser;\n", "## and we also need to read the variable name from string.\n", "include(\"Repl.jl\")\n", "r = REPL(Env.empty, Seq.empty)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Result for pair 1 and 2 is 72.83524847763572.\n", "Result for pair 1 and 3 is 73.52356773568832.\n", "Result for pair 1 and 4 is 26.396623062828702.\n", "Result for pair 1 and 5 is 31.109062879858957.\n", "Result for pair 2 and 3 is 109.996736585414.\n", "Result for pair 2 and 4 is 87.67650198559386.\n", "Result for pair 2 and 5 is 92.28913658101183.\n", "Result for pair 3 and 4 is 49.43596661544338.\n", "Result for pair 3 and 5 is 44.80651146587041.\n", "Result for pair 4 and 5 is 11.379107886488296.\n" ] } ], "source": [ "a1 = repl(r, \"function(x) x end\").value;\n", "a2 = repl(r, \"var y = function(x) x end\").value;\n", "a3 = repl(r, \"y\").value;\n", "a4 = repl(r, \"function(x) x(x) end\").value;\n", "a5 = repl(r, \"begin var y = function(x) x end; y end\").value;\n", "as = [a1, a2, a3, a4, a5];\n", "\n", "pair_computation(function(x, y) sum(abs(as[x] - as[y])) end, 1:5)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Julia 0.5.0", "language": "julia", "name": "julia-0.5" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.5.0" } }, "nbformat": 4, "nbformat_minor": 1 }