{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "STjnrIezcb-Z"
   },
   "source": [
    "## Python Intrepreter Setting Up\n",
    "\n",
    "Install a minicode to make our project use python3.8. Because of our package dependancy, our project is running on python3.8 currently. However, the mechanism does not support to change the defaulted python3.10 kernel to other python kernels.  Therefore, we need make python3.8 by ourself. \n",
    "\n",
    "The method is by installing a miniconda, and setting the python kerynel by commd. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "X-opVmkBTLkD"
   },
   "outputs": [],
   "source": [
    "!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh\n",
    "!chmod +x mini.sh\n",
    "!bash ./mini.sh -b -f -p /usr/local\n",
    "!conda install -q -y jupyter\n",
    "!conda install -q -y google-colab -c conda-forge \n",
    "!python -m ipykernel install --name \"py38\" --user"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uA_9x6OQdVxy"
   },
   "source": [
    "##  Installing required packages. \n",
    "\n",
    "+ Protobuf is to fix a feature on google colab. \n",
    "+ marllib, our main package \n",
    "+ **ray[rllib], gym**, because of the kernel changed in google colab, we need installed this package explicitly. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "A66iDxycqcv1"
   },
   "outputs": [],
   "source": [
    "!/usr/local/bin/pip install protobuf==3.20.0 marllib==1.0.3 ray[rllib]==1.8 gym==0.20.0 numpy==1.22"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-UNjYXyhd6SL"
   },
   "source": [
    "## Persist sourcode to a file\n",
    "\n",
    "If we run ```import xxxx``` directly in google colab, the google colab sitll will call its default kernel. \n",
    "\n",
    "Therefore, we need to use ```usr/local/bin/python3.8```, which was installed in the previous step, to run our program by given a file. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "aP7hpAIDknYH"
   },
   "outputs": [],
   "source": [
    "example_usage = \"\"\"\n",
    "from marllib import marl\n",
    "\n",
    "# prepare env\n",
    "env = marl.make_env(environment_name=\"mpe\", map_name=\"simple_spread\")\n",
    "\n",
    "# initialize algorithm with appointed hyper-parameters\n",
    "mappo = marl.algos.mappo(hyperparam_source=\"mpe\")\n",
    "\n",
    "# build agent model based on env + algorithms + user preference\n",
    "model = marl.build_model(env, mappo, {\"core_arch\": \"mlp\", \"encode_layer\": \"128-256\"})\n",
    "\n",
    "# start training\n",
    "mappo.fit(env, model, stop={'episode_reward_mean': 2000, 'timesteps_total': 10000000}, local_mode=False, num_gpus=0,\n",
    "          num_workers=1, share_policy='group', checkpoint_freq=50)\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "with open(\"marllib_script.py\", \"w\") as file:\n",
    "    file.write(example_usage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "onnpnBuWegfH"
   },
   "source": [
    "## settig environement variable to make python runnable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "xQ5H__7CoCR1"
   },
   "outputs": [],
   "source": [
    "!export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8uJHY6-TenG4"
   },
   "source": [
    "## Run our program finally "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "background_save": true
    },
    "id": "UU48CcI5nwby",
    "outputId": "ad70acfc-f954-454c-ba16-88fe88e5f8b6"
   },
   "outputs": [],
   "source": [
    "!/usr/local/bin/python3.8 marllib_script.py"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
