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

📄 environment.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 "environment.h"

Environment::Environment(int gseed, double gnoise)
{
	random = new Random();
	random->setSeed(gseed);
	noise = gnoise;
	//threshold = new double[10];
#ifdef MP
	data_info = new int[8];
	for(int i = 0; i < 8; i++)
		data_info[i] = 0;
#endif
	real_threshold = 0.5;	
}
double Environment::initDynEnv()
{
	count_cycle = 0;
	double temp[MAX_DYN_CYCLE] = {0.2,0.9}
	for(int i = 0; i < MAX_DYN_CYCLE; i++)
		threshold[i] = temp[i];
	real_threshold = threshold[count_cycle];
	return real_threshold;
}

double Environment::changeEnv()
{
	count_cycle++;
	real_threshold = threshold[count_cycle];
#ifdef MP
	for(int i = 0; i < 8; i++)
		data_info[i] = 0;
#endif
	return real_threshold;
}
#ifdef MP
int* Environment::getDataDistribution()
{
	return data_info;
}

#endif

input_t* Environment::getInput(int type)
{
	input_t *instance = new input_t;
	int i;
	for(i = 0; i < COND_OFFSET; i++)
	{
#ifdef REAL
#ifdef UNBIAS
		if(random->flip(0.5))
		{
			instance->attrs[i] = random->nextDouble(LOWER_BOUND, real_threshold);
		}
		else
		{
			instance->attrs[i] = random->nextDouble(real_threshold, UPPER_BOUND);
		}
#else
		instance->attrs[i] = random->nextDouble();
#endif //End unbias
#else  //binary
		instance->attrs[i] = random->nextInt(UPPER_BOUND);
#endif // End real
	}
	int theclass = getClass(instance, type);
	if (theclass < 0)
	{
		cout<<"Error at Environment, getInput function"<<endl;
		exit(0);
	}
	instance->action = theclass;
	
#ifdef COND_NOISE 
	//Noisy data
	if(type == TRAINING)
	{
		for(i = 0; i < COND_OFFSET; i++)
		{
			if(random->nextDouble() < noise)
			{
				if(random->flip(0.5))
					instance->attrs[i] += random->nextDouble();
				else
					instance->attrs[i] -= random->nextDouble();
				
				if(instance->attrs[i] < UPPER_BOUND)
					instance->attrs[i] = UPPER_BOUND;
				if(instance->attrs[i] > LOWER_BOUND)
					instance->attrs[i] = LOWER_BOUND;
			}			
		}
	}
#endif
	return instance;
}

int Environment::getClass(input_t *input, int type)
{
	int output=-1;
	int i;
	int temp[COND_OFFSET];
#ifdef MP 
	for(i = 0; i < COND_OFFSET; i++)
	{
		if(input->attrs[i] < real_threshold)
			temp[i] = 0;
		else
			temp[i] = 1;
	}
	int position = getDataPosition(temp);
	if(position < 0)
	{
		cout<<"Error at Environment, getClass function"<<endl;
		exit (0);
	}
	output = temp[position+ADDR_OFFSET ];
	if(output == 0)
		data_info[2*position]++;
	else
		data_info[2*position+1]++;
#endif
	
#ifdef CLASS_NOISE
	if(type == TRAINING)
	{
		if(random->nextDouble() < noise)
		{
			if(output == 1)
				output = 0;
			else
				output = 1;
		}
	}
#endif
	return output;
}

int Environment::getDataPosition(int binary[COND_OFFSET])
{
	int temp = -1;
	
#ifdef MP 
	temp = 0;
	for(int i = 0; i < ADDR_OFFSET ; i ++)
	{
		if(binary[i] == 1)
			temp += (int)(pow(2, ADDR_OFFSET - i - 1));
	}
	//	cout<<"Come here"<<endl;
#endif
	return temp;
}
int Environment::sgn(double num)
{
	int result;
	
	if(num >=0) 
		result = 1;
	else 
		result = -1;
	
	return result;
}

⌨️ 快捷键说明

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