Skip to content

Commit f1e8d78

Browse files
author
Maximilian Reißmann
committed
add tut
1 parent 55b1cf8 commit f1e8d78

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

tutorial/JGEP_demo.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNeSIyC8h8HTq+tCGj2n83Y"},"kernelspec":{"name":"julia","display_name":"julia 1.10.5"}},"cells":[{"cell_type":"markdown","source":["# Demo: Considering an easy regression task using the JGep\n","# Here we start by installing the Julia kernel - this may take a few moments 😴"],"metadata":{"id":"T4OGbNk5Z7nr"}},{"cell_type":"markdown","source":[],"metadata":{"id":"XudtpijzaDL1"}},{"cell_type":"code","execution_count":13,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":87},"id":"MbYd4VSkV7Nv","executionInfo":{"status":"ok","timestamp":1728281491020,"user_tz":-660,"elapsed":1094,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"eb520cac-8c01-4852-be1a-4b788de1d18c"},"outputs":[{"output_type":"display_data","data":{"text/plain":[" Unrecognized magic \u001b[36m%%shell\u001b[39m.\n","\n"," Julia does not use the IPython \u001b[36m%magic\u001b[39m syntax. To interact with the IJulia kernel, use\n"," \u001b[36mIJulia.somefunction(...)\u001b[39m, for example. Julia macros, string macros, and functions can be used to\n"," accomplish most of the other functionalities of IPython magics."],"text/markdown":"Unrecognized magic `%%shell`.\n\nJulia does not use the IPython `%magic` syntax. To interact with the IJulia kernel, use `IJulia.somefunction(...)`, for example. Julia macros, string macros, and functions can be used to accomplish most of the other functionalities of IPython magics.\n","text/latex":"Unrecognized magic \\texttt{\\%\\%shell}.\n\nJulia does not use the IPython \\texttt{\\%magic} syntax. To interact with the IJulia kernel, use \\texttt{IJulia.somefunction(...)}, for example. Julia macros, string macros, and functions can be used to accomplish most of the other functionalities of IPython magics.\n\n"},"metadata":{}}],"source":["%%shell\n","set +e\n","\n","#---------------------------------------------------#\n","JULIA_VERSION=\"1.10.5\" # any version ≥ 0.7.0\n","JULIA_PACKAGES=\"IJulia BenchmarkTools CSV DataFrames Dates DynamicExpressions FileIO ForwardDiff GZip JSON LineSearches LinearAlgebra Logging Optim OrderedCollections ProgressMeter Random Serialization StaticArrays Statistics Zygote\"\n","JULIA_NUM_THREADS=2\n","#---------------------------------------------------#\n","\n","if [ -z `which julia` ]; then\n"," # Install Julia\n"," JULIA_VER=`cut -d '.' -f -2 <<< \"$JULIA_VERSION\"`\n"," echo \"Installing Julia $JULIA_VERSION on the current Colab Runtime...\"\n"," BASE_URL=\"https://julialang-s3.julialang.org/bin/linux/x64\"\n"," URL=\"$BASE_URL/$JULIA_VER/julia-$JULIA_VERSION-linux-x86_64.tar.gz\"\n"," if ! wget -nv $URL -O /tmp/julia.tar.gz; then\n"," echo \"Failed to download Julia. Check the URL and your internet connection.\"\n"," exit 1\n"," fi\n","\n"," if ! tar -x -f /tmp/julia.tar.gz -C /usr/local --strip-components 1; then\n"," echo \"Failed to extract Julia archive. Check if you have sufficient permissions.\"\n"," exit 1\n"," fi\n","\n"," rm /tmp/julia.tar.gz\n","\n"," # Install packages\n"," echo \"Installing packages...\"\n"," if ! julia -e \"using Pkg; Pkg.add([$(echo $JULIA_PACKAGES | sed \"s/ /\\\", \\\"/g\" | sed \"s/^/\\\"/; s/$/\\\"/\")]); Pkg.precompile()\"; then\n"," echo \"Failed to install some packages. Please check the output for details.\"\n"," fi\n","\n"," # Install kernel and rename it to \"julia\"\n"," echo \"Installing IJulia kernel...\"\n"," if ! julia -e 'using Pkg; Pkg.add(\"IJulia\"); using IJulia; IJulia.installkernel(\"julia\", env=Dict(\"JULIA_NUM_THREADS\"=>\"'\"$JULIA_NUM_THREADS\"'\"))'; then\n"," echo \"Failed to install IJulia kernel. Check your internet connection and try again.\"\n"," exit 1\n"," fi\n","\n"," KERNEL_DIR=`julia -e \"using IJulia; print(IJulia.kerneldir())\"`\n"," KERNEL_NAME=`ls -d \"$KERNEL_DIR\"/julia*`\n"," if ! mv -f $KERNEL_NAME \"$KERNEL_DIR\"/julia; then\n"," echo \"Failed to rename kernel. Check if you have sufficient permissions.\"\n"," exit 1\n"," fi\n","\n"," echo ''\n"," echo \"Successfully installed Julia $JULIA_VERSION with the specified packages!\"\n"," echo \"Please reload this page (press Ctrl+R, ⌘+R, or the F5 key) then\"\n"," echo \"select 'Julia' from the kernel dropdown menu to start using Julia.\"\n","else\n"," echo \"Julia is already installed. Version: `julia -v`\"\n"," echo \"Updating packages...\"\n"," if ! julia -e \"using Pkg; Pkg.add([$(echo $JULIA_PACKAGES | sed \"s/ /\\\", \\\"/g\" | sed \"s/^/\\\"/; s/$/\\\"/\")]); Pkg.update(); Pkg.precompile()\"; then\n"," echo \"Failed to update some packages. Please check the output for details.\"\n"," fi\n","fi"]},{"cell_type":"markdown","source":["## After that, go to the right corner (small threefold pointing downwards) and change the runtime type to the julia kernel\n","\n","## In the nextline we just make sure that we have installed it"],"metadata":{"id":"97CtwiD6aaDT"}},{"cell_type":"code","source":["versioninfo()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"JFopKmJlYKMu","executionInfo":{"status":"ok","timestamp":1728281291097,"user_tz":-660,"elapsed":2462,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"0086e818-1779-435d-a238-fbd101e86706"},"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Julia Version 1.10.5\n","Commit 6f3fdf7b362 (2024-08-27 14:19 UTC)\n","Build Info:\n"," Official https://julialang.org/ release\n","Platform Info:\n"," OS: Linux (x86_64-linux-gnu)\n"," CPU: 2 × Intel(R) Xeon(R) CPU @ 2.20GHz\n"," WORD_SIZE: 64\n"," LIBM: libopenlibm\n"," LLVM: libLLVM-15.0.7 (ORCJIT, broadwell)\n","Threads: 2 default, 0 interactive, 1 GC (on 2 virtual cores)\n","Environment:\n"," LD_LIBRARY_PATH = /usr/local/nvidia/lib:/usr/local/nvidia/lib64\n"," JULIA_NUM_THREADS = 2\n"]}]},{"cell_type":"code","source":["# We install the package: - takes another minute :(\n","using Pkg\n","Pkg.add(url=\"https://github.com/maxreiss123/GEP_SBP_.git\")"],"metadata":{"id":"Ftv_eCIAYNgq","outputId":"481c1eb8-f067-4c92-ed35-4f8453138b9a","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1728281320427,"user_tz":-660,"elapsed":29332,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}}},"execution_count":2,"outputs":[{"output_type":"stream","name":"stderr","text":["\u001b[32m\u001b[1m Cloning\u001b[22m\u001b[39m git-repo `https://github.com/maxreiss123/GEP_SBP_.git`\n","\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/maxreiss123/GEP_SBP_.git`\n","\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General.toml`\n","\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n","\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.10/Project.toml`\n"," \u001b[90m[2f0a5bb0] \u001b[39m\u001b[92m+ JGep v0.1.0 `https://github.com/maxreiss123/GEP_SBP_.git#master`\u001b[39m\n","\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.10/Manifest.toml`\n"," \u001b[90m[2f0a5bb0] \u001b[39m\u001b[92m+ JGep v0.1.0 `https://github.com/maxreiss123/GEP_SBP_.git#master`\u001b[39m\n","\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n","\u001b[32m ✓ \u001b[39mJGep\n"," 1 dependency successfully precompiled in 7 seconds. 130 already precompiled.\n"]}]},{"cell_type":"code","source":["#Then we import everthing we need - add further libs if you would like to plot the result\n","using JGep\n","using DynamicExpressions\n","using OrderedCollections\n","using Random\n","\n","#If we want to reproduce our results\n","Random.seed!(1)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"sCWJL3G9bUuD","executionInfo":{"status":"ok","timestamp":1728281390542,"user_tz":-660,"elapsed":3071,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"7da72dc7-abcc-4ab9-ec3d-32e909a0c7db"},"execution_count":3,"outputs":[{"output_type":"execute_result","data":{"text/plain":["TaskLocalRNG()"]},"metadata":{},"execution_count":3}]},{"cell_type":"code","source":["#Create the utilized symbols: to make the algorithm fast in the backend, we fully tokenize the symbols to Int8 and assign an arity, meaning how many inputs a symbol can have\n","#The number of the symbols can be chosen arbitrarily, but should match there corresponding representation later on\n","\n","#Here we use:\n","#1:=+ which takes 2 arguments\n","#2:=* which takes 2 arguments\n","#3:=/ which takes 2 arguments\n","#4:=/ which takes 2 arguments\n","#5:=exp which takes 2 arguments\n","#\n","#6 x1 terminal takes 0 arguments\n","#7 x2 terminal takes 0 arguments\n","#8 2 terminal takes 0 arguments\n","#9 0 terminal takes 0 arguments\n","\n","utilized_syms = OrderedDict{Int8,Int8}(1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 1,6 => 0, 7 => 0, 8 => 0, 9 => 0)"],"metadata":{"id":"UndXHDmFb3-H","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1728281394629,"user_tz":-660,"elapsed":1232,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"ce7f412c-1251-4766-d9ee-01470b27897b"},"execution_count":4,"outputs":[{"output_type":"execute_result","data":{"text/plain":["OrderedDict{Int8, Int8} with 9 entries:\n"," 1 => 2\n"," 2 => 2\n"," 3 => 2\n"," 4 => 2\n"," 5 => 1\n"," 6 => 0\n"," 7 => 0\n"," 8 => 0\n"," 9 => 0"]},"metadata":{},"execution_count":4}]},{"cell_type":"code","source":["#Here we create a vector of symbols serving as the connection between the genes (+,*)\n","connection_syms = Int8[1, 2]"],"metadata":{"id":"8ILXrDIVfhYr","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1728281399100,"user_tz":-660,"elapsed":1206,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"26e07d73-14bb-4982-92d1-0736812de739"},"execution_count":5,"outputs":[{"output_type":"execute_result","data":{"text/plain":["2-element Vector{Int8}:\n"," 1\n"," 2"]},"metadata":{},"execution_count":5}]},{"cell_type":"code","source":["#Here, we need to create a mapping between our tokenisation and the symbols utilized for DynamicExpression.jl\n","#Mapping should corespond to the former defined symbols\n","\n","\n","operators = OperatorEnum(; binary_operators=[+, -, *, /], unary_operators=[exp])\n","\n","callbacks = Dict{Int8,Function}(\n"," 3 => (-),\n"," 4 => (/),\n"," 2 => (*),\n"," 1 => (+),\n"," 5 => (exp)\n",")\n","nodes = OrderedDict{Int8,Any}(\n"," 6 => Node{Float64}(feature=1),\n"," 7 => Node{Float64}(feature=2),\n"," 8 => 2,\n"," 9 => 0\n",")\n"],"metadata":{"id":"eh9_ESZEftH9","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1728281403420,"user_tz":-660,"elapsed":2037,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"da0ddcc2-6b29-4f4d-97bb-53f7e6da0b85"},"execution_count":6,"outputs":[{"output_type":"execute_result","data":{"text/plain":["OrderedDict{Int8, Any} with 4 entries:\n"," 6 => x1\n"," 7 => x2\n"," 8 => 2\n"," 9 => 0"]},"metadata":{},"execution_count":6}]},{"cell_type":"code","source":["#Here we define some hyperparameters for our method\n","\n","\n","gep_params = Dict{String, AbstractFloat}(\n"," \"one_point_cross_over_prob\" => 0.6,\n"," \"two_point_cross_over_prob\" => 0.5,\n"," \"mutation_prob\" => 1,\n"," \"mutation_rate\" => 0.05,\n"," \"dominant_fusion_prob\" => 0.1,\n"," \"dominant_fusion_rate\" => 0.2,\n"," \"rezessiv_fusion_prob\" => 0.1,\n"," \"rezessiv_fusion_rate\" => 0.2,\n"," \"fusion_prob\" => 0.0,\n"," \"fusion_rate\" => 0.0,\n"," \"inversion_prob\" => 0.1\n",")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"tei7VVLjgdHE","executionInfo":{"status":"ok","timestamp":1728281408428,"user_tz":-660,"elapsed":2584,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"2c6cdab6-5182-4963-ff2e-d3c4db31ba8c"},"execution_count":7,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Dict{String, AbstractFloat} with 11 entries:\n"," \"mutation_rate\" => 0.05\n"," \"dominant_fusion_prob\" => 0.1\n"," \"inversion_prob\" => 0.1\n"," \"dominant_fusion_rate\" => 0.2\n"," \"one_point_cross_over_prob\" => 0.6\n"," \"mutation_prob\" => 1.0\n"," \"rezessiv_fusion_rate\" => 0.2\n"," \"fusion_rate\" => 0.0\n"," \"rezessiv_fusion_prob\" => 0.1\n"," \"fusion_prob\" => 0.0\n"," \"two_point_cross_over_prob\" => 0.5"]},"metadata":{},"execution_count":7}]},{"cell_type":"markdown","source":["## We now define the data according to a function:\n","\n","$$\n","y = x_1^2 + 0.5 x_1x_2-2x_2^2\n","$$"],"metadata":{"id":"uhB9LsOzgmMb"}},{"cell_type":"code","source":["#Generate some data\n","x_data = randn(Float64, 2, 1000);\n","y_data = @. x_data[1,:] * x_data[1,:] + x_data[1,:] * x_data[2,:] - 2 * x_data[2,:] * x_data[2,:];"],"metadata":{"id":"_Yh3R71WglnI","executionInfo":{"status":"ok","timestamp":1728281428432,"user_tz":-660,"elapsed":320,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}}},"execution_count":9,"outputs":[]},{"cell_type":"code","source":["#Setting number of individuals\n","individuals = 1000\n","\n","#Setting number of epochs\n","epochs = 1000\n","\n","#Setting gene count\n","gene_count = 3\n","\n","#Setting head len\n","head_len = 5;\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"w5PoP0Exg8sm","executionInfo":{"status":"ok","timestamp":1728281433278,"user_tz":-660,"elapsed":658,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"8d3b4e54-b4a0-4462-ca35-800be43f3caf"},"execution_count":10,"outputs":[{"output_type":"execute_result","data":{"text/plain":["5"]},"metadata":{},"execution_count":10}]},{"cell_type":"code","source":["#running the algorithm by using an Mean-squared error\n","#employing conjugate gradient for the coefficients\n","#Setting Hall of fame to 1, which means we obtain a list with one element containing the best\n","\n","best=runGep(individuals, epochs,head_len,gene_count,\n"," utilized_syms,operators, callbacks, nodes, x_data,y_data, connection_syms, gep_params;\n"," loss_fun_str=\"mse\", opt_method_const=:cg, hof=1);"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"qm8h5e_7g6x6","executionInfo":{"status":"ok","timestamp":1728281455706,"user_tz":-660,"elapsed":12937,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"c2a35970-fad2-4552-c92c-25bb32995157"},"execution_count":11,"outputs":[{"output_type":"stream","name":"stderr","text":["\u001b[32mProgress: 100%|█████████████████████████████████████████████████████████████| Time: 0:00:05\u001b[39m\n"]}]},{"cell_type":"code","source":["#Showing the fitness and the function\n","@show string(best[1].fitness)\n","@show string(best[1].compiled_function)"],"metadata":{"id":"bfegYoZvZm34","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1728281472399,"user_tz":-660,"elapsed":659,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"c159bdda-d8e2-40e6-da41-ca2cf5c1d8c6"},"execution_count":12,"outputs":[{"output_type":"stream","name":"stdout","text":["string((best[1]).fitness) = \"1.5016484907627395e-31\"\n","string((best[1]).compiled_function) = \"(((x1 + ((0.0 - x2) - x2)) + 0.0) * x2) + (x1 * (0.0 + x1))\"\n"]},{"output_type":"execute_result","data":{"text/plain":["\"(((x1 + ((0.0 - x2) - x2)) + 0.0) * x2) + (x1 * (0.0 + x1))\""]},"metadata":{},"execution_count":12}]},{"cell_type":"code","source":[],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"bdW0q8WwiF0X","executionInfo":{"status":"ok","timestamp":1728280299389,"user_tz":-660,"elapsed":343,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"79818d64-0866-4f01-a43d-95e50508ec23"},"execution_count":10,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[32m\u001b[1mStatus\u001b[22m\u001b[39m `~/.julia/environments/v1.10/Project.toml`\n"," \u001b[90m[7073ff75] \u001b[39mIJulia v1.25.0\n"," \u001b[90m[2f0a5bb0] \u001b[39mJGep v0.1.0 `https://github.com/maxreiss123/GEP_SBP_.git#master`\n"]}]},{"cell_type":"code","source":[],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":315},"id":"b2drWwGniIL8","executionInfo":{"status":"error","timestamp":1728280610525,"user_tz":-660,"elapsed":362,"user":{"displayName":"Max Reissmann","userId":"18348963551816227661"}},"outputId":"55c1811c-adc4-41e7-f86c-4dee609e7af2"},"execution_count":11,"outputs":[{"output_type":"error","ename":"LoadError","evalue":"ArgumentError: Package BenchmarkTools not found in current path.\n- Run `import Pkg; Pkg.add(\"BenchmarkTools\")` to install the BenchmarkTools package.","traceback":["ArgumentError: Package BenchmarkTools not found in current path.\n- Run `import Pkg; Pkg.add(\"BenchmarkTools\")` to install the BenchmarkTools package.","","Stacktrace:"," [1] macro expansion"," @ ./loading.jl:1772 [inlined]"," [2] macro expansion"," @ ./lock.jl:267 [inlined]"," [3] __require(into::Module, mod::Symbol)"," @ Base ./loading.jl:1753"," [4] #invoke_in_world#3"," @ ./essentials.jl:926 [inlined]"," [5] invoke_in_world"," @ ./essentials.jl:923 [inlined]"," [6] require(into::Module, mod::Symbol)"," @ Base ./loading.jl:1746"]}]},{"cell_type":"code","source":[],"metadata":{"id":"fprwJXMDjUEz"},"execution_count":null,"outputs":[]}]}

0 commit comments

Comments
 (0)