📄 unh_cmac.c
字号:
int i; if (field_shape == ALBUS) { for (i=0; i<num_state; ++i) dv[i] = 1; } else { for (i=0; i<num_state; ++i) dv[i] = 1 + (2 * i); }}/*************************************************************************** * init_rfield_table() * * * * Input: RF_TABLE_SIZE, field_shape * * Output: rf[] * * * * Purpose: Initialize receptive field function * * * ***************************************************************************/static void init_rfield_table(int rf[], int table_size, int field_shape){ int i; long il; /* set default constant field magnitude */ for (i=0; i<table_size; ++i) rf[i] = 1; /* now handle special non-constant cases */ if (field_shape == LINEAR) { for (i=0; i<table_size; ++i) rf[i] = (i + 1); } /* now handle special non-constant cases */ if (field_shape == SPLINE) { for (i=0; i<table_size; ++i) { il = (long)(i + 1); rf[i] = (int)((((long)-2 * il * il * il) + ((long)3 * il * il * (long)table_size)) / (long)table_size / (long)table_size); } }}/* ********************************************************************* *//**************************************************************************The following CMAC functions CAN be called externally!/*************************************************************************** * allocate_cmac() * * * * Input: num_state,quant[],num_resp,num_cell,memory * * Output: returns CMAC ID (1 - 7) if ok, 0 for memory error * * * * Purpose: Allocate memory and initialize CMAC parameters * * * ***************************************************************************/int allocate_cmac(int num_state,int *qnt_state,int num_resp, int num_cell,int memory,int field_shape, int collide_flag){ int i,j; int *pntr; /* 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 */ /* allocate CMAC storage */ ap_size[i] = ((memory * (num_resp + 1)) + num_state + num_state + RF_TABLE_SIZE + 6) * sizeof(int); if (ap_a[i] = (int *)malloc(ap_size[i])) { pntr = ap_a[i]; *pntr++ = num_state; /* size of input state vector */ *pntr++ = num_resp; /* dimension of response vector */ *pntr++ = num_cell; /* generalization parameter */ *pntr++ = memory; /* # of response vectors in memory */ *pntr++ = field_shape; /* receptive field code */ *pntr++ = collide_flag; /* hashing collisions flag */ for (j = 0; j < num_state; ++j) *pntr++ = qnt_state[j]; /* input vector quantization */ init_disp_vector(pntr, num_state, field_shape); /* displacement vector */ pntr += num_state; init_rfield_table(pntr, RF_TABLE_SIZE, field_shape); /* RF function */ return(i); /* return cmac id number */ } else { return(FALSE); }}/*************************************************************************** * train_cmac() * * * * Input: cmac_id, state[], respns[], beta * * Output: ap[] * * * * Purpose: Train ap[] cells based on latest experience * * state[] is mapped to indexes[] by stoap() * * * ***************************************************************************/int train_cmac(int cmac_id,int *state,int *respns,int beta1,int beta2){ int i, j, *weights; static long sum[MAX_RESPONSE_SIZE], error[MAX_RESPONSE_SIZE]; static long delta1[MAX_RESPONSE_SIZE], delta2; 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[][] */ for (j = 0; j < rsp_size; j++) { sum[j] = 0; delta1[j] = 0; } delta2 = 0; /* Process CMACs with rectangular RF field functions */ if ((rf_shape == ALBUS) || (rf_shape == RECTANGULAR)) { /* get old response */ 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++) sum[j] /= gen_size; /* compute adjustment due to reponse error */ if (beta1 < 32) { for (j = 0; j < rsp_size; j++) { delta1[j] = ((long)respns[j] - sum[j]); if (delta1[j] >= 0) delta1[j] >>= beta1; else delta1[j] = -((-delta1[j])>>beta1); if (delta1[j] == 0) { if ((long)respns[j] > sum[j]) { delta1[j] = 1; } else if ((long)respns[j] < sum[j]) { 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++) { /* compute weight normalization adjustment */ if (beta2 < 32) { delta2 = (long)(respns[j] - weights[j]); if (delta2 >= 0) delta2 = (delta2 >> beta2); else delta2 = -((-delta2) >> beta2); } /* adjust the weight */ weights[j] += (int)delta1[j] + (int)delta2; } } in_learn = FALSE; return(TRUE); } /* Process CMACs with non-rectangular RF field functions */ /* get old response */ 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]; } /* 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); }/*************************************************************************** * adjust_cmac() * * * * Input: cmac_id, state[], drespns[], beta * * Output: ap[] * * * * Purpose: 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++) { /* adjust the weight */ weights[j] += (int)delta1[j]; } } in_learn = FALSE; return(TRUE); } /* Process CMACs with non-rectangular RF field functions */ /* update old response in direction of observed response */ for (j = 0; j < rsp_size; j++) error[j] = (long)drespns[j] * sum_rfield; /* 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 (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; } /* adjust the weight */ weights[j] += (int)delta1[j]; } } in_learn = FALSE; return(TRUE); }/*************************************************************************** * cmac_response() * * * * Input: cmac_id, state[] * * Output: respns[] * * * * Purpose: Get the response vector based on experience * * state[] is mapped to indexes[] by stoap() * * * ***************************************************************************/int cmac_response(int cmac_id,int *state,int *respns)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -