Skip to content

Commit

Permalink
Update evox install check for colab
Browse files Browse the repository at this point in the history
  • Loading branch information
BillHuang2001 committed Jan 11, 2025
1 parent f904326 commit e63f614
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
28 changes: 18 additions & 10 deletions docs/source/example/moalg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand All @@ -34,8 +44,6 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import time\n",
"\n",
"import torch\n",
Expand Down Expand Up @@ -339,7 +347,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -353,7 +361,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
14 changes: 7 additions & 7 deletions docs/source/guide/user/1-start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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()"
Expand Down Expand Up @@ -128,7 +128,7 @@
" algorithm=algorithm,\n",
" problem=problem,\n",
" monitor=monitor,\n",
" )"
")"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down
56 changes: 28 additions & 28 deletions docs/source/guide/user/2-problems.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
")"
]
},
{
Expand Down Expand Up @@ -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()"
]
},
Expand Down Expand Up @@ -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}\")"
]
},
{
Expand Down

0 comments on commit e63f614

Please sign in to comment.