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

📄 pr_perception_approach.cpp

📁 模式识别中的线性感知器算法 通过了测试
💻 CPP
字号:
#include "PR_Perception_Approach.h"

using std::cin;
using std::endl;
using std::string;

PR_Perception_Approach::PR_Perception_Approach(std::ifstream &fin):max_iterator(65535),
						C(1)
{
	int m(0),n(0);
	
	if(!fin)return;
	
	string str;

	while(!fin.eof()) 
	{ 
		getline(fin,str);//get the number of the samples

		m++;
		
		PR_unit* unit = new PR_unit();
		int k=0;
		for(int i=0;i<str.size();i++)
		{
			string t_str;
			while(str[i] != ' '&& str[i] != '\n' && i<str.size())
			{
				t_str.push_back(str[i]);
				i++;
			}
			k++;
			if( 1 == k)
			{
				unit->numcluster = atof(t_str.c_str());
			}
			else
			{
				unit->unit.push_back(atof(t_str.c_str()));
			}
			if(1 == m)
			{
				n++;
			}
		}
		samples.push_back(unit);
	}
	num_samples = m;
	num_vector = n-1;

	w = new PR_unit(num_vector+1,0);
}

void PR_Perception_Approach::PrintSamples()
{
	cout<<"Samples:"<<endl;
	for(int i=0;i<samples.size();i++)
	{
		cout<<i<<":\t";
		samples[i]->Print();
		cout<<endl;
	}
}
void PR_Perception_Approach::PrintW()
{
	cout<<"W:\t";
	for(int i=0;i<w->unit.size();i++)
	{
		cout<<w->unit[i]<<"  ";
	}
	cout<<endl;
}

void PR_Perception_Approach::Preprocessing()
{
	int t_num;
	t_num = samples[0]->numcluster;
	for(int i=0;i<samples.size();i++)
	{
		samples[i]->unit.push_back(1);
		if(samples[i]->numcluster != t_num)
		{
			*samples[i] *= -1;
		}
	}
}
void PR_Perception_Approach::Rand()
{
	//PrintSamples();
	Preprocessing();
	PrintSamples();

	bool flag_end = false;
	int	k=0;				//the number of iterator times
	int n=0;
	int m(0);

	while(!flag_end && m < max_iterator)
	{
		if( vector_multiplication(w,samples[n]) <= 0)
		{
			*w += *samples[n];
			k=0;
		}
		else
		{
			k++;
		}
		
		cout<<m<<":";
		PrintW();
		if( k > num_samples)
		{
			flag_end = true;
		}

		n++;
		n = n%num_samples;
		m++;

		
	}
	if( max_iterator == m)
	{
		cout<<"maybe the samples are  linear unseparable"<<endl;
	}
	else
	{
		cout<<"The iterator time is :"<<m<<endl;
		PrintW();
	}

}

⌨️ 快捷键说明

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