📄 unh_cmac.c
字号:
/* 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++) { /* point to the weight vector for this RF */ weights = &ap[indexes[i] * (rsp_size + 1)]; /* compute the experience sum */ 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++) { /* point to the weight vector for this RF */ weights = &ap[indexes[i] * (rsp_size + 1)]; /* compute the experience sum */ 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);}/*************************************************************************** * map_cmac_input() * * * * Input: cmac_id, state[] * * Output: *weights[], rfmags[] * * * * Purpose: returns pointers to the weight vectors mapped * * by the input vector state[], and the associated * * receptive field magnitudes * * * ***************************************************************************/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);}/*************************************************************************** * clear_cmac_weights() * * * * Input: cmac_id * * Output: ap[] * * * * Purpose: Initialize CMAC memory to all 0's * * * ***************************************************************************/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); }/*************************************************************************** * deallocate_cmac() * * * * Input: cmac_id * * Output: TRUE or FALSE (success or not) * * * * Purpose: de-allocates the specified cmac * * * ***************************************************************************/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);}/*************************************************************************** * cmac_memory_usage() * * * * Input: cmac_id * * Output: returns # of memory cells (vectors) used * * * * Purpose: returns # of memory cells used (vectors not = 0) * * * ***************************************************************************/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);}/*************************************************************************** * get_cmac() * * * * Input: cmac_id, index, buffer[], count * * Output: returns # of vectors transferred * * * * Purpose: returns raw CMAC memory vectors to buffer * * * ***************************************************************************/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);}/*************************************************************************** * put_cmac() * * * * Input: cmac_id, index, buffer[], count * * Output: returns # of vectors transferred * * * * Purpose: writes raw CMAC memory vectors from buffer * * * ***************************************************************************/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);}/*************************************************************************** * set_cmac_rf_displacement() * * * * Input: cmac_id, displacement vector * * Output: none * * * * Purpose: writes displacement vector to CMAC storage * * * ***************************************************************************/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);}/*************************************************************************** * set_cmac_rf_magnitude() * * * * Input: cmac_id, RF function table * * Output: none * * * * Purpose: writes RF magnitude table to CMAC storage * * * ***************************************************************************/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);}/*************************************************************************** * save_cmac() * * * * Input: cmac_id, file name * * Output: FALSE (0) on failure * * * * Purpose: writes all of CMAC storage to a file * * * ***************************************************************************/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);}/*************************************************************************** * restore_cmac() * * * * Input: file name * * Output: cmac_id, or FALSE (0) on failure * * * * Purpose: restore all of CMAC storage from a file * * * ***************************************************************************/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 + -