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

📄 cmac.cpp

📁 CMAC小脑模型神经网络的具体实现算法。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        /* compute the experience sum */
		
    	for (j = 0; j < rsp_size; j++)
			sum[j] += (long)weights[j] * rfield[i];
	}
	
	/* update old response in direction of observed response */

    for (j = 0; j < rsp_size; j++)
		error[j] = ((long)respns[j] * sum_rfield) - sum[j];

	/* adjust all weights of all RFs for response */
	
	for (i = 0; i < gen_size; i++) 
	{
				
		/* point to the weight vector for this RF */			
		weights = &ap[indexes[i] * (rsp_size + 1)];			
    	for (j = 0; j < rsp_size; j++)  
		{
	    	
			/* compute adjustment due to reponse error */
		
			if (beta1 < 32) 
			{
				if (error[j] >= 0) 
					delta1[j] = (((error[j] * rfield[i]) >> beta1) / sum2_rfield);
				else 
					delta1[j] = -((((-error[j]) * rfield[i]) >> beta1) / sum2_rfield);
		    	if (delta1[j] == 0) 
				{
					if (error[j] > 0)
		        		delta1[j] = 1;
					else if (error[j] < 0)
		        		delta1[j] = -1;
				}
			}

			/* compute weight normalization adjustment */
	
			if (beta2 < 32) 
			{
				delta2 = (long)(respns[j] - weights[j]) * sum_rfield * rfield[i];
				if (delta2 >= 0)
					delta2 = (delta2 >> beta2) / sum2_rfield;
				else
					delta2 = -(((-delta2) >> beta2) / sum2_rfield);
			} 
				
			/* adjust the weight */
					
			weights[j] += (int)delta1[j] + (int)delta2;
		}
	}
		
	in_learn = FALSE;
	return(TRUE);
	
}

//根据最近经验状态值训练存储区单元
//Train ap[] cells based on latest experience state[] is mapped to indexes[] by stoap() 
int adjust_cmac(int cmac_id,int *state,int *drespns,int beta1)
{
	int i, j, *weights;
	static long error[MAX_RESPONSE_SIZE];
	static long delta1[MAX_RESPONSE_SIZE];

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */ 
		
	in_learn = TRUE;

	setup(cmac_id);  	/* init CMAC parameters */

	stoap(state);  	/* generate indices into ap[][] */

	/* Process CMACs with rectangular RF field functions */
	
	if ((rf_shape == ALBUS) || (rf_shape == RECTANGULAR)) 
	{
	
    	/* compute adjustment due to reponse error */

    	for (j = 0; j < rsp_size; j++) 
		{
			delta1[j] = (long)drespns[j];
			if (delta1[j] >= 0)
				delta1[j] >>= beta1;
			else
				delta1[j] = -((-delta1[j])>>beta1);
	    	if (delta1[j] == 0) 
			{
				if (drespns[j] > 0) {
	        		delta1[j] = 1;
				} 
				else if (drespns[j] < 0) 
				{
	        		delta1[j] = -1;
	        	}
			}
		}

		/* adjust all weights of all RFs for response */
	
		for (i = 0; i < gen_size; i++) 
		{
				
			/* point to the weight vector for this RF */
						
			weights = &ap[indexes[i] * (rsp_size + 1)];
					
	    	for (j = 0; j < rsp_size; j++) 
			{
	    		weights[j] += (int)delta1[j];
			}
		}
		
		in_learn = FALSE;
		return(TRUE);
	
	} 

    for (j = 0; j < rsp_size; j++)
		error[j] = (long)drespns[j] * sum_rfield;

	for (i = 0; i < gen_size; i++) 
	{
			
		weights = &ap[indexes[i] * (rsp_size + 1)];		
    	for (j = 0; j < rsp_size; j++)  
		{
	    	
			/* compute adjustment due to reponse error */
		
			if (error[j] >= 0) 
				delta1[j] = (((error[j] * rfield[i]) >> beta1) / sum2_rfield);
			else 
				delta1[j] = -((((-error[j]) * rfield[i]) >> beta1) / sum2_rfield);
	    	if (delta1[j] == 0) 
			{
				if (error[j] > 0)
	        		delta1[j] = 1;
				else if (error[j] < 0)
	        		delta1[j] = -1;
			}

			weights[j] += (int)delta1[j];
		}
	}
		
	in_learn = FALSE;
	return(TRUE);
	
}

int cmac_response(int cmac_id,int *state,int *respns)
/* get the response vector based on experience */
{
    int i, j, *weights;
    static long sum[MAX_RESPONSE_SIZE];

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	stoap(state);  /* generate indices into ap[][] */

	/* Process CMACs with rectangular RF field functions */
	
	if ((rf_shape == ALBUS) || (rf_shape == RECTANGULAR)) 
	{
	
	    for (j = 0; j < rsp_size; j++) 
	    	sum[j] = 0;

		for(i = 0; i < gen_size; i++) 
		{

			weights = &ap[indexes[i] * (rsp_size + 1)];

	    	for (j = 0; j < rsp_size; j++)
				sum[j] += (long)weights[j];
		}

	    for (j = 0; j < rsp_size; j++)
			respns[j] = (int)(sum[j] / gen_size);
			
		return(TRUE);
		
	}

	/* Process CMACs with non-rectangular RF field functions */
	
    for (j = 0; j < rsp_size; j++) 
    	sum[j] = 0;

	for(i = 0; i < gen_size; i++) 
	{  
				
		weights = &ap[indexes[i] * (rsp_size + 1)];

    	for (j = 0; j < rsp_size; j++)
			sum[j] += (long)weights[j] * rfield[i];
	}

    for (j = 0; j < rsp_size; j++)
		respns[j] = (int)(sum[j] / sum_rfield);
			
	return(TRUE);
}

