private void learnSolver()
        {
            foreach (LearningModel learningModel in LearningList)
            {
                TaskTemplate taskTemplate = (TaskTemplate)TaskTemplate.where (new Query("TaskTemplate").addTypeQuery(TypeQuery.select)
                                                                              .addCondition("Name", "=", learningModel.SelectedPreprocessing), typeof(TaskTemplate))[0];
                Selection selection = (Selection)Selection.where (new Query("Selection").addTypeQuery(TypeQuery.select)
                                                                  .addCondition("TaskTemplateID", "=", taskTemplate.ID.ToString())
                                                                  .addCondition("Name", "=", learningModel.SelectedSelection), typeof(Selection))[0];
                int countRows = selection.RowCount;
                LearningScenario learningScenario = (LearningScenario)LearningScenario.where (new Query("LearningScenario").addTypeQuery(TypeQuery.select)
                                                                                              .addCondition("Name", "=", learningModel.SelectedScenario), typeof(LearningScenario))[0];

                List <Entity> selectionRows = SelectionRow.where (new Query("SelectionRow").addTypeQuery(TypeQuery.select)
                                                                  .addCondition("SelectionID", "=", selection.ID.ToString()), typeof(SelectionRow));
                List <Entity> parameters = dms.models.Parameter.where (new Query("Parameter").addTypeQuery(TypeQuery.select)
                                                                       .addCondition("TaskTemplateID", "=", selection.TaskTemplateID.ToString()), typeof(dms.models.Parameter));
                int       stepRow    = 0;
                float[][] inputData  = new float[countRows][];
                float[]   outputData = new float[countRows];
                for (int i = 0; i < countRows; i++)
                {
                    inputData[i] = new float[parameters.Count - 1];
                }
                int outputParam = 0;
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (((models.Parameter)parameters[i]).IsOutput == 1)
                    {
                        outputParam = parameters[i].ID;
                    }
                }

                string[][] vals  = Selection.valuesOfSelectionId(selection.ID);
                float[][]  fvals = new float[selection.RowCount][];
                for (int i = 0; i < selection.RowCount; i++)
                {
                    fvals[i] = new float[parameters.Count];
                    for (int j = 0; j < parameters.Count - 1; j++)
                    {
                        inputData[i][j] = float.Parse(vals[i][j].Replace(".", ","));
                    }
                    outputData[i] = float.Parse(vals[i][parameters.Count - 1].Replace(".", ","));
                }
                ISolver isolver = null;
                if (Solver.Description is PerceptronTopology)
                {
                    PerceptronTopology topology = Solver.Description as PerceptronTopology;
                    isolver = new PerceptronManaged(topology);
                }
                else if (Solver.Description is ConvNNTopology)
                {
                    ConvNNTopology topology = Solver.Description as ConvNNTopology;
                    isolver = new ConvNNManaged(topology);
                }
                else if (Solver.Description is WardNNTopology)
                {
                    WardNNTopology topology = Solver.Description as WardNNTopology;
                    isolver = new WardNNManaged(topology);
                }
                else if (Solver.Description is KohonenNNTopology)
                {
                    KohonenNNTopology topology = Solver.Description as KohonenNNTopology;
                    isolver = new KohonenManaged(topology);
                }
                else if (Solver.Description is TreeDescription)
                {
                    TreeDescription topology = Solver.Description as TreeDescription;
                    isolver = new DecisionTree(topology);
                }
                else if (Solver.Description is TreeDescriptionC4_5)
                {
                    TreeDescriptionC4_5 topology = Solver.Description as TreeDescriptionC4_5;
                    isolver = new DecisionTreeC4_5(topology);
                }
                else
                {
                    throw new EntryPointNotFoundException();
                }
                SeparationOfDataSet s  = new SeparationOfDataSet(isolver, learningScenario, inputData, outputData);
                LearnedSolver       ls = new LearnedSolver()
                {
                    SelectionID        = selection.ID,
                    LearningScenarioID = learningScenario.ID,
                    TaskSolverID       = Solver.ID,
                    Soul = s.separationAndLearn(selection.ID, outputParam)
                };
                ls.save();

                LearningQuality lq = new LearningQuality()
                {
                    LearnedSolverID = ls.ID,
                    MistakeTrain    = Convert.ToInt32(s.MistakeTrain),
                    MistakeTest     = Convert.ToInt32(s.MistakeTest),
                    ClosingError    = s.ClosingError
                };
                lq.save();
            }
            OnClose?.Invoke(this, null);
        }
        public ISolver simpleSeparation(int percentTrain)
        {
            int sizeTrainDataset = Convert.ToInt32(InputData.Length * ((double)percentTrain / 100));
            int sizeTestDataset  = InputData.Length - sizeTrainDataset;

            float[][] trainInputDataset  = new float[sizeTrainDataset][];
            float[][] testInputDataset   = new float[InputData.Length - sizeTrainDataset][];
            float[]   trainOutputDataset = new float[sizeTrainDataset];
            float[]   testOutputDataset  = new float[InputData.Length - sizeTrainDataset];
            Array.Copy(InputData, trainInputDataset, sizeTrainDataset);
            Array.Copy(InputData, sizeTrainDataset, testInputDataset, 0, sizeTestDataset);
            Array.Copy(OutputData, trainOutputDataset, sizeTrainDataset);
            Array.Copy(OutputData, sizeTrainDataset, testOutputDataset, 0, sizeTestDataset);

            if (ISolver is INeuralNetwork)
            {
                LearningAlgoManager la = new LearningAlgoManager()
                {
                    usedAlgo = LS.LearningAlgorithmName,
                    LAParams = LS.LAParameters
                };
                ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset);
            }
            else if (ISolver is DecisionTreeC4_5)
            {
                DecisionTreeC4_5LearningAlgo la = new DecisionTreeC4_5LearningAlgo();
                ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset);
            }
            else if (ISolver is DecisionTree)
            {
                DecisionTreeCARTLearningAlgo la = new DecisionTreeCARTLearningAlgo();
                ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset);
            }


            PreprocessingManager preprocessing = new PreprocessingManager();

            mistakeTrain = 0;
            List <string> expectedOutputValues = trainOutputDataset.Select(x => Convert.ToString(x)).ToList();
            List <string> obtainedOutputValues = new List <string>();

            if (ISolver is KohonenManaged)
            {
                KohonenManaged koh = ISolver as KohonenManaged;
                koh.startLogWinners();
            }
            for (int i = 0; i < sizeTrainDataset; i++)
            {
                obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(trainInputDataset[i])[0]));
            }
            List <bool> comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID);

            if (ISolver is KohonenManaged)
            {
                KohonenManaged koh = ISolver as KohonenManaged;
                koh.stopLogWinners();
                koh.declareWinnersOutput(comparisonOfResult);
            }

            var counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

            if (counts.ContainsKey(false))
            {
                mistakeTrain = (float)counts[false] / (float)sizeTrainDataset;
            }
            else
            {
                mistakeTrain = 0;
            }

            mistakeTest          = 0;
            expectedOutputValues = testOutputDataset.Select(x => Convert.ToString(x)).ToList();
            obtainedOutputValues.Clear();
            for (int i = 0; i < sizeTestDataset; i++)
            {
                obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(testInputDataset[i])[0]));
            }
            comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID);
            counts             = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
            if (counts.ContainsKey(false))
            {
                mistakeTest = (float)counts[false] / (float)sizeTestDataset;
            }
            else
            {
                mistakeTest = 0;
            }

            return(ISolver);
        }
        private ISolver crossValidation(int folds)
        {
            ISolver      curSolver          = ISolver.Copy();
            List <float> listOfTestMistakes = new List <float>();

            for (int k = 0; k < folds; k++)
            {
                float     kMistakeTrain      = 0;
                float     kMistakeTest       = 0;
                float[][] trainInputDataset  = GetInputTrainData(InputData, folds, k);
                float[][] testInputDataset   = GetInputTestData(InputData, folds, k);
                float[]   trainOutputDataset = GetOutputTrainData(OutputData, folds, k);
                float[]   testOutputDataset  = GetTestOutputData(OutputData, folds, k);

                LearningAlgoManager la = new LearningAlgoManager()
                {
                    usedAlgo = LS.LearningAlgorithmName,
                    LAParams = LS.LAParameters
                };
                PreprocessingManager preprocessing = new PreprocessingManager();
                ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset);
                int           sizeTrainDataset     = trainInputDataset.Length;
                List <string> expectedOutputValues = trainOutputDataset.Select(x => Convert.ToString(x)).ToList();
                List <string> obtainedOutputValues = new List <string>();

                if (ISolver is KohonenManaged)
                {
                    KohonenManaged koh = ISolver as KohonenManaged;
                    koh.startLogWinners();
                }
                for (int i = 0; i < sizeTrainDataset; i++)
                {
                    obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(trainInputDataset[i])[0]));
                }
                List <bool> comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID);
                if (ISolver is KohonenManaged)
                {
                    KohonenManaged koh = ISolver as KohonenManaged;
                    koh.stopLogWinners();
                    koh.declareWinnersOutput(comparisonOfResult);
                }

                var counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
                kMistakeTrain = (float)counts[false] / (float)sizeTrainDataset;

                int sizeTestDataset = testOutputDataset.Length;
                expectedOutputValues = testOutputDataset.Select(x => Convert.ToString(x)).ToList();
                obtainedOutputValues.Clear();
                for (int i = 0; i < sizeTestDataset; i++)
                {
                    obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(testInputDataset[i])[0]));
                }
                comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID);
                counts             = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
                kMistakeTest       = (float)counts[false] / (float)sizeTestDataset;
                if (k != 0 && kMistakeTest < listOfTestMistakes.Min())
                {
                    curSolver    = ISolver.Copy();
                    mistakeTest  = kMistakeTest;
                    mistakeTrain = kMistakeTrain;
                }
                listOfTestMistakes.Add(kMistakeTest);
            }
            ISolver = curSolver.Copy() as INeuralNetwork;

            return(ISolver);
        }
        private void learnSolver()
        {
            foreach (LearningModel learningModel in LearningList)
            {
                TaskTemplate taskTemplate = (TaskTemplate)TaskTemplate.where (new Query("TaskTemplate").addTypeQuery(TypeQuery.select)
                                                                              .addCondition("Name", "=", learningModel.SelectedPreprocessing), typeof(TaskTemplate))[0];
                Selection selection = (Selection)Selection.where (new Query("Selection").addTypeQuery(TypeQuery.select)
                                                                  .addCondition("TaskTemplateID", "=", taskTemplate.ID.ToString())
                                                                  .addCondition("Name", "=", learningModel.SelectedSelection), typeof(Selection))[0];
                int countRows = selection.RowCount;
                LearningScenario learningScenario = (LearningScenario)LearningScenario.where (new Query("LearningScenario").addTypeQuery(TypeQuery.select)
                                                                                              .addCondition("Name", "=", learningModel.SelectedScenario), typeof(LearningScenario))[0];

                List <Entity> selectionRows = SelectionRow.where (new Query("SelectionRow").addTypeQuery(TypeQuery.select)
                                                                  .addCondition("SelectionID", "=", selection.ID.ToString()), typeof(SelectionRow));
                List <Entity> parameters = dms.models.Parameter.where (new Query("Parameter").addTypeQuery(TypeQuery.select)
                                                                       .addCondition("TaskTemplateID", "=", selection.TaskTemplateID.ToString()), typeof(dms.models.Parameter));
                int       stepRow    = 0;
                float[][] inputData  = new float[countRows][];
                float[]   outputData = new float[countRows];
                for (int i = 0; i < countRows; i++)
                {
                    inputData[i] = new float[parameters.Count - 1];
                }
                int outputParam = 0;
                foreach (Entity selRow in selectionRows)
                {
                    int selectionRowId = selRow.ID;
                    int stepParam      = 0;
                    foreach (Entity param in parameters)
                    {
                        int           paramId = param.ID;
                        List <Entity> value   = ValueParameter.where (new Query("ValueParameter").addTypeQuery(TypeQuery.select)
                                                                      .addCondition("ParameterID", "=", paramId.ToString()).
                                                                      addCondition("SelectionRowID", "=", selectionRowId.ToString()), typeof(ValueParameter));
                        if (((dms.models.Parameter)param).IsOutput == 1)
                        {
                            outputParam = param.ID;
                            string outputValue = ((ValueParameter)value[0]).Value;
                            float  outputFloat;
                            if (float.TryParse(outputValue, out outputFloat))
                            {
                                outputData[stepRow] = outputFloat;
                            }
                        }
                        else
                        {
                            inputData[stepRow][stepParam] = float.Parse(((ValueParameter)value[0]).Value, CultureInfo.InvariantCulture.NumberFormat);
                        }
                        stepParam++;
                    }
                    stepRow++;
                }
                ISolver isolver = null;
                if (Solver.Description is PerceptronTopology)
                {
                    PerceptronTopology topology = Solver.Description as PerceptronTopology;
                    isolver = new PerceptronManaged(topology);
                }
                else if (Solver.Description is ConvNNTopology)
                {
                    ConvNNTopology topology = Solver.Description as ConvNNTopology;
                    isolver = new ConvNNManaged(topology);
                }
                else if (Solver.Description is WardNNTopology)
                {
                    WardNNTopology topology = Solver.Description as WardNNTopology;
                    isolver = new WardNNManaged(topology);
                }
                else if (Solver.Description is KohonenNNTopology)
                {
                    KohonenNNTopology topology = Solver.Description as KohonenNNTopology;
                    isolver = new KohonenManaged(topology);
                }
                else if (Solver.Description is TreeDescription)
                {
                    TreeDescription topology = Solver.Description as TreeDescription;
                    isolver = new DecisionTree(topology);
                }
                else
                {
                    throw new EntryPointNotFoundException();
                }
                SeparationOfDataSet s  = new SeparationOfDataSet(isolver, learningScenario, inputData, outputData);
                LearnedSolver       ls = new LearnedSolver()
                {
                    SelectionID        = selection.ID,
                    LearningScenarioID = learningScenario.ID,
                    TaskSolverID       = Solver.ID,
                    Soul = s.separationAndLearn(selection.ID, outputParam)
                };
                ls.save();

                LearningQuality lq = new LearningQuality()
                {
                    LearnedSolverID = ls.ID,
                    MistakeTrain    = Convert.ToInt32(s.MistakeTrain),
                    MistakeTest     = Convert.ToInt32(s.MistakeTest),
                    ClosingError    = s.ClosingError
                };
                lq.save();
            }
            OnClose?.Invoke(this, null);
        }