From e63f6144a7f9e65ffc229630ae1e93f1166e649e Mon Sep 17 00:00:00 2001 From: Bill Huang Date: Sat, 11 Jan 2025 20:38:44 +0800 Subject: [PATCH] Update evox install check for colab --- docs/source/example/moalg.ipynb | 28 ++++++++----- docs/source/guide/user/1-start.ipynb | 14 +++---- docs/source/guide/user/2-problems.ipynb | 56 ++++++++++++------------- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/docs/source/example/moalg.ipynb b/docs/source/example/moalg.ipynb index 78b14d781..7f7f8c194 100644 --- a/docs/source/example/moalg.ipynb +++ b/docs/source/example/moalg.ipynb @@ -18,14 +18,24 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31mRunning cells with '.venv (Python 3.12.7)' requires the ipykernel package.\n", + "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", + "\u001b[1;31mCommand: '/home/bill/Source/evox/.venv/bin/python -m pip install ipykernel -U --force-reinstall'" + ] + } + ], "source": [ "# install evox, skip it if you have already installed evox\n", - "try:\n", - " import evox\n", - "except ImportError:\n", - " !pip install --disable-pip-version-check --upgrade -q evox\n", - " import evox" + "from importlib.util import find_spec\n", + "\n", + "if find_spec(\"evox\") is None:\n", + " %pip install evox" ] }, { @@ -34,8 +44,6 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", - "import sys\n", "import time\n", "\n", "import torch\n", @@ -339,7 +347,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -353,7 +361,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/docs/source/guide/user/1-start.ipynb b/docs/source/guide/user/1-start.ipynb index 2448ae69a..55cbb9121 100644 --- a/docs/source/guide/user/1-start.ipynb +++ b/docs/source/guide/user/1-start.ipynb @@ -18,10 +18,10 @@ "outputs": [], "source": [ "# install evox, skip it if you have already installed evox\n", - "try:\n", - " import evox\n", - "except ImportError:\n", - " !pip install --disable-pip-version-check --upgrade -q evox" + "from importlib.util import find_spec\n", + "\n", + "if find_spec(\"evox\") is None:\n", + " %pip install evox" ] }, { @@ -76,7 +76,7 @@ " w=0.6,\n", " phi_p=2.5,\n", " phi_g=0.8,\n", - " )\n", + ")\n", "\n", "# Initiate a problem\n", "problem = Ackley()" @@ -128,7 +128,7 @@ " algorithm=algorithm,\n", " problem=problem,\n", " monitor=monitor,\n", - " )" + ")" ] }, { @@ -166,7 +166,7 @@ "start = time.time()\n", "\n", "# Run the workflow\n", - "for i in range(1,101):\n", + "for i in range(1, 101):\n", " workflow.step()\n", " if i % 10 == 0:\n", " run_time = time.time() - start\n", diff --git a/docs/source/guide/user/2-problems.ipynb b/docs/source/guide/user/2-problems.ipynb index f8e1d0088..db4a2affd 100644 --- a/docs/source/guide/user/2-problems.ipynb +++ b/docs/source/guide/user/2-problems.ipynb @@ -17,10 +17,10 @@ "outputs": [], "source": [ "# install evox, skip it if you have already installed evox\n", - "try:\n", - " import evox\n", - "except ImportError:\n", - " !pip install --disable-pip-version-check --upgrade -q evox brax" + "from importlib.util import find_spec\n", + "\n", + "if find_spec(\"evox\") is None:\n", + " %pip install evox" ] }, { @@ -294,22 +294,22 @@ "\n", "# Initialize the PSO, and you can also use any other algorithms\n", "algorithm = PSO(\n", - " pop_size=POP_SIZE,\n", - " lb=lower_bound,\n", - " ub=upper_bound,\n", - " device=device,\n", - " )\n", + " pop_size=POP_SIZE,\n", + " lb=lower_bound,\n", + " ub=upper_bound,\n", + " device=device,\n", + ")\n", "algorithm.setup()\n", "\n", "# Initialize the Brax problem\n", "problem = BraxProblem(\n", - " policy=model,\n", - " env_name=\"hopper\",\n", - " max_episode_length=1000,\n", - " num_episodes=3,\n", - " pop_size=POP_SIZE,\n", - " device=device,\n", - " )" + " policy=model,\n", + " env_name=\"hopper\",\n", + " max_episode_length=1000,\n", + " num_episodes=3,\n", + " pop_size=POP_SIZE,\n", + " device=device,\n", + ")" ] }, { @@ -343,11 +343,11 @@ } ], "source": [ - "#set an monitor, and it can record the top 3 best fitnesses\n", + "# set an monitor, and it can record the top 3 best fitnesses\n", "pop_monitor = EvalMonitor(\n", - " topk=3,\n", - " device=device,\n", - " )\n", + " topk=3,\n", + " device=device,\n", + ")\n", "pop_monitor.setup()" ] }, @@ -446,14 +446,14 @@ "\n", "# Run the worflow\n", "for index in range(max_generation):\n", - " print(f\"In generation {index}:\")\n", - " t = time.time()\n", - " workflow.step()\n", - " print(f\"\\tTime elapsed: {time.time() - t: .4f}(s).\")\n", - " monitor: EvalMonitor = workflow.get_submodule(\"monitor\")\n", - " print(f\"\\tTop fitness: {monitor.topk_fitness}\")\n", - " best_params = adapter.to_params(monitor.topk_solutions[0])\n", - " print(f\"\\tBest params: {best_params}\")" + " print(f\"In generation {index}:\")\n", + " t = time.time()\n", + " workflow.step()\n", + " print(f\"\\tTime elapsed: {time.time() - t: .4f}(s).\")\n", + " monitor: EvalMonitor = workflow.get_submodule(\"monitor\")\n", + " print(f\"\\tTop fitness: {monitor.topk_fitness}\")\n", + " best_params = adapter.to_params(monitor.topk_solutions[0])\n", + " print(f\"\\tBest params: {best_params}\")" ] }, {