void second_check(sparsegraph * sg) { int nCuts = (1<<(n-1))-1; for (int i=0; i<nCuts; i++) { for (int j=i+1; j<nCuts; j++) { bool merger = true; int sum = 0, m=-1; for (int o=0; o<k; o++) if (oMark[i][o] != oMark[j][o]) { merger = false; break; } else { sum += (oMark[i][o]) ? 1:0; if (!oMark[i][o]) m = o; } if (merger && sum == k-1) { //printf("%0o %0o\n", i,j); if (isMerger(m,i,j,sg)) { //printf("Never mind. There is a merger.\n"); return; } } } } nTarget++; printf("find one! graph %d (e=%d).\n", count, sg->nde/2); printNetwork(); }
void printNetworkToFile(char * filename, GAindividual individual) { ReactionNetwork * r = (ReactionNetwork*)individual; FILE *stream; int i, n; if (!r || (r->type < 0) || (r->type > NUMBER_OF_NETWORK_TYPES)) return; stream = fopen(filename, "w"); if (!stream) return; printNetwork(stream,individual); fclose(stream); }
static int callBackWithLogKeeping(int iter, int popSz, GApopulation pop, double * fitnessArray, int *** parentsArray) { unsigned long long * seeds; double f; int i,j,k,*parents, num = 10*popSz, max = 0, stop = 0; int * temp = 0, * ids = 0; GAindividual * p; //save int each_fitness = PRINT_EACH_FITNESS, each_script = PRINT_EACH_SCRIPT, each_size = PRINT_EACH_SIZE, each_best_lineage = PRINT_EACH_BEST_LINEAGE, each_all_fitness = PRINT_EACH_ALL_FITNESS, each_all_lineage = PRINT_EACH_ALL_LINEAGE; if (USER_CALLBACK_FNC) stop = USER_CALLBACK_FNC(iter,popSz,pop,fitnessArray, parentsArray); if (!stop) stop = (iter == _MAX_ITER); if (stop) { //cheat PRINT_EACH_FITNESS = PRINT_FINAL_FITNESS; PRINT_EACH_SCRIPT = PRINT_FINAL_SCRIPT; PRINT_EACH_SIZE = PRINT_FINAL_SIZE; PRINT_EACH_BEST_LINEAGE = PRINT_FINAL_BEST_LINEAGE; PRINT_EACH_ALL_FITNESS = PRINT_FINAL_ALL_FITNESS; PRINT_EACH_ALL_LINEAGE = PRINT_FINAL_ALL_LINEAGE; //printf("\n========final results=======\n"); fprintf(LOGFILE,"\n========final results=======\n"); if (LOGFILE && PRINT_SEEDS) { seeds = getMTseeds(); fprintf(LOGFILE,"random number generator seeds: %llf,%llf,%llf,%llf\n",seeds[0],seeds[1],seeds[2],seeds[3]); } } if (iter == 0) //header { printf("gen"); fprintf(LOGFILE,"gen"); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { printf("\tfitness "); fprintf(LOGFILE,"\tfitness "); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { //printf("\tfitness_%i",i); fprintf(LOGFILE,"\tfitness_%i",i); } } if (PRINT_EACH_SIZE) { printf("\tspecies\treactions"); fprintf(LOGFILE,"\tspecies\treactions"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { //printf("\tparents"); fprintf(LOGFILE,"\tparents"); } printf("\n"); fprintf(LOGFILE,"\n"); printf("---"); fprintf(LOGFILE,"---"); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { printf("\t------- "); fprintf(LOGFILE,"\t------- "); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { //printf("\t----------",i); fprintf(LOGFILE,"\t -------- ",i); } } if (PRINT_EACH_SIZE) { printf("\t-------\t---------"); fprintf(LOGFILE,"\t-------\t---------"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { //printf("\t-------"); fprintf(LOGFILE,"\t-------"); } printf("\n"); fprintf(LOGFILE,"\n"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { ids = (int*)malloc(num * sizeof(int)); for (i=0; i < num; ++i) ids[i] = 0; for (i=0; i < popSz; ++i) { p = pop[i]; if (!p) continue; parents = GAgetOriginalParents(i,iter,parentsArray); if (parents) { for (j=0; parents[j] > 0; ++j) { if (parents[j] >= max) max = parents[j]; if (parents[j] >= num) { temp = ids; ids = (int*)malloc( num * 2 * sizeof(int) ); for (k=0; k < num; ++k) ids[k] = temp[k]; num *= 2; for (; k < num; ++k) ids[k] = 0; free(temp); } ids[ parents[j] ] += 1; } } else { } if (PRINT_EACH_BEST_LINEAGE && !PRINT_EACH_ALL_LINEAGE) break; } } printf("%i",iter); fprintf(LOGFILE,"%i",iter); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { f = fitnessArray[0]; printf("\t%lf",f); fprintf(LOGFILE,"\t%lf",f); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { f = fitnessArray[i]; //printf("\t%lf",f); fprintf(LOGFILE,"\t%lf",f); } } if (PRINT_EACH_SIZE) { printf("\t%i\t%i",getNumSpecies(pop[0]),getNumReactions(pop[0])); fprintf(LOGFILE,"\t%i\t%i",getNumSpecies(pop[0]),getNumReactions(pop[0])); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { for (i=0; i < max; ++i) { //printf("\t%i",ids[i]); fprintf(LOGFILE,"\t%i",ids[i]); } } if (PRINT_EACH_SCRIPT) { //printf("\n========script=======\n"); fprintf(LOGFILE,"\n========script=======\n"); //printNetwork(stdout,pop[0]); printNetwork(LOGFILE,pop[0]); //printf("\n=====================\n"); fprintf(LOGFILE,"\n=====================\n"); } printf("\n"); fprintf(LOGFILE,"\n"); if (ids && (num > 0)) free(ids); if (iter == _MAX_ITER) { //restore PRINT_EACH_FITNESS = each_fitness; PRINT_EACH_SCRIPT = each_script; PRINT_EACH_SIZE = each_size; PRINT_EACH_BEST_LINEAGE = each_best_lineage; PRINT_EACH_ALL_FITNESS = each_all_fitness; PRINT_EACH_ALL_LINEAGE = each_all_lineage; } return stop; }
int main( int argc, char *argv[] ) { nn_type nn; double atof(); FILE *log_file; FILE *out_file; if( argc != 6 ) { fprintf( stderr, "Usage: nn learning_rate k hidden log_file out_file < digits_train.txt\n" ); fprintf( stderr, " log_file - file to record progress of training\n"); fprintf( stderr, " out_file - file to record final network\n"); exit(0); } nn.learning_rate = atof( argv[1] ); nn.k = atof( argv[2] ); nn.n_hidden = atoi( argv[3] ); if( (log_file = fopen( argv[4], "w" )) == NULL ) { fprintf( stderr, "Could not open file %s\n", argv[4] ); exit( 0 ); } if( (out_file = fopen( argv[5], "w" )) == NULL ) { fprintf( stderr, "Could not open file %s\n", argv[5] ); exit( 0 ); } fprintf( log_file, "learning rate: %0.2f\n", nn.learning_rate ); fprintf( log_file, "multiplicative constant (k): %0.1f\n", nn.k ); fprintf( log_file, "hidden units: %d\n", nn.n_hidden ); /* * Number of input lines. * NO NEED TO CHANGE THIS. */ nn.n_input = 64; /* * Number of output lines. * NO NEED TO CHANGE THIS. */ nn.n_output = 10; /* * Total amount of data. * YOU MAY WISH TO CHANGE THIS. */ all_data.n = N_EXAMPLES; /* total amount of data */ if( all_data.n > N_EXAMPLES ) { fprintf( stderr, "Too many examples; increase N_EXAMPLES\n" ); exit( 0 ); } readData( nn.n_input, nn.n_output, &all_data ); splitData( nn.n_input, nn.n_output, &all_data, &training_data, &test_data ); trainNetwork( log_file, &nn, &training_data ); testNetwork( log_file, &nn, &test_data ); printNetwork( out_file, &nn ); fclose( log_file ); fclose( out_file ); }