1
2 /* Main file sequential */
3
4 #include "defs.h"
5 #include "seq.h"
6 #include "ind.h"
7 #include "gen.h"
8 #include "back.h"
9 #include "genback.h"
10
11 #define DEFLOG AUTO
12 #define DEFFIRST 0
13 #define DEFINFO 1
14 #define DEFSTAT 1
15
16 extern char *optarg;
17 extern int optind;
18
19 char *OutOptStr() {return "l:f:i:r:\0";}
20
21 char *OutUsage() {return
22 "Output Parameters:\n"
23 "-l <frequency of log-output>: auto\n"
24 "-f <show first indiv. (0/1)>: 0\n"
25 "-i <show parameter info>: 1\n"
26 "-r <show statistic (0/1/2)>: 1\n\0";}
27
28 /* set default values */
29
30 int OptLog =DEFLOG;
31 int OptFirst =DEFFIRST;
32 int OptInfo =DEFINFO;
33 int OptStat =DEFSTAT;
34
35 int Gen; /* current generation */
36 int StartTime;
37 int EndTime;
38
39 #define SimTime() (gettime()-StartTime)
40
41 int Calcs;
42 int DecCalcs;
43 float CalcsPerGen;
44 float Diversity;
45
46 int handleOutOpt(char opt,char* arg)
47 {
48 switch(opt)
49 {
50 case 'l': OptLog =getint(arg,0,1000000000); return 0;
51 case 'f': OptFirst =getint(arg,0,2); return 0;
52 case 'i': OptInfo =getint(arg,0,1); return 0;
53 case 'r': OptStat =getint(arg,0,2); return 0;
54 default: return 1;
55 };
56 }
57
58 int initOut()
59 {
60 StartTime=gettime();
61 DecCalcs=PopSize*Decimation;
62 Calcs=0;
63
64 if(OptLog==AUTO)
65 {
66 if(MaxGen<=10)
67 OptLog=1;
68 else if(Ntrain)
69 OptLog=rounddec(500000/((1+Nback)*
70 ((1+Nin+Nout)*(1+Nhid)*NoTrain*PopSize)));
71 else
72 OptLog=rounddec(500000/((1+Nback)*
73 ((1+Nin+Nout)*(1+Nhid)*Nin*PopSize)));
74 };
75 return 0;
76 }
77
78 void printinfo()
79 {
80 printf("Simulation Parameters:\n%s%s%s%s\n",
81 IndParamStr,SeqParamStr,GenParamStr,BackParamStr);
82 }
83
84 void printlog()
85 {
86 printf("Gen%7d: t=%7d, MinErr=%8.4f, AvgErr=%8.4f",
87 Gen,SimTime(),ErrMin,ErrAvg,ptrain);
88 if(HashLen) printf(", Nuni=%6d, Nred=%6d, Nmis=%6d",
89 Nunique,Nredundant,Nmismatch);
90 printf("\n");
91 if(OptFirst==2) printind(Pop[TopInd]);
92 }
93
94 void printstat()
95 {
96 float t,g,i,b,p;
97 t=SimTime(); if(t==0.0) t=1.0;
98 g=Gen;
99 i=g*PopSize;
100 b=i*Nback;
101 p= Nback ? GenCalcs+BackCalcs : GenCalcs;
102 printf("\n"
103 "Statistic total per sec. time\n"
104 "--------------------------------------------------------\n"
105 "Generations: %10.0f%12.6f%12.6f s\n"
106 "Individuals: %10.0f%12.6f%12.6f s\n",
107 g,g/t,t/g,i,i/t,t/i);
108 if(Nback) printf(
109 "Backprop. steps: %10.0f%12.6f%12.6f s\n",b,b/t,t/b);
110 printf(
111 "evaluated Patterns:%10.0f%12.6f%12.6f ms\n",p,p/t,1000*t/p);
112 }
113
114 int main(int argc,char **argv)
115 {
116 int opt,i,j,k;
117 char optstr[128]="\0";
118
119 strcat(optstr,SeqOptStr());
120 strcat(optstr,IndOptStr());
121 strcat(optstr,GenOptStr());
122 strcat(optstr,BackOptStr());
123 strcat(optstr,OutOptStr());
124
125 while((opt=getopt(argc,argv,optstr)) != -1)
126 {
127 if( handleSeqOpt(opt,optarg) &&
128 handleIndOpt(opt,optarg) &&
129 handleGenOpt(opt,optarg) &&
130 handleBackOpt(opt,optarg) &&
131 handleOutOpt(opt,optarg) )
132 {
133 printf("%s\n%s\n%s\n%s\n%s",
134 SeqUsage(),IndUsage(),GenUsage(),
135 BackUsage(),OutUsage());
136 exit(1);
137 };
138 };
139
140 if(initSeq()) errorexit("sequential init failed\n");
141 if(initInd()) errorexit("individual init failed\n");
142 if(initGen(PopSize,PopSize)) errorexit("genetic init failed\n");
143 if(Nback && initBack()) errorexit("backpropagation init failed\n");
144 if(initOut()) errorexit("output init failed\n");
145
146 if(Nback) randomNetPop(PopSize,InitWeight);
147 if(OptInfo) printinfo();
148
149 for(Gen=1;Gen<=MaxGen;Gen++)
150 {
151 calcerrors(PopSize);
152 Calcs+= HashLen ? Nunique : PopSize;
153 if((OptLog && (Gen==1 || Gen%OptLog==0)) || (ErrMin<=MaxErr))
154 printlog();
155 if((ErrMin<=MaxErr) && (Ntrain==NoTrain))
156 {
157 CalcsPerGen=((float)Calcs)/((float)Gen);
158 Diversity=100*CalcsPerGen/PopSize;
159 if(OptStat) printstat();
160 if(OptFirst==1) {printf("\n"); printind(Pop[TopInd]);};
161 printf("\nprogram succeeded.\n");
162 exit(0);
163 };
164 selection(PopSize);
165
166 if((Ntrain>NoTrain) && (ErrMin*NoTrain<=MaxErr*Ntrain))
167 NoTrain=min(Ntrain,NoTrain*2);
168 OffsTrain++; if(OffsTrain>=Ntrain) OffsTrain=0;
169 };
170 if(OptStat==2) printstat();
171 printf("\ntime out - program failed.\n");
172 exit(0);
173 }
1
2 /* Main file parallel */
3
4 #include <stdio.h>
5
6 #include "defs.h"
7 #include "par.h"
8 #include "ind.h"
9 #include "gen.h"
10 #include "back.h"
11 #include "genback.h"
12
13 #define DEFLOG AUTO
14 #define DEFFIRST 0
15 #define DEFINFO 1
16 #define DEFSTAT 1
17
18 extern char *optarg;
19 extern int optind;
20
21 char *OutOptStr() {return "l:f:i:r:\0";}
22
23 char *OutUsage() {return
24 "Output Parameters:\n"
25 "-l <frequency of log-output>: auto\n"
26 "-f <show first indiv. (0/1)>: 0\n"
27 "-i <show parameter info>: 1\n"
28 "-r <show statistic (0/1/2)>: 1\n\0";}
29
30 /* set default values */
31
32 int OptLog =DEFLOG;
33 int OptFirst =DEFFIRST;
34 int OptInfo =DEFINFO;
35 int OptStat =DEFSTAT;
36
37 int Gen; /* current generation */
38 int StartTime;
39 int EndTime;
40
41 #define SimTime() (gettime()-StartTime)
42
43 int handleOutOpt(char opt,char* arg)
44 {
45 switch(opt)
46 {
47 case 'l': OptLog =getint(arg,0,1000000000); return 0;
48 case 'f': OptFirst =getint(arg,0,2); return 0;
49 case 'i': OptInfo =getint(arg,0,1); return 0;
50 case 'r': OptStat =getint(arg,0,2); return 0;
51 default: return 1;
52 };
53 }
54
55 int initOut()
56 {
57 StartTime=gettime();
58
59 if(OptLog==AUTO)
60 {
61 if(MaxGen<=10)
62 OptLog=1;
63 else if(Ntrain)
64 OptLog=rounddec(500000/((1+Nback)*
65 ((1+Nin+Nout)*(1+Nhid)*NoTrain*PopLocal)));
66 else
67 OptLog=rounddec(500000/((1+Nback)*
68 ((1+Nin+Nout)*(1+Nhid)*Nin*PopLocal)));
69 };
70 if(OptLog%Ntrans) OptLog=(OptLog/Ntrans+1)*Ntrans;
71 return 0;
72 }
73
74 void printinfo()
75 {
76 printf("Simulation Parameters:\n%s%s%s%s\n",
77 IndParamStr,ParParamStr,GenParamStr,BackParamStr);
78 }
79
80 void printlog()
81 {
82 printf("Gen%7d: t=%7d, MinErr=%8.4f, AvgErr=%8.4f\n",
83 Gen,SimTime(),ErrMin,ErrAvg,ptrain);
84 if(OptFirst==2) printind(Pop[TopInd]);
85 }
86
87 void printstat()
88 {
89 float t,g,i,l,b,p;
90 t=SimTime(); if(t==0.0) t=1.0;
91 g=Gen;
92 i=g*PopGlobal;
93 l=g*PopLocal;
94 b=i*Nback;
95 p= Nback ? GenCalcs+BackCalcs : GenCalcs;
96 printf("\n"
97 "Statistic total per sec. time\n"
98 "--------------------------------------------------------\n"
99 "Generations: %10.0f%12.6f%12.6f s\n"
100 "Individuals global:%10.0f%12.6f%12.6f s\n"
101 "Individuals local: %10.0f%12.6f%12.6f s\n",
102 g,g/t,t/g,i,i/t,t/i,l,l/t,t/l);
103 if(Nback) printf(
104 "Backprop. steps: %10.0f%12.6f%12.6f s\n",b,b/t,t/b);
105 printf(
106 "evaluated Patterns:%10.0f%12.6f%12.6f ms\n",p,p/t,1000*t/p);
107 }
108
109 int main(int argc,char **argv)
110 {
111 int opt,i,j,k,n;
112 char optstr[128]="\0";
113 errtyp locErrMin,locErrAvg;
114 int locTopInd;
115
116 strcat(optstr,ParOptStr());
117 strcat(optstr,IndOptStr());
118 strcat(optstr,GenOptStr());
119 strcat(optstr,BackOptStr());
120 strcat(optstr,OutOptStr());
121
122 initNetwork();
123
124 while((opt=getopt(argc,argv,optstr)) != EOF)
125 {
126 if( handleParOpt(opt,optarg) &&
127 handleIndOpt(opt,optarg) &&
128 handleGenOpt(opt,optarg) &&
129 handleBackOpt(opt,optarg) &&
130 handleOutOpt(opt,optarg) )
131 {
132 if(ProcId==0) printf("%s\n%s\n%s\n%s\n%s",
133 ParUsage(),IndUsage(),GenUsage(),
134 BackUsage(),OutUsage());
135 exit(1);
136 };
137 };
138
139 if(initPar()) errorexit("parallel init failed\n");
140 if(initInd()) errorexit("individual init failed\n");
141 if(initGen(PopLocal,ProcId==0 ? PopGlobal : PopLocal))
142 errorexit("genetic init failed\n");
143 if(Nback && initBack()) errorexit("backpropagation init failed\n");
144 if(initOut()) errorexit("output init failed\n");
145
146 if(Nback) randomNetPop(PopLocal,InitWeight);
147 if(OptInfo && ProcId==0) printinfo();
148
149 setvect(0,Pop[0],PopLocal*CrWords*WORDLEN);
150 setvect(1,&Err[0],PopLocal*ERRLEN);
151 setvect(2,&ErrMin,FLOATLEN);
152 setvect(3,&ErrAvg,FLOATLEN);
153 setvect(4,&TopInd,WORDLEN);
154
155 for(Gen=1;Gen<=MaxGen;Gen++)
156 {
157 calcerrors(PopLocal);
158 if(Gen%Ntrans)
159 {
160 selection(PopLocal);
161 continue;
162 };
163 if(ProcId==0) /* Master */
164 {
165 for(i=1;i<Procs;i++)
166 {
167 k=PopLocal*i;
168 setvect(0,Pop[k],PopLocal*CrWords*WORDLEN);
169 setvect(1,&Err[k],PopLocal*ERRLEN);
170 setvect(2,&locErrMin,FLOATLEN);
171 setvect(3,&locErrAvg,FLOATLEN);
172 setvect(4,&locTopInd,WORDLEN);
173 n=recvvectfrom(5);
174 if(ErrMin>locErrMin)
175 {
176 ErrMin=locErrMin;
177 TopInd=locTopInd+k;
178 };
179 ErrAvg+=locErrAvg;
180 };
181 ErrAvg/=Procs;
182 if(ErrMin<=MaxErr)
183 {
184 for(i=1;i<Procs;i++) send(i,&ErrMin,FLOATLEN);
185 printlog();
186 if(OptStat) printstat();
187 if(OptFirst==1) {printf("\n"); printind(Pop[TopInd]);};
188 printf("\nprogram succeeded.\n");
189 exit(0);
190 }
191 if(OptLog && (Gen==1 || Gen%OptLog==0)) printlog();
192 selection(PopGlobal);
193 for(i=1;i<Procs;i++)
194 {
195 k=PopLocal*i;
196 send(i,&ErrMin,FLOATLEN);
197 send(i,Pop[k],PopLocal*CrWords*WORDLEN);
198 };
199 }
200 else /* Slaves */
201 {
202 sendvect(0,5);
203 recv(&ErrMin,FLOATLEN);
204 if(ErrMin<=MaxErr) exit(0);
205 recv(Pop[0],PopLocal*CrWords*WORDLEN);
206 };
207
208 if((Ntrain>NoTrain) && (ErrMin*NoTrain<=MaxErr*Ntrain))
209 NoTrain=min(Ntrain,NoTrain*2);
210 OffsTrain++; if(OffsTrain>=Ntrain) OffsTrain=0;
211
212 };
213 if(ProcId==0)
214 {
215 if(OptStat==2) printstat();
216 printf("\ntime out - program failed.\n");
217 };
218 exit(0);
219 }
1
2 /* Main file backpropagation */
3
4 #include "defs.h"
5 #include "sim.h"
6 #include "ind.h"
7 #include "back.h"
8
9 extern char *optarg;
10 extern int optind;
11
12 char *OutOptStr() {return "l:f:i:r:\0";}
13
14 char *OutUsage() {return
15 "Output Parameters:\n"
16 "-l <frequency of log-output>: auto\n"
17 "-f <show weights (0/1/2)>: 0\n"
18 "-i <show parameter info (0/1)>: 1\n"
19 "-r <show statistic (0/1/2)>: 1\0\n";}
20
21 #define DEFLOG AUTO
22 #define DEFFIRST 0
23 #define DEFINFO 1
24 #define DEFSTAT 1
25
26 /* set default values */
27
28 int OptLog =DEFLOG;
29 int OptFirst =DEFFIRST;
30 int OptInfo =DEFINFO;
31 int OptStat =DEFSTAT;
32
33 int Iter; /* current generation */
34 int StartTime;
35 int EndTime;
36
37 #define SimTime() (gettime()-StartTime)
38
39 int handleOutOpt(char opt,char* arg)
40 {
41 switch(opt)
42 {
43 case 'l': OptLog =getint(arg,0,1000000000); return 0;
44 case 'f': OptFirst =getint(arg,0,2); return 0;
45 case 'i': OptInfo =getint(arg,0,1); return 0;
46 case 'r': OptStat =getint(arg,0,2); return 0;
47 default: return 1;
48 };
49 }
50
51 int initOut()
52 {
53 StartTime=gettime();
54
55 if(OptLog==AUTO)
56 {
57 if(MaxIter<=10)
58 OptLog=1;
59 else
60 OptLog=rounddec(500000/((1+Nin+Nout)*
61 (1+Nhid)*Ntrain));
62 };
63 return 0;
64 }
65
66 void errorexit(char *s)
67 {
68 printf("%s",s);
69 exit(1);
70 }
71
72 void printinfo()
73 {
74 printf("Simulation Parameters:\n%s%s%s\n",
75 SimParamStr,NetParamStr,BackParamStr);
76 }
77
78 void printlog()
79 {
80 printf("Iter%7d: t=%7d, NetErr=%8.4f\n",Iter,SimTime(),NetErr);
81 if(OptFirst==2) printnet();
82 }
83
84 void printstat()
85 {
86 float t,i,e,u;
87
88 t=SimTime(); if(t==0.0) t=1.0;
89 i=Iter;
90 e=BackCalcs;
91 u=e*((Nin+1)*Nhid+(Nhid+1)*Nout);
92 printf("\n"
93 "Statistic total per sec. time\n"
94 "--------------------------------------------------------\n"
95 "Iterations: %10.0f%12.5f%12.6f s\n"
96 "evaluated Patterns:%10.0f%12.5f%12.6f ms\n"
97 "updated Weights: %10.0f%12.0f%12.6f us\n",
98 i,i/t,t/i,e,e/t,1000*t/e,u,u/t,1000000*t/u);
99 }
100
101 int main(int argc,char **argv)
102 {
103 int opt,i,j,k;
104 char optstr[128]="\0";
105
106 strcat(optstr,SimOptStr());
107 strcat(optstr,NetOptStr());
108 strcat(optstr,BackOptStr());
109 strcat(optstr,OutOptStr());
110
111 while((opt=getopt(argc,argv,optstr)) != -1)
112 {
113 if( handleSimOpt(opt,optarg) &&
114 handleNetOpt(opt,optarg) &&
115 handleBackOpt(opt,optarg) &&
116 handleOutOpt(opt,optarg) )
117 {
118 printf("%s\n%s\n%s",
119 SimUsage(),NetUsage(),BackUsage(),OutUsage());
120 exit(1);
121 };
122 };
123
124 if(initSim()) errorexit("simulation init failed\n");
125 if(initNet()) errorexit("individual init failed\n");
126 if(initBack()) errorexit("backpropagation init failed\n");
127 if(initOut()) errorexit("output init failed\n");
128 if(OptInfo) printinfo();
129
130 for(Iter=1;Iter<=MaxIter;Iter++)
131 {
132
133 calcNetErr();
134
135 if(NetErr<=MaxErr)
136 {
137 printlog();
138 if(OptStat) printstat();
139 if(OptFirst==1) {printf("\n"); printnet();};
140 printf("\nprogram succeeded.\n");
141 exit(0);
142 };
143 if(Iter%OptLog==0 || Iter==1 || Iter==MaxIter) printlog();
144 updateNet();
145 };
146 if(OptStat==2) printstat();
147 printf("\ntime out - program failed.\n");
148 exit(0);
149 }