{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "nearby-insulin",
   "metadata": {},
   "source": [
    "# Running a simulation in Symupy \n",
    "\n",
    "In order to run a simulation you need two main elements:\n",
    "\n",
    "- A symuflow library. Although it is already within the dependencies you may retrieve the simulator via the conda package `conda install -c licit-lab symuflow`. This will obtain the traffic simulator library. \n",
    "- An XML file with an scenario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "according-checkout",
   "metadata": {},
   "outputs": [],
   "source": [
    "from symupy.runtime.api import Simulator, Simulation \n",
    "import os\n",
    "\n",
    "bottleneck = os.path.abspath(\"bottleneck_001.xml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First create a `Simulator` object, the simulator object is in charge of establishing a comunication with `Symuflow`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aggressive-happening",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Simulator()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Attach the simulation file to a simulator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "rental-ethiopia",
   "metadata": {},
   "outputs": [],
   "source": [
    "s.register_simulation(bottleneck)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Launch your simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "technological-history",
   "metadata": {},
   "outputs": [],
   "source": [
    "s.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run step by step\n",
    "\n",
    "You may interact with the simulation by running a simulation step by step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Simulator()\n",
    "s.register_simulation(bottleneck)\n",
    "with s:\n",
    "    while s.do_next:\n",
    "        s.run_step()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "End of tutorial."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "4f497e4dc9908f743db5989ad8362838d95625f77ac7c14cc2cedc2e708606f5"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('symupy-doc': conda)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
