声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2814|回复: 6

[人工智能] 求模拟退火算法和神经网络的delphi程序

[复制链接]
发表于 2006-6-6 11:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
求模拟退火算法和神经网络的delphi程序,谢谢

[ 本帖最后由 xinyuxf 于 2007-6-7 09:45 编辑 ]
回复
分享到:

使用道具 举报

发表于 2007-7-6 02:20 | 显示全部楼层
来自:中国人工智能创业研发俱乐部

ART神经网络的Delphi实现

  1. unit ARTUnit;

  2. interface
  3.   uses Windows, SysUtils, Classes, Extctrls, Math, LPR_HUnit, Dialogs;

  4. type
  5.    TArtNet = class(TObject)
  6.        private
  7.        //F1到F2的连接权
  8.            Wb : array[0..MaxCNN - 1, 0..MaxRNN - 1] of Double;
  9.        //F2到F1的连接权
  10.            Wt : array[0..MaxRNN - 1, 0..MaxCNN - 1] of Integer;
  11.            //警戒值  
  12.            VigilThresh : Double;         
  13.            L : Double;                    
  14.        //识别层的神经元数
  15.            M : Integer;                  
  16.        //比较层的神经元数
  17.            N : Integer;                  
  18.            //权文件名
  19.            FileName : string;            
  20.            //输入向量
  21.            XVect : array[0..MaxCNN - 1] of Integer;      
  22.        //比较层的输出向量
  23.            CVect : array[0..MaxCNN - 1] of Integer;      
  24.            //最优识别层神经元
  25.            BestNeuron : Integer;         
  26.            Reset : Boolean;
  27.            //识别层输出向量
  28.            RVect : array[0..MaxRNN - 1] of Integer;      
  29.        //识别层最优神经元到比较层的权
  30.            PVect : array[0..MaxCNN - 1] of Integer;      
  31.            //识别层禁止标志
  32.            Disabled : array[0..MaxRNN - 1] of Boolean;  
  33.        //对应识别字符
  34.            RecoCharASCII : array[0..MaxRNN - 1] of string[2];

  35.            procedure ClearPVect;
  36.            procedure ClearRVect;
  37.            procedure ClearDisabled;
  38.        //Calc comparison by 2/3 rule
  39.            procedure RunCompLayer;        
  40.            function RunRecoLayer : Boolean;
  41.            procedure RVect2PVect(best : Integer);
  42.        //比较层增益
  43.            function Gain1 : Integer;      
  44.        //识别层增益
  45.            function Gain2 : Integer;      
  46.        //计算警戒值
  47.            function Vigilence : Double;  
  48.        //初始化权重
  49.            procedure InitWeights;        
  50.        //调整连接权
  51.            procedure Train;              
  52.        //保存权值
  53.            procedure SaveWeights(CharImg : TGrayImg);        
  54.        //加载权值
  55.            procedure LoadWeights(CharImg : TGrayImg);        
  56.            procedure LoadInVects(SrcCharImg : TGrayImg);
  57.            function GetRecoChar : string;
  58.        public
  59.            constructor Create;
  60.            procedure InitARTNET(VT : Double);
  61.            function Run(CharImg : TGrayImg; var No : string) : Boolean;
  62.    end;
