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

📄 art_int.c

📁 自适应共振神经网络可以实现自动学习与分类具有自适应的功能
💻 C
📖 第 1 页 / 共 4 页
字号:
        art->sum_weights = (float *)malloc((size_t)(art->max_nodes * sizeof(float)));
        if (art->sum_weights == NULL) 
        {
            free(art->cat);
            art->cat = NULL;
            free(art->elig);
            art->elig = NULL;
            free(art->commit);
            art->commit = NULL;
            free(art->weight);
            art->weight = NULL;
            free(art->weight[0]);
            art->weight[0] = NULL;
            return 1;
        }

        art->sum_IW = (float *)malloc((size_t)(art->max_nodes * sizeof(float)));
        if (art->sum_IW==NULL) 
        {
            free(art->cat);
            art->cat = NULL;
            free(art->elig);
            art->elig = NULL;
            free(art->commit);
            art->commit = NULL;
            free(art->weight);
            art->weight = NULL;
            free(art->weight[0]);
            art->weight[0] = NULL;
            free(art->sum_weights);
            art->sum_weights = NULL;
            return 1;
        }


        /**
        * Read in weights 
        */
        for (i = 0; i < art->nodes_used; i++) 
        {
            for (j = 0; j < art->num_inputs; j++) 
            {
                fscanf(LoadFile, "%f ", &temp_flt);
                *(*(art->weight + i) + j) = temp_flt;
            }
            fscanf(LoadFile, "\n");
        }
        fscanf(LoadFile, "\n");

        /**
        * Re-Calculate initial weight sums 
        */
        for (i = 0; i < art->max_nodes; i++) 
        {
            *(art->sum_weights + i) = 0;
            for (j = 0; j < art->num_inputs; j++)
            {
                *(art->sum_weights + i) += (*(*(art->weight + i) + j));
            }
        }

        /**
        * Set Initial Commitments of used nodes to true 
        */
        for (i = 0; i < art->nodes_used; i++)
        {
            *(art->commit + i) = TRUE;
        }
    }

    return 0;
}

/**
 * \fn int LoadMap(mapPTR map, FILE *LoadFile)
 * \brief Load network map
 * Given a pointer to a file, loads mapfield data, setting
 * appropriate initial values.
 * \param map the map
 * \param LoadFile the data file
 * \retval 0 Successful
 * \retval 1 Out of memory
 */
int LoadMap(mapPTR map, FILE *LoadFile)
{
    float temp_flt;
    int i, j;

    fscanf(LoadFile, "%f\n", &map->vigil);
    fscanf(LoadFile, "%f\n", &map->base_vigil);

    map->num_mismatch = 0;                /**< No mismatches yet */

    if (LoadArt(&map->artA, LoadFile))
    {
        return 1;
    }
    if (LoadArt(&map->artB, LoadFile))
    {
        return 1;
    }

    map->maxA_nodes = map->artA.max_nodes;
    map->maxB_nodes = map->artB.max_nodes;

    map->map = (float *)malloc((size_t)(map->artB.max_nodes * sizeof(float)));
    if (map->map == NULL)
    {
        return 1;
    }

    map->map_weight = (float **)malloc((size_t)(map->artA.max_nodes * sizeof(float *)));
    if (map->map_weight == NULL)
    {
        free(map->map);
        map->map = NULL;
        return 1;
    }
    map->map_weight[0] = (float *)malloc((size_t)((map->artA.max_nodes * map->artB.max_nodes) * sizeof(float)));
    if (map->map_weight[0] == NULL)
    {
        free(map->map);
        map->map = NULL;
        free(map->map_weight);
        map->map_weight = NULL;
        return 1;
    }
    for(i = 1; i < map->artA.max_nodes; i++)
    {
        map->map_weight[i] = map->map_weight[i - 1] + map->artB.max_nodes;
    }

    /**< Read in map weights */
    for (i = 0; i < map->maxA_nodes; i++)
    {
        for (j = 0; j < map->maxB_nodes; j++)
        {
            fscanf(LoadFile,"%f ", &temp_flt);
            *(*(map->map_weight + i) + j) = temp_flt;
        }
        fscanf(LoadFile, "\n");
    }
    fscanf(LoadFile, "\n");

    return 0;
}

/**
 * \fn void SaveArt(artPTR art, FILE *SaveFile) 
 * \brief Given a pointer to a file, Saves art network data.
 * \param art A pointer to ART
 * \SaveFile the file
 */
void SaveArt(artPTR art, FILE *SaveFile) 
{
    int i, j;

    fprintf(SaveFile, "%i\n", art->type);
    fprintf(SaveFile, "%i\n", art->num_inputs);
    fprintf(SaveFile, "%i\n", art->nodes_used);

    if (art->type != ART_TYPE_NONE) 
    {
        fprintf(SaveFile, "%i\n", art->style);
        fprintf(SaveFile, "%f\n", art->beta);
        fprintf(SaveFile, "%f\n", art->vigil);

        /**< Save weights */
        for (i = 0; i < art->nodes_used; i++) 
        {
            for (j = 0; j < art->num_inputs; j++)
            {
                fprintf(SaveFile, "%f ", (*(*(art->weight + i) + j)));
            }
            fprintf(SaveFile, "\n");
        }
        fprintf(SaveFile, "\n");
    }
}


