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

📄 art_int.c

📁 自适应共振神经网络可以实现自动学习与分类具有自适应的功能
💻 C
📖 第 1 页 / 共 3 页
字号:

  SaveArt(&map->artA,SaveFile);
  SaveArt(&map->artB,SaveFile);

  /* Save map weights */
  for (i=0;i<map->artA.nodes_used;i++) {
    for (j=0;j<map->artB.nodes_used;j++)
      fprintf(SaveFile,"%f ",(*(*(map->map_weight+i)+j)));
    fprintf(SaveFile,"\n");
  }
  fprintf(SaveFile,"\n");
}


/* ------------------------------------------------------------------------- */
/* ActivateArt                                                               */
/*           Given a pointer to an art module, and a pointer to a input      */
/*           vector, calculates and sets current category activations and    */
/*           winning current winnning category number.                       */
/* ------------------------------------------------------------------------- */
void PREFIX  ActivateArt(artPTR art, float *input) {
  float sum_inputs;            /* Sum of all the input patterns             */
  float max_cat;               /* Largest category activation               */
  int   win_cat;               /* Category which wins the competition       */
  int   matchB;                /* Boolean to indicate when a match is found */
  int   num_elig;              /* Number of eligible nodes left             */
  int   i,j;                   /* Counters                                  */

  /* --------------------- Check Type of Art Module ----------------------- */
  if (art->type == NONE) {
    /* If none, then category activation is just the input string ----      */
    for (i=0;i<art->num_inputs;i++)
      *(art->cat+i)=(*(input+i));
    return;
  }

  /* Check if there are any used nodes                                      */
  else if (art->nodes_used==0) {
    art->win_cat = -1;
    return;
  }

  else {
    /* ------- If art module, calculate output category activations ------- */
    /* Initially all nodes are eligible to win the competition              */
    num_elig=art->nodes_used;
    for (i=0;i<art->nodes_used;i++) 
      *(art->elig+i)=TRUE;

    /* Activate categories                                                  */
    for (i=0;i<art->nodes_used;i++) {
      *(art->sum_IW+i)=0;
      for (j=0;j<art->num_inputs;j++)
	/* NOTE: Intersection depends in art type. (Fuzzy or Art1)          */
	*(art->sum_IW+i)+=Intersect((*(*(art->weight+i)+j)),(*(input+j)),art->type); 
      *(art->cat+i)=(*(art->elig+i))*((*(art->sum_IW+i))/(ALPHA+(*(art->sum_weights+i))));
    }
    
    matchB=FALSE;
    while (!matchB) {    
      /* Find most activated category */
      max_cat=0;
      win_cat=0;
      for (i=0;i<art->nodes_used;i++) 
	if (((*(art->cat+i))>max_cat)&&((*(art->elig+i))!=0)) {
	  max_cat=(*(art->cat+i));
	  win_cat=i;
	}
      
      /* Find the sum of the inputs                                          */
      /* When compliment coded, sum of inputs equal to half network inputs.  */
      if (art->style==COMPLIMENT)
	sum_inputs=(art->num_inputs)/2;
      else {
	sum_inputs=0;   
	for (i=0;i<art->num_inputs;i++)
	  sum_inputs+=(*(input+i));   
      }



      /* Test to see if winning category meets vigilence level               */
      if (((*(art->sum_IW+win_cat))/sum_inputs)>=art->vigil)  {
	/* If so, found a match                                              */
	matchB = TRUE;                       
	/* Set winner-take-all category activations                          */
	for (i=0;i<art->nodes_used;i++) {
	  if (i==win_cat)
	    *(art->cat+i)=1;
	  else
	    *(art->cat+i)=0;
	}

	art->win_cat = win_cat;
      }
      else {  
	/* Otherwise, node becomes ineligible */
	*(art->elig+win_cat)=FALSE;       
	art->num_reset++;                     
	num_elig--;
	
	/* Check if any nodes still eligible */
	if (num_elig==0) {
	  /* Return that no match found */
	  art->win_cat = -1;
	  return;
	}
      }
    }
  }
}



/* ------------------------------------------------------------------------- */
/* TrainArtPat                                                               */
/*           Given a pointer to an activated network, and a pointer to an    */
/*           input pattern, trains the Art module based on the current       */
/*           winning node and the input pattern.                             */
/*                                                                           */
/* Return Value:                                                             */
/*           0 - If successful                                               */
/*           1 - Memory allocation failure                                   */
/* ------------------------------------------------------------------------- */
int PREFIX  TrainArtPat(artPTR art, float *input) {
  int      i;               /* Counter                                     */
  

  /* ------- Train Art Module with Fast Learning and Slow Recode --------- */
  /*       NOTE: Slow recode is eliminated if beta is set to one.          */

  /* When there was no winning category                                    */
  if (art->win_cat==-1) {
    art->win_cat=art->nodes_used;      /* Winner becomes next unused node  */
    art->nodes_used++;                 /* Increment used nodes counter     */



    /* If hit the max number of allocated Art nodes                        */
    /*    Allocate more space for Art nodes                                */
    if (art->nodes_used==art->max_nodes)
      if (AddCats(art,25)) return 1;

    *(art->commit+art->nodes_used)=TRUE; /* Note it as now used          */

    /* Set winner-take-all category activations                            */
    for (i=0;i<art->nodes_used;i++) {
      if (i==art->win_cat)
	*(art->cat+i)=1;
      else
	*(art->cat+i)=0;
    }

    /* Fast learning for uncommited node.                       (beta = 1) */
    for (i=0;i<art->num_inputs;i++)
      *(*(art->weight+art->win_cat)+i)=Intersect((*(input+i)),(*(*(art->weight+art->win_cat)+i)),art->type);                     
  }
  else {
    /* Slow Recoding for commited node.                         (beta < 1) */
    for (i=0;i<art->num_inputs;i++)
      *(*(art->weight+art->win_cat)+i)=((art->beta)*Intersect((*(input+i)),(*(*(art->weight+art->win_cat)+i)),art->type))+((1-(art->beta))*(*(*(art->weight+art->win_cat)+i)));
  }
  
  /* Calculate Art weight sum for speeding up later calculations           */
  *(art->sum_weights+art->win_cat)=0;
  for (i=0;i<art->num_inputs;i++)
    *(art->sum_weights+art->win_cat)+=(*(*(art->weight+art->win_cat)+i));

  return 0;
}



/* ------------------------------------------------------------------------- */
/* TrainMapPat                                                               */
/*           Given a pointer to a mapfield, and a pointer to a single        */
/*           pattern trains the mapfield network on the pattern.             */
/*                                                                           */
/* Return Value:                                                             */
/*           0 - If successful                                               */
/*           1 - When memory allocation failure                              */
/* ------------------------------------------------------------------------- */
int PREFIX  TrainMapPat(mapPTR map, patPTR pat) {
  float    sum_inputs;      /* Sum of all the active inputs                */
  float    sum_cat;         /* Sum of all the active Art-B categories      */
  float    sum_map;         /* Sum of activations of map nodes             */
  int      doneB;           /* Boolean to signal when done with pattern    */
  int      i,j;             /* Counters                                    */
  

  /* -------------- First activate and train Art-B network --------------- */
  /* Find winning Art-B category                                           */
  ActivateArt(&map->artB,pat->output);

  /* If Art-B not of type NONE train it                                    */
  if (map->artB.type!=NONE) { 
    if (TrainArtPat(&map->artB,pat->output)) return 1;

    /* Check if TrainArtPat allocated new space for ArtB                   */
    if (map->maxB_nodes!=map->artB.max_nodes)
      if (AddMaps(map)) return 1;
  }

  /* -------- Find an Art-A category that meets mapfield vigilence -------- */
  doneB           = FALSE;                 /* Haven't found one yet         */
  map->artA.vigil = map->base_vigil;       /* Reset vigilence to base level */
      
  while (!doneB) {
    /* Find the winning category */
    ActivateArt(&map->artA,pat->input);  

    /* When no previously used Art-A node was able to win, a new Art-A node */
    /* is created and trained on the mapfield                               */
    if (map->artA.win_cat == -1) {
      /* Train New Unused Node in ArtA Module                               */
      if (TrainArtPat(&map->artA,pat->input)) return 1;
      /* Train mapfield weights for new node                                */
      for (i=0;i<map->artB.nodes_used;i++) 
	*(*(map->map_weight+map->artA.win_cat)+i)=(*(map->artB.cat+i));
      /* Check if TrainArtPat allocated new space for ArtA                  */
      if (map->maxA_nodes!=map->artA.max_nodes)
	if (AddMaps(map)) return 1;


      
      doneB=TRUE;
    }

    /* Otherwise check map vigilence for the used node                       */
    else { /* Calculate Mapfield Activations */
      for (i=0;i<map->artB.nodes_used;i++)         /* For each mapfield node */ 
	*(map->map+i)=(*(*(map->map_weight+map->artA.win_cat)+i)); 

      sum_map = 0;
      sum_cat = 0;
      for (i=0;i<map->artB.nodes_used;i++) {
	sum_map += FuzzInt((*(map->map+i)),
			   (*(map->artB.cat+i)));
	sum_cat += FuzzUni((*(map->map+i)),
			   (*(map->artB.cat+i)));
      }
      if (map->artB.type != NONE)
	sum_cat = 1;
      
      /* Calculate sum of inputs                                              */  
      sum_inputs=0;                        /* Note: When compliment coded,    */
      for (i=0;i<map->artA.num_inputs;i++) /*       sum of inputs is equal to */
	sum_inputs+=(*(pat->input+i));     /*       half the network inputs.  */
      


      /* Check vigilence at mapfield                                           */
      if ((sum_map/sum_cat)<map->vigil) {
	/* If mapfield mismatch then ArtA isn't done                           */
	doneB=FALSE;                       
	/* Error causes increase in Art-A vigilence unless doing so exceeds    */
	/* the highest vigilence level.  If this is the case, then mismatch    */
	/* was caused by inconsistent or noisy data so ignore and go on.       */
	map->artA.vigil=((*(map->artA.sum_IW+map->artA.win_cat))/sum_inputs)
	+0.0001;
	if (map->artA.vigil>1.0) 
	  doneB=TRUE;
	else
	  map->num_mismatch++;
    }
      else {      
	/* Train mapfield weights                                              */
	for (i=0;i<map->artB.nodes_used;i++) 
	*(*(map->map_weight+map->artA.win_cat)+i)=(*(map->artB.cat+i));
	/* Train ArtA Module                                                   */
      if (TrainArtPat(&map->artA,pat->input)) return 1;
	/* Check if TrainArtPat allocated new space for ArtA                   */
      if (map->maxA_nodes!=map->artA.max_nodes)
	if (AddMaps(map)) return 1;
	doneB=TRUE;
      }
    }
  }
  return 0;
}



/* ------------------------------------------------------------------------- */
/* CheckNetSet                                                               */
/*           Given a pointer to a network and a pointer to a pattern set     */
/*           checks to make sure they are compatible.                        */
/*                                                                           */
/* Return values:                                                            */
/*           0 - No errors, they are compatible                              */
/*           3 - ANALOG inputs given to BINARY network                       */
/*           4 - Input Size Incompatible                                     */
/*           5 - Output Size Incompatible                                    */
/*           6 - No output patterns for ARTMAP network                       */
/*           7 - Memory Allocation failure (out of memory)                   */
/* ------------------------------------------------------------------------- */
int PREFIX  CheckNetSet(netPTR net, setPTR set) {

  if (net->type==ART) {
    /* Check Art Component Input Type Compatibility                        */
    if ((net->art.type==ART1)&&(set->type_inputs==ANALOG))        return 3;

    if ((net->art.style==NONE)&&
	(net->num_inputs!=set->num_inputs))                       return 4;
    if ((net->art.style==COMPLIMENT)&&
	(net->num_inputs!=2*(set->num_inputs)))                   return 4;

    /* Check if set data needs to be compliment coded */
    if (Compliment(set,net->art.style,NONE))                      return 7;
  }

  if (net->type==ARTMAP) {
    if ((net->map.artA.type==ART1)&&(set->type_inputs==ANALOG))   return 3;
    if ((net->map.artB.type==ART1)&&(set->type_outputs==ANALOG))  return 3;
    if (set->type_outputs==NONE)                                  return 6;

    /* Check input size                                                   */
    if ((net->map.artA.style==NONE)&&
	(net->num_inputs!=set->num_inputs))                       return 4;
    if ((net->map.artA.style==COMPLIMENT)&&
	(net->num_inputs!=2*(set->num_inputs)))                   return 4;

    /* Check output size                                                  */
    if ((net->map.artB.style==NONE)&&
	(net->num_outputs!=set->num_outputs))                     return 5;
    if ((net->map.artB.style==COMPLIMENT)&&
	(net->num_outputs!=2*(set->num_outputs)))                 return 5;
    if ((net->map.artB.type==NONE)&&
	(net->num_outputs!=set->num_outputs))                     return 5;

    /* Check if set data needs to be compliment coded                     */
    if (Compliment(set,net->map.artA.style,net->map.artB.style))  return 7;
  }

  /* Everything looks ok, so mark as checked                              */
  net->checkB = TRUE;  
  set->checkB = TRUE;
  /* Training not done since either pattern set or network changed        */
  net->doneB  = FALSE;
  return 0;
}





⌨️ 快捷键说明

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