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

📄 classifier.cpp

📁 - XCS for Dynamic Environments + Continuous versions of XCS + Test problem: real multiplexer +
💻 CPP
字号:
/*	XCSR_DE1.0
*	--------------------------------------------------------
*	Learning classifier system based on accuracy in dynamic environments
*
*	by  Huong Hai (Helen) Dam
*	z3140959@itee.adfa.edu.au
*	UNSW @ ADFA, Canberra Australia
*	Artificial Life and Adaptive Robotics Laboratory
*	http://www.itee.adfa.edu.au/~alar
*
*	Last modified: 24-11-2005
*
*/
#include "classifier.h"
#include"iostream.h"

Classifier::Classifier(int type)
{
	m_interval = new d_interval_t;
	m_fitness = FITNESS;
	m_prediction = PREDICTION;
	m_prediction_error = PERROR;
	m_accuracy = 0.01;
	m_num = 1;
	m_exp = 0;
	m_as = 1;
	m_timestamp = 0;
	rep_type = type;
	schema = -1;
	
}
Classifier::~Classifier()
{
	delete m_interval;
	
}
void Classifier::resetParam()
{
	m_fitness = FITNESS;
	m_prediction = PREDICTION;
	m_prediction_error = PERROR;
	m_accuracy = 0.01;
	m_num = 1;
	m_exp = 0;
	m_as = 1;
	
}
int Classifier::matchClassifier(Classifier *c)
{
	int check1 = TRUE, check2 = TRUE, check3=TRUE;
	
	if(getAction() != c->getAction())
		check1 = FALSE;
	
	for(int j = 0; j < COND_OFFSET; j++)
	{
		if((m_interval->point1[j] != c->getInterval()->point1[j])||
			(m_interval->point2[j] != c->getInterval()->point2[j]))
		{
			check2 = FALSE;
			break;
		}
#ifdef NOISE_AT_ALLELE
		if(m_interval->noise[j] != c->getInterval()->noise[j])
			check3 = FALSE;
#endif
	}
	return check1 && check2 && check3;
}

int Classifier::matchInput(input_t *input)
{
	int check = TRUE;
	for(int j = 0; j <COND_OFFSET; j++)
	{
		if((input->attrs[j]<getIntervalMin(j)) ||
			(input->attrs[j] >= getIntervalMax(j)))
		{
			
			check = FALSE;
			break;
		}
	}
	
	return check;
}
void Classifier::classifiercpy(Classifier *c)
{
	
	for(int i = 0; i < COND_OFFSET; i++)
	{
		m_interval->point1[i] = c->getInterval()->point1[i];
		m_interval->point2[i] = c->getInterval()->point2[i];
#ifdef NOISE_AT_ALLELE
		m_interval->noise[i] = c->getInterval()->noise[i];
#endif
	}
	m_interval->action = c->getAction();
	
	m_fitness = c->getFitness();
	m_prediction = c->getPrediction();
	m_prediction_error = c->getPredictionError();
	m_exp = c->getExp();
	m_num = c->getNum();
	m_as = c->getAS();
	m_timestamp = c->getTimeStamp();
	rep_type = c->getRepresentationType();
	
}

double Classifier::getIntervalMin(int i)
{
	double min;
	if (rep_type == MIN_MAX)
	{
		if(m_interval->point1[i] > m_interval->point2[i])
			min = m_interval->point2[i];
		else
			min = m_interval->point1[i];
	}
	else if (rep_type == CENTER_SPREAD)
	{
		min = m_interval->point1[i] - m_interval->point2[i];
	}
	else if (rep_type == CENTER_PERCENTAGE)
	{
		double minrange = UPPER_BOUND - m_interval->point1[i];
		if(minrange > m_interval->point1[i] - LOWER_BOUND)
			minrange = m_interval->point1[i] - LOWER_BOUND;
		
		double spread = m_interval->point2[i] * minrange;
		min = m_interval->point1[i] - spread;
	}
	else if(rep_type == MIN_PERCENTAGE)
	{
		min = m_interval->point1[i];
		
	}
	if(min > UPPER_BOUND)
		min = UPPER_BOUND;
	if(min < LOWER_BOUND)
		min = LOWER_BOUND;
	return min;
}

