{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Survival Driven Development\n", "===========================\n", "\n", "Survival Driven Development (SDD) is the newest software development fad. In this development framework, you specify what the software is supposed to do, then randomly generate source code to fulfill these requirements. Most of these attempts will fail, but hopefully one will succeed.\n", "\n", "Your task is to use SDD to make a function to approximate `x**2 + x`.\n", "\n", "Hint 1: Randomly generate lambda functions using a restricted vocabulary of source fragments.
\n", "`vocab = ['x', 'x', ' ', '+', '-', '*', '/', '1', '2', '3']`\n", "\n", "Hint 2: Only evaluate `x` at a small-ish number of values and save the difference between those answers and the true value of `x**2 + x`.\n", "\n", "Hint 3: SDD is error prone. Be sure to catch your errors!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "vocab = ['x', 'x', ' ', '+', '-', '*', '/', '1', '2', '3']\n", "n_chars = 10 # how many characters to try\n", "n_tries = 10000 # how many trials before we give up\n", "\n", "x = numpy.arange(-3, 3, 0.4)\n", "known_y = x**2 + x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def build_exp(voc, n_chars):\n", " \"\"\"Build a (str) expression of n_chars from the vocabular list voc\"\"\"\n", " exp = ''\n", " for n in range(n_chars):\n", " i = random.randint(0, len(voc)-1)\n", " exp += voc[i]\n", " return exp" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_err(y, Y):\n", " \"\"\"Compute the aggregate error between y and Y\"\"\"\n", " sq = numpy.abs(y - Y)\n", " return numpy.sum(sq)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "err = None\n", "exp = None\n", "exp_log = []\n", "TOL = 1.0e-6\n", "\n", "for n in range(n_tries):\n", " exp = build_exp(vocab, n_chars)\n", "\n", " # build a string to define a lambda function\n", " temp = 'f = lambda x: ' + exp\n", " temp += '\\n'\n", " # evaluate the new function with argument x, store the result as rez\n", " temp += 'rez = f(x)'\n", "\n", " try:\n", " exec(temp)\n", " err = get_err(known_y, rez)\n", " except Exception as e:\n", " # failed to compute function or error. move on to test try\n", " continue\n", "\n", " exp_log.append((err, exp))\n", "\n", " if err < TOL:\n", " print('success')\n", " break\n", "else:\n", " print('failure')\n", "\n", "exp_log = sorted(exp_log)\n", "best_err, best_exp = exp_log[0]\n", "\n", "print('best error', err)\n", "print('best exp ', best_exp)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }