From 3e8b8f5284b784c157f5e732eb6b4e150e989cc7 Mon Sep 17 00:00:00 2001 From: youainti Date: Wed, 8 Sep 2021 16:14:38 -0700 Subject: [PATCH] reviewed basic neural net (minor change) and changed the destruction (satellite loss) function in successfull recursion --- Code/BasicNeuralNet.ipynb | 20 ++--- Code/successful_recursion.ipynb | 149 ++++++++++++++++++++++++++------ 2 files changed, 132 insertions(+), 37 deletions(-) diff --git a/Code/BasicNeuralNet.ipynb b/Code/BasicNeuralNet.ipynb index eb17260..553e84c 100644 --- a/Code/BasicNeuralNet.ipynb +++ b/Code/BasicNeuralNet.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 16, - "id": "3867293b-8f5e-426a-a223-834c7e04daef", + "id": "heavy-turkish", "metadata": {}, "outputs": [], "source": [ @@ -13,7 +13,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "e73a6d0e-4db9-4b0d-ab8f-e421373f1944", + "id": "ordered-disease", "metadata": {}, "outputs": [], "source": [ @@ -37,7 +37,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2870086a-cb32-4f7e-9ee3-2e7a545c86cb", + "id": "quiet-syria", "metadata": {}, "outputs": [], "source": [ @@ -58,7 +58,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "fb6c7507-5abf-428e-8644-d0e42525368d", + "id": "fleet-letter", "metadata": {}, "outputs": [], "source": [ @@ -68,7 +68,7 @@ { "cell_type": "code", "execution_count": 15, - "id": "b3244863-39f7-4fa6-a284-059b858569cf", + "id": "provincial-crime", "metadata": {}, "outputs": [ { @@ -89,7 +89,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "3480a0ec-7b5b-46d7-bdd2-631e57be9c85", + "id": "particular-response", "metadata": {}, "outputs": [], "source": [ @@ -99,7 +99,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "e1ace0be-9b05-4c77-b076-a05b68cf8f51", + "id": "passing-termination", "metadata": {}, "outputs": [ { @@ -120,7 +120,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "464ae8e4-f71e-494f-9845-fefe5c75a7b1", + "id": "public-speaking", "metadata": {}, "outputs": [ { @@ -172,7 +172,7 @@ { "cell_type": "code", "execution_count": null, - "id": "04bca0f6-dfb9-4013-ac2a-f4efd2f93c15", + "id": "original-warehouse", "metadata": {}, "outputs": [], "source": [] @@ -194,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.5" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/Code/successful_recursion.ipynb b/Code/successful_recursion.ipynb index 845a8c2..0ada24c 100644 --- a/Code/successful_recursion.ipynb +++ b/Code/successful_recursion.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "sustained-board", + "id": "closed-glenn", "metadata": { "tags": [] }, @@ -16,7 +16,7 @@ }, { "cell_type": "markdown", - "id": "numeric-victoria", + "id": "naval-ivory", "metadata": {}, "source": [ "# Setup Functions\n", @@ -26,7 +26,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "virtual-arlington", + "id": "italian-enforcement", "metadata": {}, "outputs": [], "source": [ @@ -39,17 +39,22 @@ " return lambda *args: f(g(*args))\n", "\n", "def compose_recursive_functions(fn,n):\n", + " #takes a function fn and composes it with itself n times\n", + " #Returns each of the iterations of the functions.\n", + " \n", " #Set base conditions\n", " out_func = None\n", " out_func_list =[]\n", "\n", " #build the compositions of functions\n", " for f in itertools.repeat(fn, n):\n", + " #if first iteration\n", " if out_func == None:\n", " out_func = f\n", " else:\n", " out_func = compose(f,out_func)\n", "\n", + " #append the ou\n", " out_func_list.append(out_func)\n", " \n", " return out_func_list" @@ -57,7 +62,7 @@ }, { "cell_type": "markdown", - "id": "wrapped-message", + "id": "fancy-tucson", "metadata": {}, "source": [ "## Setup functions related to the problem" @@ -65,8 +70,8 @@ }, { "cell_type": "code", - "execution_count": 3, - "id": "neutral-vietnamese", + "execution_count": 15, + "id": "outside-arrangement", "metadata": {}, "outputs": [], "source": [ @@ -77,17 +82,18 @@ " #Gompertz distribution for simplicity\n", " #commonly used with saturation\n", " #TODO: ACTUALLY DERIVE A SURVIVAL FUNCTION. THIS IS JUST A PLACEHOLDER. PROBABLY SHOULD BE AN EXPONENTIAL DISTRIBUTION\n", - " eta = 1.0/(SCALING@stock)\n", - " b = 1/debris\n", " \n", - " return 1 - ( b*eta*torch.exp(eta+b*stock-eta*torch.exp(b*stock)))\n", + " #eta = 1.0/(SCALING@stock)\n", + " #b = 1/debris\n", + " #return 1 - ( b*eta*torch.exp(eta+b*stock-eta*torch.exp(b*stock)))\n", + " return 1 - torch.exp(-SCALING * stock-debris)\n", "\n", "def test_launch(stock, debris):\n", " return torch.ones(5, requires_grad=True)\n", "\n", "def laws_of_motion(stock, debris, launches):\n", " \n", - " new_stock = stock*survival(stock,debris) + launches(stock,debris) #TODO: Launches will become a function (neural network)\n", + " new_stock = stock*survival(stock,debris) + launches#(stock,debris) #TODO: Launches will become a function (neural network)\n", " \n", " #TODO: Currently Ignoring autocatalysis\n", " new_debris = (1-DELTA)*debris + LAUNCH_DEBRIS_RATE * launches.sum() + COLLISION_DEBRIS_RATE*(1-survival(stock,debris)) @ stock\n", @@ -101,8 +107,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "considered-configuration", + "execution_count": 16, + "id": "romance-generation", "metadata": {}, "outputs": [], "source": [ @@ -119,7 +125,7 @@ "# This function wraps the single transition and handles updating dates etc.\n", "def transition_wrapper(data_in):\n", " #unpack states and functions\n", - " stocks, debris,profit, launch, laws_motion,item_to_transition = data_in\n", + " stocks, debris,profit, launch, laws_motion, item_to_transition = data_in\n", " \n", " #Calculate new states\n", " new_stocks, new_debris = laws_motion(stocks,debris,launch)\n", @@ -128,7 +134,10 @@ " #This gets the transition of the value function derivatives over time.\n", " transitioned = single_transition(\n", " item_to_transition, #item to iterate, i.e. the derivatives of the value function\n", - " laws_motion, profit, launch, #functions #TODO: reimplement with launch as a function\n", + " #functions\n", + " laws_motion, \n", + " profit, \n", + " launch, #TODO: reimplement with launch as a function\n", " stocks, debris #states\n", " )\n", " \n", @@ -140,7 +149,7 @@ }, { "cell_type": "markdown", - "id": "premium-lesbian", + "id": "fluid-parks", "metadata": {}, "source": [ "# Actual calculations" @@ -148,8 +157,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "id": "suffering-google", + "execution_count": 17, + "id": "changing-january", "metadata": {}, "outputs": [], "source": [ @@ -180,32 +189,118 @@ }, { "cell_type": "code", - "execution_count": 10, - "id": "checked-medication", + "execution_count": 19, + "id": "dominant-boost", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "tensor([-0.1543, 1.3888, 1.1316, 1.1316, 1.1553], grad_fn=)\n", - "tensor([-1.2150, 1.6724, 1.1912, 1.1912, 1.2161], grad_fn=)\n", - "tensor([-2.3316, 1.9710, 1.2539, 1.2539, 1.2801], grad_fn=)\n", - "tensor([nan, nan, nan, nan, nan], grad_fn=)\n", - "tensor([nan, nan, nan, nan, nan], grad_fn=)\n" + "tensor([0.0000, 1.2632, 1.0526, 1.0526, 1.0892], grad_fn=)\n", + "tensor([-0.9519, 1.3928, 1.0020, 1.0020, 1.0575], grad_fn=)\n", + "tensor([-1.8565, 1.5150, 0.9530, 0.9530, 0.9882], grad_fn=)\n", + "tensor([-2.8103, 1.6872, 0.9376, 0.9376, 0.9474], grad_fn=)\n", + "tensor([-3.8626, 1.9131, 0.9505, 0.9505, 0.9408], grad_fn=)\n", + "tensor([-5.0235, 2.1830, 0.9819, 0.9819, 0.9598], grad_fn=)\n", + "tensor([-6.2860, 2.4869, 1.0247, 1.0247, 0.9951], grad_fn=)\n", + "tensor([-7.6403, 2.8175, 1.0746, 1.0746, 1.0403], grad_fn=)\n", + "tensor([-9.0802, 3.1712, 1.1293, 1.1293, 1.0918], grad_fn=)\n", + "tensor([-10.6035, 3.5462, 1.1879, 1.1879, 1.1477],\n", + " grad_fn=)\n", + "tensor([-12.2108, 3.9422, 1.2501, 1.2501, 1.2075],\n", + " grad_fn=)\n", + "tensor([-13.9045, 4.3597, 1.3157, 1.3157, 1.2708],\n", + " grad_fn=)\n", + "tensor([-15.6882, 4.7995, 1.3849, 1.3849, 1.3375],\n", + " grad_fn=)\n", + "tensor([-17.5662, 5.2625, 1.4577, 1.4577, 1.4079],\n", + " grad_fn=)\n", + "tensor([-19.5432, 5.7500, 1.5345, 1.5345, 1.4820],\n", + " grad_fn=)\n", + "tensor([-21.6244, 6.2631, 1.6152, 1.6152, 1.5600],\n", + " grad_fn=)\n", + "tensor([-23.8151, 6.8033, 1.7002, 1.7002, 1.6421],\n", + " grad_fn=)\n", + "tensor([-26.1212, 7.3719, 1.7897, 1.7897, 1.7285],\n", + " grad_fn=)\n", + "tensor([-28.5486, 7.9704, 1.8839, 1.8839, 1.8195],\n", + " grad_fn=)\n", + "tensor([-31.1038, 8.6004, 1.9831, 1.9831, 1.9152],\n", + " grad_fn=)\n", + "tensor([-33.7934, 9.2636, 2.0874, 2.0874, 2.0160],\n", + " grad_fn=)\n", + "tensor([-36.6247, 9.9617, 2.1973, 2.1973, 2.1221],\n", + " grad_fn=)\n", + "tensor([-39.6049, 10.6965, 2.3129, 2.3129, 2.2338],\n", + " grad_fn=)\n", + "tensor([-42.7420, 11.4700, 2.4347, 2.4347, 2.3514],\n", + " grad_fn=)\n", + "tensor([-46.0442, 12.2842, 2.5628, 2.5628, 2.4751],\n", + " grad_fn=)\n", + "tensor([-49.5202, 13.1413, 2.6977, 2.6977, 2.6054],\n", + " grad_fn=)\n", + "tensor([-53.1792, 14.0435, 2.8397, 2.8397, 2.7425],\n", + " grad_fn=)\n", + "tensor([-57.0307, 14.9931, 2.9891, 2.9891, 2.8869],\n", + " grad_fn=)\n", + "tensor([-61.0850, 15.9928, 3.1465, 3.1465, 3.0388],\n", + " grad_fn=)\n", + "tensor([-65.3526, 17.0450, 3.3121, 3.3121, 3.1988],\n", + " grad_fn=)\n", + "tensor([-69.8449, 18.1526, 3.4864, 3.4864, 3.3671],\n", + " grad_fn=)\n", + "tensor([-74.5736, 19.3186, 3.6699, 3.6699, 3.5443],\n", + " grad_fn=)\n", + "tensor([-79.5511, 20.5459, 3.8630, 3.8630, 3.7309],\n", + " grad_fn=)\n", + "tensor([-84.7907, 21.8378, 4.0664, 4.0664, 3.9272],\n", + " grad_fn=)\n", + "tensor([-90.3060, 23.1976, 4.2804, 4.2804, 4.1339],\n", + " grad_fn=)\n", + "tensor([-96.1115, 24.6291, 4.5057, 4.5057, 4.3515],\n", + " grad_fn=)\n", + "tensor([-102.2227, 26.1359, 4.7428, 4.7428, 4.5805],\n", + " grad_fn=)\n", + "tensor([-108.6555, 27.7220, 4.9924, 4.9924, 4.8216],\n", + " grad_fn=)\n", + "tensor([-115.4268, 29.3916, 5.2552, 5.2552, 5.0754],\n", + " grad_fn=)\n", + "tensor([-122.5545, 31.1490, 5.5318, 5.5318, 5.3425],\n", + " grad_fn=)\n", + "tensor([-130.0574, 32.9990, 5.8229, 5.8229, 5.6237],\n", + " grad_fn=)\n", + "tensor([-137.9552, 34.9463, 6.1294, 6.1294, 5.9197],\n", + " grad_fn=)\n", + "tensor([-146.2686, 36.9961, 6.4520, 6.4520, 6.2313],\n", + " grad_fn=)\n", + "tensor([-155.0196, 39.1538, 6.7916, 6.7916, 6.5592],\n", + " grad_fn=)\n", + "tensor([-164.2312, 41.4251, 7.1490, 7.1490, 6.9044],\n", + " grad_fn=)\n", + "tensor([-173.9275, 43.8158, 7.5253, 7.5253, 7.2678],\n", + " grad_fn=)\n", + "tensor([-184.1343, 46.3325, 7.9213, 7.9213, 7.6503],\n", + " grad_fn=)\n", + "tensor([-194.8782, 48.9815, 8.3382, 8.3382, 8.0530],\n", + " grad_fn=)\n", + "tensor([-206.1876, 51.7701, 8.7771, 8.7771, 8.4768],\n", + " grad_fn=)\n", + "tensor([-218.0922, 54.7053, 9.2391, 9.2391, 8.9230],\n", + " grad_fn=)\n" ] } ], "source": [ "#calculate results for first 5 iterations\n", - "for f in compose_recursive_functions(transition_wrapper,5):\n", + "for f in compose_recursive_functions(transition_wrapper,50):\n", " result = f(base_data)\n", " print(result[5])" ] }, { "cell_type": "markdown", - "id": "wanted-principal", + "id": "unnecessary-architect", "metadata": {}, "source": [ "Note how this fails on the last few iterations.\n", @@ -220,7 +315,7 @@ { "cell_type": "code", "execution_count": null, - "id": "solid-correlation", + "id": "varying-organization", "metadata": {}, "outputs": [], "source": []