int main(int argc, char * argv[]){ if (argc == 4){ printf("%s\n", argv[3]); } char * path_nn1; //path to first layer of network char * path_nn2; //path to second layer int fd_pipe; //file descriptor to wii data pipe struct fann *ann1; struct fann *ann2; int inputs_ann1, inputs_ann2; pid_t wiimote; char path_to_test[100]; // printf("%d\n\n", argc); if(argc != 3 && argc !=4){ printf("Run the program as follows:\n" "wiigesturedata <Path_to_network1> <Path_to_network2>\n"); return -1; } if((access(argv[1], F_OK)==-1) || (access(argv[2], F_OK)==-1)){ printf("Please specify valid paths to the networks.\n"); return -2; } ann1 = fann_create_from_file(argv[1]); ann2 = fann_create_from_file(argv[2]); if(argc !=4){ //setup pipe to wiidata fd_pipe =open(PATH_TO_WIIDATA, 0666); if((wiimote = launch_wii_process()) <0){ return 1; } if(wait_on_signal(fd_pipe)){ printf("Wiimote detected successfully\n"); }else{ printf("No wiimotes detected\n"); return -3; } // signal(SIGINT, signal_handler); while(keepgoing){ process(ann1, ann2, fd_pipe); } printf("Killing wiimote process\n"); kill(wiimote, SIGINT); return 0; } else{ if((access(argv[3], F_OK)==-1)){ printf("Please specify a valid path to the test data\n"); return -2; } process_test_data(argv[3], ann1, ann2); } }
void cunit_xor_test(void) { fann_type *calc_out = NULL; unsigned int i; int ret = 0; struct fann *ann = NULL; struct fann_train_data *data = NULL; #ifdef FIXEDFANN ann = fann_create_from_file("xor_fixed.net"); #else ann = fann_create_from_file("xor_float.net"); #endif CU_ASSERT_PTR_NOT_NULL_FATAL(ann); #ifdef FIXEDFANN data = fann_read_train_from_file("xor_fixed.data"); #else data = fann_read_train_from_file("xor.data"); #endif CU_ASSERT_PTR_NOT_NULL_FATAL(data); for(i = 0; i < fann_length_train_data(data); i++) { fann_reset_MSE(ann); calc_out = fann_test(ann, data->input[i], data->output[i]); CU_ASSERT_PTR_NOT_NULL_FATAL(calc_out); #ifdef FIXEDFANN /*printf("XOR test (%d, %d) -> %d, should be %d, difference=%f\n", data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0], (float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann));*/ if((float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann) > 0.2) { CU_FAIL("XOR test failed."); ret = -1; } #else /*printf("XOR test (%f, %f) -> %f, should be %f, difference=%f\n", data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0], (float) fann_abs(calc_out[0] - data->output[i][0]));*/ #endif } fann_destroy_train(data); fann_destroy(ann); }
/*** * @function rspamd_fann.load(file) * Loads neural network from the file * @param {string} file filename where fann is stored * @return {fann} fann object */ static gint lua_fann_load_file (lua_State *L) { #ifndef WITH_FANN return 0; #else struct fann *f, **pfann; const gchar *fname; fname = luaL_checkstring (L, 1); if (fname != NULL) { f = fann_create_from_file (fname); if (f != NULL) { pfann = lua_newuserdata (L, sizeof (gpointer)); *pfann = f; rspamd_lua_setclass (L, "rspamd{fann}", -1); } else { lua_pushnil (L); } } else { lua_pushnil (L); } return 1; #endif }
int main( int argc, char** argv ) { fann_type *calc_out; unsigned int i; int ret = 0; struct fann *ann; struct fann_train_data *data; printf("Creating network.\n"); ann = fann_create_from_file("xor_float.net"); if(!ann) { printf("Error creating ann --- ABORTING.\n"); return 0; } fann_print_connections(ann); fann_print_parameters(ann); printf("Testing network.\n"); data = fann_read_train_from_file("5K.txt"); for(i = 0; i < fann_length_train_data(data); i++) { fann_reset_MSE(ann); fann_scale_input( ann, data->input[i] ); calc_out = fann_run( ann, data->input[i] ); fann_descale_output( ann, calc_out ); printf("Result %f original %f error %f\n", calc_out[0], data->output[i][0], (float) fann_abs(calc_out[0] - data->output[i][0])); } printf("Cleaning up.\n"); fann_destroy_train(data); fann_destroy(ann); return ret; }
int main() { int i, j, k,temp, num_test, num_input; fann_type *calc_out; fann_type input[NUM_INPUT]; FILE *in = fopen("test.data", "r"); fscanf(in, "%d", &num_test); printf("%d\n", num_test); //skip_space_newline(in); fscanf(in, "%d", &num_input); printf("%d\n", num_input); struct fann *ann = fann_create_from_file("ann.net"); for(i=0; i<num_test; i++){ for(j=0; j<num_input; j++){ fscanf(in, "%d", &temp); input[j] = temp; } calc_out = fann_run(ann, input); for(k=0; k<NUM_OUTPUT; k++){ printf("%f ", calc_out[k]); } printf("\n"); } //input[0] = -1; //input[1] = 1; //calc_out = fann_run(ann, input); //printf("xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); //for(i=0; i<NUM_OUTPUT; i++){ // printf("%f\n", calc_out[i]); //} fann_destroy(ann); return 0; }
void cunit_simple_test(void) { fann_type *calc_out; fann_type input[2]; struct fann *ann = fann_create_from_file("xor_float.net"); CU_ASSERT_PTR_NOT_NULL_FATAL(ann); input[0] = 1; input[1] = 1; calc_out = fann_run(ann, input); CU_ASSERT(IS_NEG(calc_out[0])); input[0] = -1; input[1] = 1; calc_out = fann_run(ann, input); CU_ASSERT(IS_POS(calc_out[0])); input[0] = 1; input[1] = -1; calc_out = fann_run(ann, input); CU_ASSERT(IS_POS(calc_out[0])); input[0] = 1; input[1] = 1; calc_out = fann_run(ann, input); CU_ASSERT(IS_NEG(calc_out[0])); fann_destroy(ann); }
int main() { //unsigned int layers[] = {17, 500, 400, 300, 200, 1}; //unsigned int layerCount = sizeof(layers) / sizeof(unsigned int); const char* netLoadFile = "data/AAPL_ADBE_ATVI_COKE_EBAY_EXPE_NFLX_VA.300.250.200_100.net"; const char* netSaveFile = "data/AAPL_ADBE_ATVI_COKE_EBAY_EXPE_NFLX_VA.300.250.200_200.net"; const float desired_error = (const float) 0.10; const unsigned int max_epochs = 100; const unsigned int epochs_between_reports = 1; //struct fann *ann = fann_create_standard_array(layerCount, layers); struct fann *ann = fann_create_from_file(netLoadFile); fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); printf("%s\n", netSaveFile); fann_train_on_file(ann, "data/AAPL_ADBE_ATVI_COKE_EBAY_EXPE_NFLX_VA.train.fann", max_epochs, epochs_between_reports, desired_error); fann_save(ann, netSaveFile); fann_destroy(ann); printf("%s\n", netSaveFile); return 0; }
bool ViFann::loadFromFile(const QString &path) { QFile file(path); if(!file.exists()) return false; clear(); mNetwork = fann_create_from_file(path.toLatin1().data()); if(mNetwork == NULL) return false; fann_nettype_enum type = fann_get_network_type(mNetwork); //TODO: get type int layerCount = fann_get_num_layers(mNetwork); unsigned int layers[layerCount]; fann_get_layer_array(mNetwork, layers); for(int i = 0; i < layerCount; ++i) { mNeurons.append(layers[i]); } mInputCount = mNeurons.first(); mOutputCount = mNeurons.last(); if(mInput == NULL) delete [] mInput; if(mOutput == NULL) delete [] mOutput; mInput = new float[mInputCount]; mOutput = new float[mOutputCount]; mConnectionRate = fann_get_connection_rate(mNetwork); //TODO: get weights return true; }
const float CNeuroNetwok::Recognize(const _tstring& sPath) { if (!m_bIsNetworkTeached) throw std::runtime_error("You should teach network first!"); // Восстанавливаем нейросеть из файла m_pANN = fann_create_from_file(NETWORK_FILE_NAME); if (!m_pANN) { std::string sError = "Failed to load data from: "; sError += NETWORK_FILE_NAME; throw std::runtime_error(sError); } // Подгружаем данные указанного файла std::list< float > BmpData; AnalizeBMP(sPath, BmpData); // Преобразуем TTrainData TestData; TestData.push_back(std::pair< std::list< float >, bool > (BmpData, false)); boost::scoped_ptr< fann_train_data > pTestData(MakeTrainData(TestData)); #ifdef _DEBUG // Для дебага fann_save_train(pTestData.get(), "debug_data.dat"); #endif // Получаем результат fann_reset_MSE(m_pANN); fann_type * pResult = fann_test(m_pANN, pTestData->input[0], pTestData->output[0]); return *pResult; }
int main() { fann_type *calc_out; fann_type input[2]; struct fann *ann = fann_create_from_file("and_float.net"); input[0] = 0; input[1] = 0; calc_out = fann_run(ann, input); printf("AND test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); input[0] = 1; input[1] = 0; calc_out = fann_run(ann, input); printf("AND test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); input[0] = 0; input[1] = 1; calc_out = fann_run(ann, input); printf("AND test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); input[0] = 1; input[1] = 1; calc_out = fann_run(ann, input); printf("AND test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); fann_destroy(ann); return 0; }
void run_network(char * path_to_network,int num_iterations, FILE * results, int n){ FANN_EXTERNAL fann_type input[] = {M_PI}; FANN_EXTERNAL fann_type * output; int i; double time; // clock_t start, end, t; struct timeval start, end; suseconds_t elapsed_utime; long int elapsed_time, usec_diff, sec_diff; struct fann *ann = fann_create_from_file(path_to_network); // t = clock(); gettimeofday(&start, NULL); for(i=0; i<num_iterations; i++){ output = fann_run(ann, input); } // t=clock() - t; gettimeofday(&end, NULL); usec_diff = end.tv_usec - start.tv_usec; sec_diff = end.tv_sec - start.tv_sec; elapsed_time = sec_diff == 0 ? end.tv_usec - start.tv_usec : (end.tv_usec+ sec_diff*1000000) - start.tv_usec ; // fprintf(results, "%d\t%lf\t%lf\n",n,output[0], ((float)t)/CLOCKS_PER_SEC); fprintf(results, "%d\t%lf\t%ld\n",n,output[0], elapsed_time); }
/*** * @function rspamd_fann.load_data(data) * Loads neural network from the data * @param {string} file filename where fann is stored * @return {fann} fann object */ static gint lua_fann_load_data (lua_State *L) { #ifndef WITH_FANN return 0; #else struct fann *f, **pfann; gint fd; struct rspamd_lua_text *t; gchar fpath[PATH_MAX]; if (lua_type (L, 1) == LUA_TUSERDATA) { t = lua_check_text (L, 1); if (!t) { return luaL_error (L, "text required"); } } else { t = g_alloca (sizeof (*t)); t->start = lua_tolstring (L, 1, (gsize *)&t->len); t->flags = 0; } /* We need to save data to file because of libfann stupidity */ rspamd_strlcpy (fpath, "/tmp/rspamd-fannXXXXXXXXXX", sizeof (fpath)); fd = mkstemp (fpath); if (fd == -1) { msg_warn ("cannot create tempfile: %s", strerror (errno)); lua_pushnil (L); } else { if (write (fd, t->start, t->len) == -1) { msg_warn ("cannot write tempfile: %s", strerror (errno)); lua_pushnil (L); unlink (fpath); close (fd); return 1; } f = fann_create_from_file (fpath); unlink (fpath); close (fd); if (f != NULL) { pfann = lua_newuserdata (L, sizeof (gpointer)); *pfann = f; rspamd_lua_setclass (L, "rspamd{fann}", -1); } else { lua_pushnil (L); } } return 1; #endif }
int main(int argc, char* argv[]) { if(argc!= 2) error("Remember to specify an input file"); struct fann *ann = fann_create_from_file("default-net-1024-500-32-1.0e-6.net"); FILE *fp; char c; int i = 0; float bools[1024]; while if(!(fp = fopen(argv[1],"r"))) error("Cannot open input file", argv[1]); while(i<1024){ c = fgetc(fp); if(c == '0'){ bools[i]= 0; i++; } if(c == '1'){ bools[i]= 1; i++; } } float *output = fann_run(ann, bools); std::cout << "1: " << output[0] << std::endl << "3: " << output[1] << std::endl << "5: " << output[2] << std::endl << "6: " << output[3] << std::endl << "7: " << output[4] << std::endl << "8: " << output[5] << std::endl << "A: " << output[6] << std::endl << "B: " << output[7] << std::endl << "C: " << output[8] << std::endl << "D: " << output[9] << std::endl << "E: " << output[10] << std::endl << "F: " << output[11] << std::endl << "G: " << output[12] << std::endl << "H: " << output[13] << std::endl << "I: " << output[14] << std::endl << "J: " << output[15] << std::endl << "K: " << output[16] << std::endl << "L: " << output[17] << std::endl << "M: " << output[18] << std::endl << "N: " << output[19] << std::endl << "O: " << output[20] << std::endl << "P: " << output[21] << std::endl << "Q: " << output[22] << std::endl << "R: " << output[23] << std::endl << "S: " << output[24] << std::endl << "T: " << output[25] << std::endl << "U: " << output[26] << std::endl << "V: " << output[27] << std::endl << "W: " << output[28] << std::endl << "X: " << output[29] << std::endl << "Y: " << output[30] << std::endl << "Z: " << output[31] << std::endl; return 0; }
int main( int argc, char* argv[]) { const char * netfilename = argv[1]; const char * datfilename = argv[2]; const char * resfilename = argv[3]; FILE* fp_data; fp_data = fopen (datfilename,"r"); int i; int s; fann_type *calc_out; fann_type input[16]; //float ainput[16]; char* cinput[16]; struct fann *ann = fann_create_from_file( netfilename ); char line [400]; char flc [400]; FILE* fp_out; fp_out = fopen ( resfilename , "w"); while(fgets(line, 400, fp_data) != NULL) { line[strcspn(line, "\r\n")] = 0; snprintf(flc,sizeof(flc),"%s",line); // Read in line of input values cinput[0] = strtok(flc," "); input[0] = atof(cinput[0]); for (i=1; i<16; ++i) { cinput[i] = strtok(NULL," "); input[i] = atof(cinput[i]); } // run input through network calc_out = fann_run(ann, input); // Write to output file for (i=0; i<6; i++) { fprintf(fp_out,"%f ",calc_out[i]); } fprintf(fp_out,"\n"); } fclose(fp_data); fclose(fp_out); fann_destroy(ann); return 0; }
ann_t *ann_load() { ann_t *net = (ann_t *) fann_create_from_file((const char *) options->knowledge_file); if(!net) { /* FIXME */ exit(0); } return net; }
int main(int argc, char **argv) { fann_type *calc_out; unsigned int i, j; struct fann *ann; struct fann_train_data *data; float error = 0.0; if (argc < 3) { fprintf(stderr, "Use: %s FANN_network.net patternsFile\n", argv[0]); exit(1); } printf("Openning ANN `%s'\n", argv[1]); ann = fann_create_from_file(argv[1]); if (!ann) { fprintf(stderr, "Error creating the ANN.\n"); return (1); } printf("Running ANN.\n"); data = fann_read_train_from_file(argv[2]); for(i = 0; i < fann_length_train_data(data); i++) { calc_out = fann_run(ann, data->input[i]); printf("ANN: %f ", calc_out[0]); printf("Expected: %f ", data->output[i][0]); printf("Error: %f ", (float) (data->output[i][0] -calc_out[0])); printf("Throttle_Effort: %f Brake_Effort: %f Current_Velocity: %f\n", // essa multiplicacao ocorre para desfazer o que o 'gerarEntrada.c' fez data->input[i][360-3]*100.0, data->input[i][360-2]*100.0, data->input[i][360-1]*5.0); error += (float) powf(fann_abs(calc_out[0] - data->output[i][0]),2); } printf("Test:: Squared Error: %f Mean Squared Error: %f\n", error, error/(fann_length_train_data(data)-1)); printf("Cleaning memory.\n"); fann_destroy_train(data); fann_destroy(ann); return (0); }
int main() { struct fann *ann ; ann = fann_create_from_file("gesture_classify.net"); if(ann==NULL){ ROS_INFO("Creating an ANN...."); ann = fann_create(1,0.7,3,16,18,20); ROS_INFO("Done...."); } fann_train_on_file(ann, "training.data",2000,100,0.1); fann_save(ann, "gesture_classify.net"); fann_destroy(ann); return 0; }
int main(int argc, char* argv[]) { bool iflag, oflag, tflag, nflag, gflag; int flag; struct fann *nNetwork; char* inputFileName; char* dataFileName; char* outputFileName; srand(time(NULL)); while((flag = getopt(argc,argv,"i:o:t:")) != -1) { switch(flag) { case 'i': inputFileName = optarg; iflag = true; break; case 'o': outputFileName = optarg; oflag = true; break; case 't': dataFileName = optarg; tflag = true; break; } } if(iflag) { nNetwork = fann_create_from_file(inputFileName); } else { nNetwork = fann_create_standard(3,NUM_IN_OUT,NUM_IN_OUT,NUM_IN_OUT); } fann_set_activation_function_hidden(nNetwork, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(nNetwork, FANN_SIGMOID_SYMMETRIC); if(tflag) { fann_train_on_file(nNetwork,dataFileName,MAX_ITERATIONS,50,ERROR); } if(oflag) { fann_save(nNetwork,outputFileName); } fann_destroy(nNetwork); return 0; }
int main(int argc , char **argv) { fann_type *calc_out; int i; if (argc==1) { printf("usage : eval testFile.txt network.net\n"); return 0; } struct fann_train_data *testData = fann_read_train_from_file(argv[1]); struct fann *ann = ann = fann_create_from_file(argv[2]); int output_size = fann_get_num_output(ann); for(i=0;i<testData->num_data ;i++) { // calcul du résultat de la prédiction calc_out = fann_run(ann, testData->input[i]); // interprétation et affichage du résultat switch (maxIndex(calc_out,output_size)){ case 0 : printf ("fr\n"); break; case 1 : printf ("en\n"); break; case 2 : printf ("de\n"); break; case 3 : printf ("es\n"); break; case 4 : printf ("pt\n"); break; case 5 : printf ("it\n"); break; case 6 : printf ("tr\n"); break; } } fann_destroy(ann); return 0; }
int main() { fann_type *calc_out; fann_type input[2]; struct fann *ann = fann_create_from_file("t5.net"); int N; double err_acc = 0; double err_med = 0; scanf("%d%*d%*d",&N); FILE* f1, *f2; f1 = fopen("out.dat", "w+"); f2 = fopen("sol.dat", "w+"); double m_f = 0; std::vector<double> vf; for (int i = 0; i < N; ++i) { float esp; scanf("%f%f%f", &input[0], &input[1], &esp); calc_out = fann_run(ann, input); printf("(%f,%f) -> %f [%f]\n", input[0], input[1], calc_out[0], esp); fprintf(f1, "%d %f\n", i, calc_out[0]); fprintf(f2, "%d %f\n", i, esp); err_acc += fabs(esp - calc_out[0])/(1+fabs(esp)); vf.push_back(esp); m_f += esp; } m_f /= N; for (int i = 0; i < N; ++i) { err_med += fabs(vf[i] - m_f)/(1+fabs(vf[i])); } printf("err = %lf\n", 100*(err_acc / N)); printf("errM = %lf %lf\n", 100*(err_med / N), m_f); fann_destroy(ann); fclose(f1); fclose(f2); return 0; }
int main(int argc, char **argv) { fann_type *calc_out; unsigned int i, j; struct fann *ann; struct fann_train_data *data; if (argc < 2) { fprintf(stderr, "Use: %s arquivoTeste\n", argv[0]); exit(1); } printf("Abrindo a Rede `%s'\n", ARQ_RNA); ann = fann_create_from_file(ARQ_RNA); if (!ann) { fprintf(stderr, "Erro criando a RNA.\n"); return (1); } //fann_print_connections(ann); //fann_print_parameters(ann); printf("Testando a RNA.\n"); data = fann_read_train_from_file(argv[1]); for(i = 0; i < fann_length_train_data(data); i++) { fann_reset_MSE(ann); calc_out = fann_run(ann, data->input[i]); printf("Resultado: %f ", calc_out[0]); printf("Original: %f " , data->output[i][0]); printf("Erro: %f\n" , (float) fann_abs(calc_out[0] - data->output[i][0])); } printf("Limpando memoria.\n"); fann_destroy_train(data); fann_destroy(ann); return (0); }
void MainWindow::onLoadPericarditisANN() { QString filename = QFileDialog::getOpenFileName(this, tr("Open ann file"), "", tr("ANN file (*.net)")); if(filename.isEmpty()) return; if(pAnn != 0) fann_destroy(pAnn); QByteArray ba = filename.toLocal8Bit(); pAnn = fann_create_from_file(ba.data()); ui->tabWidget->setTabEnabled(2, true); }
void setup(void) { int i, r; char *ann_paths[2]; ann_paths[0] = "../data/ann/nextwall.net"; ann_paths[1] = "../../data/ann/nextwall.net"; /* for `make distcheck` */ for (i = 0; i < 2; i++) { if ( (r = g_file_test(ann_paths[i], G_FILE_TEST_IS_REGULAR)) ) { ann = fann_create_from_file(ann_paths[i]); break; } } if (!r) exit(EXIT_FAILURE); }
int main() { fann_type *calc_out; fann_type input[9]; int i; struct fann *ann = fann_create_from_file("comp.net"); //0 0 1 4 5 5 0 9 8 1 0 9 4 7 /*input[0] = 0; input[1] = 0; input[2] = 1; input[3] = 4; input[4] = 5; input[5] = 5; input[6] = 0; input[7] = 9; input[8] = 8; input[9] = 1; input[10] = 0; input[11] = 9; input[12] = 4; input[13] = 7;*/ //0 7 1 2 3 7 9 6 5 3 input[0] = 0; input[1] = 7; input[2] = 1; input[3] = 2; input[4] = 3; input[5] = 7; input[6] = 9; input[7] = 6; input[8] = 5; input[9] = 3; calc_out = fann_run(ann, input); //printf("xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); for(i = 0; i < 156; i++) { printf("%f ", calc_out[i]); } printf("\n"); //printf("0 7 1 2 3 7 9 6 5 3 test %f %f %f %f %f %f %f %f %f %f\n", calc_out[0], calc_out[1], calc_out[2], calc_out[3], calc_out[4], calc_out[5], calc_out[6], calc_out[7], calc_out[8], calc_out[9]); fann_destroy(ann); return 0; }
int main(int argc, char *argv[]) { fann_type *calc_out; fann_type input[3]; struct fann *ann = fann_create_from_file("fann_interface/net_eeg_data_float.net"); input[0] = strtof (argv[1], NULL); input[1] = strtof (argv[2], NULL); input[2] = strtof (argv[3], NULL); calc_out = fann_run(ann, input); // printf("is it blink? (%f,%f,%f) -> %f \n", input[0], input[1], input[2], calc_out[0]); printf("%f", calc_out[0]); fann_destroy(ann); return 0; }
// Neural Fitted Q Iteration - First Experiences with a data efficient neural reinforcement learning method : // <http://ml.informatik.uni-freiburg.de/_media/publications/rieecml05.pdf> // Fig. 1. void compute_steering_with_qlearning(double *steering_command, double atan_desired_curvature, double atan_current_curvature, double delta_t, carmen_simulator_ackerman_config_t *simulator_config) { int b; unsigned int num_data = 50; static state *s; static action *a; state s_temp; action a_temp; static unsigned int pattern_number = 0; static struct fann *qlearning_ann = NULL; double min_b_ann __attribute__ ((unused)); if (delta_t == 0 || atan_desired_curvature == 0.00) return; if (qlearning_ann == NULL) { qlearning_ann = fann_create_from_file("qlearning_ann.net"); if (qlearning_ann == NULL) {printf("Error: Could not create qlearning_ann\n");exit(1);} s = (state *) calloc(num_data, sizeof(state)); a = (action *) calloc(num_data, sizeof(action)); } if (pattern_number == 0) fprintf(stdout, "\n"); s_temp.atan_desired_curvature = atan_desired_curvature; s_temp.atan_current_curvature = atan_current_curvature; a_temp.steering_command = *steering_command; min_b_ann = min_b_Q(&b, qlearning_ann, s_temp, a_temp, simulator_config); qlearning_integrator(steering_command, b); fprintf(stdout, "b: %d min_b_Q: %.5lf s: %.5lf ", b, min_b_ann, *steering_command); store_transition_experience_for_latter_training(s, a, pattern_number, *steering_command, atan_desired_curvature, atan_current_curvature, b); if ((pattern_number + 1) == num_data) NFQ_train(qlearning_ann, num_data, s, a, simulator_config); pattern_number = (pattern_number + 1) % num_data; }
int main() { //FANN TRAINING: const unsigned int num_input = 4; const unsigned int num_output = 3; const unsigned int num_layers = 3; const unsigned int num_neurons_hidden = 4; const float desired_error = (const float) 0.001; const unsigned int max_epochs = 200000; const unsigned int epochs_between_reports = 1000; struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output); fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); fann_train_on_file(ann, "irisDataSet.data", max_epochs, epochs_between_reports, desired_error); fann_save(ann, "irisFANN_float.net"); fann_destroy(ann); //FANN TESTING fann_type *calc_out; fann_type input[4]; struct fann *annR = fann_create_from_file("irisFANN_float.net"); input[0] = 5.1; input[1] = 3.5; input[2] = 1.4; input[3] = 0.2; calc_out = fann_run(annR, input); std::cout << "Testing irisFANN, with data: " << input[0] << ", " << input[1] << ", " << input[2] << ", " << input[3] << ", " << ", output: " << calc_out[0] << ", " << calc_out[1] << ", " << calc_out[2] << "\n"; fann_destroy(annR); return 0; }
double compute_new_velocity_with_ann(carmen_simulator_ackerman_config_t *simulator_config) { static double throttle_command = 0.0; static double brakes_command = 100.0 / 100.0; static int gear_command = 0; static fann_type velocity_ann_input[NUM_VELOCITY_ANN_INPUTS]; fann_type *velocity_ann_output; static struct fann *velocity_ann = NULL; if (velocity_ann == NULL) { velocity_ann = fann_create_from_file("velocity_ann.net"); if (velocity_ann == NULL) { printf("Error: Could not create velocity_ann\n"); exit(1); } init_velocity_ann_input(velocity_ann_input); } carmen_ford_escape_hybrid_velocity_PID_controler(&throttle_command, &brakes_command, &gear_command, simulator_config->target_v, simulator_config->v, simulator_config->delta_t); if (gear_command == 129) // marcha reh { build_velocity_ann_input(velocity_ann_input, throttle_command, brakes_command, -simulator_config->v); velocity_ann_output = fann_run(velocity_ann, velocity_ann_input); simulator_config->v = velocity_ann_output[0]; } else { build_velocity_ann_input(velocity_ann_input, throttle_command, brakes_command, simulator_config->v); velocity_ann_output = fann_run(velocity_ann, velocity_ann_input); simulator_config->v = velocity_ann_output[0]; } simulator_config->v = simulator_config->v + simulator_config->v * carmen_gaussian_random(0.0, 0.007); // add some noise return (simulator_config->v); }
double compute_new_phi_with_ann_new(carmen_simulator_ackerman_config_t *simulator_config) { static double steering_command = 0.0; double atan_current_curvature, atan_desired_curvature; static fann_type steering_ann_input[NUM_STEERING_ANN_INPUTS]; fann_type *steering_ann_output __attribute__ ((unused)); static struct fann *steering_ann = NULL; if (steering_ann == NULL) { steering_ann = fann_create_from_file("steering_ann.net"); if (steering_ann == NULL) { printf("Error: Could not create steering_ann\n"); exit(1); } init_steering_ann_input(steering_ann_input); } atan_current_curvature = atan(compute_curvature(simulator_config->phi, simulator_config)); atan_desired_curvature = atan(compute_curvature(simulator_config->target_phi, simulator_config)); compute_steering_with_qlearning(&steering_command, atan_desired_curvature, atan_current_curvature, simulator_config->delta_t, simulator_config); build_steering_ann_input(steering_ann_input, steering_command, atan_current_curvature); steering_ann_output = fann_run(steering_ann, steering_ann_input); // Alberto: O ganho de 1.05 abaixo foi necessario pois a rede nao estava gerando curvaturas mais extremas // que nao aparecem no treino, mas apenas rodando livremente na simulacao //simulator_config->phi = 1.05 * get_phi_from_curvature(tan(steering_ann_output[0]), simulator_config); simulator_config->phi += steering_command / 500.0; simulator_config->phi = carmen_clamp(carmen_degrees_to_radians(-30.0), simulator_config->phi , carmen_degrees_to_radians(30.0)); if (simulator_config->delta_t != 0 && atan_desired_curvature != 0.0) fprintf(stdout, "d_phi: %.5lf phi: %.5lf\n", simulator_config->target_phi, simulator_config->phi); return (simulator_config->phi); }
unsigned int recogDigital(float *vector, float *fout) { fann_type *calc_out; // int calc_iout[4]; fann_type input[MAX_FEATURE_LEN]; int rec_num; int i=0; for (i=0; i<MAX_FEATURE_LEN; i++){ input[i] = (fann_type)vector[i]; } /*create ANN*/ struct fann *ann = fann_create_from_file("digital.net"); calc_out=fann_run(ann,input); for (i=0; i<ANN_OUTPUT_NUM; i++){ fout[i] = calc_out[i]; } fann_destroy(ann); return 0; }