⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rbf.cpp

📁 介绍RBF的算法,很好,希望大家喜欢.
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;

double width=0.009;

double P[32]={0.222222, 0.625000, 0.067797, 0.041667, //输入向量
             0.166667, 0.416667, 0.067797, 0.041667, 
             0.111111, 0.500000, 0.050847, 0.041667, 
             0.083333, 0.458333, 0.084746, 0.041667,
             0.194444, 0.666667, 0.067797, 0.041667, 
             0.083333, 0.583333, 0.067797, 0.083333, 
             0.194444, 0.583333, 0.084746, 0.041667, 
             0.166667, 0.458333, 0.084746, 0.000000};

//double weight[2][3]={1,2,3,4,1,5};//有问题,
const int N=32;//输入向量所有的分量个数
const int M=16;//输出来向量所有的分量个数
const int Iweishu=4;
double weight[2][N/Iweishu];

//for(int y1=0;y1<N/Iweishu; y1++)
//	{
//	    weight[0][y1]=0;
//		weight[1][y1]=0;
//	};


const int Onn=2;



//理想输出
double A[M]={ 0.222222, 0.625000, 0.067797, 0.041667, 
             0.166667, 0.416667, 0.067797, 0.041667, 
             0.111111, 0.500000, 0.050847, 0.041667, 
             0.083333, 0.458333, 0.084746, 0.041667,
             };

//实际输出的存放数组,初始全部为0;
double a[M]={0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000,
              0.000000, 0.000000};

int midindex[N]={-1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1,
                 -1,-1,-1,-1};//各输入向量所对应的中心向量在MID数组中的标号
int num=0;//中间层神经元的个数,也就是中心向量的个数
double mid[M];//存放中心向量的数组,

int first=0;
int second=0;
double min=100000000;//double中最大的数
double $=0.2;//学习速度

///////////////////////////////////////////////
double GSFUNC(double p1,double p2,double p3,double p4,double c1,double c2,double c3,double c4)
{
 double X= -((p1-c1)*(p1-c1)+(p2-c2)*(p2-c2)+(p3-c3)*(p3-c3)+(p4-c4)*(p4-c4))/width;
 return exp(X);
};

double FUNC(double p1)//激励函数y=x;
{
 return p1;
};



void initWEIGHTANDmid()//初始化权向量全部为零,在不知道中心向量的个数的前提下以输入的个数进行,但不一定用的完
{
 for(int y1=0;y1<N/Iweishu; y1++)
	{
	    weight[0][y1]=0.25;
		weight[1][y1]=0.3;
	}
for(y1=0;y1<M; y1++)
     mid[y1]=0;

};

