{ "cells": [ { "cell_type": "markdown", "id": "87f95752", "metadata": {}, "source": [ "# Using MOSES to evolve programs\n", "\n", "This Jupyter notebook shows how [MOSES](https://github.com/opencog/asmoses) can be used to evolve programs and [mevis](https://pypi.org/project/mevis) can be used to visualize the results. The .ipynb file can be found [here](https://github.com/robert-haas/mevis/tree/master/examples).\n", "\n", "Sources:\n", "\n", "- OpenCog Wiki\n", " - Python console: https://wiki.opencog.org/w/Python\n", " - MOSES: [Meta-Optimizing Semantic Evolutionary Search](https://wiki.opencog.org/w/Meta-Optimizing_Semantic_Evolutionary_Search)\n", "- GitHub\n", " - [asmoses](https://github.com/opencog/asmoses) with [examples/asmoses.py]((https://github.com/opencog/asmoses/blob/master/examples/asmoses.py))" ] }, { "cell_type": "code", "execution_count": null, "id": "bbf943e2", "metadata": {}, "outputs": [], "source": [ "from opencog.asmoses.pyasmoses import moses\n", "\n", "import mevis as mv" ] }, { "cell_type": "code", "execution_count": null, "id": "d99d8bb8", "metadata": {}, "outputs": [], "source": [ "moses = moses()" ] }, { "cell_type": "markdown", "id": "c0bff851", "metadata": {}, "source": [ "## Define training data\n", "\n", "A list of input-output pairs\n", "\n", "- Input: first two columns\n", "- Output: last column" ] }, { "cell_type": "code", "execution_count": null, "id": "7022fd3a", "metadata": {}, "outputs": [], "source": [ "input_data = [\n", " [0, 0, 0],\n", " [1, 1, 0],\n", " [1, 0, 1],\n", " [0, 1, 1],\n", "]\n", "\n", "print('Training data: {}'.format(input_data))" ] }, { "cell_type": "markdown", "id": "6abf9c43", "metadata": {}, "source": [ "## Evolve Python programs" ] }, { "cell_type": "code", "execution_count": null, "id": "b3c25db6", "metadata": {}, "outputs": [], "source": [ "output = moses.run(input=input_data, python=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "6d418422", "metadata": {}, "outputs": [], "source": [ "best_result = output[0]\n", "program_text = best_result.program.decode()\n", "\n", "print('Score:', best_result.score)\n", "print('\\nPython program:\\n', program_text)" ] }, { "cell_type": "code", "execution_count": null, "id": "43c83bb9", "metadata": {}, "outputs": [], "source": [ "model = output[0].eval\n", "\n", "print('Testing model on data:')\n", "print('[0, 0]: {}'.format(model([0, 0])))\n", "print('[0, 1]: {}'.format(model([0, 1])))\n", "print('[1, 0]: {}'.format(model([1, 0])))\n", "print('[1, 1]: {}'.format(model([1, 1])))" ] }, { "cell_type": "markdown", "id": "dfe31179", "metadata": {}, "source": [ "## Evolve Scheme programs" ] }, { "cell_type": "code", "execution_count": null, "id": "a291fd1c", "metadata": {}, "outputs": [], "source": [ "output = moses.run(input=input_data, scheme=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "d77715a3", "metadata": {}, "outputs": [], "source": [ "best_result = output[0]\n", "program_text = best_result.program.decode()\n", "\n", "print('Score:', best_result.score)\n", "print('\\nScheme program:\\n', program_text)" ] }, { "cell_type": "markdown", "id": "22767aaa", "metadata": {}, "source": [ "## Evolve a complex Scheme program and put it into an AtomSpace" ] }, { "cell_type": "code", "execution_count": null, "id": "421bbf34", "metadata": {}, "outputs": [], "source": [ "from opencog.atomspace import AtomSpace\n", "from opencog.scheme import scheme_eval\n", "from opencog.utilities import set_default_atomspace" ] }, { "cell_type": "code", "execution_count": null, "id": "0c1054b1", "metadata": {}, "outputs": [], "source": [ "input_data = [\n", " [0, 0, 0, 0, 0, 1],\n", " [0, 0, 0, 0, 1, 0],\n", " [0, 0, 0, 1, 0, 0],\n", " [0, 0, 0, 1, 1, 0],\n", " [0, 0, 1, 0, 0, 1],\n", " [0, 0, 1, 0, 1, 0],\n", " [0, 0, 1, 1, 0, 1],\n", " [0, 0, 1, 1, 1, 1],\n", " [0, 1, 0, 0, 0, 0],\n", " [0, 1, 0, 0, 1, 1],\n", " [0, 1, 0, 1, 0, 0],\n", " [0, 1, 0, 1, 1, 1],\n", " [0, 1, 1, 0, 0, 1],\n", " [0, 1, 1, 0, 1, 1],\n", " [0, 1, 1, 1, 0, 0],\n", " [0, 1, 1, 1, 1, 0],\n", " [1, 0, 0, 0, 0, 1],\n", " [1, 0, 0, 0, 1, 1],\n", " [1, 0, 0, 1, 0, 0],\n", " [1, 0, 0, 1, 1, 1],\n", " [1, 0, 1, 0, 0, 0],\n", " [1, 0, 1, 0, 1, 1],\n", " [1, 0, 1, 1, 0, 0],\n", " [1, 0, 1, 1, 1, 0],\n", " [1, 1, 0, 0, 0, 1],\n", " [1, 1, 0, 0, 1, 1],\n", " [1, 1, 0, 1, 0, 0],\n", " [1, 1, 0, 1, 1, 1],\n", " [1, 1, 1, 0, 0, 0],\n", " [1, 1, 1, 0, 1, 1],\n", " [1, 1, 1, 1, 0, 1],\n", " [1, 1, 1, 1, 1, 0],\n", "]\n", "\n", "output = moses.run(input=input_data, scheme=True)\n", "best_result = output[0]\n", "\n", "score = best_result.score\n", "program_text = best_result.program.decode()\n", "\n", "print('Score:', best_result.score)\n", "print('\\nPython program:\\n', program_text)" ] }, { "cell_type": "code", "execution_count": null, "id": "d44d7019", "metadata": {}, "outputs": [], "source": [ "atomspace = AtomSpace()\n", "set_default_atomspace(atomspace)\n", "\n", "string = scheme_eval(atomspace, program_text)\n", "print(string.decode())" ] }, { "cell_type": "code", "execution_count": null, "id": "da0a5654", "metadata": {}, "outputs": [], "source": [ "mv.plot(atomspace, 'vis', 'dot')" ] }, { "cell_type": "code", "execution_count": null, "id": "b3a3c8db", "metadata": {}, "outputs": [], "source": [ "mv.store(atomspace, 'moses_result.scm', overwrite=True)\n", "\n", "graph = mv.convert(atomspace)\n", "mv.export(graph, 'moses_result.gml', overwrite=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }