Multi-Objective Algorithm#

In this notebook, we will use the Reference Vector Guided Evolutionary Algorithm (RVEA) to find the optimal solutions of the DTLZ2 problem.

import time
import torch

from evox.algorithms import RVEA
from evox.metrics import igd
from evox.problems.numerical import DTLZ2
from evox.workflows import StdWorkflow, EvalMonitor

(Optional) Use GPU to run the code#

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.

# Use GPU first to run the code.
torch.set_default_device("cuda" if torch.cuda.is_available() else "cpu")
print(torch.get_default_device())
cuda:0

Running example: RVEA on DTLZ2 problem#

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.

# Init the problem, algorithm and workflow.
prob = DTLZ2(m=3)
pf = prob.pf()
algo = RVEA(pop_size=100, n_objs=3, lb=-torch.zeros(12), ub=torch.ones(12))
monitor = EvalMonitor()
workflow = StdWorkflow()
workflow.setup(algo, prob, monitor)
workflow.init_step()

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

# Run the workflow for 100 steps
t = time.time()
for i in range(100):
    workflow.step()
    fit = workflow.algorithm.fit
    fit = fit[~torch.isnan(fit).any(dim=1)]
    if i % 10 == 0:
        print(igd(fit, pf))
/opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/functional.py:1478: UserWarning: Attempting to use hipBLASLt on an unsupported architecture! Overriding blas backend to hipblas (Triggered internally at ../aten/src/ATen/Context.cpp:296.)
  return _VF.cdist(x1, x2, p, None)  # type: ignore[attr-defined]
tensor(0.5008, device='cuda:0')
tensor(0.1702, device='cuda:0')
tensor(0.0918, device='cuda:0')
tensor(0.0658, device='cuda:0')
tensor(0.0581, device='cuda:0')
tensor(0.0561, device='cuda:0')
tensor(0.0555, device='cuda:0')
tensor(0.0551, device='cuda:0')
tensor(0.0546, device='cuda:0')
tensor(0.0544, device='cuda:0')
workflow.get_submodule("monitor").plot()