MOEAD算法中均匀权向量的实现---Python

MOEAD算法不详细介绍了,网上一大堆:
在这里插入图片描述
在这里插入图片描述
这里主要介绍lameda权重向量的均匀分布的实现,2维,3维也许姑且可以手动计算,但是任意维任意大小的均匀分布向量怎么实现?网上几乎没有现成例子,好不容易发现网上只有这个博客有大致介绍了c++的实现,资料少的可怜,我依据这个写了个Python实现,以我所见这是第一个实现这个功能的Python代码,另外的价值在于我在后面给出了均匀向量的matplotlib可视化展示代码:

import numpy as np


class Mean_vector:
    # 对m维空间,目标方向个数H
    def __init__(self, H=5, m=3):
        self.H = H
        self.m = m
        self.stepsize = 1 / H

    def perm(self, sequence):
        # !!! 序列全排列,且无重复
        l = sequence
        if (len(l) <= 1):
            return [l]
        r = []
        for i in range(len(l)):
            if i != 0 and sequence[i - 1] == sequence[i]:
                continue
            else:
                s = l[:i] + l[i + 1:]
                p = self.perm(s)
                for x in p:
                    r.append(l[i:i + 1] + x)
        return r

    def get_mean_vectors(self):
    #生成权均匀向量
        H = self.H
        m = self.m
        sequence = []
        for ii in range(H):
            sequence.append(0)
        for jj in range(m - 1):
            sequence.append(1)
        ws = []

        pe_seq = self.perm(sequence)
        for sq in pe_seq:
            s = -1
            weight = []
            for i in range(len(sq)):
                if sq[i] == 1:
                    w = i - s
                    w = (w - 1) / H
                    s = i
                    weight.append(w)
            nw = H + m - 1 - s
            nw = (nw - 1) / H
            weight.append(nw)
            if weight not in ws:
                ws.append(weight)
        return ws

    def save_mv_to_file(self, mv, name='out.csv'):
    #保存为csv
        f = np.array(mv, dtype=np.float64)
        np.savetxt(fname=name, X=f)

    def test(self):
    #测试
        m_v = self.get_mean_vectors()
        self.save_mv_to_file(m_v, 'test.csv')
# mv = Mean_vector(30, 3)
# mv.test()

更加详细的原理参考上面那个博客原理介绍

最后我给出了基于生成文件的matplotlib的展示:
3维空间每个方向30个:
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = np.loadtxt('test.csv')
print(data.shape[0])

fig = plt.figure()
ax = Axes3D(fig)

x, y, z = data[:, 0], data[:, 1], data[:, 2]

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

ax.scatter(x, y, z, marker='.', s=50, label='',color='r')

VecStart_x = np.zeros(data.shape[0])
VecStart_y = np.zeros(data.shape[0])
VecStart_z = np.zeros(data.shape[0])
VecEnd_x = data[:, 0]
VecEnd_y = data[:, 1]
VecEnd_z = data[:, 2]

for i in range(VecStart_x.shape[0]):
    ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i], VecEnd_y[i]], zs=[VecStart_z[i], VecEnd_z[i]])

plt.show()

最后展示生成文件效果如下:
在这里插入图片描述

另外代码有个MOEAD的python实现,里面有完整的流程:
https://blog.csdn.net/jiang425776024/article/details/84635353

posted @ 2018-11-26 10:50  jj千寻  阅读(982)  评论(0编辑  收藏  举报