复制代码
发表于 2007-7-6 02:22 | 显示全部楼层
  1. //出口函数
  2. function GetCharByCharImg(SrcCharImg : TGrayImg;
  3.                          CharType : Integer; var No : string) : Boolean;


  4. implementation
  5.    uses MainUnit;
  6. constructor TArtNet.Create;
  7. begin
  8.      inherited Create;
  9. end;

  10. procedure TArtNet.ClearPVect;
  11. var
  12.      i : Integer;
  13. begin
  14.      for  i := 0 to N - 1 do
  15.          PVect[i] := 0;
  16. end;
  17. procedure TArtNet.ClearRVect;
  18. var
  19.      i : Integer;
  20. begin
  21.      for  i := 0 to N - 1 do
  22.          RVect[i] := 0;
  23. end;

  24. procedure TArtNet.ClearDisabled;
  25. var
  26.      i : Integer;
  27. begin
  28.      for i := 0 to M - 1 do
  29.          Disabled[i] := False;
  30. end;

  31. procedure TArtNet.RunCompLayer;
  32. var
  33.      i, x : Integer;
  34. begin
  35.      for i := 0 to N - 1 do
  36.      begin
  37.           x := XVect[i] + Gain1() + PVect[i];
  38.           if x >= 2 then
  39.                CVect[i] := 1
  40.           else
  41.                CVect[i] := 0;
  42.      end;
  43. end;

  44. function TArtNet.RunRecoLayer : Boolean;
  45. var
  46.      i, j : Integer;
  47.      Net : array[0..MaxRNN] of Double;
  48.      NetMax : Double;
  49. begin
  50.      NetMax := -1;
  51.      BestNeuron := -1;
  52.      for j := 0 to M - 1 do
  53.      begin
  54.           Net[j] := 0;
  55.           for i := 0 to N - 1 do
  56.           begin
  57.                Net[j] := Net[j] + Wb[i, j] * CVect[i];
  58.           end;

  59.           if (Net[j] > NetMax) and (not Disabled[j]) then
  60.           begin
  61.                BestNeuron := j;
  62.                NetMax := Net[j];
  63.           end;
  64.      end;
  65.      if BestNeuron = -1 then
  66.      begin
  67.           //新分配一个识别单元
  68.           BestNeuron := M;
  69.           if BestNeuron > MAXRNN - 1 then
  70.           begin
  71.                Result := False;
  72.                Exit;
  73.           end;
  74.      end;
  75.      RVect[BestNeuron] := 1;
  76.      Result := True;
  77. end;
  78. procedure TArtNet.RVect2PVect(best : Integer);
  79. var
  80.      i : Integer;
  81. begin
  82.      for i := 0 to N - 1 do
  83.           PVect[i] := Wt[best, i];
  84. end;
  85. procedure TArtNet.InitWeights;
  86. var
  87.      i, j : Integer;
  88.      b : Double;
  89. begin
  90.      b := L / (L - 1 + N);
  91.      for i := 0 to N - 1 do
  92.          for j := 0 to MaxRNN - 1 do
  93.              Wb[i, j] := b;

  94.      for i := 0 to N - 1 do
  95.          for j := 0 to MaxRNN - 1 do
  96.              Wt[j, i] := 1;
  97. end;
  98. procedure TArtNet.Train;
  99. var
  100.      i ,z : Integer;
  101. begin
  102.      z := 0;
  103.      for i := 0 to N - 1 do
  104.          Inc(z, CVect[i]);

  105.      for i := 0 to N - 1 do
  106.      begin
  107.          Wb[i, BestNeuron] := L * CVect[i] / (L - 1 + z);
  108.          Wt[BestNeuron, i] := CVect[i];
  109.      end;
  110. end;
  111. procedure TArtNet.LoadInVects(SrcCharImg : TGrayImg);
  112. var
  113.      i, j : Integer;
  114. begin
  115.      for i := 0 to SrcCharImg.Height - 1 do
  116.           for j := 0 to SrcCharImg.Width - 1 do
  117.                XVect[i * SrcCharImg.Width + j] := SrcCharImg.Img[i, j] div  

  118. 255;
  119. end;
复制代码

