/***non recursive permutation module***/ int permutation_non_recursive(const char* s){ /*wrapper*/ int len=strlen(s); char* tmp=(char *)malloc((len+1)*sizeof(char)); if(!tmp) return -1; int* address=(int *)malloc(len*sizeof(int)); if(!address) return -1; strcpy(tmp,s); permutation_init(s,address,len); /*permutate all possibilities*/ while (permutation_next(s,address,tmp,len)!=-1) { continue; } free(tmp); free(address); printf("\n"); }
int train_network_pool(float error_limit) { int i, init = 0; //init is 0 for the first generation of the algorithm char index[10],file_name[100], selected_network_file[100]; dnc* dnc_vars; Marchand* marchand_vars; float learning_rate, momentum, local_error, threshold = 0.001; int selected_index, selected_source; // selected_source = 0 -> dnc, 1 -> marchand selected index x-> xth network of selected_source int present_gen = 0; int count_mc = 0; // count misclassification float init_error = 1.0; srand(time(NULL)); dnc_vars = new dnc[INIT_NET]; marchand_vars = new Marchand[INIT_NET]; nn test_network; float present_error, previous_error; int increase_count = 0; present_error = previous_error = 1.0; char file_name_to_save[100]; FILE *fp_output = fopen("graph_input.csv","w"); if(fp_output == NULL) printf("Error Opening File ... %s\n", "graph_input.csv"); fprintf(fp_output, "%d,%f\n", present_gen, 1.0*NUM_GEN); while(present_gen < NUM_GEN) { if(present_gen) //if(true) { float local_error = 0.0; learning_rate = (double)(rand() % 10 + 2) / (double)(100); momentum = (double)(rand() % 9 + 1) / (double)(10); cout << "GEN learing rate : " << learning_rate << " momentum : " << momentum << endl; int layer[3] = {ATTR_NUM, 1, NUM_CLASS}; test_network.create_nn(3, layer, 0, learning_rate, momentum, MAX_NODES); printf("running .."); test_network.load(selected_network_file); //for( int i = 0; i < TRAIN_SIZE; i++) for( int i = 0; i < VALIDATION_SIZE; i++) { test_network.calculate(validation_input[i]); local_error += test_network.get_error(validation_output[i]); } printf("Total error: %f Average error: %f\n", local_error, (float)(local_error/ TRAIN_SIZE)); fprintf(fp_output, "%d,%f\n", present_gen, NUM_GEN * (float)(local_error/ TRAIN_SIZE)); if( (float)(local_error/ TRAIN_SIZE) < error_limit) { test_network.save((char *) "genetic_ouptut.txt", 2); printf("The network is converged ...\n"); fclose(fp_output); return 0; } else { test_network.save((char *) "genetic_ouptut.txt", 2); printf("The network is SAVED ...\n"); } present_error = (float)(local_error/ TRAIN_SIZE); if(fabs(present_error - previous_error) < (error_limit/10)) { printf("Network Converged ... final case\n"); test_network.save((char *) "genetic_ouptut.txt", 2); fclose(fp_output); return 0; } if(present_error > previous_error) { increase_count++; sprintf(file_name_to_save, "genetic/genetic_output%d", increase_count); test_network.save((char*) file_name_to_save); sprintf(file_name_to_save, ""); } else if(present_error < previous_error) increase_count = 0; if(increase_count == MAIN_ERROR_INCREASE) { test_network.save((char *) "genetic_ouptut.txt", 2); printf("Returning from the main function ... %d\n", increase_count); fclose(fp_output); return 0; } else if( (present_error > previous_error) && (present_error - previous_error) < MAIN_ERROR_CHANGE) { sprintf(file_name_to_save, "genetic/genetic_outputLastCase"); test_network.save((char*) file_name_to_save); sprintf(file_name_to_save, ""); printf("Last case, Increase Count : %d\n", increase_count); fclose(fp_output); return 0; } printf("The increase count is : %d\n", increase_count); previous_error = present_error; } int m; for(i = 0; i < INIT_NET; i++) { sprintf(file_name, "dnc%d",i); //puts(file_name); //supply each network a time to stop, a random learning rate and a random permutation learning_rate = (double)(rand() % 10 + 2) / (double)(100); momentum = (double)(rand() % 7 + 1) / (double)(10); cout << "learing rate : " << learning_rate << " momentum : " << momentum << endl; dnc_vars[i].init_dnc(ATTR_NUM, NUM_CLASS, TRAIN_SIZE, EPOCH_DNC, 0.001, MAX_NODES, learning_rate, momentum, file_name, TRAIN_TIME); dnc_vars[i].set_monotone_increament(5); dnc_vars[i].take_validation_data((float *) validation_input, (float *)validation_output, VALIDATION_SIZE); // num of validation data as last argument if(present_gen) dnc_vars[i].load(selected_network_file); error[i] = dnc_vars[i].execute((float *)input, (float *)output); strcpy(file_name,""); } m = i; // hold the value INIT_NET for(i = 0; i < INIT_NET; i++) { strcpy(file_name,""); sprintf(file_name, "marchand%d", i); //puts(file_name); if(!present_gen) { permutation_next(); marchand_vars[i].init_marchand(ATTR_NUM, NUM_CLASS, TRAIN_SIZE, 200, 0.05, MAX_NODES, file_name, TRAIN_TIME/3.0); marchand_vars[i].take_validation_data((float *) validation_input, (float *)validation_output, VALIDATION_SIZE); error[m+i] = marchand_vars[i].execute((float *)input_temp, (float *)output_temp); } else { permutation_next_another((float *)remaining_input, (float *)remaining_output, count_mc); marchand_vars[i].init_marchand(ATTR_NUM, NUM_CLASS, count_mc, 200, 0.05, MAX_NODES, file_name, TRAIN_TIME/3.0); marchand_vars[i].load(selected_network_file); marchand_vars[i].take_validation_data((float *) validation_input, (float *)validation_output, VALIDATION_SIZE); error[m+i] = marchand_vars[i].execute((float *)remaining_input, (float *)remaining_output); } } //best selection select_network(&selected_index, &selected_source); cout << "selected index: " << selected_index << " selected source: " << selected_source << endl; count_mc = 0; if(selected_source == 0) { for(int m = 0; m < TRAIN_SIZE; m++) { local_error = dnc_vars[selected_index].calculate_for_single(input[m],output[m]); if(local_error > threshold) { for(int k = 0; k < ATTR_NUM; k++) remaining_input[count_mc][k] = input[m][k]; for(int k = 0; k < NUM_CLASS; k++) remaining_output[count_mc][k] = output[m][k]; count_mc++; } } } if(!selected_source) sprintf(selected_network_file, "dnc%d", selected_index); else sprintf(selected_network_file, "marchand%d", selected_index); cout << "Generation is : " << present_gen << endl; present_gen++; } delete []dnc_vars; delete []marchand_vars; return 0; }