多目标算法#
在这个笔记本中,我们将使用参考向量引导演化算法(RVEA)来寻找 DTLZ2 问题的最优解。
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
(可选)使用GPU运行代码#
我们通常更喜欢在GPU上运行代码以加快执行速度。然而,如果没有GPU,使用CPU运行也是可以接受的。
# 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
运行示例:RVEA 在 DTLZ2 问题上#
以下代码用于设置 DTLZ2
问题和 RVEA
算法。关于该问题和算法的更多信息可以在文档的相应部分找到。
# 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()
在这个设置完成后,我们现在可以开始优化。我们设置让多目标算法在这个问题上优化100步。
# 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()