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

📄 art_ext.c

📁 自适应共振神经网络可以实现自动学习与分类具有自适应的功能
💻 C
📖 第 1 页 / 共 5 页
字号:
  fscanf(LoadFile,"%i\n",&net->type);
  fscanf(LoadFile,"%i\n",&net->num_inputs);
  fscanf(LoadFile,"%i\n",&net->num_outputs);

  /* Load sub-components and allocate some extra space */
  if (net->type == ART) {
    if (LoadArt(&net->art,LoadFile)) return 2;
    if (AddCats(&net->art,5)) return 2;
  }
  else { /* if type == ARTMAP */
    if (LoadMap(&net->map,LoadFile)) return 2;
    /* Add a few unused nodes in case training resumes checking for memory */
    /* allocation failure each time.                                       */
    if (AddCats(&net->map.artA,5)) return 2;  
    if (net->map.artB.type!=NONE)   /* Don't add ArtB nodes if   */
      /* ArtB network is type NONE */
      if (AddCats(&net->map.artB,5)) return 2;
    if (AddMaps(&net->map)) return 2; 
  }
  fscanf(LoadFile,"%i\n",&net->num_patterns);
  fscanf(LoadFile,"%i\n",&net->num_epochs);

  fclose(LoadFile);
  
  net->initB  = TRUE;   /* The network is initialized                     */
  net->checkB = FALSE;  /* The network has not been check against the set */
  return 0;
}



/* --------------------------------------------------------------------------*/
/* SaveSet                                                                   */
/*    Given a pointer to a pattern set, and filename prefix, saves the       */
/*    pattern set to {filename}.pat file                                     */
/*                                                                           */
/* Arguments:                                                                */
/*      set         - A pointer to a set of patterns of type setTYPE         */
/*      file_prefix - A pointer to a charater string                         */
/*                                                                           */
/* Return Value:                                                             */
/*      0           - When sucessful                                         */
/*      1           - When set not initialized or NULL                       */
/*      2           - Error opening save file                                */
/* --------------------------------------------------------------------------*/
int VBPREFIX   SaveSet(setPTR set, char *file_prefix) {
  char  out_fname[84];
  FILE  *outfile;
  int   i,j;             /* Counters                                        */

  if ((set==NULL)||(set->initB!=TRUE))
    return 1;

  /* Add ".pat" to save file prefix */
  strncpy(out_fname,file_prefix,80);
  strcat(out_fname,".pat");
  outfile=fopen(out_fname,"w");

  if (outfile==NULL)
    return 2;

  fprintf(outfile,"%i\n",set->num_patterns);
  fprintf(outfile,"%i\n",set->num_inputs);
  fprintf(outfile,"%i\n",set->type_inputs);
  fprintf(outfile,"%i\n",set->num_outputs);
  fprintf(outfile,"%i\n",set->type_outputs);

  /* Save a pattern set.  Complement coding is dropped in the process. */
  for (i=0;i<set->num_patterns;i++) { 
    for (j=0;j<(set->num_inputs);j++)
      fprintf(outfile,"%f\n",(*(set->pattern[i].input+j)));

    /* If there are output patterns, save them */
    if (set->type_outputs!=NONE) {
      for (j=0;j<(set->num_outputs);j++)
	fprintf(outfile,"%f ",(*(set->pattern[i].output+j)));
      fprintf(outfile,"\n");
    }
    fprintf(outfile,"\n");
  }
  fclose(outfile);
  return 0;
}