/**
 * \fn void SaveMap(mapPTR map, FILE *SaveFile)
 * \brief Given a pointer to a file, Saves mapfield data.
 * \param map A pointer to the mapfield.
 * \SaveFile the file.
 */
void SaveMap(mapPTR map, FILE *SaveFile) 
{
    int i, j;

    fprintf(SaveFile, "%f\n", map->vigil);
    fprintf(SaveFile, "%f\n", map->base_vigil);

    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");
}

/**
 * \fn int CheckPattern (setPTR set, int num_inputs, float *input, int num_outputs, 
                         float *output) 
 * \brief Check the pattern values valid
 *            Given a pattern set pointer and pattern data, check to make
 *            the pattern values fit the declared pattern type (NODE_BINARY or
 *            NODE_ANALOG) and if the are ANALOG that they are all between [0 1].
 * \param set A pointer to the set
 * \param num_inputs input nodes number
 * \param input the input value
 * \param num_outputs output nodes number
 * \param output the output value
 * \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 ALALOG pattern data is not properly bounded [0 1]
 * \retval 4 When input or output vector has only zero values
 */
int CheckPattern (setPTR set, int num_inputs, float *input, int num_outputs, 
                  float *output) 
{
    int i;
    int zero_flag;

    /**< Check size of input/output patterns                                  */
    if  (set->num_inputs != num_inputs)   
    {
        return 1;
    }
    if  ((set->type_outputs != NODE_NONE)&&(set->num_outputs != num_outputs)) 
    {
        return 1;
    }

    /**< Check Input for NODE_ANALOG pattern in NODE_BINARY set                         */
    if (set->type_inputs == NODE_BINARY)
    {
        for (i = 0; i < num_inputs; i++)
        {
            if (((*(input + i)) != 1.0)&&((*(input + i)) != 0.0)) return 2;
        }
    }

    /**< Check Input for improperly bounded NODE_ANALOG patterns                   */
    if (set->type_inputs == NODE_ANALOG)
    {
        for (i = 0; i < num_inputs; i++)
        {
            if (((*(input + i)) > 1.0)||((*(input + i)) < 0.0)) return 3;
        }
    }

    /**< Check Output for NODE_ANALOG pattern in NODE_BINARY set                        */
    if (set->type_outputs == NODE_BINARY)
    {
        for (i = 0; i < num_outputs; i++)
        {
            if (((*(output + i)) != 1.0)&&((*(output + i)) != 0.0)) return 2;
        }
    }

    /**< Check Output for improperly bounded NODE_ANALOG patterns                  */
    if (set->type_outputs == NODE_ANALOG)
    {
        for (i = 0; i < num_outputs; i++)
        {
            if (((*(output + i)) > 1.0)||((*(output + i)) < 0.0)) return 3;
        }
    }

    /**< Check That At Least One Input is Non-Zero                            */
    zero_flag = TRUE;
    for (i = 0; i < num_inputs; i++)
    {
        if ((*(input + i)) > 0.0)
        {
            zero_flag = FALSE;
        }
    }
    if (zero_flag)
    {
        return 4;
    }

    /**< Check That At Least One Output is Non-Zero                           */
    if (set->type_outputs != NODE_NONE) 
    {
        zero_flag = TRUE;
        for (i = 0; i < num_outputs; i++)
        {
            if ((*(output + i)) > 0.0)
            {
                zero_flag = FALSE;
            }
        }
        if (zero_flag)
        {
            return 4;
        }
    }

    /**< Everything appears to be ok */
    return 0;
}

/**
 * \fn patTYPE   *AddPats(patTYPE *pat, int old_max, int newvalue, 
                          int num_inputs, int num_outputs)  
 * \brief Add new patterns
 *         Allocates more space for patterns given a pointer set of patterns
 *         the old number of allocated patterns, the number of new patterns
 *         to allocate and the size of the input and output patterns.
 *         Returns a pointer to the new space.
 * \param pat A pointer to the patterns
 * \param old_max old max patterns
 * \param newvalue new patterns
 * \param num_inputs the input numbers
 * \param num_outputs the output number
 * \retval 0 When successful
 * \retval 1 Out of memory
 */
patTYPE   *AddPats(patTYPE *pat, int old_max, int newvalue, 
                   int num_inputs, int num_outputs)  
{
    int i;
    patTYPE *temppat;

    /**< Allocate space for new patterns */
    temppat = (patTYPE *)malloc((size_t)((old_max + newvalue) * sizeof(patTYPE))); 
    if (temppat == NULL) 
    {
        return NULL;
    }

    /**< Allocate space for input/output vectors patterns */
    for (i = 0; i < (old_max + newvalue); i++) 
    {
        /**< Number of set inputs are doubled for compliment coding */
        temppat[i].input = (float *)malloc((size_t)((num_inputs) * 2 * sizeof(float)));
        if (temppat[i].input == NULL) 
        {
            return NULL; 
        }

        if (num_outputs != 0) 
        {
            temppat[i].output = (float *)malloc((size_t)(num_outputs * sizeof(float)));
            if (temppat[i].output == NULL) 
            {
                return NULL;
            }
        }
    }

    /**< Copy values into new array */	
    for (i = 0; i < old_max; i++)
    {
        *(temppat + i) = *(pat + i);
    }

    /**< Free Old Space */
    free(pat);

    return temppat;
}

⌨️ 快捷键说明

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