double Classifier::getIntervalMax(int i)
{
	double max;
	if (rep_type == MIN_MAX)
	{
		if(m_interval->point1[i] > m_interval->point2[i])
			max = m_interval->point1[i];
		else
			max = m_interval->point2[i];
	}
	else if (rep_type == CENTER_SPREAD)
	{
		max = m_interval->point1[i] + m_interval->point2[i];
	}
	else if (rep_type == CENTER_PERCENTAGE)
	{
		double minrange = UPPER_BOUND - m_interval->point1[i];
		if(minrange > m_interval->point1[i] - LOWER_BOUND)
			minrange = m_interval->point1[i] - LOWER_BOUND;
		
		double spread = m_interval->point2[i] * minrange;
		max = m_interval->point1[i] + spread;
	}
	else if(rep_type == MIN_PERCENTAGE)
	{
		double minrange = UPPER_BOUND - m_interval->point1[i];
		max = m_interval->point1[i] + minrange * m_interval->point2[i];
	}
	if (max > UPPER_BOUND)
		max = UPPER_BOUND;
	if(max < LOWER_BOUND)
		max = LOWER_BOUND;
	return max;
}
int Classifier::getSchemaOrder()
{
	int order = 0;
	int p1 = 0, p2 = 0;
	for(int i = 0; i < COND_OFFSET; i++)
	{
		if(getIntervalMin(i) <= EPSILON && getIntervalMax(i) >= 1-EPSILON)
		{
			order ++;
		}
	}
	return order;
}

int Classifier::getSchemaLength()
{
	int length = 0;
	int p1 = 0, p2 = 0;
	for(int i = 0; i < COND_OFFSET; i++)
	{
		if(getIntervalMin(i) <= EPSILON && getIntervalMax(i) >= 1-EPSILON)
		{
			if(p1 == 0)
				p1 = i+1;
			else
				p2 = i+1;
		}
	}
	if(p2 == 0 && p1 !=0)
		length = 1;
	else
		length = p2 - p1;
	return length;
}


void Classifier::validateClassifier()
{
	//	cout<<"Come here"<<endl;
	for(int i = 0; i < COND_OFFSET; i++)
	{
		if(m_interval->point1[i] > UPPER_BOUND)
			m_interval->point1[i] = UPPER_BOUND;
		if(m_interval->point2[i] > UPPER_BOUND)
			m_interval->point2[i] = UPPER_BOUND;
		if(m_interval->point1[i] < LOWER_BOUND)
			m_interval->point1[i] = LOWER_BOUND;
		if(m_interval->point2[i] < LOWER_BOUND)
			m_interval->point2[i] = LOWER_BOUND;
	}
}
void Classifier::savePopulation(ofstream &of)
{
	
}
#ifdef MP
int Classifier::isASchema(double real_threshold, int id)
{
	int res = FALSE, check = TRUE;
	int i, pos = 0, num = 0;
	for(i = ADDR_OFFSET -1; i >= 0; i--)
	{
		if(	getIntervalMax(i)<=real_threshold+EPSILON)//0
		{
			pos++;
		}
		else if(getIntervalMin(i)>=real_threshold-EPSILON)// &&
		{
			num += (int)pow(2,pos);
			pos++;
		}
		else
		{
			check = FALSE;
			break;
		}
	}
	if(check == TRUE && num == (int)id/2) // Considering only the classifier which has the same address as the schema
	{
		if((id%2 == 0 && //getIntervalMin(ADDR_OFFFSET+num) <LOWER_BOUND+EPSILON &&
			getIntervalMax(ADDR_OFFSET + num) <= real_threshold+EPSILON)||
			//Considering only the classifier which the data at the address is 0
			(id%2 == 1 && getIntervalMin(ADDR_OFFSET + num) >=real_threshold-EPSILON))
			
		{
			res = TRUE;
			for(i = COND_OFFSET -1; i > ADDR_OFFSET -1; i--)
			{
				if(i!= ADDR_OFFSET + num)
				{
					if(getIntervalMin(i)> LOWER_BOUND + EPSILON ||
						getIntervalMax(i)< UPPER_BOUND - EPSILON)
					{
						res = FALSE;
						break;
					}
				}
			} 
		}
	}
	return res;
}
int Classifier::belongToSchema(double real_threshold, int id)
{
	int res = FALSE, check = TRUE;
	int i;
	int pos = 0, num=0;
	for(i = ADDR_OFFSET -1; i >= 0; i--)
	{
		if( getIntervalMax(i)<=real_threshold+EPSILON)//0
		{
			pos++;
		}
		else if(getIntervalMin(i)>=real_threshold-EPSILON )//1
		{
			num += (int)pow(2,pos);
			pos++;
		}
		else
		{
			check = FALSE;
			break;
		}
	}
	if(check == TRUE && num == (int)id/2) // Considering only the classifier which has the same address as the schema
	{
		if((id%2 == 0 && getIntervalMax(ADDR_OFFSET + num) <= real_threshold+EPSILON)||
			//Considering only the classifier which the data at the address is 0
			(id%2 == 1 && getIntervalMin(ADDR_OFFSET + num) >=real_threshold-EPSILON))
		{
			res = TRUE;
		}
	}
	
	
	return res;
}

#endif
void Classifier::print()
{
	for(int i = 0; i < COND_OFFSET; i++)
		cout<<getIntervalMin(i)<<", "<<getIntervalMax(i)<<" |";
	cout<<getAction()<<endl;
}
#ifdef MP

#endif

⌨️ 快捷键说明

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