예제 #1
0
int main()
{
    //srand((int)time(0));
    LKDTreeMatrix dataSet(10, 3, 0.0f);
    LKDTreeMatrix data(1, 3);
    data[0][0] = 3.0f;
    data[0][1] = 2.5f;
    data[0][2] = -5.0f;


    LKDTree tree;

    for (unsigned int i = 0; i < 500; i++)
    {
        printf("Test Index: %u\n", i);

        RandMatrix(dataSet, -10.0f, 10.0f);

        if (i == 739)
        {
            printf("Stop\n");
        }

        tree.BuildTree(dataSet);


        int predictNN = tree.SearchNearestNeighbor(data);

        int actualNN = FindNearestNeighbor(dataSet, data);

        if (actualNN != predictNN)
        {
            printf("Fail\n");
            break;
        }

    }

    system("pause");
    return 0;
}
void ModleNetwork::Network_1(double **ReguMatrix,int nx,int ny)
{
	double *a,*b,*c,*d,*e,*f,sum(0),AbsValue(1),MaxScore(-1),Score(0);
	double score[101];
	int i(0),j=0,cou(0),k(0);
	double **TempMatrix;
	a=new double[GENEAM];
	b=new double[GENEAM];
	c=new double[GENEAM];
	d=new double[GENEAM];
	e=new double[GENEAM];
	f=new double[GENEAM];
	TempMatrix=new double*[GENEAM];
	for(i=0;i<GENEAM;++i)
		TempMatrix[i]=new double[TFScale];
	double step=STEP;
	int pets=PETS;
	for(i=0;i<101;++i)
		score[i]=0;
	for(i=0;i<ny;++i)
		a[i]=b[i]=e[i]=INITIALVALUE;  
	while(k<NN)                               
	{
		++k;
		RandMatrix(ReguMatrix,TempMatrix,nx,ny); 
		AbsValue=10;
		j=0;
		step=STEP;
		pets=PETS;
		while(AbsValue>0.000001&&cou<MAXTIME)
		{
			++j;
			for(i=0;i<ny;++i)
			{
				a[i]+=FaNexVal(TempMatrix,b,nx,i,p,q,nn,r)*step;
				if(a[i]<0.000001)
				{
					cou=MAXTIME+2;
					break;
				}
			}
			if(cou>=MAXTIME)
				break;   
			for(i=0;i<ny;++i)
				b[i]=a[i];
			if(j%(pets/8)==0)
			{
				AbsValue=0;
				for(i=0;i<ny;++i)
				{
					AbsValue+=fabs(a[i]-e[i]);
				}
				for(i=0;i<ny;++i)
					e[i]=a[i];
				if(AbsValue<5&&pets==PETS&&j==PETS)
				{
					step=STEP*4.0;
					pets=PETS/4;
					++cou;
					j=0;
					continue;
				}
			}
			if(j%pets==0)
			{
				++cou;
				j=0;
			}
		}
		step=STEP;
		pets=PETS;
		if(cou>=MAXTIME)break;
		for(i=0;i<ny;++i)
		{
			c[i]=d[i]=a[i];
		}
		c[ny]=0;
		d[ny]=c[ny];
		cou=0;
		AbsValue=10;
		j=0;
		while(AbsValue>0.000001&&cou<MAXTIME)
		{
			++j;
			for(i=0;i<ny+1;++i)
			{
				c[i]+=FaNexVal(TempMatrix,d,nx+1,i,p,q,nn,r)*step;
				if(c[i]<0.000001)
				{
					cou=MAXTIME+2;
					break;
				}
			}
			for(i=0;i<ny+1;++i)
				d[i]=c[i];
			if(j%(pets/8)==0)
			{
				AbsValue=0;
				for(i=0;i<ny+1;++i)
				{
					AbsValue+=fabs(c[i]-f[i]);
				}
				for(i=0;i<ny+1;++i)
					f[i]=c[i];
				if(AbsValue<5&&pets==PETS&&j==PETS)
				{
					step=STEP*4.0;
					pets=PETS/4;
					++cou;
					j=0;
					continue;
				}
			}
			if(j%pets==0)
			{
				j=0;
				++cou;
			}
		}
		if(cou>=MAXTIME)
		{
			sum+=1;
			score[0]+=1;
			break;
		} 
		AbsValue=0;
		sum+=1;
		for(i=0;i<ny;++i)
			AbsValue+=(fabs(c[i]-a[i]))/(c[i]>a[i]?a[i]:c[i]);
		Score=1-AbsValue*AbsValue/(ny*ny/9.0+AbsValue*AbsValue);
		Score*=100;
		Score*=(1-(pow(((double)cou)/MAXTIME,3)));
		if(Score>MaxScore)    
		{
			for(i=0;i<ny+1;++i)
				for(j=0;j<nx+1;++j)
					MaxMa[i][j]=TempMatrix[i][j];
			MaxScore=Score;
		}
		score[(int)Score]+=1;   
	}
	for(i=0;i<101;++i)
		score[i]/=sum;  
	ofstream fi1;
	fi1.open("Score");
	for(i=0;i<101;++i)
		fi1<<i+0.5<<' '<<score[i]<<endl;
		fi1.close();
	for(i=0;i<ny;++i)
		a[i]=b[i]=e[i]=INITIALVALUE;
	AbsValue=10;
	j=0;
	cou=0;
	step=STEP;
	pets=PETS;
	while(AbsValue>0.000001&&cou<MAXTIME)    
	{
		++j;
		for(i=0;i<ny;++i)
			a[i]+=FaNexVal(MaxMa,b,nx,i,p,q,nn,r)*step;
		for(i=0;i<ny;++i)
			b[i]=a[i];
		if(j%(pets/8)==0)
		{
			AbsValue=0;
			for(i=0;i<ny;++i)
				AbsValue+=fabs(a[i]-e[i]);
			for(i=0;i<ny;++i)
				e[i]=a[i];
			if(AbsValue<5&&pets==PETS&&j==PETS)
			{
				step=STEP*4.0;
				pets=PETS/4;
				++cou;
				j=0;
				continue;
			}
		}
		if(j%pets==0)
		{
			++cou;
			j=0;
		}
	}
	for(i=0;i<ny;++i)
	{
		c[i]=d[i]=f[i]=a[i];
	}
	c[ny]=0;
	d[ny]=f[ny]=c[ny];
	ofstream igemSfw;
	igemSfw.open("EachGeneChange");
	if(!igemSfw)
		exit(0);
	j=0;
	cou=0;
	AbsValue=10;
	step=STEP;
	pets=PETS;
	while(AbsValue>0.00000001&&cou<MAXTIME)  
	{
		++j;
		for(i=0;i<ny+1;++i)
			c[i]+=FaNexVal(MaxMa,d,nx+1,i,p,q,nn,r)*step;
		for(i=0;i<ny+1;++i)
			d[i]=c[i];
		if(j%(pets/8)==0)
		{
			igemSfw<<cou+j*step<<' '<<flush;
			for(i=0;i<ny;++i)igemSfw<<c[i]<<' '<<flush;
			igemSfw<<c[ny]<<endl;
			AbsValue=0;
			for(i=0;i<ny+1;++i)
				AbsValue+=fabs(c[i]-f[i]);
			for(i=0;i<ny+1;++i)
				f[i]=c[i];
			if(AbsValue<5&&pets==PETS&&j==PETS)
			{
				step=STEP*4.0;
				pets=PETS/4;
				++cou;
				j=0;
				continue;
			}
		}
		if(j%pets==0)
		{
			++cou;
			j=0;
		}
	}
	delete[] a;
	delete[] b;
	delete[] c;
	delete[] d;
	delete[] e;
	delete[] f;
	for(i=0;i<GENEAM;++i)
		delete[] TempMatrix[i];
}