/* --------------------------------------------------------------------------*/
/* LoadSet                                                                   */
/*         Given a pattern set pointer, and filename prefix, loads the       */
/*         pattern set from {filename}.pat                                   */
/*                                                                           */
/* Arguments:                                                                */
/*      set         - A pointer to a set of patterns of type setTYPE         */
/*      file_prefix - A pointer to a charater string                         */
/*                                                                           */
/* Return Value:                                                             */
/*      0           - If successful                                          */
/*      1           - When file not found                                    */
/*      2           - Memory Allocation Error                                */
/*      3           - When set NULL                                          */
/* --------------------------------------------------------------------------*/
int VBPREFIX   LoadSet(setPTR set, char *file_prefix) {
  char  in_fname[84];    /* A charcter string to be used as a filename      */
  FILE  *infile;         /* A pointer to a file                             */
  float temp_flt;
  int   i,j;
  
  if (set==NULL)
    return 3;

  /* Add ".pat" to end of input file prefix                                 */
  strncpy(in_fname,file_prefix,80);
  strcat(in_fname,".pat");

  errno=0;
  infile=fopen(in_fname,"r");
  
  if (infile==NULL) {
    if (errno==ENOENT) return 1;
    else return 2;
  }

  fscanf(infile,"%i\n",&set->num_patterns);
  fscanf(infile,"%i\n",&set->num_inputs);
  fscanf(infile,"%i\n",&set->type_inputs);
  fscanf(infile,"%i\n",&set->num_outputs);
  fscanf(infile,"%i\n",&set->type_outputs);

  set->initB = FALSE; /* Not loaded yet */
 
 /* Leave some space for adding more patterns                             */
  set->max_num_patterns = set->num_patterns + 5;     

  /* Allocate space for patterns                                           */
  set->pattern = (patTYPE *)malloc((size_t) 
				   (set->max_num_patterns*sizeof(patTYPE))); 
  if (set->pattern==NULL) return 2;

  /* Allocate space for input/output vectors patterns                      */
  for (i=0;i<set->max_num_patterns;i++) {
      set->pattern[i].input=(float *)malloc((size_t) (set->num_inputs*sizeof(float)));
      if (set->pattern[i].input==NULL) return 2;
      set->pattern[i].output=(float *)malloc((size_t) (set->num_outputs*sizeof(float)));
      if (set->pattern[i].output==NULL) return 2;
    }

  /* Read in the pattern set                                               */
  for (i=0;i<set->num_patterns;i++) { 
    for (j=0;j<set->num_inputs;j++) {
      fscanf(infile,"%f\n",&temp_flt);
      *(set->pattern[i].input+j)=temp_flt;
    }
    for (j=0;j<set->num_outputs;j++) {
      fscanf(infile,"%f ",&temp_flt);
      *(set->pattern[i].output+j)=temp_flt;
    }
    fscanf(infile,"\n",&temp_flt);
  }
  set->initB  = TRUE;  /* The set has been initialized                     */
  set->checkB = FALSE; /* The set has not been checked against the network */
  return 0;
}




