基于matlab的模拟退火算法的实现,模拟退火算法matlab实现,大家多多指教

clear

%Receive input

firstLayer_Neural_Num=input('This is the 1st layer.How many neurals do you want to use?  ');

secondLayer_Neural_Num=input('This is the 2nd layer.How many neurals do you want to use?  ');

thirdLayer_Neural_Num=input('This is the 3rd layer.How many neurals do you want to use?  ');

%Initialize W and V

V=rands(firstLayer_Neural_Num,secondLayer_Neural_Num)-0.5;

W=rands(secondLayer_Neural_Num,thirdLayer_Neural_Num)-0.5;

V_copy=V;

W_copy=W;

%Initialize X and Y

X=[0,0;0,1;1,0];

Y=[0.6;1;1];

[x_rows x_cols]=size(X);

[y_rows y_cols]=size(Y);

sampNum=x_rows;

outputMension=y_cols;

%Initialize  Temperature

Temperature=10;

%Begin to train

while Temperature>0.9

for sampIndex=1:sampNum

pre_energy=return_energy(X(sampIndex,:),Y(sampIndex,:),V,W);

%initialize p and r

flag=1;

%pre_adjustWeight is a 1*4 matrix,which means[state,row,col,value]

%state means if it comes from V ,the state equals 0. Also,if it

% comes from W, the state equals 1.

while flag==1

pre_adjustWeight=generate_randWeight(V,W);

adjust_rows=pre_adjustWeight(2);

adjust_cols=pre_adjustWeight(3);

adjust_val=pre_adjustWeight(4);

%Adjust pre_adjustWeight

Delta_weight=adjust_randWeight(Temperature);

if pre_adjustWeight(1)==0

V_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

else

W_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

end

pro_energy=return_energy(X,Y,V_copy,W_copy);

Delta_energy=pro_energy-pre_energy;

if Delta_energy>0

r=rand(1);

p=Temperature/(Temperature^2+pro_energy^2);

if p

flag=0;

end

end

end

if pre_adjustWeight(1)==0

V(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

else

W(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

end

end

Temperature=0.9*Temperature;

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值