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

📄 new_art2.cpp

📁 art2神经元网络的 source code c
💻 CPP
字号:
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

#define N 36     
#define EPS 0.1   //0.0001
#define pi 3.1415927
#define vig 0.955  //0.903
#define MaxCol 100

//#define   CEILING   3   
//#define   FLOOR     1   

double a,b,c,d,value;
double W[MaxCol][N+1],Wi[N+1][MaxCol];// weight*critical*data
double W0[N+1],Wi0[N+1];
double Wr[N+1],Wri[N+1];
double r[N+1];//WeightLearn
double Up[N+1],Vp[N+1],Zp[N+1],Sp[N+1],Up0[N+1];//F0
double U[N+1],P[N+1],Q[N+1],V[N+1],S[N+1],Z[N+1],P0[N+1];//F1
double Input[36001];

double f(double x)

{
	double temp;
	if (x>value) temp = x;
	else temp = 0;
	return temp;
}

double SumOutMode(double InputVector[], int indim)

{
	double sum = 0;
	for (int i=1;i<=indim; i++)
	{
		sum += InputVector[i] * InputVector[i];
	}

	sum = sqrt(sum);
	return sum;
}

void F0_Function(double In[], int indim)

{
	int bFlag = 1;
	double temp;
	int count = 0;

	for (int i=1; i<=indim; i++)
    {
		Up[i] = 0;
		Vp[i] = 0;
	}

	while (bFlag == 1)
	{
		count++;
		for (int j=1; j<=indim; j++)
		{
			Zp[j] = In[j] + a * Up[j];
		}

		temp = SumOutMode(Zp,N);

		for (j=1; j<=indim; j++)
		{
			Sp[j] = Zp[j] / temp;
		}

		for (j=1; j<=indim; j++)
		{
			Vp[j] = f(Sp[j]);
		}

		for (j=1; j<=indim; j++)
		{
			Up0[j] = Up[j];
			Up[j] = Vp[j] / SumOutMode(Vp,N);
		}

		temp = 0;

		for (j=1; j<=indim; j++)
		{
			temp += (Up[j] - Up0[j]) * (Up[j] - Up0[j]);
		}

		if (temp<EPS) bFlag = 0;
		else bFlag = 1;
	}
}

void F1_Function(double up[], double wiact[], int indim)

{
	int count = 0;
	int bFlag = 1;

	for (int i=1; i<=N; i++)
	{
		U[i] = 0;
		P[i] = 0;
		Q[i] = 0;
	}

	while (bFlag == 1)
	{
		for (i=1; i<=N; i++)
		{
			Z[i] = Up[i] + a * U[i];
		}

		for (i=1; i<=N; i++)
		{
			S[i] = Z[i] / SumOutMode(Z,N);
		}

		for (i=1; i<=N; i++)
		{
			V[i] = f(S[i]) + b * f(Q[i]);
		}

		for (i=1; i<=N; i++)
		{
			U[i] = V[i] / SumOutMode(V,N);
		}

		for (i=1; i<=N; i++)
		{
			P0[i] = P[i];
			P[i] = U[i] + d * wiact[i];
		}

		for (i=1; i<=N; i++)
		{
			Q[i] = P[i] / SumOutMode(P,N);
		}

		double temp = 0;
		for (i=1; i<=N; i++)
		{
			temp += (P[i] - P0[i]) * (P[i] - P0[i]);
		}
		temp = sqrt(temp);
		if (temp<EPS) bFlag = 0;
		else bFlag = 1;
	}
}

void WeightLearnFunction(double w0[], double wi0[], double up[], double p[], int indim)

{
	double temp1 = 0,temp2 = 0,temp3 = 0;
	int i;
	int bFlag = 1;
	int count = 0;

	while (bFlag == 1)
	{
		count++;
		for (i=1; i<=indim; i++)
		{
			Wri[i] = wi0[i] + d * (P[i] - wi0[i]);
			Wr[i] = w0[i] + d * (P[i] - w0[i]);
		}

		F1_Function(Up,Wri,N);

		temp1 = SumOutMode(Up,N) + c * SumOutMode(P,N);
		for (i=1; i<=indim; i++)
		{
			r[i] = (Up[i] + c * P[i]) / temp1;
		}

		temp1 = SumOutMode(r,N);
		temp2 = 0;
		for (i=1; i<=indim; i++)
		{
			temp2 += (wi0[i] - Wri[i]) * (wi0[i] - Wri[i]);
		}

		temp2 = sqrt(temp2);
		temp3 = 0;
		for (i=1; i<=indim; i++)
		{
			temp3 += (w0[i] - Wr[i]) * (w0[i] - Wr[i]);
		}
		temp3 = sqrt(temp3);

		if (temp1>vig && temp2<EPS && temp3<EPS) bFlag = 0;

		for (i=1; i<=indim; i++)
		{
			wi0[i] = Wri[i];
			w0[i] = Wr[i];
		}
	}
}