///////////////////////////////////////////////////
double mean()
{
   
  for(int y=0; y<=N-Iweishu;) //输入向量所有的分量个数
   {     
     if(first==0)//初始的时候由第一个输入向量默认为中心向量
      {
          first=1;

         for(int i=0;i<Iweishu;i++)
            {
             mid[i]=P[y];
             y++;
            }
          midindex[0]=0;//各输入向量所对应的中心向量(都是以起始下标开始)
          num++;//记录中心向量的个数
	 // cout <<num<< endl;
             
      }

     else
      {  


         double tmp1[Iweishu];
         double tmp2[Iweishu];
         
         for(int i=0;i<Iweishu;i++) 
           {   
             tmp1[i]=P[y];//取出待判别的输入向量
             y++;
           }
        int tempnum=num;

		//cout <<num<< endl;

         for(int y1=0;y1<tempnum; y1++)//和所有的中心向量依次对比    num+1
           {
              for(int i=0;i<Iweishu;i++) 
			  {   
                  tmp2[i]=mid[y1*Iweishu+i];
                 
               }//y1++
              if((tmp2[0]*tmp1[0]+ tmp2[1]*tmp1[1]+tmp1[2]*tmp2[2]+tmp1[3]*tmp2[3]<width)&&(tmp2[0]*tmp1[0]+ tmp2[1]*tmp1[1]+tmp1[2]*tmp2[2]+tmp1[3]*tmp2[3]<min))
			  {
                  min=tmp2[0]*tmp1[0]+ tmp2[1]*tmp1[1]+tmp1[2]*tmp2[2]+tmp1[3]*tmp2[3];
				  midindex[y-Iweishu]=y1*Iweishu+0;
                 // mid[y1*Iweishu+0]=tmp1[0];
				 // mid[y1*Iweishu+1]=tmp1[1];
				 // mid[y1*Iweishu+2]=tmp1[2];
                 // mid[y1*Iweishu+3]=tmp1[3];
                 
			  }
             
              //没有和已经存在的中心向量匹配,所以自己作为中心向量
              if((midindex[y-Iweishu]==-1)&&(y1+1==tempnum))
			  {
				   midindex[y-Iweishu]=(y1+1)*Iweishu+0;
                   num++;//增加一个神经元
                   mid[(y1+1)*Iweishu+0]=tmp1[0];
                   mid[(y1+1)*Iweishu+1]=tmp1[1];
                   mid[(y1+1)*Iweishu+2]=tmp1[2];
                   mid[(y1+1)*Iweishu+3]=tmp1[3];

				   tempnum=num;
				   //	cout <<num<< endl;
			  }
               

          }
          
      }
      


 }
  return 1;
};
/////////////////////////////////////////////////////     



  void trainRBF(int midNNnum)
{    int index=0 ;
	cout <<num<< endl;
	
	for(int f=0;f<20000; f++)	
	{
	for(int y=0; y<=N-Iweishu;) 
	  {   double tmp3[Iweishu];
          //double tmp4[3];
          for(int i=0;i<Iweishu;i++) //取出输入向量,进行GS计算
           {   
             if(i%Iweishu==0)
			 {
                  index=midindex[y];
			 }

			  tmp3[i]=P[y];
             y++;
           }
           
          for(int y1=0;y1<num;y1++)//
		      a[y1]=GSFUNC(tmp3[0],tmp3[1],tmp3[2],tmp3[3],mid[y1*Iweishu+0],mid[y1*Iweishu+1],mid[y1*Iweishu+2],mid[y1*Iweishu+3]);
		      //进行GS计算
		  double sum1=0;
		  double sum2=0;
          for( y1=0;y1<num;y1++)
		  {
		     // sum1=+weight[y1][1]*a[y1];
			   sum1=+(weight[0][y1]*GSFUNC(tmp3[0],tmp3[1],tmp3[2],tmp3[3],mid[y1*Iweishu+0],mid[y1*Iweishu+1],mid[y1*Iweishu+2],mid[y1*Iweishu+3]));
		       sum2=+(weight[1][y1]*GSFUNC(tmp3[0],tmp3[1],tmp3[2],tmp3[3],mid[y1*Iweishu+0],mid[y1*Iweishu+1],mid[y1*Iweishu+2],mid[y1*Iweishu+3]));
		   }
           
		   sum1=FUNC(sum1);
		   sum2=FUNC(sum2);//产生的向量中的两个分向量;

          int r=y1*2; 
		  double e0=abs(A[r+0]-sum1);
          double e1=abs(A[r+1]-sum2);//和理想输出进行比较

		  for(y1=0; y1<num; y1++)
		  {
		      
		     weight[0][y1]=weight[0][y1]+2*$*(e0*a[y1]);
             weight[1][y1]=weight[1][y1]+2*$*(e1*a[y1]);
		  
		  }
	 


	}

}


         





		   


		  

		  
		  
	}

















void main()
{    
     ofstream fout("average.txt");
	//	float Average = ValueSum / ValuesProcessed;
	//	for(int i=1;i<=100;i++)
	
	 
	//fout <<  i << endl;

     double tmp;
     cout <<"请输入宽度"<< endl;
     cin >> tmp;
     width=tmp;
	 initWEIGHTANDmid();
     mean();
     //cout <<num<< endl;
	 for(int h1=0;h1<10;h1++) {
     trainRBF(num);

     for(int h=0;h<2;h++)
         for(int h1=0;h1<num;h1++)
		 {  
			 
			 fout <<weight[h][h1]<< endl;
		 }

      fout << endl; 
	  fout << endl; 
      
	 }


     
     
}





/*class P
{
public:
	double x;
	double y;
        double z;
        double a1;
        double a2;

	P()
	{
	      x=0;
              y=0;
              z=0;
              a1=0;
              a2=0;	
	}
     
        void P-init(double x1; double y1; double z1;double a11;double a22)
	{
		x=x1;
		y=y1;
                z=z1;
                a1=a11;
                a2=a22;

	}
	
	
	void P-output()
	{
		cout<<x<<endl<<y<<endl<<y<<endl;
	}


        ~Point()
	{
	}
	
};*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -