📄 art.c
字号:
/***************************************************************
*
* ADAPTIVE RESONANCE THEORY <ART> NETWORK
*
***************************************************************/
#include "stdio.h"
#include "fstream.h"
#include "math.h"
#include "stdlib.h"
#include "iomanip.h"
#include "string.h"
#include "conio.h"
#define MAXCNEURONS 75
#define MAXRNEURONS 30
#define MAXPATTERNS 30
#define VERBOSE 1
int BestNeuron;
class ARTNET
{
private:
double Wb[MAXCNEURONS][MAXRNEURONS]; //30*75
int Wt[MAXRNEURONS][MAXCNEURONS]; //75*30
int InData[MAXPATTERNS][MAXCNEURONS];
int NumPatterns;
double VigilThresh;
double L;
int M;
int N;
int XVect[MAXCNEURONS];
int CVect[MAXCNEURONS];
int BestNeuron;
int Reset;
int RVect[MAXCNEURONS];
int PVect[MAXCNEURONS];
int Disabled[MAXRNEURONS];
int Trained[MAXRNEURONS];
void ClearPvect();
void ClearDisabled();
void RecoPhase();
void CompPhase();
void SearchPhase();
void RunCompLayer();
void RunRecoLayer();
void Rvect2Pvect(int);
int Gain1();
int Gain2();
double Vigilence();
void InitWeights();
void Train();
public:
ARTNET(void);
int LoadInVects(char *Fname);
void Run(int i);
void ShowWeights();
void ShowInVect();
void ShowOutVect();
};
ARTNET::ARTNET()
{
int i;
L = 2.0;
N = MAXRNEURONS;
for(i=0; i<N; i++)
{
Trained[i] = 0;
Disabled[i] = 0;
}
}
int ARTNET:: LoadInVects(char *Fname)
{
FILE *PFILE;
int i,j,k;
PFILE = fopen(Fname,"r");
if(PFILE == NULL)
{
printf("\nUnable to open the file: %s\n", Fname);
exit(0);
}
fscanf(PFILE, "%d", &NumPatterns);
fscanf(PFILE, "%d", &M);
fscanf(PFILE, "%lf", &VigilThresh);
for(i=0; i<NumPatterns; i++)
for(j=0; j<M; j++)
{
fscanf(PFILE, "%d", &k);
InData[i][j]=k;
//cout<<"InData["<<i<<"]["<<j<<"]="<<InData[i][j]<<endl;
}
InitWeights();
return NumPatterns;
}
void ARTNET::Rvect2Pvect(int best)
{
int i;
for(i=0; i<M; i++)
{
//PVect[i]=Wt[best][i]*RVect[i];
PVect[i]=Wt[best][i];
}
}
int ARTNET::Gain2()
{
int i;
int TTT=0;
for(i=0; i<M; i++)
{
if(XVect[i]==1) return 1;
}
return TTT;
}
int ARTNET::Gain1()
{
int i,G;
G=Gain2();
for(i=0; i<M; i++)
{
if(RVect[i]==1) return 0;
}
return G;
}
void ARTNET::RunCompLayer()
{
int i,x;
for(i=0; i<M; i++)
{
x=XVect[i]+Gain1()+PVect[i];
if(x>=2)
CVect[i]=1;
else
CVect[i]=0;
//cout<<"RunCompLayer==>CVect["<<i<<"]="<<CVect[i]<<endl;
}
}
double ARTNET::Vigilence()
{
int i;
double S,D,K;
K=0.0;
D=0.0;
for(i=0; i<M; i++)
{
K+=CVect[i];
D+=XVect[i];
}
S=K/D;
return S;
}
void ARTNET::RunRecoLayer()
{
int i,j,k;
double Net[MAXRNEURONS];
double NetMax = 0.0;
BestNeuron = -1; //我的改动
for(i=0; i<N; i++)
{
Net[i]=0;
for(j=0; j<M; j++)
Net[i]+=Wb[i][j]*CVect[j];
if(Net[i]>NetMax&&Disabled[i]==0)
{
BestNeuron=i;
NetMax=Net[i];
}
}
for(k=0; k<N; k++)
{
if(k==BestNeuron)
RVect[k]=1;
else
RVect[k]=0;
}
}
void ARTNET::RecoPhase()
{
int i;
for(i=0; i<N; i++) RVect[i]=0;
for(i=0; i<M; i++) PVect[i]=0;
RunCompLayer();
RunRecoLayer();
Rvect2Pvect(BestNeuron);
}
void ARTNET::CompPhase()
{
double S;
RunCompLayer();
S=Vigilence();
if(S<VigilThresh)
{
Reset=1;
RVect[BestNeuron]=0;
Disabled[BestNeuron]=1;
}
else
Reset=0;
}
void ARTNET::SearchPhase()
{
double S;
while(Reset)
{
//Rvect2Pvect(0);
ClearPvect();
RunCompLayer();
RunRecoLayer();
Rvect2Pvect(BestNeuron);
S=Vigilence();
if(S<VigilThresh&&BestNeuron!=-1)
{
Reset=1;
RVect[BestNeuron]=0;
Disabled[BestNeuron]=1;
}
else
Reset=0;
}
if(BestNeuron!=-1) Train();
else
printf("Out of neurons F2\n");
ClearDisabled();
}
void ARTNET::ClearDisabled()
{
int i;
for(i=0; i<M; i++) Disabled[i]=0;
}
void ARTNET::ClearPvect()
{
int i;
for(i=0; i<M; i++) PVect[i]=0;
}
void ARTNET::Train()
{
int i,z=0;
for(i=0; i<M; i++)
z+=CVect[i];
for(i=0; i<M; i++)
{
Wb[BestNeuron][i]=L*CVect[i]/(L-1+z);
Wt[BestNeuron][i]=CVect[i];
}
Trained[BestNeuron]=1;
}
void ARTNET::Run(int tp)
{
int i;
ClearPvect();
for(i=0; i<M; i++)
{
XVect[i]=InData[tp][i];
//cout<<i<<setw(12)<<XVect[i]<<endl;
}
RecoPhase();
CompPhase();
SearchPhase();
}
void ARTNET::InitWeights()
{
int i,j;
double b;
for(i=0; i<N; i++)
for(j=0; j<M; j++) Wt[i][j]=1;
b=L/(L-1+M);
for(i=0; i<N; i++)
for(j=0; j<M; j++) Wb[i][j]=b;
}
void ARTNET::ShowWeights()
{
int i,j;
printf("\nTop Down weights:\n");
for(i=0; i<N; i++)
if(Trained[i]==1)
{
for(j=0; j<M; j++) printf("%d ",Wt[i][j]);
printf("\n");
}
printf("\nDown Top weights:\n");
for(i=0; i<N; i++)
if(Trained[i]==1)
{
for(j=0; j<M; j++)
{
printf("%f ",Wb[i][j]);
if((j+1)%5==0) printf("\n");
}
printf("\n");
}
}
void ARTNET::ShowInVect()
{
int i;
printf("BEST NEURON: %d\nIN: ", BestNeuron);
for(i=0; i<M; i++) printf("%d ", XVect[i]);
printf("\n");
}
void ARTNET::ShowOutVect()
{
int i;
printf("OUT ");
for(i=0; i<M; i++) printf("%d ", CVect[i]);
printf("\n");
}
ARTNET ART;
int main(int argc, char *argv[])
{
int TstSetSize;
int i;
if(argc>1)
{
TstSetSize=ART.LoadInVects(argv[1]);
for(i=0; i<TstSetSize; i++)
{
cout<<endl<<"*******************************"<<endl;
cout<<"inputing pattern sample: #"<<i+1<<endl;
ART.Run(i);
printf("\n");
ART.ShowInVect();
ART.ShowOutVect();
if(VERBOSE==1) ART.ShowWeights();
}
}
else
{
printf("USAGE: ART PATTERN_FILE_NAME\n");
exit(0);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -