From 21b91cd4e8570787a24d11855156d80c64908f33 Mon Sep 17 00:00:00 2001 From: youainti Date: Thu, 23 Sep 2021 08:16:50 -0700 Subject: [PATCH] Changed to have launches as a global function --- Code/connect_transition_to_optimality.ipynb | 168 +++++++++++--------- 1 file changed, 90 insertions(+), 78 deletions(-) diff --git a/Code/connect_transition_to_optimality.ipynb b/Code/connect_transition_to_optimality.ipynb index 74455b9..015dc22 100644 --- a/Code/connect_transition_to_optimality.ipynb +++ b/Code/connect_transition_to_optimality.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "variable-telephone", + "id": "behavioral-session", "metadata": { "tags": [] }, @@ -16,7 +16,7 @@ }, { "cell_type": "markdown", - "id": "minimal-quarterly", + "id": "vocational-rover", "metadata": {}, "source": [ "# Setup Functions\n", @@ -26,7 +26,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "instructional-guide", + "id": "physical-guidance", "metadata": {}, "outputs": [], "source": [ @@ -73,78 +73,20 @@ }, { "cell_type": "markdown", - "id": "spread-humor", + "id": "difficult-drinking", "metadata": {}, "source": [ - "## Setup functions related to the problem" + "# functions related to transitions" ] }, { "cell_type": "code", "execution_count": 3, - "id": "supposed-chorus", + "id": "beautiful-northwest", "metadata": {}, "outputs": [], "source": [ - "### Classes\n", - "class States():\n", - " \"\"\"\n", - " This class represents the state variables associated with the problems.\n", - " \n", - " In this problem, the two types of states are constellation stocks and debris.\n", - " \n", - " I'm not sure how useful it will be. We'll see.\n", - " \"\"\"\n", - " def __init__(self, satellite_stock, debris):\n", - " self.stock = satellite_stock\n", - " self.debris = debris\n", - " \n", - " \n", - "\n", - "### functions\n", - "\n", - "def survival(stock, debris):\n", - " \"\"\"\n", - " This is a basic, deterministic survival function. \n", - " It is based on the CDF of an exponential distribution.\n", - " \"\"\"\n", - " #TODO: ACTUALLY DERIVE A SURVIVAL FUNCTION. THIS IS JUST A PLACEHOLDER. PROBABLY SHOULD BE AN EXPONENTIAL DISTRIBUTION\n", - " return 1 - torch.exp(-SCALING * stock - debris)\n", - "\n", - "def test_launch(stock, debris):\n", - " \"\"\"\n", - " Temporary launch function \n", - " \"\"\"\n", - " return torch.ones(5, requires_grad=True)\n", - "\n", - "def laws_of_motion(stock, debris, launches):\n", - " \"\"\"\n", - " This function updates state variables (stock and debris), according \n", - " to the laws of motion.\n", - " \n", - " It returns the state variables as \n", - " \"\"\"\n", - " \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", - " \n", - " return (new_stock, new_debris)\n", - "\n", - "#This is not a good specification of the profit function, but it will work for now.\n", - "def profit(stock, debris):\n", - " return UTIL_WEIGHTS @ stock" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "analyzed-drink", - "metadata": {}, - "outputs": [], - "source": [ - "def single_transition(item_to_iterate, laws_motion, profit, launch, stocks, debris ):\n", + "def single_transition(item_to_iterate, laws_motion, profit, stocks, debris ):\n", " \"\"\"\n", " This function represents the inverted envelope conditions.\n", " It allows us to describe the derivatives of the value function evaluated at time $t+1$ in terms based in time period $t$.\n", @@ -159,7 +101,7 @@ " #it consists of the derivative of the laws of motion with respect to stocks and debris\n", " \n", " #Get the jacobian\n", - " a = jacobian(laws_motion, (stocks,debris,launch))\n", + " a = jacobian(laws_motion, (stocks,debris))\n", " \n", " #Reassemble the Jacobian nested tuples into the appropriate tensor\n", " A = BETA * torch.cat((torch.cat((a[0][0],a[0][1]),dim=1),torch.cat((a[1][0],a[1][1]),dim=1)), dim=0)\n", @@ -183,10 +125,10 @@ " \"\"\"\n", " \"\"\"\n", " #unpack states and functions\n", - " stocks, debris,profit, launch, laws_motion, item_to_transition = data_in\n", + " stocks, debris,profit, laws_motion, item_to_transition = data_in\n", " \n", " #Calculate new states\n", - " new_stocks, new_debris = laws_motion(stocks,debris,launch)\n", + " new_stocks, new_debris = laws_motion(stocks,debris)\n", " \n", " #WARNING: RECURSION: You may break your head...\n", " #This gets the transition of the value function derivatives over time.\n", @@ -195,19 +137,89 @@ " #functions\n", " laws_motion, \n", " profit, \n", - " launch, #TODO: reimplement with launch as a function\n", " stocks, debris #states\n", " )\n", " \n", " #collects the data back together for return, including the updated state variables\n", - " data_out = new_stocks, new_debris, profit, launch, laws_motion, transitioned\n", + " data_out = new_stocks, new_debris, profit, laws_motion, transitioned\n", " \n", " return data_out" ] }, { "cell_type": "markdown", - "id": "martial-station", + "id": "entertaining-theorem", + "metadata": {}, + "source": [ + "## Setup functions related to the problem" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "modern-kentucky", + "metadata": {}, + "outputs": [], + "source": [ + "### Classes\n", + "class States():\n", + " \"\"\"\n", + " This class represents the state variables associated with the problems.\n", + " \n", + " In this problem, the two types of states are constellation stocks and debris.\n", + " \n", + " I'm not sure how useful it will be. We'll see. It is missing a lot\n", + " \"\"\"\n", + " def __init__(self, satellite_stock, debris):\n", + " self.stock = satellite_stock\n", + " self.debris = debris\n", + " \n", + " \n", + "\n", + "### functions\n", + "\n", + "def survival(stock, debris):\n", + " \"\"\"\n", + " This is a basic, deterministic survival function. \n", + " It is based on the CDF of an exponential distribution.\n", + " \"\"\"\n", + " #SURVIVAL FUNCTION BASED ON AN EXPONENTIAL DISTRIBUTION\n", + " return 1 - torch.exp(-SCALING * stock - debris)\n", + "\n", + "def test_launch(stock, debris):\n", + " \"\"\"\n", + " Temporary launch function \n", + " \"\"\"\n", + " return torch.ones(5, requires_grad=True)\n", + "\n", + "def laws_of_motion(stock, debris):\n", + " \"\"\"\n", + " This function updates state variables (stock and debris), according \n", + " to the laws of motion.\n", + " \n", + " It returns the state variables as \n", + " \"\"\"\n", + " l = launches(stock,debris)\n", + " #Notes: Launches is a global function.\n", + " s = survival(stock,debris)\n", + " #Notes: Survival is a global function.\n", + " \n", + " new_stock = stock*s + l\n", + " \n", + " \n", + " #TODO: Currently Ignoring autocatalysis\n", + " new_debris = (1-DELTA)*debris + LAUNCH_DEBRIS_RATE * l.sum() + COLLISION_DEBRIS_RATE*(1-s) @ stock\n", + " \n", + " return (new_stock, new_debris)\n", + "\n", + "#This is not a good specification of the profit function, but it will work for now.\n", + "def profit(stock, debris):\n", + " return UTIL_WEIGHTS @ stock" + ] + }, + { + "cell_type": "markdown", + "id": "broadband-technique", "metadata": {}, "source": [ "# Actual calculations" @@ -216,7 +228,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "ambient-breakfast", + "id": "adjustable-harvey", "metadata": {}, "outputs": [], "source": [ @@ -231,10 +243,10 @@ "debris = torch.tensor([2.2],requires_grad=True)\n", "\n", "#CHANGE LATER: Launch is currently a value, should be a function (i.e. neural network)\n", - "launch = torch.ones(5, requires_grad=True)\n", + "launches = test_launch\n", "\n", "#compose the functions together.\n", - "base_data = (stocks,debris, profit, launch, laws_of_motion, torch.ones(6, requires_grad=True))\n", + "base_data = (stocks,debris, profit, laws_of_motion, torch.ones(6, requires_grad=True))\n", "\n", "#Parameters\n", "SCALING = torch.ones(5)\n", @@ -248,7 +260,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "deluxe-remains", + "id": "cordless-wages", "metadata": {}, "outputs": [ { @@ -287,12 +299,12 @@ "#Get the values from 5 transitions\n", "for f in compose_recursive_functions(transition_wrapper,5):\n", " result = f(base_data)\n", - " print(result[5], \"\\n\"*3)" + " print(result[4], \"\\n\"*3)" ] }, { "cell_type": "markdown", - "id": "parallel-classroom", + "id": "shaped-zambia", "metadata": {}, "source": [ "Also, maybe I can create a `Model` class that upon construction will capture the necesary constants, functions, etc.\n" @@ -301,7 +313,7 @@ { "cell_type": "code", "execution_count": null, - "id": "copyrighted-freeze", + "id": "canadian-excitement", "metadata": {}, "outputs": [], "source": []