经济全球化程度日益增大,各国经济贸易往来频繁带动航运行业欣欣向荣,海域航道以及港口水域的负载增加,造成船舶交通阻塞、船舶碰撞事件事故频发等问题。因此,准确获取船舶航行实时动态信息,及时发现船舶异常轨迹情况并提前预测船舶的航行运动状态,成为国内外学者的研究热点。
随着AIS系统的普及,现如今收到的AIS数据远超预期,我国AIS系统大概每天收到AIS实时报文一亿条,数据量爆炸的同时蕴含很多对船舶轨迹预测和减少船舶碰撞有用的信息。AIS发送的信息包括水上移动通信业务标识码(Maritime Mobile Service identify,MMSI) ,船舶经纬度、船舶的对地航向和对地航速等。与雷达、声纳或闭路电视等传统海事方法相比,AIS具有许多优势,首先AIS消息几乎以实时方式提供了其主机的丰富信息,其次AIS数据可以在很长的距离内发送和接收(从机载收发器的20海里到卫星接收器的上千海里),且AIS受海洋和天气等外部因素的影响较小,对AIS数据的有效利用能对船舶轨迹预测有非常重要的现实意义。
参考如下
(8条消息) LSTM模型 轨迹经纬度预测_Muzi_Water的博客-CSDN博客_轨迹预测模型
'''train and test dataes''' ship_data1 = pd.read_csv('D:\船舶轨迹预测\服务器实验结果\ship trajectory prediction--can run/11_30_four_input.txt', sep=',', engine='python').iloc[:, 0:4].values dataset1 = ship_data1 train_size1 = int(len(dataset1)*0.8) test_size1 = len(dataset1)-train_size1 train1, test1 = dataset1[0:train_size1, :], dataset1[train_size1:len(dataset1), :] data1 = train1 ship_data2 = pd.read_csv('D:\船舶轨迹预测\服务器实验结果/3_18新增实验/ship1.txt', sep=',', engine='python').iloc[:, 0:4].values #data = pd.read_csv('D:/ship trajectory prediction--can run/11_30_four_input.txt', sep=',', engine='python') dataset2 = ship_data2 train_size2 = int(len(dataset2)*0.8) test_size2 = len(dataset2)-train_size2 train2, test2 = dataset2[0:train_size2, :], dataset2[train_size2:len(dataset2), :] data2 = train2 data = np.vstack((data1, data2))
def create_dataset(data, n_predictions, n_next): ''' 对数据进行处理 ''' dim = data.shape[1] # train_X是n个经纬度, train_Y是下一个(n+1)经纬度 train_X, train_Y = [], [] for i in range(data.shape[0] - n_predictions - n_next - 1): a = data[i:(i + n_predictions), :] train_X.append(a) tempb = data[(i + n_predictions):(i + n_predictions + n_next), :] b = [] for j in range(len(tempb)): for k in range(dim): b.append(tempb[j, k]) train_Y.append(b) train_X = np.array(train_X, dtype='float64') train_Y = np.array(train_Y, dtype='float64') return train_X, train_Y
def trainModel(train_X, train_Y): # trainX,trainY: 训练LSTM模型所需要的数据 model = Sequential() model.add(LSTM( 80,activation='tanh', input_shape=(train_X.shape[1], train_X.shape[2]), # 将输出维度传递给下一层LSTM输入 return_sequences=False)) # 1000个神经元大概300被置为0 model.add(Dropout(0.2)) # model.add(LSTM( # 120, # return_sequences=False)) # model.add(Dropout(0.3)) model.add(Dense( train_Y.shape[1])) model.add(Activation("tanh")) # 方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率评测标准 model.compile(loss='mse', optimizer='adam', metrics=['acc']) fig1 = plt.figure(figsize=(12, 8)) history = model.fit(train_X, train_Y, epochs=500, batch_size=16, verbose=2) plt.plot(history.history['loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.show() model.summary() return model
if __name__ == "__main__": train_num = 5 per_num = 1 # set_range = False set_range = True
# 生成训练数据 train_X, train_Y = create_dataset(data, train_num, per_num) # train_X, train_Y, test_X, test_Y = create_dataset(data, train_num, per_num) print("x\n", train_X.shape) print("y\n", train_Y.shape) # 训练模型 # train_X, train_Y放进去训练 model = trainModel(train_X, train_Y) loss, acc = model.evaluate(train_X, train_Y, verbose=2) print('Loss : {}, Accuracy: {}'.format(loss, acc * 100)) # 保存模型 np.save(".2022_03_18_lstm_model_trueNorm.npy", normalize) model.save(".2022_03_18_lstm_model.h5")