现在的位置: 首页 > 综合 > 正文

算法

2013年08月31日 ⁄ 综合 ⁄ 共 2308字 ⁄ 字号 评论关闭
  1. [code=C/C++]  
  2.   
  3. #include <stdio.h>  
  4. #include <time.h>  
  5.   
  6. int M[10] = {0};                                //权值  
  7. int X[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};    //输入向量  
  8. int Y[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};     //理想输出向量 注:1 表示奇数; 0 表示偶数  
  9. int O[10] = {0};                                //保存输出向量  
  10. int ST = 52;                                    //阈值,默认值:52  
  11.   
  12. //初始化权值  
  13. void initM()  
  14. {  
  15.     srand((unsigned int)time(0));  
  16.       
  17.     for (int x=0; x<10; ++x)  
  18.     {  
  19.         //初始化权值所使用的随机数在 0 - 99 之间  
  20.         M[x] = rand()%100;  
  21.     }  
  22. }  
  23.   
  24. //激活函数  
  25. int active(int m, int x)  
  26. {  
  27.     int o = m * x;  
  28.       
  29.     if (o > ST)  
  30.     {  
  31.         return 0;  
  32.     }  
  33.     else  
  34.     {  
  35.         return 1;  
  36.     }  
  37. }  
  38.   
  39. //计算输出向量  
  40. void calcY()  
  41. {  
  42.     for (int x=0; x<10; ++x)  
  43.     {  
  44.         O[x] = active(M[x], X[x]);  
  45.     }  
  46. }  
  47.   
  48. //根据实际输出向量和理想输出向量调整权向量,返回实际输出和理想输出不匹配的数目  
  49. int adjustM()  
  50. {  
  51.     int err = 0;  
  52.     for (int x=0; x<10; ++x)  
  53.     {  
  54.         if (O[x] != Y[x])  
  55.         {  
  56.             err++;  
  57.               
  58.             if (1 == O[x])  
  59.             {  
  60.                 M[x] += X[x];  
  61.             }  
  62.             else  
  63.             {  
  64.                 M[x] -= X[x];  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69.     return err;  
  70. }  
  71.   
  72. //打印权向量  
  73. void printM()  
  74. {  
  75.     printf("/n最终训练结果:/n");  
  76.     for (int x=0; x<10; ++x)  
  77.     {  
  78.         printf("M[%i] = %i/n", x, M[x]);  
  79.     }  
  80. }  
  81.   
  82. //测试已经训练好的ANN  
  83. void test(int input)  
  84. {  
  85.     if ( 0==active(M[input], X[input]) )  
  86.     {  
  87.         printf("%d 是 偶数 ", input+1);  
  88.     }  
  89.     else  
  90.     {  
  91.         printf("%d 是 奇数 ", input+1);  
  92.     }  
  93.       
  94.     printf("/n/n");  
  95. }  
  96.   
  97. //主函数入口  
  98. int main()  
  99. {  
  100.     printf("请输入阈值:");  
  101.     scanf("%d", &ST);  
  102.     printf("/n");  
  103.       
  104.     initM();  
  105.       
  106.     int n = 0;  
  107.     //一直训练直到能够100%正确为止  
  108.     while (1)  
  109.     {  
  110.         n++;  
  111.         calcY();  
  112.         int err = adjustM();  
  113.         if (0 >=err)  
  114.         {  
  115.             //能够100%正确地回答问题了,结束训练  
  116.             break;  
  117.         }  
  118.         printf("第%0.2d次训练后的结果中存在的错误数 %d/n", n,err);  
  119.     }  
  120.       
  121.     printM();  
  122.     printf("/n阈值=%d 训练次数=%d/n/n", ST, n);  
  123.       
  124.     while (true)  
  125.     {  
  126.         int a = 0;  
  127.         printf("请输入范围为1~10的数字:");  
  128.         scanf("%d", &a);  
  129.         if (1 > a || 10 < a)  
  130.         {  
  131.             break;  
  132.         }  
  133.           
  134.         test(a-1);  
  135.     }  
  136.       
  137.     return 0;  
  138. }  
  139.   
  140. [/code]  

抱歉!评论已关闭.