Example #1
0
        //Todo: Create dialog
        private void optimizeParametersNN_btn_Click(object sender, EventArgs e)
        {
            new Thread(delegate() {
                //Todo: Drop fields random

                string[] inputFields = new string[] { "ssi-mt4", "spread", "mid-TradingTime", "mid-Stoch_600000", "mid-Stoch_1800000", "mid-Stoch_3600000", "mid-Stoch_7200000", "mid-Stoch_14400000", "mid-Stoch_21600000", "mid-MA_600000", "mid-MA_1800000", "mid-MA_3600000", "mid-MA_7200000", "mid-MA_14400000", "mid-MA_21600000", "mid-Range_1800000", "mid-Range_3600000", "mid-Range_7200000" };
                string outputField   = "buy-outcomeCode-0,001_600000";
                int epochs           = 10;
                int networksCount    = 500;

                string path = Application.StartupPath + "/AI/";
                GeneralExcelGenerator excel = new GeneralExcelGenerator(path + "excel.xlsx");
                string sheetName            = "OptimizeNN-E10";

                string[] header = new string[2 + AdvancedNeuralNetwork.getExcelHeader().Length];
                AdvancedNeuralNetwork.getExcelHeader().CopyTo(header, 2);
                header[0] = "ValidationE";
                header[1] = "TrainingE";

                excel.CreateSheet(sheetName, header);

                double[][] inputsTraining  = new double[][] { };
                double[][] outputsTraining = new double[][] { };
                dataminingDb.getInputOutputArrays(inputFields, outputField, "EURUSD", ref inputsTraining, ref outputsTraining, DataGroup.All, 100 * 1000, 0);

                double[][] inputsValidation  = new double[][] { };
                double[][] outputsValidation = new double[][] { };
                dataminingDb.getInputOutputArrays(inputFields, outputField, "EURUSD", ref inputsValidation, ref outputsValidation, DataGroup.All, 10 * 1000, 1);

                dataminingDb.unloadPair("EURUSD");

                for (int ns = 0; ns < networksCount; ns++)
                {
                    IMachineLearning network = AdvancedNeuralNetwork.getRandom(inputFields, outputField);
                    network.train(inputsTraining, outputsTraining, epochs);

                    double trainingError   = network.getError();
                    double validationError = network.getPredictionErrorFromData(inputsValidation, outputsValidation);

                    string[] excelRow = new string[2 + network.getInfo(inputFields, outputField).Length];
                    network.getInfo(inputFields, outputField).CopyTo(excelRow, 2);
                    excelRow[0] = validationError.ToString();
                    excelRow[1] = trainingError.ToString();

                    excel.addRow(sheetName, excelRow);

                    setState("Optimizing NN " + ns + "/" + networksCount);
                }

                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }

                excel.FinishSheet(sheetName);
                excel.FinishDoc();
                excel.save();
            }).Start();
        }
        public double TestMachineLearning(IMachineLearning learning)
        {
            double[][] input = new double[10][];
            input[0] = new double[] { 1, 2, 1 };
            input[1] = new double[] { 1, 2, 1 };
            input[2] = new double[] { 1, 2, 2 };
            input[3] = new double[] { 2, 2, 1 };
            input[4] = new double[] { 2, 2, 3 };
            input[5] = new double[] { 3, 2, 2 };
            input[6] = new double[] { 3, 2, 1 };
            input[7] = new double[] { 3, 2, 5 };
            input[8] = new double[] { 4, 3, 3 };
            input[9] = new double[] { 4, 3, 2 };

            double[][] output = new double[10][];
            output[0] = new double[] { 1, 0 };
            output[1] = new double[] { 1, 0 };
            output[2] = new double[] { 1, 0 };
            output[3] = new double[] { 1, 1 };
            output[4] = new double[] { 1, 1 };
            output[5] = new double[] { 1, 1 };
            output[6] = new double[] { 0, 1 };
            output[7] = new double[] { 0, 1 };
            output[8] = new double[] { 0, 1 };
            output[9] = new double[] { 0, 1 };

            learning.train(input, output, 1);

            double[] low  = learning.getPrediction(new double[] { 0, 0, 0 });
            double[] mid  = learning.getPrediction(new double[] { 2, 2, 2 });
            double[] high = learning.getPrediction(new double[] { 5, 5, 5 });

            double score = Math.Abs(low[0] - 1) + Math.Abs(low[1] - 0) +
                           Math.Abs(mid[0] - 1) + Math.Abs(mid[1] - 1) +
                           Math.Abs(high[0] - 0) + Math.Abs(high[1] - 1);

            score /= 6;

            Assert.IsTrue(false, "Low: " + low[0] + " " + low[1] + Environment.NewLine + "Mid: " + mid[0] + " " + mid[1] + Environment.NewLine + "Heigh: " + high[0] + " " + high[1]);

            return(score);
        }