単純な強化学習実装シナリオ−学習オートマチックに基づくリンク予測モデル

7022 ワード

学習を強化する.
    (  :Reinforcement learning,  RL)           ,           ,           。                 ,                     ,          ,               。         ,             ,     、   、   、   、    、       、    、         。               ,       “      ”(approximate dynamic programming,ADP)。                ,                    ,           。         ,                        。

        ,               (MDP),                        。                  ,       MDP   ,                MDP。

                    ,            /   ,              。             ,     (      )   (    )      。      “  -  ”   ,           MDP      。

学習オートマトン
                       ,                    。               ,                        。

         ,                。          π,             。                。


リンク予測
        (Link Prediction)                                                。             (exist yet unknown links)            (future links)   。                           。



学習オートマチックに基づくリンク予測モデルの実現
import numpy as npimport timefrom random import choice import pandas as pdimport os
共通隣接指標を計算する方法を定義する
define some functions to calculate some baseline index
'''def Cn(MatrixAdjacency):
Matrix_similarity = np.dot(MatrixAdjacency,MatrixAdjacency)
return Matrix_similarity

'''
Jaccard類似性指標の計算
def Jaccavrd(MatrixAdjacency_Train):

Matrix_similarity = np.dot(MatrixAdjacency_Train,MatrixAdjacency_Train)

deg_row = sum(MatrixAdjacency_Train)
deg_row.shape = (deg_row.shape[0],1)
deg_row_T = deg_row.T
tempdeg = deg_row + deg_row_T
temp = tempdeg - Matrix_similarity

Matrix_similarity = Matrix_similarity / temp
return Matrix_similarity

Salton指標の計算方法の定義
def Salton_Cal(MatrixAdjacency_Train):
similarity = np.dot(MatrixAdjacency_Train,MatrixAdjacency_Train)

deg_row = sum(MatrixAdjacency_Train)
deg_row.shape = (deg_row.shape[0],1)
deg_row_T = deg_row.T
tempdeg = np.dot(deg_row,deg_row_T)
temp = np.sqrt(tempdeg)

np.seterr(divide='ignore', invalid='ignore')
Matrix_similarity = np.nan_to_num(similarity / temp)

print np.isnan(Matrix_similarity)
Matrix_similarity = np.nan_to_num(Matrix_similarity)
print np.isnan(Matrix_similarity)
return Matrix_similarity


Katz 1指標の計算方法の定義
def Katz_Cal(MatrixAdjacency):
#α  
Parameter = 0.01
Matrix_EYE = np.eye(MatrixAdjacency.shape[0])
Temp = Matrix_EYE - MatrixAdjacency * Parameter

Matrix_similarity = np.linalg.inv(Temp)

Matrix_similarity = Matrix_similarity - Matrix_EYE
return Matrix_similarity

ローカルパスLP類似性指標を計算する方法を定義する
'''def LP_Cal(MatrixAdjacency):
Matrix_similarity = np.dot(MatrixAdjacency,MatrixAdjacency)

Parameter = 0.05
Matrix_LP = np.dot(np.dot(MatrixAdjacency,MatrixAdjacency),MatrixAdjacency) * Parameter

Matrix_similarity = np.dot(Matrix_similarity,Matrix_LP)
return Matrix_similarity

'''
リソース割当類似性指標の計算
def RA(MatrixAdjacency_Train):
RA_Train = sum(MatrixAdjacency_Train)
RA_Train.shape = (RA_Train.shape[0],1)
MatrixAdjacency_Train_Log = MatrixAdjacency_Train / RA_Train
MatrixAdjacency_Train_Log = np.nan_to_num(MatrixAdjacency_Train_Log)

Matrix_similarity = np.dot(MatrixAdjacency_Train,MatrixAdjacency_Train_Log)
return Matrix_similarity

ランダム環境1:アクティブなノードペア
def RandomEnviromentForActive(MatrixAdjacency,i,j):
Index = np.random.randint(1, 5)
print(Index)
global IndexName
if Index == 1:
    IndexName = '      :Jaccard Index'
    print(IndexName)
    similarity_matrix = Jaccavrd(MatrixAdjacency)
    similarity = similarity_matrix[i,j]
elif Index == 2:
    IndexName = '      :Salton Index'
    print(IndexName)
    similarity_matrix = Salton_Cal(MatrixAdjacency)
    similarity = similarity_matrix[i,j]
elif Index == 3:
    IndexName = '      :Katz Index'
    print(IndexName)
    similarity_matrix = Katz_Cal(MatrixAdjacency)
    similarity = similarity_matrix[i,j]
else index == 4:
    IndexName = '      :RA Index'
    print(IndexName)
    similarity_matrix = RA(MatrixAdjacency)
    similarity = similarity_matrix[i,j]
return similarity 


ランダム環境2:主に非アクティブなノードペア
def RandomEnviromentForNonActive():
Action = np.random.randint(1, 4)
if Action == 1:
    ActionName = 'ID3'
    similarity_matrix = ID3_Cal(MatrixAdjacency)
    #similarity = similarity_matrix[i,j]
elif Action == 2:
    ActionName = 'CART'
    similarity_matrix = Cart_Cal(MatrixAdjacency)
    #similarity = similarity_matrix[i,j]
elif Action == 3:
    ActionName = 'C4.5'
    similarity_matrix = C4_Cal(MatrixAdjacency)
    #similarity = similarity_matrix[i,j]
return similarity

学習オートマトンの構築(To Construct the agent)
def ContructionAgent(filepath,n1,n2):
f = open(filepath)
lines = f.readlines()
A = np.zeros((50, 50), dtype=float)
A_row = 0
for line in lines:
    list = line.strip('
').split(' ') A[A_row:] = list[0:50] A_row += 1 # p1 p2 a = 0.05 b = 0.01 p1 =0.5 p2 =0.5 Action = 1 # 1 ‘Yes’, 2 ‘No’ for i in range(1): # global Action # (the threashhold_value of similarity) if (p1 >= p2): Action = 1 else: Action = 2 print(' :' + str(Action)) threshhold_value = 0.3 similarity = RandomEnviromentForActive(A, n1, n2) # p1 1'Yes' ,p2 2'No' # ‘Yes’, if (similarity > threshhold_value) and (Action == 1): p1 = p1 + a * (1 - p1) p2 = 1-p1 # p2 = (1 - a) * p2 # 'No', elif (similarity < threshhold_value) and (Action == 2): p2 = (1-a)*p2 p1 = 1-p2 # p1 = (1 - a) * p1 # ‘Yes’, elif (similarity < threshhold_value) and (Action == 1): p2 = 1-b*p2 p1 = 1-p2 #p2 = 1 - b * p2 # ‘No’, elif (similarity > threshhold_value) and (Action == 2): p1 = b + (1 - b) * (1 - p1) p2 = 1-p1 # p1 = 1 - b * p1 if (p1 >= p2): print(' :Yes') else: print(' :No') return p1, p2 import os

import pandas as pdimport numpy as nppath=r'../Data/itcmatrixs/36000/'result = np.zeros((50, 50))for i in os.walk(path):
#print(i)
#print(type(i))
for m in range(50):
    for n in range(50):
        r = None
        for j in range(26):
            datapath = path+i[2][j]
            p1,p2 = ContructionAgent(datapath,m,n)
            r = int(p1>=p2)
        result[m,n] = r;

r.save('result.npy') pass
ノート冊
ソースコードとデータセットが必要な場合は[email protected]あ、ご注目ありがとうございます.