📄 mainseq.c
字号:
/* Main file sequential */#include "defs.h"#include "seq.h"#include "ind.h"#include "gen.h"#include "back.h"#include "genback.h"#define DEFLOG AUTO#define DEFFIRST 0#define DEFINFO 1#define DEFSTAT 1extern char *optarg;extern int optind;char *OutOptStr() {return "l:f:i:r:\0";}char *OutUsage() {return "Output Parameters:\n" "-l <frequency of log-output>: auto\n" "-f <show first indiv. (0/1)>: 0\n" "-i <show parameter info>: 1\n" "-r <show statistic (0/1/2)>: 1\n\0";}/* set default values */int OptLog =DEFLOG;int OptFirst =DEFFIRST;int OptInfo =DEFINFO;int OptStat =DEFSTAT;int Gen; /* current generation */int StartTime;int EndTime;#define SimTime() (gettime()-StartTime)int Calcs;int DecCalcs;float CalcsPerGen;float Diversity;int handleOutOpt(char opt,char* arg){ switch(opt) { case 'l': OptLog =getint(arg,0,1000000000); return 0; case 'f': OptFirst =getint(arg,0,2); return 0; case 'i': OptInfo =getint(arg,0,1); return 0; case 'r': OptStat =getint(arg,0,2); return 0; default: return 1; };}int initOut(){ StartTime=gettime(); DecCalcs=PopSize*Decimation; Calcs=0; if(OptLog==AUTO) { if(MaxGen<=10) OptLog=1; else if(Ntrain) OptLog=rounddec(500000/((1+Nback)* ((1+Nin+Nout)*(1+Nhid)*NoTrain*PopSize))); else OptLog=rounddec(500000/((1+Nback)* ((1+Nin+Nout)*(1+Nhid)*Nin*PopSize))); }; return 0;}void printinfo(){ printf("Simulation Parameters:\n%s%s%s%s\n", IndParamStr,SeqParamStr,GenParamStr,BackParamStr);}void printlog(){ printf("Gen%7d: t=%7ld, MinErr=%8.4f, AvgErr=%8.4f", Gen,SimTime(),ErrMin,ErrAvg); if(HashLen) printf(", Nuni=%6d, Nred=%6d, Nmis=%6d", Nunique,Nredundant,Nmismatch); printf("\n"); if(OptFirst==2) printind(Pop[TopInd]); }void printstat(){ float t,g,i,b,p; t=SimTime(); if(t==0.0) t=1.0; g=Gen; i=g*PopSize; b=i*Nback; p= Nback ? GenCalcs+BackCalcs : GenCalcs; printf("\n" "Statistic total per sec. time\n" "----------------------------------------------------------\n" "Generations: %10.0f%14.6f%12.6f s\n" "Individuals: %10.0f%14.6f%12.6f s\n", g,g/t,t/g,i,i/t,t/i); if(Nback) printf( "Backprop. steps: %10.0f%14.6f%12.6f s\n",b,b/t,t/b); printf( "evaluated Patterns:%10.0f%14.6f%12.6f ms\n",p,p/t,1000*t/p);}int main(int argc,char **argv){ int opt; char optstr[128]="\0"; strcat(optstr,SeqOptStr()); strcat(optstr,IndOptStr()); strcat(optstr,GenOptStr()); strcat(optstr,BackOptStr()); strcat(optstr,OutOptStr()); while((opt=getopt(argc,argv,optstr)) != -1) { if( handleSeqOpt(opt,optarg) && handleIndOpt(opt,optarg) && handleGenOpt(opt,optarg) && handleBackOpt(opt,optarg) && handleOutOpt(opt,optarg) ) { printf("%s\n%s\n%s\n%s\n%s", SeqUsage(),IndUsage(),GenUsage(), BackUsage(),OutUsage()); exit(1); }; }; if(initSeq()) errorexit("sequential init failed\n"); if(initInd()) errorexit("individual init failed\n"); if(initGen(PopSize,PopSize)) errorexit("genetic init failed\n"); if(Nback && initBack()) errorexit("backpropagation init failed\n"); if(initOut()) errorexit("output init failed\n"); if(Nback) randomNetPop(PopSize,InitWeight); if(OptInfo) printinfo(); for(Gen=1;Gen<=MaxGen;Gen++) { calcerrors(PopSize); Calcs+= HashLen ? Nunique : PopSize; if((OptLog && (Gen==1 || Gen%OptLog==0)) || (ErrMin<=MaxErr)) printlog(); if((ErrMin<=MaxErr) && (Ntrain==NoTrain)) { CalcsPerGen=((float)Calcs)/((float)Gen); Diversity=100*CalcsPerGen/PopSize; if(OptStat) printstat(); if(OptFirst==1) {printf("\n"); printind(Pop[TopInd]);}; printf("\nprogram succeeded.\n"); exit(0); }; selection(PopSize); if((Ntrain>NoTrain) && (ErrMin*NoTrain<=MaxErr*Ntrain)) NoTrain=min(Ntrain,NoTrain*2); OffsTrain++; if(OffsTrain>=Ntrain) OffsTrain=0; }; if(OptStat==2) printstat(); printf("\ntime out - program failed.\n"); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -