// gcc -Wall -mpreferred-stack-boundary=2 -DNO_COMPILE -DCONFIG_TCC_PREFIX=\"/usr/local\" -I. -o pheno_main pheno_main.c -L. -ltcc -ldl -lm //gcc -Wall -mpreferred-stack-boundary=2 -DCONFIG_TCC_PREFIX=\"/usr/local\" -I. -o pheno_main pheno_main.c -L. -ltcc -ldl -lm #include "libtcc.h" #include #include #include //extern double c_symb(double x); #ifdef NO_COMPILE double c_symb(double x) { return (exp(1.0)/log(1.0)); } double dabs (double x) { return x > 0 ? x : -x; } double c_eval() { double x_sample_points[] = { -1, -0.9, -0.8, -0.76, -0.72, -0.68, -0.64, -0.4, -0.2, 0, 0.2, 0.4, 0.63, 0.72, 0.81, 0.90, 0.93, 0.96, 0.99, 1}; double y_sample_points[] = { 0, -0.162900, -0.262400, -0.287754, -0.306109, -0.318218, -0.324772, -0.278400, -0.166400, 0, 0.249600, 0.649600, 1.43448, 1.88039, 2.42801, 3.09510, 3.34731, 3.61568, 3.90100, 4}; double accumulated_error = 0.0; int i; for(i=0; i<20; i++) { accumulated_error += dabs(c_symb(x_sample_points[i]) - y_sample_points[i]); } return accumulated_error; } #endif char my_program[] = "double c_symb(double x) {\n" " return (exp(1.0)/log(1.0));\n" "}\n" "double dabs (double x) {\n" " return x > 0 ? x : -x;\n" "}\n" "double c_eval() {\n" "double x_sample_points[] = { -1, -0.9, -0.8, -0.76, -0.72, -0.68,\n" " -0.64, -0.4, -0.2, 0, 0.2, 0.4, 0.63,\n" " 0.72, 0.81, 0.90, 0.93, 0.96, 0.99, 1};\n" " double y_sample_points[] = { 0, -0.162900, -0.262400, -0.287754, -0.306109,\n" " -0.318218, -0.324772, -0.278400, -0.166400, 0,\n" " 0.249600, 0.649600, 1.43448, 1.88039,\n" " 2.42801, 3.09510, 3.34731, 3.61568, 3.90100, 4};\n" " double accumulated_error = 0.0;\n" " int i;\n" " for(i=0; i<20; i++) {\n" " accumulated_error += dabs(c_symb(x_sample_points[i]) - y_sample_points[i]);\n" " }\n" " return accumulated_error;\n" "}\n"; int main() { TCCState *tcc_state; // FILE *tmp; char filename[256]; char chromosome[1024]; int i, j, c; int raised_exceptions = 0; double the_fitness_error; tcc_state = tcc_new(); tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY); #ifdef NO_COMPILE tcc_add_symbol(tcc_state, "c_eval", (unsigned long)&c_eval); #endif #ifndef NO_COMPILE tcc_compile_string(tcc_state, my_program); #endif tcc_relocate(tcc_state); double (*func)(void) = (double (*)(void))tcc_get_symbol(tcc_state, "c_eval"); feclearexcept(FE_ALL_EXCEPT); the_fitness_error = func(); raised_exceptions = fetestexcept(FE_DIVBYZERO); if(raised_exceptions > 0) { printf("exception!\n"); } printf("fitness: %f\n", the_fitness_error); // fclose(tmp); tcc_delete(tcc_state); return 0; }