[TSP] TSP solution based on matlab particle swarm algorithm Hopfield [including Matlab source code 224]

1. Introduction

1 Hopfield neural network
Insert picture description here
2 Discrete Hopfield network
Insert picture description here
Insert picture description here
3 Continuous Hopfield network
Insert picture description here
4
CHNN is described by nonlinear differential equations. The stability of the network is described by constructing its energy function (also known as Lyapunov function), and using Lyapunov second stability Theorems make judgments.
Explanation:
(1) The Lyapunov function is not unique;
(2) If the Lyapunov function of the network cannot be found, the network cannot be proved to be unstable;
(3) There is currently no unified Lyapunov search Function method;
(4) Using energy function method to study the stability of the network is not mathematically rigorous.
If the objective function of an optimization problem is converted into the energy function of the network, and the variables of the problem correspond to the state of the network, then the Hopfield neural network can be used to solve the optimization combination problem.
The general steps for applying Hopfield neural network to solve optimization calculation problems are:
(1) Analyze the problem: the network output corresponds to the solution of the problem;
(2) Construct the network energy function: make the minimum value corresponding to the best solution of the problem;
(3) Design the network structure: design the network parameters from the energy function and network stability conditions to obtain the dynamic equation;
(4) MATLAB software simulation.

Second, the source code

%% 连续Hopfield神经网络的优化—旅行商问题优化计算
% function main
%% 清空环境变量、定义全局变量
clc % 清屏
clear all; % 删除workplace变量
close all; % 关掉显示图形窗口
global A D
%% 导入城市位置
load city_location
%% 计算相互城市间距离
distance=dist(citys,citys');
%% 初始化网络
N=size(citys,1);
A=200;
D=100;
U0=0.1;
step=0.0001;
delta=2*rand(N,N)-1;
U=U0*log(N-1)+delta;
V=(1+tansig(U/U0))/2;
iter_num=10000;
E=zeros(1,iter_num);
%% 寻优迭代
for k=1:iter_num  
    % 动态方程计算
    dU=diff_u(V,distance);
    % 输入神经元状态更新
    U=U+dU*step;
    % 输出神经元状态更新
    V=(1+tansig(U/U0))/2;
    % 能量函数计算
    e=energy(V,distance);
    E(k)=e;  
end
 %% 判断路径有效性
[rows,cols]=size(V);
V1=zeros(rows,cols);
[V_max,V_ind]=max(V);
for j=1:cols
    V1(V_ind(j),j)=1;
end
C=sum(V1,1);
R=sum(V1,2);
flag=isequal(C,ones(1,N)) & isequal(R',ones(1,N));
%% 结果显示
if flag==1
   % 计算初始路径长度
   sort_rand=randperm(N);
   citys_rand=citys(sort_rand,:);
   Length_init=dist(citys_rand(1,:),citys_rand(end,:)');
   for i=2:size(citys_rand,1)
       Length_init=Length_init+dist(citys_rand(i-1,:),citys_rand(i,:)');
   end
   % 绘制初始路径
   figure(1)
   plot([citys_rand(:,1);citys_rand(1,1)],[citys_rand(:,2);citys_rand(1,2)],'o-')
   for i=1:length(citys)
       text(citys(i,1),citys(i,2),['   ' num2str(i)])
   end
   text(citys_rand(1,1),citys_rand(1,2),['       起点' ])
   text(citys_rand(end,1),citys_rand(end,2),['       终点' ])
   title(['优化前路径(长度:' num2str(Length_init) ')'])
   axis([0 1 0 1])
   grid on
   xlabel('城市位置横坐标')
   ylabel('城市位置纵坐标')
   % 计算最优路径长度
   [V1_max,V1_ind]=max(V1);
   citys_end=citys(V1_ind,:);
   Length_end=dist(citys_end(1,:),citys_end(end,:)');
   for i=2:size(citys_end,1)
       Length_end=Length_end+dist(citys_end(i-1,:),citys_end(i,:)');
   end
   disp('最优路径矩阵');V1
   % 绘制最优路径
   figure(2)
   plot([citys_end(:,1);citys_end(1,1)],...
       [citys_end(:,2);citys_end(1,2)],'o-')
   for i=1:length(citys)
       text(citys(i,1),citys(i,2),['  ' num2str(i)])
   end
   text(citys_end(1,1),citys_end(1,2),['       起点' ])
   text(citys_end(end,1),citys_end(end,2),['       终点' ])
   title(['优化后路径(长度:' num2str(Length_end) ')'])
   axis([0 1 0 1])
   grid on
   xlabel('城市位置横坐标')
   ylabel('城市位置纵坐标')
   % 绘制能量函数变化曲线
   figure(3)
   plot(1:iter_num,E);
   ylim([0 2000])
   title(['能量函数变化曲线(最优能量:' num2str(E(end)) ')']);
   xlabel('迭代次数');
   ylabel('能量函数');
else
   disp('寻优路径无效');
end
%% 连续Hopfield神经网络的优化—旅行商问题优化计算
% function TSP_hopfield()
%% 清空环境变量、定义全局变量
clc % 清屏
clear all; % 删除workplace变量
close all; % 关掉显示图形窗口
% step 1
A=1.5;
D=1;
u0=0.02;
step=0.01;
% step 2
N=8;
DistanceCity=dist(citys,citys');
% step 3
u=2*rand(N,N)-1;
U=0.5*u0*log(N-1)+u;
V=(1+tanh(U/u0))/2;
end

Three, running results

Insert picture description here
Insert picture description here

Four, remarks

Complete code or writing add QQ1564658423 past review
>>>>>>
[prediction model] lssvm prediction based on matlab particle swarm [including Matlab source code 103]
[lSSVM prediction] based on matlab whale optimization algorithm lSSVM data prediction [including Matlab Source code 104]
[lstm prediction] Improved lstm prediction based on matlab whale optimization algorithm [including Matlab source code 105]
[SVM prediction] Improved SVM prediction based on matlab bat algorithm (1) [Containing Matlab source code 106]
[ SVM prediction 】Based on matlab gray wolf algorithm to optimize svm support vector machine prediction [including Matlab source code 107]
[Prediction model] based on matlab BP neural network prediction [including Matlab source code 108]
[lssvm prediction model] based on bat algorithm improved least squares Support vector machine lssvm prediction [Matlab 109 issue]
[lssvm prediction] Least squares support vector machine lssvm prediction based on moth extinguishing algorithm improved [Matlab 110]
[SVM prediction] Improved SVM prediction based on matlab bat algorithm (two ) [Include Matlab source code 141 period]

[Lssvm prediction] improved least squares support vector machine lssvm prediction based on matlab moth fire fighting algorithm [including Matlab source code 142]
[ANN prediction model] based on matlab difference algorithm to improve ANN network prediction [including Matlab source code 151]
[ Prediction model] based on matlab RBF neural network prediction model [including Matlab source code 177 period]
[Prediction model] based on matlab SVM regression prediction algorithm to predict stock trends [including Matlab source code 180 period]
[prediction model] based on matlab BP neural network model optimization Prediction [Contains Matlab source code 221]
[Prediction model] Data prediction based on matlab RLS algorithm [Contains Matlab source code 222]
[Prediction model] Optimal prediction of coal consumption based on matlab carbon emission constraints [Contains Matlab source code 223]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/113693450