/* ------------------------------------------------------------------------- */
/* AddPattern                                                                */
/*            Given a pattern set pointer and pattern data, adds the         */
/*            pattern to the pattern set.                                    */
/*                                                                           */
/* Arguments:                                                                */
/*     set         - A pointer to a set of patterns of type setTYPE          */
/*     num_inputs  - Number of input patterns (integer)                      */
/*     *input      - A pointer to a list of inputs num_inputs long (float)   */
/*     num_outputs - Number of output patterns (integer)                     */
/*     *output     - A pointer to a list of outputs num_outputs long (float) */
/*                                                                           */
/* Return values:                                                            */
/*           0 - When successful                                             */
/*           1 - When input or output size is incompatible                   */
/*           2 - When ANALOG data given for BINARY set type                  */
/*           3 - When ALALOG pattern data is not properly bounded [0 1]      */
/*           4 - When input or output vector has only zero values            */
/*           5 - When a memory allocation error occurs (out of memory)       */
/*           6 - When set uninitialized or NULL                              */
/* ------------------------------------------------------------------------- */
int VBPREFIX  AddPattern (setPTR set, int num_inputs, float *input,
                                    int num_outputs, float *output) {

  patPTR  temppat;      /* A temporary pattern pointer                     */
  int     check_val;    /* Return value from compatibility check           */
  int     i;            /* Counter                                         */
  
  if ((set==NULL)||(set->initB!=TRUE))
    return 6;

  /*
  /* --------- First check network & Pattern set compatibility ----------- */
  check_val=CheckPattern(set, num_inputs, input, num_outputs, output);
  if (check_val)
    return check_val;
  
  /* --------- If everything in order, then add the pattern -------------- */
  for (i=0;i<num_inputs;i++) 
    *(set->pattern[set->num_patterns].input+i)=(*(input+i));
  for (i=0;i<num_outputs;i++) 
    *(set->pattern[set->num_patterns].output+i)=(*(output+i));
  set->num_patterns++;

  /* If input compliment coding is active, compliment code new pattern     */
  if (set->style_inputs==COMPLIMENT)
    for (i=0;i<set->num_inputs;i++)
      *(set->pattern[set->num_patterns].input+(i+set->num_inputs))=(1-(*(input+i)));

  /* If output compliment coding is active, compliment code new pattern    */
  if (set->style_outputs==COMPLIMENT)
    for (i=0;i<set->num_outputs;i++) 
       *(set->pattern[set->num_patterns].output+(i+set->num_outputs))=(1-(*(output+i)));

  /* Check if hit maximum number of patterns */
  if (set->num_patterns==set->max_num_patterns) {
    temppat = AddPats(set->pattern,set->max_num_patterns,50,
		      set->num_inputs,set->num_outputs);
    if (temppat==NULL) return 5;
    else
      set->pattern = temppat;
    set->max_num_patterns += 50;
  }
  return 0;
}



/* ------------------------------------------------------------------------- */
/* MergeSets                                                                 */
/*     Given three pattern set prefixes, loads patterns using the first      */
/*     two pattern sets files, merges them and saves them and saves them     */
/*     using the third file prefix.                                          */
/*                                                                           */
/* Arguments:                                                                */
/*     file1_prefix - The first file to load from {file1_prefix}.pat         */
/*     file2_prefix - The second file to load from {file2_prefix}.pat        */
/*     save_prefix  - The file to save the megered set to {save_prefix}.pat  */
/*                                                                           */
/* Return errs:                                                              */
/*           0 - When successful                                             */
/*           1 - When input or output size is incompatible                   */
/*           2 - When file1 not found                                        */
/*           3 - When file2 not found                                        */
/*           4 - Memory Allocation Failure (out of memory)                   */
/* ------------------------------------------------------------------------- */
int VBPREFIX  MergeSets (char *file1_prefix, char *file2_prefix, char *save_prefix) {
  setTYPE set1;            
  setTYPE set2;
  char    in_fname[84];    /* A charcter string to be used as a filename   */
  FILE    *infile;         /* A pointer to a file                          */
  int     i;               /* Counter                                      */
  int     status;

  /* Add ".pat" to end of the file prefix                                  */
  strncpy(in_fname,file1_prefix,80);
  strcat(in_fname,".pat");

  /* Load sets                                                             */
  status=LoadSet(&set1,file1_prefix);
  if (status==1) return 2;
  if (status==2) return 4;
  status=LoadSet(&set2,file2_prefix);
  if (status==1) return 3;
  if (status==2) return 4;

  /* Check compatibility of the two sets                                   */
  if (set1.num_inputs  != set2.num_inputs)   return 1;
  if (set1.num_outputs != set2.num_outputs)  return 1;
  if (set1.type_inputs != set2.type_inputs)  return 1;
  if (set1.type_outputs!= set2.type_outputs) return 1;
  
  /* -------- If everything in order, then merge the sets ---------------- */
  for (i=0;i<set2.num_patterns;i++) {
    status=AddPattern(&set1, set2.num_inputs, set2.pattern[i].input,
		      set2.num_outputs, set2.pattern[i].output);
    if (status==4)
      return status;
  }
  SaveSet(&set1,save_prefix);
  return 0;
}


⌨️ 快捷键说明

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