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

📄 art_ext.c

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



/* ------------------------------------------------------------------------- */
/* LoadNet                                                                   */
/*          Given a filename prefix, loads {filename}.net into a network     */
/*          structure setting appropriate initial values.                    */
/*                                                                           */
/* Arguments:                                                                */
/*      net         -  A pointer to an initialized netowrk of type netTYPE   */
/*      file_prefix -  A pointer to a charater string                        */
/*                                                                           */
/* Return Value:                                                             */
/*      0           - If successful                                          */
/*      1           - When file not found                                    */
/*      2           - Memory Allocation Failure (out of memory)              */
/*      3           - When net NULL                                          */
/*-------------------------------------------------------------------------- */
int   LoadNet(netPTR net, char *file_prefix) 
{
    char  load_fname[84];
    FILE  *LoadFile;

    if (net==NULL)
        return 3;

    /* Add ".net" to load file prefix */
    strncpy(load_fname,file_prefix,80);
    strcat(load_fname,".net");

    LoadFile=fopen(load_fname,"r");

    if (LoadFile==NULL)
    {
        return 1;
    }

    net->initB = FALSE;   /* The network is not initialized yet */
    net->doneB = FALSE;
    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))
        {
            fclose(LoadFile);
            return 2;
        }
        if (AddCats(&net->art, 5)) 
        {
            fclose(LoadFile);
            return 2;
        }
    }
    else
    { /* if type == ARTMAP */
        if (LoadMap(&net->map, LoadFile)) 
        {
            fclose(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)) 
        {
            fclose(LoadFile);
            return 2;
        }
        if (net->map.artB.type != ART_TYPE_NONE)   /**< Don't add ArtB nodes if ArtB network is type ART_TYPE_NONE */
        {
            if (AddCats(&net->map.artB, 5)) 
            {
                fclose(LoadFile);
                return 2;
            }
        }
        if (AddMaps(&net->map)) 
        {
            fclose(LoadFile);
            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    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!=NODE_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    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");

    infile=fopen(in_fname,"r");

    if (infile==NULL)
    {
        return 1;
    }

    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) 
    {
        fclose(infile);
        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) 
        {
            fclose(infile);
            return 2;
        }
        set->pattern[i].output=(float *)malloc((size_t) (set->num_outputs*sizeof(float)));
        if (set->pattern[i].output==NULL)
        {
            fclose(infile);
            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,"%f\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;
}

/**
 * \fn int   AddPattern (setPTR set, int num_inputs, float *input,
                         int num_outputs, float *output) 
 * \brief Given a pattern set pointer and pattern data, adds the pattern to the pattern set.
 * \param set A pointer to a set of patterns of type setTYPE
 * \param num_inputs Number of input patterns (integer)
 * \param input A pointer to a list of inputs num_inputs long (float)
 * \param num_outputs Number of output patterns (integer)
 * \param output A pointer to a list of outputs num_outputs long (float)
 * \retval 0 When successful
 * \retval 1 When input or output size is incompatible
 * \retval 2 When NODE_ANALOG data given for NODE_BINARY set type
 * \retval 3 When NODE_ANALOG pattern data is not properly bounded [0 1]
 * \retval 4 When input or output vector has only zero values
 * \retval 5 When a memory allocation error occurs (out of memory)
 * \retval 6 When set uninitialized or NULL
 */
int   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 == ART_STYLE_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 == ART_STYLE_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                   */

⌨️ 快捷键说明

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