Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Multi-Objective Algorithm examples page #178

Merged
merged 6 commits into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions docs/source/example/moalg.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multi-Objective Algorithm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we will use the Reference Vector Guided Evolutionary Algorithm (**RVEA**) to find the optimal solutions of the **DTLZ2** problem."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import torch\n",
"from torch.profiler import ProfilerActivity, profile\n",
"\n",
"from evox.algorithms import RVEA\n",
"from evox.metrics import igd\n",
"from evox.problems.numerical import DTLZ2\n",
"from evox.workflows import StdWorkflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (Optional) Use GPU to run the code\n",
"We often prefer to run our code on a GPU for faster execution. However, if a GPU is unavailable, running on a CPU is also acceptable."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cuda:0\n"
]
}
],
"source": [
"# Use GPU first to run the code.\n",
"torch.set_default_device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"print(torch.get_default_device())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running example: RVEA on DTLZ2 problem\n",
"The following code is used to set up the DTLZ2 problem and the RVEA algorithm. More information about the problem and algorithm can be found in the corresponding section of the documentation."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Init the problem, algorithm and workflow.\n",
"prob = DTLZ2(m=3)\n",
"pf = prob.pf()\n",
"algo = RVEA(pop_size=100, n_objs=3, lb=-torch.zeros(12), ub=torch.ones(12))\n",
"workflow = StdWorkflow()\n",
"workflow.setup(algo, prob)\n",
"workflow.init_step()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this setup in place, we can now start to optimize. We set to let the multi-objective algorithm optimize for 100 steps on this problem"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(0.4746, device='cuda:0')\n",
"tensor(0.1650, device='cuda:0')\n",
"tensor(0.0902, device='cuda:0')\n",
"tensor(0.0653, device='cuda:0')\n",
"tensor(0.0586, device='cuda:0')\n",
"tensor(0.0564, device='cuda:0')\n",
"tensor(0.0559, device='cuda:0')\n",
"tensor(0.0551, device='cuda:0')\n",
"tensor(0.0546, device='cuda:0')\n",
"tensor(0.0544, device='cuda:0')\n"
]
}
],
"source": [
"# Run the workflow for 100 steps\n",
"t = time.time()\n",
"with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], record_shapes=True, profile_memory=True) as prof:\n",
" for i in range(100):\n",
" workflow.step()\n",
" fit = workflow.algorithm.fit\n",
" fit = fit[~torch.isnan(fit).any(dim=1)]\n",
" if i % 10 == 0:\n",
" print(igd(fit, pf))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Optional) If needed, you can use the following code to get more information"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# print(prof.key_averages().table())\n",
"# torch.cuda.synchronize()\n",
"# print(time.time() - t)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading