/// <summary>
 /// Number of hidden layers changed
 /// </summary>
 private void NudHiddenLayersValueChanged(object sender, EventArgs e)
 {
     if (_predictor != null)
     {
         if (MessageBox.Show("ChangedNetwork", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
         {
             _predictor = null;
         }
     }
 }
        /// <summary>
        /// Predict the values
        /// </summary>
        private void BtnPredictClick(object sender, EventArgs e)
        {
            if (_dgvPredictionResults.Rows.Count != 0)
            {
                _dgvPredictionResults.Rows.Clear();
            }

            if (_predictor == null)         /*The network is untrained*/
            {
                _reloadFiles = false;

                switch (MessageBox.Show("UntrainedPredictorWarning", "NoNetwork", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information))
                {
                case DialogResult.Yes:
                    break;

                case DialogResult.No:
                    /*Load the network*/
                    this.Cursor   = Cursors.WaitCursor;
                    _hiddenLayers = (int)_nudHiddenLayers.Value;
                    _hiddenUnits  = (int)_nudHiddenUnits.Value;
                    try
                    {
                        _predictor = new PredictIndicators(listPredicInput, _hiddenUnits, _hiddenLayers);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        _predictor = null;
                        return;
                    }
                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                    using (OpenFileDialog ofd = new OpenFileDialog()
                    {
                        FileName = "predictor.ntwrk", Filter = "*.ntwrk"
                    })
                    {
                        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            try
                            {
                                _predictor.LoadNeuralNetwork(Path.GetFullPath(ofd.FileName));
                            }
                            catch
                            {
                                MessageBox.Show("Exception", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                        }
                    }
                    break;

                case DialogResult.Cancel:
                    return;
                }
            }
            DateTime predictFrom = _dtpPredictFrom.Value;
            DateTime predictTo   = _dtpPredictTo.Value;

            if (predictFrom.Date > predictTo.Date)
            {
                MessageBox.Show("PredictFromToWarning", "BadParameters", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _dtpPredictFrom.Focus();
                return;
            }

            if (_predictor == null)
            {
                Cursor        = Cursors.WaitCursor;
                _hiddenLayers = (int)_nudHiddenLayers.Value;
                _hiddenUnits  = (int)_nudHiddenUnits.Value;
                try
                {
                    _predictor = new PredictIndicators(listPredicInput, _hiddenUnits, _hiddenLayers);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _predictor = null;
                    return;
                }
                finally
                {
                    Cursor = Cursors.Default;
                }
            }
            PredicResults        predicResults;
            List <PredicResults> results = null;

            try
            {
                results = _predictor.Predict(predictFrom, predictTo);
                //predicResults = _predictor.Predict();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            foreach (var item in results)
            {
                _dgvPredictionResults.Rows.Add(item.Date.ToShortDateString(), item.ActualClose,
                                               item.PredictedClose.ToString("F6", CultureInfo.InvariantCulture),
                                               item.Error.ToString("F6", CultureInfo.InvariantCulture));
            }

            //_dgvPredictionResults.Rows.Add(predicResults.Date.ToShortDateString(), predicResults.ActualClose,
            //                                   predicResults.PredictedClose.ToString("F6", CultureInfo.InvariantCulture),
            //                                   predicResults.Error.ToString("F6", CultureInfo.InvariantCulture));

            //ve do thi
            //DrawGraph(DoThi_GiaiTri, results);
            _predictor.ShowError();
        }
        /// <summary>
        /// Start training button pressed
        /// </summary>
        private void BtnStartTrainingClick(object sender, EventArgs e)
        {
            if (_dgvTrainingResults.Rows.Count != 0)
            {
                _dgvTrainingResults.Rows.Clear();
            }

            if (_predictor == null)
            {
                _reloadFiles = false;
            }

            DateTime trainFrom = _dtpTrainFrom.Value;
            DateTime trainTo   = _dtpTrainUntil.Value;

            if (trainFrom > trainTo)
            {
                MessageBox.Show("thong bao", "Tham so khong dung", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _dtpTrainFrom.Focus();
                return;
            }
            FadeControls(true);
            if (_predictor == null)
            {
                Cursor        = Cursors.WaitCursor;
                _hiddenLayers = (int)_nudHiddenLayers.Value;
                _hiddenUnits  = (int)_nudHiddenUnits.Value;
                try
                {
                    _predictor = new PredictIndicators(listPredicInput, _hiddenUnits, _hiddenLayers);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _predictor = null;
                    return;
                }
                finally
                {
                    Cursor = Cursors.Default;
                }
            }
            else if (_reloadFiles) /*Reload training sets*/
            {
                _dtpTrainFrom.MinDate  = _predictor.MinIndexDate;
                _dtpTrainUntil.MaxDate = _predictor.MaxIndexDate;
            }
            /*Verify if dates do conform with the min/max ranges*/
            if (trainFrom < _predictor.MinIndexDate)
            {
                _dtpTrainFrom.MinDate = _dtpTrainFrom.Value = trainFrom = _predictor.MinIndexDate;
            }
            if (trainTo > _predictor.MaxIndexDate)
            {
                _dtpTrainUntil.MaxDate = _dtpTrainUntil.Value = trainTo = _predictor.MaxIndexDate;
            }
            _predictor.timeTrainningSet += PredictorOnTimeTrainningSet;
            TrainingStatus callback = TrainingCallback;

            _predictor.TrainNetworkAsync(trainFrom, trainTo, callback);
        }