-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.cpp
225 lines (203 loc) · 4.22 KB
/
genesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Genesis VIII.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include <stdio.h>
#include <iostream>
#include <math.h>
#include "utils/logger.h"
#include "utils/scriptmanager.h"
#include "utils/matrix.h"
#include "utils/statistics.h"
#include "gp/program.h"
#include "gp/var_program.h"
#include "gp/terrorism_program.h"
#include "gp/symbolic_program.h"
#include "gp/distribution_program.h"
#include "gp/copula_program.h"
#include "gp/tree.h"
#include "gp/branch.h"
#include "gp/leaf.h"
#include "gp/population.h"
#include "gp/es_program.h"
#ifndef VISUAL_CPP
#include<unistd.h>
#endif
int silent=0;//Controlled by the -s switch. Shuts down input,output and error channels.
int daemonize=0;//Daemonizes the program, i.e. makes it run in the background. Controlled by the -d switch.
int interpret_file=0;//Interpret a file???
int redirect=0;//Redirect stdout and stderr to a logfile???
char *filename=NULL;
char *redirect_file=NULL;
int do_eval=0;
char *eval_string=NULL;
void process_flag(char flag,char *optarg)
{
switch (flag)
{
case 's':
silent=1;
break;
case 'e':
if (optarg!=NULL)
{
do_eval=1;
eval_string=optarg;
}
break;
case 'd':
daemonize=1;
break;
case 'r':
if (optarg!=NULL)
{
redirect=1;
redirect_file=optarg;
}
break;
case 'f':
if (optarg!=NULL)
{
filename=optarg;
interpret_file=1;
}
break;
default:
break;
}
}
//Makes just what it says.
void parse_command_line(int argc,char *argv[])
{
int c=0;
extern char *optarg;
extern int optind, optopt;
#ifndef VISUAL_CPP
while ((c=getopt(argc,argv,"e:sdf:r:"))!=-1)
{
process_flag(c,optarg);
}
#else
int x;
char flag[1024];
for(x=0;x<argc;x++)
{
if(sscanf(argv[x],"-%s",flag))
{
process_flag(flag[0],x<argc-1?argv[x+1]:NULL);
}
}
#endif
}
void Setup(void)
{
srand(time(NULL));
Logger *ML=ML->i();
ScriptManager *SM=SM->i();
//We set up the scripting and logging facilities...
ML->Setup();
SM->Setup();
LuaMatrix::Setup();
LuaLogger::Setup();
LuaStat::Setup();
LuaPopulation::Setup();
}
void console(void)
{
ScriptManager *SM=SM->i();
char buffer[2048];
char *tmp;
/*signal(SIGABRT,new_flux_handler);
signal(SIGINT,handler);*/
fflush(stdin);
buffer[0]=0;
while(1)
{
cout << "\ngenesis>";
fflush(stdout);
if (cin.getline(buffer,2048))
{
tmp=(char *)malloc(sizeof(char)*(strlen(buffer)+1));
strcpy(tmp,buffer);
SM->Eval(tmp);
}
}
}
Program *my_var_generator()
{
Program *p=new VaRProgram();
return p;
}
Program *my_terrorism_generator()
{
Program *p=new TerrorismProgram();
return p;
}
Program *my_symbolic_generator()
{
Program *p=new SymbolicProgram();
return p;
}
Program *my_copula_generator()
{
Program *p=new CopulaProgram();
return p;
}
Program *my_es_generator()
{
Program *p=new ESProgram();
return p;
}
Program *my_distribution_generator()
{
Program *p=new DistributionProgram();
return p;
}
int main(int argc, char *argv[])
{
Setup();
ScriptManager *SM=SM->i();
SM->RegisterUserdata("var","program",(void *)my_var_generator);
SM->RegisterUserdata("es","program",(void *)my_es_generator);
SM->RegisterUserdata("terror","program",(void *)my_terrorism_generator);
SM->RegisterUserdata("symbolic","program",(void *)my_symbolic_generator);
SM->RegisterUserdata("copula","program",(void *)my_copula_generator);
SM->RegisterUserdata("distribution","program",(void *)my_distribution_generator);
parse_command_line(argc,argv);
Leaf MyLeaf;
unsigned int i,j;
/* Population *Pop=new Population;
Pop->Create(10000,10,my_generator);
while(1)
{
Pop->Evaluate();
Pop->Evolve();
}
Pop->Evaluate();
cout << "Best program:" << Pop->Best->Print() << "(" << Pop->Best->Eval() << ")\n";
*/
if(silent)
{
cout << "Going to silent mode...\n";
fclose(stdout);
fclose(stdin);
fclose(stderr);
}
if (redirect)
{
cout << "Redirecting stdout/stderr to file \"" << redirect_file << "\"\n";
fflush(stdout);
fclose(stderr);
fclose(stdin);
freopen(redirect_file,"w+",stdout);
}
if (do_eval)
{
SM->Eval(eval_string);
}
if (interpret_file)
{
SM->Require("startup",filename);
SM->Run("startup");
}
console();
return 0;
}