private void btnTest_Click(object sender, EventArgs e) { if (tbxTestFilePath.Text == "" || tbxModelFilePath.Text == "") { MessageBox.Show("Error: You must fill all required inputs!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } #region Phần chung //Ma trận với dòng thứ 1 chứa các giá trị thực và dòng thứ 2 chứa các giá trị dự đoán. double[][] dblActual_Forecast = new double[2][]; dblActual_Forecast[0] = null; dblActual_Forecast[1] = null; int iPos = tbxTestFilePath.Text.LastIndexOf('\\'); string strFolderPath = tbxTestFilePath.Text.Remove(iPos+1); #endregion #region Phần riêng #region Dự đoán giá if (rdPricePrediction.Checked)//Dự đoán giá { iPos = tbxTestFilePath.Text.LastIndexOf('_'); string strMutualPath = tbxTestFilePath.Text.Remove(iPos + 1); string strPredictedFile = strMutualPath + "predict.txt"; #region SVR if (rdSVR.Checked)//Mô hình SVR { string[] strArgs = new string[3]; strArgs[0] = tbxTestFilePath.Text; strArgs[1] = tbxModelFilePath.Text; strArgs[2] = strPredictedFile; StreamReader reader = new StreamReader(tbxModelFilePath.Text); string strLine = reader.ReadLine(); if (strLine == "Multiple") //Bag Pattern Search { StreamWriter measure = new StreamWriter(strMutualPath + "SingleModelMeasure.txt"); strLine = reader.ReadLine(); //Số mô hình int iNumModel = Convert.ToInt32(strLine); double[][][] dblActual_ForecastSeries = new double[iNumModel][][]; for (int i = 0; i < iNumModel; i++) { dblActual_ForecastSeries[i] = new double[2][]; dblActual_ForecastSeries[i][0] = null; dblActual_ForecastSeries[i][1] = null; strLine = reader.ReadLine(); //Đường dẫn đến file model strArgs[1] = strLine; dblActual_ForecastSeries[i] = svm_predict.MainProcess(strArgs); MeasureBUS measureBUS = new MeasureBUS(); double dblMSE = measureBUS.MSE(dblActual_ForecastSeries[i][0], dblActual_ForecastSeries[i][1]); double dblNMSE = measureBUS.NMSE(dblActual_ForecastSeries[i][0], dblActual_ForecastSeries[i][1]); double dblDS = measureBUS.DS(dblActual_ForecastSeries[i][0], dblActual_ForecastSeries[i][1]); measure.WriteLine((i + 1).ToString() + " " + dblMSE.ToString() + " " + dblNMSE.ToString() + " " + dblDS.ToString()); } measure.Close(); //Để có kết quả sau cùng, ta sẽ lấy trung bình kết quả dự đoán của tất cả các mô hình lại int iCounter = dblActual_ForecastSeries[0][0].Count(); dblActual_Forecast[0] = new double[iCounter]; dblActual_Forecast[1] = new double[iCounter]; for (int i = 0; i < iCounter; i++) { //Giá thực dblActual_Forecast[0][i] = dblActual_ForecastSeries[0][0][i]; //Giá dự đoán dblActual_Forecast[1][i] = 0; for (int j = 0; j < iNumModel; j++) { dblActual_Forecast[1][i] += dblActual_ForecastSeries[j][1][i]; } dblActual_Forecast[1][i] /= iNumModel; } //Ghi kết quả cuối cùng ra file StreamWriter finalResult = new StreamWriter(strPredictedFile); for (int i = 0; i < iCounter; i++) { finalResult.WriteLine(dblActual_Forecast[0][i].ToString() + " " + dblActual_Forecast[1][i].ToString()); } finalResult.Close(); reader.Close(); } else { reader.Close(); dblActual_Forecast = svm_predict.MainProcess(strArgs); } } #endregion #region ANN else//Mô hình ANN { ANNModelBUS.AnnModelFile = tbxModelFilePath.Text; ANNParameterBUS.LoadParameter(); ANNPredictBUS annPredict = new ANNPredictBUS(); annPredict.LoadDataSet(tbxTestFilePath.Text); dblActual_Forecast = annPredict.MainProcess(); //dblActual_Forecast = annPredict.StepTrainingMethod(); annPredict.WritePredictPrice(strPredictedFile); } #endregion } #endregion #region Dự đoán xu hướng else//Dự đoán xu hướng { if (rdSVR.Checked)//Mô hình SVR { iPos = tbxTestFilePath.Text.LastIndexOf('_'); string strMutualPath = tbxTestFilePath.Text.Remove(iPos + 1); string strPredictedFile = strMutualPath + "predict.txt"; string[] strArgs = new string[3]; strArgs[0] = tbxTestFilePath.Text; strArgs[1] = tbxModelFilePath.Text; strArgs[2] = strPredictedFile; StreamReader reader = new StreamReader(tbxModelFilePath.Text); string strLine = reader.ReadLine(); if (strLine == "Multiple") //Bag Pattern Search { StreamWriter measure = new StreamWriter(strMutualPath + "SingleModelMeasure.txt"); strLine = reader.ReadLine(); //Số mô hình int iNumModel = Convert.ToInt32(strLine); double[][][] dblActual_ForecastSeries = new double[iNumModel][][]; for (int i = 0; i < iNumModel; i++) { dblActual_ForecastSeries[i] = new double[2][]; dblActual_ForecastSeries[i][0] = null; dblActual_ForecastSeries[i][1] = null; strLine = reader.ReadLine(); //Đường dẫn đến file model strArgs[1] = strLine; dblActual_ForecastSeries[i] = svm_predict.MainProcess(strArgs); MeasureBUS measureBUS = new MeasureBUS(); double dblNMSE = measureBUS.NMSE(dblActual_ForecastSeries[i][0], dblActual_ForecastSeries[i][1]); measure.WriteLine((i + 1).ToString() + " " + dblNMSE.ToString()); } measure.Close(); //Để có kết quả sau cùng, ta sẽ lấy trung bình kết quả dự đoán của tất cả các mô hình lại int iCounter = dblActual_ForecastSeries[0][0].Count(); dblActual_Forecast[0] = new double[iCounter]; dblActual_Forecast[1] = new double[iCounter]; for (int i = 0; i < iCounter; i++) { //Giá thực dblActual_Forecast[0][i] = dblActual_ForecastSeries[0][0][i]; //Giá dự đoán dblActual_Forecast[1][i] = 0; for (int j = 0; j < iNumModel; j++) { dblActual_Forecast[1][i] += dblActual_ForecastSeries[j][1][i]; } dblActual_Forecast[1][i] /= iNumModel; } //Ghi kết quả cuối cùng ra file StreamWriter finalResult = new StreamWriter(strPredictedFile); for (int i = 0; i < iCounter; i++) { finalResult.WriteLine(dblActual_Forecast[0][i].ToString() + " " + dblActual_Forecast[1][i].ToString()); } finalResult.Close(); reader.Close(); } else { reader.Close(); dblActual_Forecast = svm_predict.MainProcess(strArgs); } } else//Mô hình ANN { iPos = tbxTestFilePath.Text.LastIndexOf('_'); string strPredictedFile = tbxTestFilePath.Text.Remove(iPos + 1) + "predict.txt"; ANNModelBUS.AnnModelFile = tbxModelFilePath.Text; ANNParameterBUS.LoadParameter(); ANNPredictBUS annPredict = new ANNPredictBUS(); annPredict.LoadDataSet(tbxTestFilePath.Text); dblActual_Forecast = annPredict.MainProcessTrend(); annPredict.WritePredictPrice(strPredictedFile); } } #endregion #endregion #region Phần chung HandleMeasure(strFolderPath+"PerformanceMeasure.txt", dblActual_Forecast[0], dblActual_Forecast[1]); MessageBox.Show("Finish!"); #endregion }
private void HandleMeasure(string fileName, double []actual, double []forecast) { StreamWriter writer = new StreamWriter(fileName); MeasureBUS measureBUS = new MeasureBUS(); CheckedListBox.CheckedItemCollection checkedItems = clbMeasures.CheckedItems; for (int i = 0; i < checkedItems.Count; i++) { switch (checkedItems[i].ToString()) { case "MSE": double dblMSE = measureBUS.MSE(actual, forecast); writer.WriteLine("MSE " + dblMSE.ToString()); break; case "NMSE": double dblNMSE = measureBUS.NMSE(actual, forecast); writer.WriteLine("NMSE " + dblNMSE.ToString()); break; case "RMSE": double dblRMSE = measureBUS.RMSE(actual, forecast); writer.WriteLine("RMSE " + dblRMSE.ToString()); break; case "APE": double dblAPE = measureBUS.APE(actual, forecast); writer.WriteLine("APE " + dblAPE.ToString()); break; case "MAPE": double dblMAPE = measureBUS.MAPE(actual, forecast); writer.WriteLine("MAPE " + dblMAPE.ToString()); break; case "DS": double dblMeasure = measureBUS.DS(actual, forecast); writer.WriteLine("DS " + dblMeasure.ToString()); break; case "WDS": double dblWDS = measureBUS.WDS(actual, forecast); writer.WriteLine("WDS " + dblWDS.ToString()); break; case "Sign": double dblSign = measureBUS.Sign(actual, forecast); writer.WriteLine("Sign " + dblSign.ToString()); break; case "DM4Price": double[] dblResult = measureBUS.DirectionMeasure4Price(actual, forecast); writer.WriteLine("Direction measure for price:"); writer.WriteLine("\t+ Right: "+dblResult[0]+"%"); writer.WriteLine("\t+ Wrong: " + dblResult[1] + "%"); writer.WriteLine("\t+ Cannot measure: " + dblResult[2] + "%"); break; } } writer.Close(); }