void InitiateArtWeight(int indim, int outdim)

{
	int i,j;

	srand((unsigned)time(NULL));

	for (i=1; i<=indim; i++)
	{
		for (j=1; j<=outdim; j++)
		{
			W[j][i] = 1.0 / (1-d) * rand() / 32767;
			Wi[i][j] = 0;
		}
	}
}

void InitiateInputValue()

{
	//随机改变sin图形
	int i;
/*	int r[36001];
	srand((unsigned)time(NULL));;//初始化随机种子。   
	for (i=1; i<=36000; i++)
	{
		r[i] = rand() % (CEILING - FLOOR + 1) + FLOOR;
	}
	//生成随机数r[i]

	/*for (i=1; i<36000; i++)

	{
		cout<<r[i]<<endl;
	}
	*/
	for (i=1; i<=36000; i++)
	{
		Input[i] = sin(i*2*pi/360.0) +1.1;        //1.1
		//Input2[i] = 2*sin(i*2*pi/360.0+0.75*pi) +2.1;
		//Input3[i] = cos(i*pi/360) +1.1;
	}
}

void main()

{
	double X[N+1],t[MaxCol+1];
	double Wt[N+1],Wti[N+1];
	double Wiact[N+1];
	double temp1,temp2;
	int index[MaxCol+1];
	a = 10.0;   //20
	b = 10.0;   //20
	c = 0.1;
	d = 0.9;
	int M = 1;
	int AddFlag ;

	int output = 0;
	double max = 0;

	value = 1 / sqrt(N);

	InitiateInputValue();
	InitiateArtWeight(N,M);
	
	for (int jj=1; jj<=1000; jj++)///////////////////////////////////////////////////
	{
		AddFlag = 0;
		max = 0;
		for (int i=1; i<=N; i++)
		{
			X[i] = Input[(jj-1)*36+i];
		}

		F0_Function(X,N);

		for (int j=1; j<=N; j++)
		{
			Wiact[j] = 0;
		}

		F1_Function(Up,Wiact,N);

		for (j=1; j<=N; j++)
		{
			t[j] = 0;
			for (i=1; i<=N; i++)
			{
				t[j] = t[j] + W[j][i] * P[i];
			}
		}

		for (i=1; i<=M; i++)
		{
			index[i] = 0;
			for (j=1; j<=M; j++)
			{
				if (t[i]>=t[j]) index[i] += 1;
			}
		}

		for (int k=M; k>=1; k--)
		{
			int actn;
			actn = index[k];
			for (int l=1; l<=N; l++)
			{
				Wiact[l] = Wi[l][actn];
			}

			F1_Function(Up,Wiact,N);
			temp1 = SumOutMode(Up,N) + c * SumOutMode(P,N);
			for (l=1; l<=N; l++)
			{
				temp2 = Up[l] - P[l];
			}

			for (l=1; l<N; l++)
			{
				r[l] = (Up[l] + c * P[l]) / temp1;
			}

			for (l=1; l<=N; l++)
			{
				W0[l] = W[actn][l];
			}

			if (SumOutMode(r,N)>vig)
			{
				WeightLearnFunction(W0, Wiact, Up, P, N);
				
				for (int l=1; l<=N; l++)
				{
					W[actn][l] = Wr[l];
					Wi[l][actn] = Wri[l];
				}
				
				for (j=1; j<=M; j++)
				{
					if (t[j]>max) {max = t[j]; output = j;}
				}
				cout<<"获胜端口是  "<<output<<"     ";
				
				break;
			}

			if (k==1) AddFlag = 1;
		}

		if (AddFlag == 1)
		{
			M = M + 1;
			cout<<"the new output is "<<M<<"     ";
			for (int n=1; n<=N; n++)
			{
				Wt[n] = d / (1 - d) * (double)rand()/32767;
				Wti[n] = 0;
			}

			F1_Function(Up,Wti,N);

			WeightLearnFunction(Wt,Wti,Up,P,N);
			for (n=1; n<=N; n++)
			{
				W[M][n] = Wr[n];
				Wi[n][M] = Wri[n];
			}
		}

		cout <<"端口总数  "<<M<<endl;
	}//for
}//main


⌨️ 快捷键说明

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