多目标优化ZDT测试函数的python实现

1. ZDT测试函数(ZDT1-4, 6)

均为双目标函数,无约束条件,函数表达形式、变量可行域如下:

函数介绍和最优情况下的变量值在多目标优化的ZDT基准问题详细介绍(最全概括)_小怪兽会微笑的博客-CSDN博客_zdt1

中有比较详细的介绍,不过在ZDT4问题中变量可行域不同,但不影响优化结果

2. ZDT函数的python实现

def zdt1(x):
    n = len(x)
    f1 = x[0]
    g = 1 + 9/(n - 1) * sum(x[1:-1], x[-1])
    h = 1 - np.sqrt(f1 / g)
    f2 = g * h
    return [f1, f2]

def zdt2(x):
    n = len(x)
    f1 = x[0]
    g = 1 + 9/(n - 1) * sum(x[1:-1], x[-1])
    h = 1 - (f1 / g) ** 2
    f2 = g * h
    return [f1, f2]

def zdt3(x):
    n = len(x)
    f1 = x[0]
    g = 1 + 9/(n - 1) * sum(x[1:-1], x[-1])
    h = 1 - np.sqrt(f1 / g) - f1 / g * math.sin(10 * math.pi * f1)
    f2 = g * h
    return [f1, f2]

def zdt4(x):
    n = len(x)
    f1 = x[0]
    g = 1 + 10 * (n - 1)
    for i in range(1, n):
        g = g + x[i] ** 2 - 10 * math.cos(4 * math.pi * x[i])
    h = 1 - np.sqrt(f1 / g)
    f2 = g * h
    return [f1, f2]

def zdt6(x):
#    n = len(x)
    f1 = 1 - math.exp(-4 * x[0]) * math.sin(6 * math.pi * x[0]) ** 6
    g = 1 + 9 * (sum(x[1:-1], x[-1])/9) ** 0.25
    h = 1 - (f1 / g) ** 2
    f2 = g * h
    return [f1, f2]

3. ZDT函数的pareto front

Emoobook - Appendix D

  • 5
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是一个用Python绘制多目标优化算法测试函数的例子,包括ZDT1和DTLZ2。你可以根据需要自行修改。 ```python import numpy as np import matplotlib.pyplot as plt # ZDT1测试函数 def zdt1(x): f1 = x[0] g = 1 + 9 / (len(x)-1) * np.sum(x[1:]) f2 = g * (1 - np.sqrt(f1/g)) return np.array([f1, f2]) # DTLZ2测试函数 def dtlz2(x, k=10): n = len(x) g = sum((xi-0.5)**2 for xi in x[k:]) theta = [np.pi/(4*(1+g))* (1+2*g*xi) for xi in x[:k-1]] f = [0.5*(1+g)] for i in range(k-1): fi = 0.5*(1+g)*np.prod([np.cos(theta[j]) for j in range(i)]) if i > 0: fi *= np.sin(theta[i-1]) f.append(fi) return np.array(f) # 绘制Pareto前沿 def plot_pareto_front(f): plt.scatter(f[:,0], f[:,1], c='b', marker='o') plt.xlabel('$f_1$') plt.ylabel('$f_2$') plt.title('Pareto Front') plt.show() # 绘制Pareto前沿和种群分布 def plot_pareto_front_and_population(f, x): plt.scatter(f[:,0], f[:,1], c='b', marker='o', label='Pareto Front') plt.scatter(x[:,0], x[:,1], c='r', marker='x', label='Population') plt.xlabel('$f_1$') plt.ylabel('$f_2$') plt.title('Pareto Front and Population') plt.legend() plt.show() # 在二维空间中绘制多个函数 def plot_functions_2d(f_list, label_list): x = np.linspace(0, 1, 100) y_list = [f(x) for f in f_list] for y, label in zip(y_list, label_list): plt.plot(x, y, label=label) plt.xlabel('$x$') plt.ylabel('$f(x)$') plt.legend() plt.show() # 测试函数 if __name__ == '__main__': # 测试ZDT1函数 x = np.linspace(0, 1, 100) f = np.array([zdt1([xi, 0]) for xi in x]) plot_pareto_front(f) # 测试DTLZ2函数 k = 10 x = np.random.random(size=(100, k)) f = np.array([dtlz2(xi) for xi in x]) plot_pareto_front_and_population(f, x) # 在二维空间中绘制多个函数 f_list = [np.sin, np.cos, lambda x: x**2, lambda x: np.exp(x)] label_list = ['$\sin(x)$', '$\cos(x)$', '$x^2$', '$\exp(x)$'] plot_functions_2d(f_list, label_list) ``` 这段代码中,我们定义了两个多目标优化算法测试函数ZDT1和DTLZ2,并且提供了绘制Pareto前沿和种群分布的函数`plot_pareto_front_and_population`和绘制二维函数的函数`plot_functions_2d`。在主程序中,我们分别测试了ZDT1和DTLZ2函数,并且使用`plot_pareto_front`和`plot_pareto_front_and_population`绘制了它们的Pareto前沿和种群分布。最后,我们使用`plot_functions_2d`绘制了四个二维函数。 希望这个例子能够帮助你实现自己的多目标优化算法测试函数
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值