int map_cmac_input(int cmac_id,int *state,int *weights[], int *rfmags)
/* get the response vector based on experience */
{
    int i;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	stoap(state);  /* generate indices into ap[][] */

	/* Process CMACs with rectangular RF field functions */
	
	if ((rf_shape == ALBUS) || (rf_shape == RECTANGULAR)) 
	{
	
		for(i = 0; i < gen_size; i++) 
		{
		
			/* point to the weight vector for this RF */
					
			weights[i] = &ap[indexes[i] * (rsp_size + 1)];
			
			/* return the constant RF magnitude		  */
			
			rfmags[i] = 1;
				
		}

		return(TRUE);
	}

	/* Process CMACs with non-rectangular RF field functions */
	
	for(i = 0; i < gen_size; i++) 
	{  
	
		/* point to the weight vector for this RF */
					
		weights[i] = &ap[indexes[i] * (rsp_size + 1)];
				
		/* return the RF magnitudes	*/
			
		rfmags[i] = (int)rfield[i];
	}

	return(TRUE);
}


int clear_cmac_weights(int cmac_id)
{
    int i, j;
	int *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	pntr = ap;
	j = mem_size * (rsp_size + 1);
    for (i = 0; i < j; i++) 
		*pntr++ = 0;
    
    return(TRUE);
 }

int deallocate_cmac(int cmac_id)
{

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	free((void *)ap_a[cmac_id]);    /* free the cmac memory */
	ap_a[cmac_id] = 0;              /* mark cmac released   */
	ap_size[cmac_id] = 0;
	return(TRUE);
}


int cmac_memory_usage(int cmac_id)
{
	int i;
	int value;
	int *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	pntr = &ap[rsp_size];
	value = 0;
	for (i = 0; i < mem_size; i++) 
	{
		if (*pntr != 0) 
			++value;
		pntr += (rsp_size + 1);
	}
	return(value);
}

int get_cmac(int cmac_id,int index,int *buffer,int count)
{
	int i, j, *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	pntr = buffer;

	for (i = 0; i < count; i++) 
	{
		if ((index + i) >= mem_size) break;
		for(j = 0; j <= rsp_size ; j++) 
		{
			*pntr++ = ap[((index + i) * (rsp_size + 1)) + j];
		}
	}
	return(i);
}

int put_cmac(int cmac_id,int index,int *buffer,int count)
{
	int i, j, *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	pntr = buffer;

	for (i = 0; i < count; i++) 
	{
		if ((index + i) >= mem_size) 
			break;
		for(j = 0; j <= rsp_size ; j++) 
		{
			ap[((index + i) * (rsp_size + 1)) + j] = *pntr++;
		}
	}
	return(i);
}


int set_cmac_rf_displacement(int cmac_id,int *buffer)
{
	int i, *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	/* only customize "custom" CMACs */
	if (rf_shape != CUSTOM) 
		return(FALSE);

	pntr = buffer;

	for (i = 0; i < st_size; i++) 
		disp[i] = *pntr++;

	return(TRUE);
}


int set_cmac_rf_magnitude(int cmac_id,int *buffer)
{
	int i, *pntr;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */

	setup(cmac_id);  /* init CMAC parameters */

	/* only customize "custom" CMACs */
	if (rf_shape != CUSTOM) 
		return(FALSE);

	pntr = buffer;

	for (i = 0; i < RF_TABLE_SIZE; i++) 
		rfieldt[i] = *pntr++;

	return(TRUE);
}


int save_cmac(int cmac_id,char *filename)
{
	int i;

	if ((cmac_id < 1)|(cmac_id > NUM_CMACS)|(ap_a[cmac_id] == 0))
		return(FALSE);  /* reject bad ID */


	/* open the file */ 
	if ((i = open(filename, O_BINARY | O_CREAT | O_WRONLY,
			     S_IREAD | S_IWRITE)) == -1) 
		return(FALSE);

	/* write size of CMAC storage followed by data */
	write(i, &ap_size[cmac_id], sizeof(int));
	write(i, ap_a[cmac_id], ap_size[cmac_id]);

	close(i);

	return(TRUE);
}


int restore_cmac(char *filename)
{
	int i, j;

	/* initialize data structures on first call to driver */

	if (first_call) 
	{
		genmap();
		for (i = 0; i <= NUM_CMACS; i++) 
			ap_a[i] = 0;
		first_call = FALSE;
	}

	/* find an empty id */

	for (i = 1; i <= NUM_CMACS; i++) 
		if (ap_a[i] == 0) 
			break;
	if (i > NUM_CMACS) 
		return(FALSE);  /* return 0 for no more cmacs */

	/* open the file */ 
	if ((j = open(filename, O_BINARY | O_RDONLY)) == -1) 
		return(FALSE);

	/* allocate CMAC storage and restore it from file */
	read(j, &ap_size[i], sizeof(int));
	if (ap_a[i] = (int *)malloc(ap_size[i])) 
	{
		read(j, ap_a[i], ap_size[i]);
		close(j);
		return(i);                  /* return cmac id number */
	} 
	else 
	{
		close(j);
		return(FALSE);
	}

}


⌨️ 快捷键说明

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