[ 本帖最后由 frogfish 于 2007-7-6 02:23 编辑 ]
发表于 2007-7-6 02:24 | 显示全部楼层
  1. function TArtNet.Run(CharImg : TGrayImg; var No : string) : Boolean;
  2. var
  3.      S : Double;
  4. begin
  5.      LoadInVects(CharImg);
  6.      LoadWeights(CharImg);
  7.      While Reset do
  8.      begin
  9.           ClearRVect;
  10.           ClearPVect;
  11.           RunCompLayer;            //XVect => CVect
  12.           if not RunRecoLayer then //Get BestNeuron
  13.           begin
  14.                Result := False;    //分类超出最大识别单元数
  15.                Exit;
  16.           end;
  17.           RVect2PVect(BestNeuron); [color=#008000]//Wt[BestNeuron,i] = >  

  18. PVect[/color]
  19.           RunCompLayer;            //XVect * PVect => CVect
  20.           S := Vigilence;          //Sum(CVect) / Sum(XVect)
  21.           if S < VigilThresh then
  22.           begin
  23.                Reset := True;
  24.                RVect[BestNeuron] := 0;
  25.                Disabled[BestNeuron] := True;
  26.           end
  27.           else begin
  28.                Reset := False;
  29.                Train;
  30.           end;
  31.      end;
  32.      SaveWeights(CharImg);
  33.      No := GetRecoChar;
  34.      Result := True;
  35. end;

  36. procedure TArtNet.SaveWeights(CharImg : TGrayImg);
  37. var
  38.      FileStream : TFileStream;
  39.      WeightRecord : TWeightRecord;
  40.      WeightRecordLength : Integer;
  41.      i, k : Integer;
  42.      TempM : Integer;
  43. begin
  44.      WeightRecordLength := sizeof(TWeightRecord);
  45.      //权库文件不存在
  46.      if FileExists(FileName) then
  47.      begin
  48.           //打开权文件
  49.           FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
  50.           //如果有新分配单元,则修改文件中的M
  51.           if BestNeuron >= M then
  52.           begin
  53.                TempM := M + 1;
  54.                FileStream.WriteBuffer(TempM, sizeof(TempM));
  55.                //索引
  56.                WeightRecord.RecordIndex := BestNeuron;
  57.                //权值
  58.                for i := 0 to N - 1 do
  59.                begin
  60.                     WeightRecord.PWb[i] := Wb[i, BestNeuron];
  61.                     WeightRecord.PWt[i] := Wt[BestNeuron, i];
  62.                end;
  63.                //结果
  64.                WeightRecord.CharResult := '?';
  65.                //该次识别对应的字符图象
  66.                WeightRecord.CharImgWidth := CharImg.Width;
  67.                WeightRecord.CharImgHeight := CharImg.Height;
  68.                for i := 0 to CharImg.Height - 1 do
  69.                     for k := 0 to CharImg.Width - 1 do
  70.                          WeightRecord.CharImg[i * CharImg.Width + k] :=  

  71. CharImg.Img[i, k];
  72.                //写入文件
  73.                FileStream.Seek(BestNeuron * WeightRecordLength + sizeof(M),  

  74. soFromBeginning);
  75.                FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
  76.           end
  77.           else begin
  78.                //如果不是新分配的单元,则先填充WeightRecord结构
  79.                FileStream.Seek(BestNeuron * WeightRecordLength +  

  80. sizeof(M),0);
  81.                FileStream.ReadBuffer(WeightRecord, WeightRecordLength);
  82.                //修改WeightRecord结构的权值
  83.                for i := 0 to N - 1 do
  84.                begin
  85.                     WeightRecord.PWb[i] := Wb[i, BestNeuron]; //权值
  86.                     WeightRecord.PWt[i] := Wt[BestNeuron, i];
  87.                end;
  88.                //写入文件
  89.                FileStream.Seek(BestNeuron * WeightRecordLength + sizeof(M),  

  90. soFromBeginning);
  91.                FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
  92.           end;
  93.           FileStream.Free;
  94.      end;
  95. end;
  96. procedure TArtNet.LoadWeights(CharImg : TGrayImg);
  97. var
  98.      FileStream : TFileStream;
  99.      WeightRecord : TWeightRecord;
  100.      i, j, k : Integer;
  101.      WeightRecordLength : LongInt;
  102. begin
  103.      WeightRecordLength := sizeof(TWeightRecord);
  104.      InitWeights;
  105.      //权库文件不存在
  106.      if not FileExists(FileName) then
  107.      begin
  108.           //创建权文件
  109.           FileStream := TFileStream.Create(FileName, fmCreate);
  110.           //先写入识别层单元数
  111.           FileStream.WriteBuffer(M, sizeof(M));
  112.           //填充WeightRecord结构
  113.           for j := 0 to M - 1 do
  114.           begin
  115.                WeightRecord.RecordIndex := j;     //索引
  116.                for i := 0 to N - 1 do
  117.                begin
  118.                    WeightRecord.PWb[i] := Wb[i, j]; //权值
  119.                    WeightRecord.PWt[i] := Wt[j, i];
  120.                end;
  121.                WeightRecord.CharResult := '?';  //结果
  122.                WeightRecord.CharImgWidth := CharImg.Width;
  123.                WeightRecord.CharImgHeight := CharImg.Height;
  124.                for i := 0 to CharImg.Height - 1 do
  125.                     for k := 0 to CharImg.Width - 1 do
  126.                          WeightRecord.CharImg[i * CharImg.Width + k] :=  

  127. CharImg.Img[i, k];
  128.                FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
  129.           end;
  130.           FileStream.Free;
  131.      end
  132.      else begin
  133.           FileStream := TFileStream.Create(FileName, fmOpenRead);
  134.           //跳过识别层单元数
  135.           FileStream.Seek(sizeof(M), soFromBeginning);
  136.           for j := 0 to M - 1 do
  137.           begin
  138.                FileStream.ReadBuffer(WeightRecord, WeightRecordLength);
  139.                //从文件中读入权值
  140.                for i := 0 to N - 1 do
  141.                begin
  142.                     Wb[i, j] := WeightRecord.PWb[i];
  143.                     Wt[j, i] := WeightRecord.PWt[i];
  144.                end;
  145.                //读入对应识别字符的ASCII
  146.                RecoCharASCII[j] := WeightRecord.CharResult;
  147.           end;
  148.           FileStream.Free;
  149.      end;
  150. end;
复制代码
发表于 2007-7-6 02:25 | 显示全部楼层
  1. function TArtNet.Gain1;
  2. var
  3.      i, G : Integer;
  4. begin
  5.      G := Gain2;
  6.      for i := 0 to N - 1 do
  7.      begin
  8.           if RVect[i] = 1 then
  9.           begin
  10.                Result := 0;
  11.                Exit;
  12.           end;
  13.      end;
  14.      Result := G;
  15. end;
  16. function TArtNet.Gain2;
  17. var
  18.      i : Integer;
  19. begin
  20.      for i := 0 to N - 1 do
  21.      begin
  22.           if XVect[i] = 1 then
  23.           begin
  24.               Result := 1;
  25.               Exit;
  26.           end;
  27.      end;
  28.      Result := 0;
  29. end;
  30. function TArtNet.Vigilence : Double;
  31. var
  32.      i : Integer;
  33.      S, K , D : Double;
  34. begin
  35.      K := 0.0;
  36.      D := 0.0;
  37.      for i := 0 to N - 1 do
  38.      begin
  39.           K := K + CVect[i];
  40.           D := D + XVect[i];
  41.      end;
  42.      S := K / D;
  43.      Result := S;
  44. end;
  45. procedure TArtNet.InitARTNET(VT : Double);
  46. var
  47.      i : Integer;
  48.      PPath : PChar;
  49.      FileStream : TFileStream;
  50. begin
  51.      L := 2.0;
  52.      N := MaxCNN;
  53.      PPath := AllocMem(MAX_PATH);
  54.      GetModuleFileName(0, PPath, MAX_PATH);
  55.      FileName := ExtractFilePath(string(PPath)) + 'Lpr.art';
  56.      if not FileExists(FileName) then
  57.            M := 1
  58.      else begin
  59.            FileStream := TFileStream.Create(FileName,fmOpenRead);
  60.            FileStream.ReadBuffer(M,sizeof(M));
  61.            FileStream.Free;
  62.      end;

  63.      Reset := True;
  64.      VigilThresh := VT;
  65.      ClearDisabled;
  66.      //初始化识别字符
  67.      for i := 0 to MaxRNN - 1 do
  68.          RecoCharASCII[i] := '?';
  69. end;
复制代码
发表于 2007-7-6 02:25 | 显示全部楼层
  1. function TARTNET.GetRecoChar : string;
  2. var
  3.      Temp : string[2];
  4.      TempChr : Char;
  5. begin
  6.      Temp := RecoCharASCII[BestNeuron];
  7.      TempChr := Temp[1];
  8.      if Ord(TempChr) < 128 then
  9.      begin
  10.           Result := Temp;
  11.      end
  12.      else begin
  13.           Result := '粤';
  14.      end;
  15. end;
  16. function GetCharByCharImg(SrcCharImg : TGrayImg;
  17.                                   CharType : Integer; var No : string) :  

  18. Boolean;
  19. var
  20.      ARTNET : TARTNET;
  21.      TempImg : TGrayImg;
  22.      CharASCII : Byte;
  23. begin
  24.      if SrcCharImg.Width / SrcCharImg.Height < 0.2 then
  25.      begin
  26.           No := '1';
  27.           Result := True;
  28.           Exit;
  29.      end;
  30.      if not Zoom(SrcCharImg, 15, 30,TempImg) then
  31.      begin
  32.           Result := False;
  33.           Exit;
  34.      end;
  35.      ARTNET := TARTNET.Create;
  36.      ARTNET.InitARTNET(0.8);
  37.      if not ARTNET.Run(TempImg, No) then
  38.      begin
  39.           Result := False;
  40.           Exit;
  41.      end;
  42.      Result := True;
  43. end;

  44. end.
复制代码
发表于 2007-7-6 02:28 | 显示全部楼层
其实神经网络有专门的Delphi库可以调用,比如 Neuro VCL1.2

至于模拟退火算法,没有看到
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 00:50 , Processed in 0.066674 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表