多目標演算法

多目標演算法

在本筆記本中,我們將使用參考向量引導進化演算法 (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())

執行範例:在 DTLZ2 問題上執行 RVEA

以下程式碼用於設定 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(algo, prob, monitor)
compiled_step = torch.compile(workflow.step)

設定完成後,我們現在可以開始進行最佳化。我們設定讓多目標演算法在這個問題上最佳化 100 步。

# Run the workflow for 100 steps
t = time.time()
workflow.init_step()
for i in range(100):
    compiled_step()
    fit = workflow.algorithm.fit
    fit = fit[~torch.isnan(fit).any(dim=1)]
    if i % 10 == 0:
        print(igd(fit, pf))
monitor.plot()