📄 art_int.c
字号:
/* ------------------------------------------------------------------------- */
/* The ART Gallery */
/* ------------------------------------------------------------------------- */
/* Art_Int.c */
/* Version: 1.0 */
/* Written By: Lars H. Liden laliden@cns.bu.edu */
/* Last Update: 8/02/95 */
/* */
/* The following code contains the internal functions of The ART Gallery. */
/* */
/* Please see Art_Doc.txt for a full documentation. */
/* */
/* Send all bug reports to laliden@cns.bu.edu */
/* ------------------------------------------------------------------------- */
#ifndef __Art_Gal_h
#include "Art_Gal.h"
#endif
/* ========================================================================= */
/* INTERNAL PROCEDURES */
/* The following procedures are used by the library functions. They were */
/* not intended to be called externally and are not equipped to check for */
/* errors in their arguments. */
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/* Intersect */
/* When type is FUZZYART, returns fuzzy intersection */
/* When type is ART1, returns normal intersection */
/* ------------------------------------------------------------------------- */
float PREFIX Intersect(float x, float y, int type) {
if ((type==ART1)||(type==NONE))
return x*y;
else { /* if (type==FUZZYART) */
if (x>y)
return y;
else
return x;
}
}
/* ------------------------------------------------------------------------- */
/* FuzzInt */
/* Returns the fuzzy intersection between two numbers [0,1] */
/* ------------------------------------------------------------------------- */
float PREFIX FuzzInt(float x, float y) {
if (x>y)
return y;
else
return x;
}
/* ------------------------------------------------------------------------- */
/* FuzzUni */
/* Returns the fuzzy union between two numbers [0,1] */
/* ------------------------------------------------------------------------- */
float PREFIX FuzzUni(float x, float y) {
if (x>y)
return x;
else
return y;
}
/* ------------------------------------------------------------------------- */
/* BinToInt */
/* Convert binary array to an integer */
/* ------------------------------------------------------------------------- */
int PREFIX BinToInt (patPTR pat,int num_outputs) {
int i,position;
position=0;
for (i=0;i<num_outputs;i++)
position+=(*(pat->output+i)) * pow(2,i);
return position;
}
/* ------------------------------------------------------------------------- */
/* IntToBin */
/* Convert integer to binary array */
/* ------------------------------------------------------------------------- */
void PREFIX IntToBin (int num_outputs,int position, int *output_list) {
int i,j;
for (i=(num_outputs-1);i>-1;i--) {
j=pow(2,i);
if (position>=j) {
*(output_list+i)=1;
position-=j;
}
else
*(output_list+i)=0;
}
}
/* ------------------------------------------------------------------------- */
/* InitArt */
/* Initializes art network values and allocate space */
/* */
/* Return Value: */
/* 0 - If successful */
/* 1 - When memory allocation failure */
/* ------------------------------------------------------------------------- */
int PREFIX InitArt(artPTR art, int type, int style, int num_inputs) {
int i,j;
art->type = type;
art->num_inputs = num_inputs;
if (type==NONE) {
art->nodes_used = num_inputs; /* Inputs just copied so number of */
art->max_nodes = num_inputs; /* categories is a constant. */
art->cat=(float *)malloc((size_t) (art->max_nodes*sizeof(float)));
if (art->cat==NULL) return 1;
art->style = NONE;
}
else {
art->style = style;
art->nodes_used = 0; /* All categories are currently unused */
art->max_nodes = MAX; /* Starting number of available categories */
art->beta = 1; /* No recoding */
art->vigil = 0;
art->num_reset = 0;
art->cat=(float *)malloc((size_t) (art->max_nodes*sizeof(float)));
if (art->cat==NULL) return 1;
art->elig=(float *)malloc((size_t) (art->max_nodes*sizeof(float)));
if (art->elig==NULL) return 1;
art->commit=(int *)malloc((size_t) (art->max_nodes*sizeof(int)));
if (art->commit==NULL) return 1;
art->weight=(float **)malloc((size_t) (art->max_nodes*sizeof(float*)));
if (art->weight==NULL) return 1;
art->weight[0]=(float *)malloc((size_t) ((art->max_nodes*art->num_inputs)*sizeof(float)));
if (art->weight[0]==NULL) return 1;
for(i=1;i<art->max_nodes;i++)
art->weight[i]=art->weight[i-1]+art->num_inputs;
art->sum_weights=(float *)malloc((size_t) (art->max_nodes*sizeof(float)));
if (art->sum_weights==NULL) return 1;
art->sum_IW=(float *)malloc((size_t) (art->max_nodes*sizeof(float)));
if (art->sum_IW==NULL) return 1;
/* Set Initial Weight Values */
for (i=0;i<art->max_nodes;i++)
for (j=0;j<art->num_inputs;j++)
*(*(art->weight+i)+j)=1;
/* 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 to false */
for (i=0;i<art->max_nodes;i++)
*(art->commit+i)=FALSE;
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* InitMap */
/* Initialize mapfield values and allocate space */
/* */
/* Return Value: */
/* 0 - If successful */
/* 1 - When memory allocation failure */
/* -------------------------------------------------------------------------- */
int PREFIX InitMap(mapPTR map, int typeA, int styleA, int typeB, int styleB,
int num_inputs, int num_outputs) {
int i,j;
map->vigil = 1; /* Require perfect mapfield match */
map->base_vigil = 0;
map->num_mismatch = 0;
/* Initialize Art Networks */
if (InitArt(&map->artA,typeA,styleA,num_inputs)) return 1;
if (InitArt(&map->artB,typeB,styleB,num_outputs)) return 1;
map->maxA_nodes = map->artA.max_nodes;
map->maxB_nodes = map->artB.max_nodes;
/* Allocate space for map and weights */
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) 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) return 1;
for(i=1;i<map->artA.max_nodes;i++)
map->map_weight[i]=map->map_weight[i-1]+map->artB.max_nodes;
/* Set Initial Map Field Weight Values */
for (i=0;i<map->artA.max_nodes;i++) {
for (j=0;j<map->artB.max_nodes;j++)
*(*(map->map_weight+i)+j)=1;
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* Compliment */
/* Compilement codes members of a pettern set, when specified, */
/* allocated space for the compliment coded patterns. */
/* */
/* Arguements: */
/* set - A pointer to a set of patterns of type setTYPE */
/* input_style - NONE or COMPLIMENT */
/* output_style - NONE or COMPLIMENT */
/* */
/* Return Value: */
/* 0 - If successful */
/* 1 - When memory allocation failure */
/* ------------------------------------------------------------------------- */
int PREFIX Compliment(setTYPE *set, int input_style, int output_style) {
float *tempflt;
int i,j;
/* If compliment coding is requested of the input patterns */
if (input_style==COMPLIMENT) {
/* Allocate space for compliment coded inputs and copy old inputs */
for (j=0;j<set->num_patterns;j++) {
tempflt=(float *)malloc((size_t) ((2*set->num_inputs)*sizeof(float)));
if (tempflt==NULL) return 1;
for (i=0;i<set->num_inputs;i++)
*(tempflt+i)=*(set->pattern[j].input+i);
free(set->pattern[j].input);
set->pattern[j].input = tempflt;
/* Complement coding input patterns in new space */
for (i=0;i<set->num_inputs;i++)
*(set->pattern[j].input+(i+set->num_inputs))=(1-(*(set->pattern[j].input+i)));
}
set->style_inputs = COMPLIMENT;
}
/* If compliment coding is requested of the output patterns */
if (output_style==COMPLIMENT) {
/* Allocate space for compliment coded outputs and copy old outputs */
for (j=0;j<set->num_patterns;j++) {
tempflt=(float *)malloc((size_t) ((2*set->num_outputs)*sizeof(float)));
if (tempflt==NULL) return 1;
for (i=0;i<set->num_outputs;i++)
*(tempflt+i)=*(set->pattern[j].output+i);
free(set->pattern[j].output);
set->pattern[j].output = tempflt;
/* Complement coding output patterns in new space */
for (i=0;i<set->num_outputs;i++)
*(set->pattern[j].output+(i+set->num_outputs))=(1-(*(set->pattern[j].output+i)));
}
set->style_outputs = COMPLIMENT;
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* AddPats */
/* 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. */
/* */
/* Return Value: */
/* When successful */
/* A pointer to the new pattern set */
/* When memory allocation failure occurs */
/* NULL */
/* ------------------------------------------------------------------------- */
patTYPE PREFIX *AddPats(patTYPE *pat, int old_max, int new,
int num_inputs, int num_outputs) {
int i;
patTYPE *temppat;
/* Allocate space for new patterns */
temppat = (patTYPE *)malloc((size_t)
((old_max+new)*sizeof(patTYPE)));
if (temppat==NULL) return NULL;
/* Allocate space for input/output vectors patterns */
for (i=0;i<(old_max+new);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;
}
/* -------------------------------------------------------------------------*/
/* AddCats */
/* Allocates space for more category nodes and sets initial */
/* values for reallocted space. */
/* NOTE: When using ARTMAP, AddMaps must be called following a */
/* a call to AddCats, to update mapfield size. */
/* */
/* Return Value: */
/* 0 - If successful */
/* 1 - When memory allocation failure */
/* -------------------------------------------------------------------------*/
int PREFIX AddCats(artPTR art,int new_nodes) {
int i,j;
int *tempvint; /* Temporary pointer for copying */
float *tempvflt; /* Temporary pointer for copying */
float **tempflt; /* Temporary pointer for copying */
/* Allocate new space */
tempvflt=(float *)malloc((size_t) ((art->max_nodes+new_nodes)*sizeof(float)));
if (tempvflt==NULL) return 1;
for (i=0;i<art->max_nodes;i++)
*(tempvflt+i)=*(art->cat+i);
free(art->cat);
art->cat = tempvflt;
tempvflt=(float *)malloc((size_t) ((art->max_nodes+new_nodes)*sizeof(float)));
if (tempvflt==NULL) return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -