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

📄 pattern recognition.cpp

📁 这是用vc++编写的克隆选择算法的源程序
💻 CPP
字号:

#include <iostream.h>                  // cout
#include <fstream>                   // ifstream / ofstream
#include <stdlib.h>                    // rand()
#include <stdio.h>
#include <math.h>
#include <iomanip.h>                   // set weight of output
#include <time.h>                      // time()  

using namespace std;

const int gen=100;
const int NC=10;
const int L=361;
const int N=12;
const int n=5;
const int belta=8;
const int HHH=10;

struct rec
{
	int A[L];         // index of cities in visiting ways
	float Distance;
};
struct rec antibody[N]; //抗体:NC个记忆抗体
struct rec* point[N];  //定义结构指针数组,分别指向结构体数组
struct rec anti_clone[n][belta]; 
struct rec anti_temp[N];

int antigen[NC][L]; //输入NC个长度为L的antigen
int antigen_noise[L]; //噪声Pattern
clock_t tstart,tend;
int q[n];
int affinity_final[NC];
float times; //时间计数器
int category,category_s=0; //识别分类
float CRR[250]; //Correct recognition rate

ofstream generation("memory.txt",ios::app);
ofstream antigen_out("antigen.txt",ios::app);
ofstream CRR_out("CRR.xls",ios::app);
ifstream in_file("prototyp.dat");


void quick_struct(rec* rr[], int );
void qs_struct(rec* rr[], int, int);
void antigeninput();
int affinity_H(int a[L], int b[L]);//Hamming Distance
void memory_antigen();
void hypermutate(int temp[L]);
void noisePattern(int a[],int);

void main()
{
	srand((unsigned)time(NULL));
	tstart=clock();

	antigeninput();
	memory_antigen();
	
	for (int i=0;i<NC;i++)
	{
		for (int t=0;t<gen;t++)
		{
			for (int j=0;j<N;j++)
			{
				antibody[j].Distance=affinity_H(antibody[j].A,antigen[i]);
				anti_temp[j]=antibody[j];
				point[j]=&anti_temp[j];   //定义结构指针数组,分别指向结构体数组
				
			}

			quick_struct(point,N);

			for (j=0;j<n;j++)
			{
				q[j]=(int)((n-j)*belta/n);
				for (int k=0;k<q[j];k++)
				{
					anti_clone[j][k]=anti_temp[j];
					hypermutate(anti_clone[j][k].A);
					anti_clone[j][k].Distance =affinity_H(anti_clone[j][k].A,antigen[i]);
					if (anti_clone[j][k].Distance > antibody[i].Distance) 
					{
						antibody[i]=anti_clone[j][k];
					}
				}
			}
			affinity_final[i]=antibody[i].Distance;
		}//t,gen 
			
		

	}//i,NC

	tend=clock();
	times=(tend-tstart)/1000.0;
	cout<<"times:"<<times<<endl;
	
	for (i=0;i<NC;i++)
	{
		cout<<"affinity:"<<affinity_final[i]<<endl;
	}
	cout<<endl;

/*	for(int i=0;i<NC;i++) //antigen Output
	{
		for (int j=0;j<L;j++)
		{
			if((j+1)%19!=0)
			{
				antigen_out<<antigen[i][j]<<" ";
			}
			else
				antigen_out<<antigen[i][j]<<endl;
		}
		antigen_out<<endl;
	}

	for (int k=0;k<NC;k++)
	{
		for (int j=0;j<L;j++)
		{
			if ((j+1)%19!=0)
				cout<<antibody[k].A[j]<<" ";
			else
				cout<<antibody[k].A[j]<<endl;
		}
		cout<<endl;
	}
*/

//*********
//Pattern recongnition 
//output: 识别category 
//*********

	for(int Ci=0;Ci<250;Ci++)
	{
		category_s=0;
		for(int k=0;k<NC;k++)
		{
			for(int j=0;j<HHH;j++)
			{
				int temp=0;

				noisePattern(antigen[k],Ci);

				//output the noisepattern
/*				for(i=0;i<L;i++)
				{
					if((i+1)%19!=0)
						cout<<antigen_noise[i]<<" ";
					else
						cout<<antigen_noise[i]<<endl;
				}
*/
				//recongnize
				for(i=0;i<NC;i++)
				{
					antibody[i].Distance =affinity_H(antibody[i].A,antigen_noise);
					if(antibody[i].Distance > temp)
					{
						temp=antibody[i].Distance;
						category=i;
					}
				}
				if(category==k) category_s=category_s+1;
			}//j,HHH
		}//k,NC
			
		CRR[Ci]=(float)category_s/100;
		CRR_out<<CRR[Ci]<<endl;
	}//Ci
	

//	cout<<"category_s:"<<category_s<<endl<<"CRR:"<<CRR<<endl;
}//main

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//antigeninput
void antigeninput()
{
	int temp;
	for(int i=0;i<NC;i++)
	{
		for(int j=0;j<L;j++)
		{
			in_file>>temp;
			antigen[i][j]=temp;
		}
	}
}

//Hamming Distance
int affinity_H(int a[L], int b[L])
{
	int Distance_H=0;
	for(int i=0;i<L;i++)
	{
		if(a[i]==b[i]) Distance_H=Distance_H+1;
	}
	return Distance_H;
}

void memory_antigen()
{
	for(int i=0;i<NC;i++)
	{
		for(int j=0;j<L;j++)
		{
			antibody[i].A[j]=rand()%2;
		}
	}
}

void hypermutate(int temp[L])
{
	int a=0;
	a=rand()%L;
	temp[a]=1-temp[a];
}

//*********
//input: a[L] 待加入噪声Pattern
//		 b    Noise Number
//output: antigen_noise[L]
//*********
void noisePattern(int a[L],int b)
{	
	int temp1=0;
	int temp2=0;
	
	for(int i=0;i<L;i++)
	{
		if(temp2<=b)
		{
			temp1=rand()%2;
			if(temp1==1)
			{
				antigen_noise[i]=1-a[i];
				temp2=temp2+1;
			}
			else
				antigen_noise[i]=a[i];
		}
		else
			antigen_noise[i]=a[i];
	}

}



/*The Quicksort*/
void quick_struct(rec* rr[], int count)
{
	qs_struct(rr,0,count-1);
}

void qs_struct(rec* rr[], int left, int right)
{
	register int i,j;
	float x;
	struct rec temp;

	i=left; j=right;
	x=point[(left+right)/2]->Distance;

	do
	{
		while((point[i]->Distance>x) && (i<right)) i++;
		while((x>point[j]->Distance) && (j>left)) j--;
		if(i<=j)
		{
			temp=*point[i];
			*point[i]=*point[j];
			*point[j]=temp;
			i++; j--;
		}
	}while(i<=j);

	if(left<j) qs_struct(point, left,j);
	if(i<right) qs_struct(point,i,right);
}

⌨️ 快捷键说明

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