Weather casting with Machine Learning (SVM and SRNN).
Dependencies: EthernetInterface GraphicHandler NTPClient SRNN SVM SensorModule mbed-rtos mbed
Diff: setup.cpp
- Revision:
- 2:20ecfe6edd71
- Parent:
- 1:8538381cae81
- Child:
- 3:5add3759e08a
--- a/setup.cpp Mon Feb 16 07:53:45 2015 +0000 +++ b/setup.cpp Wed Feb 18 15:02:16 2015 +0000 @@ -8,11 +8,11 @@ int ret, line; float buf_data[DIM_SIGNAL]; - float* tmp_sample = new float[MCSVM_NUM_SAMPLES * DIM_SIGNAL]; - int* tmp_sample_label = new int[MCSVM_NUM_SAMPLES]; - float* tmp_mc_alpha = new float[MCSVM_NUM_SAMPLES * NUM_WEATHERS * (NUM_WEATHERS - 1) / 2]; + float* svm_tmp_sample = new float[MCSVM_NUM_SAMPLES * DIM_SIGNAL]; + int* svm_tmp_sample_label = new int[MCSVM_NUM_SAMPLES]; + float* svm_tmp_mc_alpha = new float[MCSVM_NUM_SAMPLES * NUM_WEATHERS * (NUM_WEATHERS - 1) / 2]; - svm_setup_fp = fopen( "/local/svm_samp.csv" , "r" ); + svm_setup_fp = fopen( "/local/SVM_SAMP.CSV" , "r" ); if( svm_setup_fp == NULL ) { fprintf( stderr, "Error in svm setup : sample file cannot open. \r \n" ); exit(1); @@ -22,29 +22,29 @@ while( ( ret = fscanf( svm_setup_fp, " %[^\n,],%f,%f,%f", buf_str, &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { if ( !strcmp(buf_str,"shiny") ) { - tmp_sample_label[line] = SHINY; + svm_tmp_sample_label[line] = SHINY; } else if ( !strcmp(buf_str,"cloudy") ) { - tmp_sample_label[line] = CLOUDY; + svm_tmp_sample_label[line] = CLOUDY; } else if ( !strcmp(buf_str,"rainy") ) { - tmp_sample_label[line] = RAINY; + svm_tmp_sample_label[line] = RAINY; } else if ( !strcmp(buf_str,"snowy") ) { - tmp_sample_label[line] = SNOWY; + svm_tmp_sample_label[line] = SNOWY; } else { continue; } - tmp_sample[line * 3] = buf_data[0]; - tmp_sample[line * 3 + 1] = buf_data[1]; - tmp_sample[line * 3 + 2] = buf_data[2]; + memcpy(&(svm_tmp_sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); + // printf("svm sample loading.... ret : %d line : %d %s %f %f %f \r\n", ret, line, buf_str, svm_tmp_sample[line*3], svm_tmp_sample[line*3+1], svm_tmp_sample[line*3+2]); line++; - // printf("svm sample loading.... %d \r\n", line); } - // fclose( svm_setup_fp ); + mcsvm = new MCSVM(NUM_WEATHERS, DIM_SIGNAL, MCSVM_NUM_SAMPLES, svm_tmp_sample, svm_tmp_sample_label); - mcsvm = new MCSVM(NUM_WEATHERS, DIM_SIGNAL, MCSVM_NUM_SAMPLES, tmp_sample, tmp_sample_label); - - svm_setup_fp = fopen("/local/alpha.csv", "r"); + // Thank you freopen. + // Here, we should not use fclose -> fopen + svm_setup_fp = freopen("/local/SVM_ALPH.CSV", "r", svm_setup_fp ); + fflush( svm_setup_fp ); // required. + if ( svm_setup_fp == NULL ) { fprintf( stderr, "Error in open learned alpha data. \r\n"); exit(1); @@ -52,21 +52,19 @@ // 一列のデータではfscanfフォーマットがだるいので, fgetsを使用 line = 0; - while( fgets( buf_str, 20, svm_setup_fp) != NULL ) { - tmp_mc_alpha[line] = atof(buf_str); + while( fgets( buf_str, 20, svm_setup_fp ) != NULL ) { + svm_tmp_mc_alpha[line] = atof(buf_str); // printf("%d %f \r\n", line, tmp_mc_alpha[line]); line++; } - mcsvm->set_alpha(tmp_mc_alpha, MCSVM_NUM_SAMPLES, NUM_WEATHERS); + fclose( svm_setup_fp ); + + mcsvm->set_alpha(svm_tmp_mc_alpha, MCSVM_NUM_SAMPLES, NUM_WEATHERS); - delete [] tmp_sample; - delete [] tmp_sample_label; - delete [] buf_data; - delete [] tmp_mc_alpha; - delete [] buf_str; - fclose( svm_setup_fp ); - free( svm_setup_fp ); // mbed BUG - we must free file pointer. + delete [] svm_tmp_sample; + delete [] svm_tmp_sample_label; + delete [] svm_tmp_mc_alpha; } @@ -78,6 +76,7 @@ float buf_data[DIM_SIGNAL]; float* sample = new float[LEN_DATA_SEQUENCE * DIM_SIGNAL]; float* sample_maxmin = new float[DIM_SIGNAL * 2]; + char buf_str[20]; // 信号の正規化のために, 信号の最大値と最小値を決めてやる必要がある. sample_maxmin[0] = 50; @@ -87,26 +86,27 @@ sample_maxmin[4] = 100; sample_maxmin[5] = 0; // 湿度 - srnn_setup_fp = fopen( "/local/srnninit.csv" , "r" ); + srnn_setup_fp = fopen( SEQUENCE_DATA_NAME, "r"); if( srnn_setup_fp == NULL ) { - fprintf( stderr, "Error in SRNN setup. init sample file cannot open. \r\n"); + fprintf( stderr, "Error in SRNN setup. sample file cannot open. \r\n"); exit(1); } int line = 0; - while( ( ret = fscanf( srnn_setup_fp, "%f,%f,%f", &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { + while( ( ret = fscanf( srnn_setup_fp, " %[^\n,],%f,%f,%f", buf_str, &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { memcpy(&(sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); // printf("sample %d : %f %f %f \r\n", line, MATRIX_AT(sample,DIM_SIGNAL,line,0), MATRIX_AT(sample,DIM_SIGNAL,line,1), MATRIX_AT(sample,DIM_SIGNAL,line,2)); line++; } + fclose( srnn_setup_fp ); + /* アドバイス:RNNにおいては,ダイナミクス(中間層のニューロン数)は多いほど良い */ srnn = new SRNN(DIM_SIGNAL, 20, LEN_DATA_SEQUENCE, PREDICT_LENGTH, sample, sample_maxmin); delete [] sample; delete [] sample_maxmin; - fclose( srnn_setup_fp ); - free( srnn_setup_fp ); + } // センサーのセットアップ. @@ -129,6 +129,7 @@ } if ( eth_if.connect() < 0 ) { + // (offlineが確定する -> offline modeへ). fprintf( stderr, "%s Ethernet connect failed. \r\n", prefix_net_str); exit(1); } @@ -138,45 +139,51 @@ // Please specify near ntp server. ex) Japan -> ntp.nict.jp:123 if (ntp_client.setTime("ntp.nict.jp") == 0) { - time_mutex.lock(); - global_time = time(NULL); - time_mutex.unlock(); - printf("%s Set time successfully! \n Time is set to (UTC): %s\r\n", prefix_net_str, ctime(&global_time)); + printf("%s Set time successfully! \r\n", prefix_net_str); } else { fprintf( stderr, "%s Error in setup time \r\n", prefix_net_str); } - //setup tcp socket - if(http_server.bind(80) < 0) { - fprintf( stderr, "%s HTTP server bind port 80 failed.\n\r", prefix_net_str); - exit(1); - } else { - printf("%s HTTP server bind successed.\n\r", prefix_net_str); - } - - if(http_server.listen(1) < 0) { - fprintf( stderr, "%s HTTP server listen failed.\n\r", prefix_net_str); - exit(1); - } else { - printf("%s HTTP server is listening...\n\r", prefix_net_str); - } + // setup http server + http_server = new HTTPServer(80, "/local/"); printf("%s IP Address : %s \r\n", prefix_net_str, eth_if.getIPAddress()); printf("%s Network setup finished! \r\n", prefix_net_str); } +// グラフィックハンドラの初期化 +static void graphic_handler_setup(void) +{ + graphic_handler = new GraphicHandler(DIM_SIGNAL, PREDICT_INTERVAL_TIME, PREDICT_LENGTH, get_JST()); +} + +// データの初期化(アロケート) +static void data_setup(void) +{ + new_seqence_data = new float[DIM_SIGNAL]; // 現在の(一番新しい)系列データ + new_predict_data = new float[DIM_SIGNAL * PREDICT_LENGTH]; // 現在の予測結果 + new_predict_weather = new int[PREDICT_LENGTH]; // 現在の予測天気 + new_predict_probability = new float[PREDICT_LENGTH]; // 現在の予測天気の確率 +} + // セットアップ. void setup(void) { - printf("SETUP START. \r\n"); + printf("SETUP START "); + printf("-------------------------- \r\n"); + mcsvm_setup(); + printf("SVM ...OK \r\n"); srnn_setup(); - printf("SRNN OK. \r\n"); - mcsvm_setup(); - printf("SVM OK. \r\n"); + printf("SRNN ...OK \r\n"); sensor_setup(); - printf("SENSOR OK. \r\n"); - network_setup(); - printf("NETWORK OK. \r\n"); - + printf("SENSOR ...OK \r\n"); + // network_setup(); + printf("NETWORK ...OK \r\n"); + graphic_handler_setup(); + printf("GRAPHIC ...OK \r\n"); + data_setup(); + printf("SHARED DATA ...OK \r\n"); + printf("SETUP SUCESS "); + printf("-------------------------- \r\n"); }