📄 nn_nodes_optm.m
字号:
clc
clear
close all
%node NO.
N=4;
%load input
load d.mat
%network parameter
A=500;
B=500;
C=1500;
D=500;
u0=0.02;
lamda=0.0001;
%how many times to get the validity result
total=0;
%end signal
toend=0;
while toend==0
total=total+1
%2 weight matrix
V=rand(N,N);
U=atanh(2*V-1)*u0;
%refresh the statu
for renew=1:1:1000
%Energy function
for ux=1:N
for ui=1:N
m1=0;
m2=0;
m3=0;
m4=0;
%first part
for j=1:N
if j~=ui
m1=m1+V(ux,j);
end
end
m1=-A*m1;
%second part
for y=1:N
if y~=ux
m2=m2+V(y,ui);
end
end
m2=-B*m2;
%third part
for x=1:N
for j=1:N
m3=m3+V(x,j);
end
end
m3=-C*(m3-N);
%fourth part
for y=1:N
if y~=ux
if ui==1
m4=m4+d(ux,y)*(V(y,ui+1)+V(y,N));
elseif ui==N
m4=m4+d(ux,y)*(V(y,ui-1)+V(y,1));
else
m4=m4+d(ux,y)*(V(y,ui+1)+V(y,ui-1));
end
end
end
m4=-D*m4;
Udao(ux,ui)=-U(ux,ui)+m1+m2+m3+m4;
end
end
%matrix renew
U=U+lamda*Udao;
V=(1+tanh(U/u0))/2;
for ux=1:N
for ui=1:N
if V(ux,ui)<0.3
V(ux,ui)=0;
end
if V(ux,ui)>0.7
V(ux,ui)=1;
end
end
end
end
V;
%check if validity
%check that should have N 1s
test1=0;
for ux=1:N
for ui=1:N
test1=test1+V(ux,ui);
end
end
%line check no more than1
test2=0;
for x=1:N
for i=1:N-1
for j=i+1:N
test2=test2+V(x,i)*V(x,j);
end
end
end
%column check no more than1
test3=0;
for i=1:N
for x=1:N-1
for y=x+1:N
test3=test3+V(x,i)*V(y,i);
end
end
end
%diagonal line check
test4=0;
for ux = 1:N
test4=test4+V(ux,ux);
end
%round check
test5=0;
for ux=1:N
for ui=1:N
test5=test5+(V(ux,ui)==1 && V(ui,ux)==1);
end
end
%when valid, jump out
if test1==N && test2==0 && test3==0 && test4 ==0 && test5 ==0
toend = 1;
else
toend=0;
end
end
%output
V
%time cost
tc = 0;
for ux=1:N
for ui=1:N
if V(ux,ui)==1
tc = tc+d(ux,ui);
end
end
end
tc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -