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

📄 frdnrl.cpp

📁 这是我找到的BP算法C语言程序软件包
💻 CPP
字号:
//Header:	Frdnrl.hpp
//Language:	Borland C++ 3.1
//Version:	1.0
//Environ:	Any
//Date:		3/1996
//Purpose:	Provide a base class of forword neural netword

#include "frdnrl.hpp"
#include <iostream.h>
extern "C"
  {
  #include <process.h>
  #include <string.h>
  }

//constructor
FrdNrl::FrdNrl()
{
  ErrSet(NT_NOERR);
  LayerNum=0;
  LyNodeNum=NULL;
  ErrHandler=NULL;
  LrnNumAllow=10000;
  LayerIn = NULL;
  strcpy(NetType,"Forword Network");
}
FrdNrl::FrdNrl(char* name)
{
  ErrSet(NT_NOERR);
  strcpy(NtName,name);
  if(!CFGLoad())
    ErrSet(NT_INITERR);
}
FrdNrl::FrdNrl(int ln, int nn[], char *nname)
{
  ErrSet(NT_NOERR);
  FrdInit(ln,nn,nname);
  strcpy(NetType,"Forword Network");
}
void FrdNrl::FrdInit(int ln, int nn[], char *nname)
{
  LayerNum = ln;
  LrnNumAllow = 10000;
  if((LyNodeNum=new int[ln])==NULL)
    ErrSet(NT_MEM);
  int allnode=0;
  for(int i=0; i<ln; ++i)
  {
    LyNodeNum[i] = nn[i];
    allnode += nn[i];
  }
  Init(allnode);
  SetNtName(nname);
  unsigned int ilayerNumAll=0,i_1layerNum;
  for(i=1; i<LayerNum; i++)
  {
    i_1layerNum = ilayerNumAll;
    ilayerNumAll += LyNodeNum[i-1];
    for(int j=1; j<=LyNodeNum[i]; j++)
      for(int k=1; k<=LyNodeNum[i-1]; k++)
	(*Conect)(ilayerNumAll+j,i_1layerNum+k) = 1;
  }
  if((LayerIn=new DblArray*[LayerNum])==NULL)
    ErrSet(NT_MEM);
  if((LayerOut=new DblArray*[LayerNum])==NULL)
    ErrSet(NT_MEM);
  for(i=0; i<LayerNum; i++)
  {
    if((LayerIn[i]=new DblArray(1,LyNodeNum[i]))==NULL)
      ErrSet(NT_MEM);
    if((LayerOut[i]=new DblArray(1,LyNodeNum[i]))==NULL)
      ErrSet(NT_MEM);
  }
}

//destructor
FrdNrl::~FrdNrl()
{
  if(!strcmp(NetType,"Forword Network"))
    CFGWrite();
  if(LyNodeNum != NULL) delete LyNodeNum;
  if(LayerIn != NULL)
  {
    for(int i=0; i<LayerNum; i++)  delete LayerIn[i];
    delete LayerIn;
  }
  if(LayerOut != NULL)
  {
    for(int i=0; i<LayerNum; i++)  delete LayerOut[i];
    delete LayerOut;
  }
}
//translate for Lth layer and Nth node to the node number of NRLNET
int FrdNrl::NumTrans(int l, int n)
{
  int count=0;
  for(int i=0; i<l-1; i++)
    count += LyNodeNum[i];
  count += n;
  return count;
}
//method of run network
const double *FrdNrl::Run(double *input, double *output)
{
  if(LayerNum <= 0) return output;
  for(int i=0; i<LyNodeNum[0]; i++)
  {
    (*LayerIn[0])[i+1] = input[i];
    (*LayerOut[0])[i+1] = Node[i].Fun((*LayerIn[0])[i+1]-Node[i].GetThred());
  }
  for(i=1; i<LayerNum; i++)   //for each layer of two to last
  {
    //calculate input and output of this layer
    for(int j=1; j<=LyNodeNum[i]; j++)
    {
      (*LayerIn[i])[j] = 0;
      for(int k=1; k<=LyNodeNum[i-1]; k++)
	(*LayerIn[i])[j] += (*LayerOut[i-1])[k]*GetWeight(i,k,i+1,j);
      int count = NumTrans(i+1,j)-1;
      (*LayerOut[i])[j] = Node[count].Fun((*LayerIn[i])[j]-Node[count].GetThred());
    }
  }
  //set output
  for(i=0; i<LyNodeNum[LayerNum-1]; i++)
    output[i] = (*LayerOut[LayerNum-1])[i+1];
  return output;
}
//get weight of Lth layer and Nth node to LLth layer and NNth node
double FrdNrl::GetWeight(int l,int n, int ll,int nn)
{
  int from = NumTrans(l,n);
  int to = NumTrans(ll,nn);
  return (*Weight)(to,from);
}
//get connect of Lth layer and Nth node to LLth layer and NNth node
double FrdNrl::GetConect(int l,int n, int ll,int nn)
{
  int from = NumTrans(l,n);
  int to = NumTrans(ll,nn);
  return (*Conect)(to,from);
}
//set weight of Lth layer and Nth node to LLth layer and NNth node
void FrdNrl::SetWeight(double w,int l,int n,  int ll,int nn)
{
  int from = NumTrans(l,n);
  int to = NumTrans(ll,nn);
  (*Weight)(to,from) = w;
}
//load config file
int FrdNrl::CFGLoad()
{
  if(!strcmp(NtName,"")) return 0;
  char flname[20];
  strcpy(flname,NtName);
  strcat(flname,".CFG");
  ifstream infile(flname);
  if(!infile)
    return 0;
  if(!CFGLoadIn(infile))
    return 0;
  else
    return 1;
}
int FrdNrl::CFGLoadIn(istream& infile)
{
  infile.getline(NetType,sizeof(NetType));
  infile>>LayerNum;
  int *tmp=new int[LayerNum];
  for(int i=0; i<LayerNum; i++)
    infile>>tmp[i];
  if(!infile)
  {
    LayerNum=0;
    FrdInit(LayerNum,NULL,NtName);
    LyNodeNum=NULL;
    ErrHandler=NULL;
    LayerIn = NULL;
    delete tmp;
    return 0;
  }
  else
  {
    FrdInit(LayerNum,tmp,NtName);
    delete tmp;
    infile>>LdType>>LrnNumAllow;
    if(!infile) return 0;
    return 1;
  }
}
//write config file
int FrdNrl::CFGWrite()
{
  if(!strcmp(NtName,"")) return 0;
  char flname[20];
  strcpy(flname,NtName);
  strcat(flname,".CFG");
  ofstream outfile(flname);
  if(!outfile)
  {
    ErrSet(NT_FILEERR);
    return 0;
  }
  return CFGWriteIn(outfile);
}
int FrdNrl::CFGWriteIn(ostream& outfile)
{
  outfile<<NetType<<"\n"<<LayerNum;
  for(int i=0; i<LayerNum; i++)
    outfile<<"\n"<<LyNodeNum[i];
  if(!outfile)
    return 0;
  else
  {
    outfile<<"\n"<<LdType<<"\n"<<LrnNumAllow;
    return 1;
  }
}
//get structure of network
int* FrdNrl::GetStruct(int* s)
{
  for(int i=0; i<LayerNum; i++)
    s[i] = LyNodeNum[i];
  return s;
}

⌨️ 快捷键说明

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