1. 程式人生 > >MOEAD演算法中均勻權向量的實現---Python

MOEAD演算法中均勻權向量的實現---Python

MOEAD演算法不詳細介紹了,網上一大堆:
在這裡插入圖片描述
在這裡插入圖片描述
這裡主要介紹lameda權重向量的均勻分佈的實現,2維,3維也許姑且可以手動計算,但是任意維任意大小的均勻分佈向量怎麼實現?這個部落格有大致介紹c++的實現,我依據這個寫了個Python實現:
`

class Mean_vector:
def __init__(self, H=5, m=3):
    #m維空間,目標方向個數H
    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'):
    #儲存權向量為檔案,方便以後執行測試
    out = open(file=name, mode='w')
    for i in range(len(mv)):
        al = []
        for j in range(len(mv[i])):
            al.append(str(mv[i][j]) + ',')
        al.append('\n')
        out.writelines(al)
    out.close()
    
#---------測試執行-------------
def test(self):
    m_v = self.get_mean_vectors()
    self.save_mv_to_file(m_v, 'test.csv')

`
更加詳細的原理參考上面那個部落格原理介紹