模拟退火算法matlab实现.docx

上传人:b****5 文档编号:11853016 上传时间:2023-04-06 格式:DOCX 页数:11 大小:17.33KB
下载 相关 举报
模拟退火算法matlab实现.docx_第1页
第1页 / 共11页
模拟退火算法matlab实现.docx_第2页
第2页 / 共11页
模拟退火算法matlab实现.docx_第3页
第3页 / 共11页
模拟退火算法matlab实现.docx_第4页
第4页 / 共11页
模拟退火算法matlab实现.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

模拟退火算法matlab实现.docx

《模拟退火算法matlab实现.docx》由会员分享,可在线阅读,更多相关《模拟退火算法matlab实现.docx(11页珍藏版)》请在冰豆网上搜索。

模拟退火算法matlab实现.docx

模拟退火算法matlab实现

文章来源:

http:

//www.smatrix.org/bbs/read.php?

tid=5130

模拟退火算法matlab实现

复制代码

1.functionoutPut=Activation_func(x,w)

2.temp=x*w;

3.outPut=temp;

4.[rowscols]=size(temp);

5.fori=1:

rows

6.    forj=1:

cols

7.        outPut(i,j)=1/(1+exp(-temp(i,j)));

8.    end

9.end

10.

复制代码

1.functionoutPut=adjust_randWeight(T)

2.outPut=zeros

(1);

3.Delta_W=0.5*T*tan(rand

(1));

4.outPut=Delta_W;

5.

复制代码

1.clear

2.%Receiveinput

