{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "standing-catch",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd.functional import jacobian\n",
    "import itertools\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "going-accident",
   "metadata": {},
   "outputs": [],
   "source": [
    "import combined as c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "severe-employment",
   "metadata": {},
   "source": [
    "So, this contains a bunch of initial tests of my abstractions. I eventually need to change these to assertions and package them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ranking-family",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Instantiate some objects\n",
    "pm = c.PhysicalModel(1.0,1e-6,0.01,2.0,1e-8)\n",
    "s = c.States(torch.tensor([1.0,2,3]), torch.tensor([0.0]))\n",
    "lp = c.LinearProfit(0,0.95,torch.tensor([1.0,0,0]), 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "premium-brisbane",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-7.5676e+05,  2.8893e+05,  4.6783e+05,  1.5236e+00]),\n",
       " <combined.States at 0x7f31f0146c10>)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.single_transition(pm,lp,s,est_int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "raised-worthy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-718920.5625,  274490.1562,  444444.6250])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "minimand, iterated_partials, iterated_state = c.optimality(pm,lp,s,est_int)\n",
    "minimand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "horizontal-insight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-2285563., -2285557., -2285557.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "est_int2 = c.EstimandInterface(iterated_partials,torch.ones(3))\n",
    "\n",
    "minimand2, iterated_partials2, iterated_state2 = c.optimality(pm,lp,iterated_state,est_int2)\n",
    "minimand2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "automatic-builder",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-2405858.5000, -2405852.5000, -2405852.5000])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "est_int3 = c.EstimandInterface(iterated_partials2,torch.ones(3))\n",
    "\n",
    "minimand3, iterated_partials3, iterated_state3 = c.optimality(pm,lp,iterated_state2,est_int3)\n",
    "minimand3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "changing-mainland",
   "metadata": {},
   "source": [
    "So, this succesfylly let me link the two. I'm going to move to another notebook, clean up, and start integrating the system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "assumed-midwest",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = DoubleNetwork(input_size = 5, output_size=5, layers_size=15)\n",
    "\n",
    "data_in = torch.tensor([1.5,2,3,4,5])\n",
    "\n",
    "data_in\n",
    "\n",
    "target = torch.zeros(5)\n",
    "\n",
    "def loss_fn2(output,target):\n",
    "    return sum((output[1] +output[0] - target)**2)\n",
    "    #could add a simplicity assumption i.e. l1 on parameters.\n",
    "\n",
    "#Prep Optimizer\n",
    "optimizer = torch.optim.SGD(model.parameters(),lr=0.01)\n",
    "\n",
    "for i in range(20):\n",
    "    #training loop\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    output = model.forward(data_in)\n",
    "    output\n",
    "\n",
    "    l = loss_fn2(output, target)\n",
    "\n",
    "    l.backward()\n",
    "\n",
    "    optimizer.step()\n",
    "\n",
    "    print(\"\\n\",l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "surprising-fundamentals",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "parliamentary-delta",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "emotional-castle",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "substantial-exhibit",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