3.firstLayer_Neural_Num=input('Thisisthe1stlayer.Howmanyneuralsdoyouwanttouse?

  ');

4.secondLayer_Neural_Num=input('Thisisthe2ndlayer.Howmanyneuralsdoyouwanttouse?

  ');

5.thirdLayer_Neural_Num=input('Thisisthe3rdlayer.Howmanyneuralsdoyouwanttouse?

  ');

6.%InitializeWandV

7.V=rands(firstLayer_Neural_Num,secondLayer_Neural_Num)-0.5;

8.W=rands(secondLayer_Neural_Num,thirdLayer_Neural_Num)-0.5;

9.V_copy=V;

10.W_copy=W;

11.

12.%InitializeXandY

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

14.Y=[0.6;1;1];

15.[x_rowsx_cols]=size(X);

16.[y_rowsy_cols]=size(Y);

17.sampNum=x_rows;

18.outputMension=y_cols;

19.%Initialize  Temperature

20.Temperature=10;

21.  

22.%Begintotrain

23.whileTemperature>0.9

24.    forsampIndex=1:

sampNum

25.        

26.        pre_energy=return_energy(X(sampIndex,:

),Y(sampIndex,:

),V,W);

27.        

28.        %initializepandr

29.        flag=1;

30.        

31.        %pre_adjustWeightisa1*4matrix,whichmeans[state,row,col,value]

32.        %statemeansifitcomesfromV,thestateequals0.Also,ifit

33.        %comesfromW,thestateequals1.

34.      whileflag==1

35.        pre_adjustWeight=generate_randWeight(V,W);

36.        adjust_rows=pre_adjustWeight

(2);

37.        adjust_cols=pre_adjustWeight(3);

38.        adjust_val=pre_adjustWeight(4);

39.        

40.        %Adjustpre_adjustWeight

41.        Delta_weight=adjust_randWeight(Temperature);

42.        ifpre_adjustWeight

(1)==0

43.            V_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

44.        else

45.            W_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

46.        end

47.            

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

49.        

50.        Delta_energy=pro_energy-pre_energy;

51.        

52.        ifDelta_energy>0

53.            r=rand

(1);

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

55.            ifp

56.                flag=0;

57.            end    

58.        end

59.      end

60.    

61.        ifpre_adjustWeight

(1)==0

62.            V(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

63.        else

64.            W(adjust_rows,adjust_cols)=adjust_val+Delta_weight;

65.        end

66.      

67.        

68.    end

69.    Temperature=0.9*Temperature;

70.    

71.end

72.

复制代码

1.functionoutPut=generate_randWeight(v,w)

2.outPut=zeros(1,4);

3.[v_rowsv_cols]=size(v);

4.[w_rowsw_cols]=size(w);

5.totalNum=v_rows*v_cols+w_rows*w_cols;

6.temp1=round(rand

(1)*(totalNum-1))+1;

7.iftemp1

8.    outPut

(1)=0;

9.    outPut

(2)=ceil(temp1/v_cols);

10.      iftemp1/v_cols==outPut

(2)

11.        outPut(3)=v_cols;

12.      else

13.        outPut(3)=temp1-floor(temp1/v_cols)*v_cols;

14.      end

15.    outPut(4)=v(outPut

(2),outPut(3));

16.else

17.    temp2=temp1-v_rows*v_cols;

18.    outPut

(1)=1;

19.    outPut

(2)=ceil(temp2/w_cols);

20.      iftemp2/w_cols==outPut

(2)

21.          outPut(3)=w_cols;

22.      else

23.          outPut(3)=temp2-floor(temp2/w_cols)*w_cols;

24.      end

25.    outPut(4)=w(outPut

(2),outPut(3));

26.end

27.

复制代码

1.

2.functionoutPut=return_energy(X,Y,V,W)

3.        

4.        outPut=zeros

(1);

5.        

6.        [rows_Ycols_Y]=size(Y);

7.        outputMension=cols_Y;

8.        

9.        %InitializeE

10.        E=0;

11.        

12.        %Computetheoutputforthecurrentsample

13.        output_1=Activation_func(X,V);

14.        output=Activation_func(output_1,W);

15.        

16.        %Computetheenergy  forthecurrentsample

17.        foroutput_Idx=1:

outputMension

18.            Ep=(Y(1,output_Idx)-output(1,output_Idx))^2;

19.            E=E+Ep;

20.        end

21.        

22.        outPut=E;

23.

复制代码

1.

2.functionoutPut=return_output(X,V,W)

3.        

4.        %Computetheoutputforthecurrentsample

5.        output_1=Activation_func(X,V);

6.        output=Activation_func(output_1,W);

7.        

8.        outPut=output;

9.

 

文章来源:

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

模拟退火算法的matlab实现,附程序思想详细介绍!

附件所含文件:

Activation_func.m代码:

复制内容到剪贴板

代码:

functionoutPut=Activation_func(x,w)

temp=x*w;

outPut=temp;

[rowscols]=size(temp);

fori=1:

rows

    forj=1:

cols

      outPut(i,j)=1/(1+exp(-temp(i,j)));

    end

end

adjust_randWeight.m代码:

复制内容到剪贴板

代码:

functionoutPut=adjust_randWeight(T)

outPut=zeros

(1);

Delta_W=0.5*T*tan(rand

(1));

outPut=Delta_W;

Anneal_realize.m代码:

复制内容到剪贴板

代码:

clear

%Receiveinput

firstLayer_Neural_Num=input('Thisisthe1stlayer.Howmanyneuralsdoyouwanttouse?

  ');

secondLayer_Neural_Num=input('Thisisthe2ndlayer.Howmanyneuralsdoyouwanttouse?

  ');

thirdLayer_Neural_Num=input('Thisisthe3rdlayer.Howmanyneuralsdoyouwanttouse?

  ');

%InitializeWandV

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;

%InitializeXandY

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

Y=[0.6;1;1];

[x_rowsx_cols]=size(X);

[y_rowsy_cols]=size(Y);

sampNum=x_rows;

outputMension=y_cols;

%Initialize  Temperature

Temperature=10;

  

%Begintotrain

whileTemperature>0.9

    forsampIndex=1:

sampNum

      

      pre_energy=return_energy(X(sampIndex,:

),Y(sampIndex,:

),V,W);

      

      %initializepandr

      flag=1;

      

      %pre_adjustWeightisa1*4matrix,whichmeans[state,row,col,value]

      %statemeansifitcomesfromV,thestateequals0.Also,ifit

      %comesfromW,thestateequals1.

    whileflag==1

      pre_adjustWeight=generate_randWeight(V,W);

      adjust_rows=pre_adjustWeight

(2);

      adjust_cols=pre_adjustWeight(3);

      adjust_val=pre_adjustWeight(4);

      

      %Adjustpre_adjustWeight

      Delta_weight=adjust_randWeight(Temperature);

      ifpre_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;

      

      ifDelta_energy>0

        r=rand

(1);

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

        ifp

            flag=0;

        end    

      end

    end

    

      ifpre_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

generate_randWeight.m代码:

复制内容到剪贴板

代码:

functionoutPut=generate_randWeight(v,w)

outPut=zeros(1,4);

[v_rowsv_cols]=size(v);

[w_rowsw_cols]=size(w);

totalNum=v_rows*v_cols+w_rows*w_cols;

temp1=round(rand

(1)*(totalNum-1))+1;

iftemp1

  outPut

(1)=0;

  outPut

(2)=ceil(temp1/v_cols);

    iftemp1/v_cols==outPut

(2)

      outPut(3)=v_cols;

    else

      outPut(3)=temp1-floor(temp1/v_cols)*v_cols;

    end

  outPut(4)=v(outPut

(2),outPut(3));

else

  temp2=temp1-v_rows*v_cols;

  outPut

(1)=1;

  outPut

(2)=ceil(temp2/w_cols);

    iftemp2/w_cols==outPut

(2)

      outPut(3)=w_cols;

    else

      outPut(3)=temp2-floor(temp2/w_cols)*w_cols;

    end

  outPut(4)=w(outPut

(2),outPut(3));

end

return_energy.m代码:

复制内容到剪贴板

代码:

functionoutPut=return_energy(X,Y,V,W)

      

      outPut=zeros

(1);

      

      [rows_Ycols_Y]=size(Y);

      outputMension=cols_Y;

      

      %InitializeE

      E=0;

      

      %Computetheoutputforthecurrentsample

      output_1=Activation_func(X,V);

      output=Activation_func(output_1,W);

      

      %Computetheenergy  forthecurrentsample

      foroutput_Idx=1:

outputMension

        Ep=(Y(1,output_Idx)-output(1,output_Idx))^2;

        E=E+Ep;

      end

      

      outPut=E;

return_output.m代码:

复制内容到剪贴板

代码:

functionoutPut=return_output(X,V,W)

      

      %Computetheoutputforthecurrentsample

      output_1=Activation_func(X,V);

      output=Activation_func(output_1,W);

      

      outPut=output;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1