Exemple #1
0
        public void iteration_of_optimization()
        {
            if (opt_inc == 1)
            {
                string new_save_folder;
                expert.copyExpertParametersToAlgorithms();
                for (int j = 0; j < expert.algorithms.Count; j++)
                {
                    new_save_folder = form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[0]" + "\\" + expert.algorithms[j].getValueByName("model_name") + "\\";
                    Directory.CreateDirectory(new_save_folder);
                    expert.algorithms[j].h.setValueByName("save_folder", new_save_folder);
                    string predictionsFilePath = new_save_folder + "predictions.txt";
                    expert.algorithms[j].h.setValueByName("predictions_file_path", predictionsFilePath);
                    expert.algorithms[j].h.setValueByName("json_file_path", new_save_folder + "h.json");
                    File.WriteAllText(new_save_folder + "h.json", expert.algorithms[j].h.toJSON(0), System.Text.Encoding.Default);

                    expert.algorithms[j].train();
                }
                expert.copyHyperparametersFromAlgorithmsToExpert();
                expert.H.setValueByName("report_path", form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[0]");
                expert.H.setValueByName("code", "0");
                expert.Save(form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[0]");


                expert.test_trading(date1, date2, rawDatasetFilePath);

                //отрисовка истрии баланса
                for (int i = 0; i < expert.deposit1History.Count; i++)
                {
                    variablesVisualizer.addPoint(expert.deposit2History[i] + (expert.deposit1History[i] * expert.closeValueHistory[i]), "exit [0]");
                    variablesVisualizer.addPoint(expert.deposit2History[i], "deposit2 [0]");
                    variablesVisualizer.addPoint(expert.closeValueHistory[i], "close [0]");
                }

                for (int i = 0; i < population_value; i++)
                {
                    expert.H.setValueByName("code", i.ToString());
                    population[i] = Copy(expert.H, form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[0]" + "\\", form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[" + i.ToString() + "]" + "\\");
                }

                variablesIDs.Clear();
                recurciveVariableAdding(population[0], 0, name + "[0]");

                /*   for (int i = 1; i < population_value; i++)
                 * {
                 *     variablesIDs.Clear();
                 *     recurciveVariableAdding(population[i], 0, name + "[" + i.ToString() + "]");
                 * }     */
                refreshEOTree();
                variablesVisualizer.refresh();
            }
            else
            {
                //kill and concieve
                kill_and_conceive();

                //mutation
                for (int i = 0; i < mutation_rate; i++)
                {
                    mutation();
                }

                for (int i = 0; i < population_value; i++)
                {
                    File.WriteAllText(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]" + "\\h.json", population[i].toJSON(0), System.Text.Encoding.Default);
                }


                //   ВВЕДЕНО МНОГОКРАТНОЕ ТЕСТИРОВНИЕ ИНДИВИДОВ ВСЕХ КАТЕГОРИЙ ДЛЯ ПОВЫШЕНИЯ ПОВТОРЯЕМОСТИ РЕЗУЛЬТАТОВ

                //   target_functions - матрица результатов тестирования,
                //   где номер строки (первый индекс (i)) - индекс индивида, а номер столбца (второй индекс (j)) - итерация тестирования
                double[,] target_functions = new double[population_value, test_count];

                for (int tc = 0; tc < test_count; tc++)
                {
                    if (opt_inc == 2)
                    {
                        if (agentManager.agents.Count == 0)
                        {
                            if (multiThreadTraining)
                            {
                                if (form1.multiThreadTrainingRATE != 0)
                                {
                                    multiThreadTrainingRATE = form1.multiThreadTrainingRATE;
                                }
                                log("multiThreadTrainingRATE = " + multiThreadTrainingRATE.ToString(), Color.Yellow);
                                if (multiThreadTrainingRATE > population_value)
                                {
                                    multiThreadTrainingRATE = population_value;
                                }

                                for (int begin = 0; begin < population_value; begin += multiThreadTrainingRATE)
                                {
                                    var now1   = new DateTimeOffset(DateTime.Now);
                                    var start1 = now1.ToUnixTimeSeconds();
                                    int end    = 0;
                                    if (begin + multiThreadTrainingRATE >= population_value)
                                    {
                                        end = population_value;
                                    }
                                    else
                                    {
                                        end = begin + multiThreadTrainingRATE;
                                    }
                                    List <Expert> experts = new List <Expert>();
                                    for (int i = begin; i < end; i++)
                                    {
                                        Expert exp = Expert.Open(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]", name, form1);
                                        //   exp.H = population[i].Clone();
                                        experts.Add(exp);
                                    }

                                    Task[] trainTasks = new Task[end - begin];
                                    foreach (Expert exp in experts)
                                    {
                                        trainTasks[experts.IndexOf(exp)] = new Task(() => experts[experts.IndexOf(exp)].trainAllAlgorithms(false));
                                        trainTasks[experts.IndexOf(exp)].Start();
                                    }

                                    foreach (var task in trainTasks)
                                    {
                                        task.Wait();
                                    }

                                    for (int i = begin; i < end; i++)
                                    {
                                        // experts[i].copyHyperparametersFromAlgorithmsToExpert();
                                        experts[i - begin].Save(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]");
                                        population[i] = experts[i - begin].H.Clone();
                                    }
                                    log((end).ToString() + '/' + population_value.ToString() + " training comlete" + TimeSpan.FromSeconds((new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) - start1).ToString(), Color.LimeGreen);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < population_value; i++)
                                {
                                    expert = Expert.Open(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]", name, form1);
                                    expert.trainAllAlgorithms(false);
                                    expert.Save(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]");
                                    population[i] = expert.H.Clone();
                                    log((i + 1).ToString() + '/' + population_value.ToString() + " training comlete", Color.LimeGreen);
                                }
                            }
                        }
                        else
                        {
                            for (int i = 0; i < population_value; i++)
                            {
                                var algorithmBranches = population[i].getNodesByparentID(expert.committeeNodeID);
                                foreach (Node algorithmBranch in algorithmBranches)
                                {
                                    agentManager.tasks.Add(new AgentTask("train", new Hyperparameters(population[i].toJSON(algorithmBranch.ID), form1)));
                                }
                                Task.Factory.StartNew(() => { agentManager.work(); }).Wait();
                                //  СПИСОК ВЕТВЕЙ АЛГОРИТМОВ
                                List <Node> toDelete = population[i].getNodesByparentID(expert.committeeNodeID);
                                //удаление старых конфигураций алгоритмов
                                for (int j = 0; j < toDelete.Count; j++)
                                {
                                    population[i].deleteBranch(toDelete[j].ID);
                                }

                                //приращение новых конфигураций к узлу "committee"
                                for (int j = 0; j < agentManager.tasks.Count; j++)
                                {
                                    population[i].addBranch(agentManager.tasks[j].h, agentManager.tasks[j].h.nodes[0].name(), expert.committeeNodeID);
                                }
                                agentManager.tasks.Clear();

                                //   expert.synchronizeHyperparameters();
                                File.WriteAllText(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]" + "\\h.json", population[i].toJSON(0), System.Text.Encoding.Default);
                            }
                        }
                    }
                    for (int i = 0; i < population_value; i++)
                    {
                        variablesVisualizer.Clear("committee response [" + i.ToString() + "]");
                        variablesVisualizer.Clear("deposit2 [" + i.ToString() + "]");
                        variablesVisualizer.Clear("deposit1 [" + i.ToString() + "]");
                        variablesVisualizer.Clear("exit [" + i.ToString() + "]");
                        variablesVisualizer.Clear("close [" + i.ToString() + "]");
                    }
                    //Тестирование (вычисление целевой функции)
                    for (int i = 0; i < population_value; i++)
                    {
                        expert = Expert.Open(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]", name, form1);
                        expert.test_trading(date1, date2, rawDatasetFilePath);
                        log("test[" + i.ToString() + "]: " + expert.H.getValueByName("expert_target_function"), Color.Purple);
                        population[i] = expert.H.Clone();
                        File.WriteAllText(form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]" + "\\h.json", population[i].toJSON(0), System.Text.Encoding.Default);
                        target_functions[i, tc] = Convert.ToDouble(population[i].getValueByName("expert_target_function").Replace('.', ','));

                        //отрисовка истрии баланса
                        for (int j = 0; j < expert.deposit1History.Count; j++)
                        {
                            variablesVisualizer.addPoint(expert.committeeResponseHistory[j][0], "committee response [" + i.ToString() + "]");
                            // variablesVisualizer.addPoint(expert.actionHistory[j], "actions [" + i.ToString() + "]");
                            variablesVisualizer.addPoint(expert.deposit2History[j] + (expert.deposit1History[j] * expert.closeValueHistory[j]), "exit [" + i.ToString() + "]");
                            variablesVisualizer.addPoint(expert.deposit1History[j], "deposit1 [" + i.ToString() + "]");
                            variablesVisualizer.addPoint(expert.closeValueHistory[j], "close [" + i.ToString() + "]");
                        }
                        variablesVisualizer.refresh();
                    }
                    log((tc + 1).ToString() + '/' + test_count.ToString() + " test comlete", Color.LimeGreen);
                }
                variablesVisualizer.refresh();
                //вычисление итоговых значений критерия оптимальности
                for (int i = 0; i < population_value; i++)
                {
                    double sum = 0;

                    // AVG
                    for (int j = 0; j < test_count; j++)
                    {
                        sum += target_functions[i, j];
                    }
                    double AVG = sum / test_count;

                    sum = 0;
                    // StdDev
                    for (int j = 0; j < test_count; j++)
                    {
                        sum += (AVG - target_functions[i, j]) * (AVG - target_functions[i, j]);
                    }

                    double StdDev = Math.Sqrt(sum / test_count);

                    // если  target_function равна  (AVG - StdDev), то последующее вычисление критерия оптимальности будет давать результаты ВЫШЕ, чем  target_function
                    population[i].setValueByName("expert_target_function", (AVG - StdDev).ToString().Replace(',', '.'));
                    population[i].setValueByName("expert_target_function_AVG", (AVG).ToString().Replace(',', '.'));
                    population[i].setValueByName("expert_target_function_StdDev", (StdDev).ToString().Replace(',', '.'));
                    // File.WriteAllText(population[i].getValueByName("json_file_path"), population[i].toJSON(0), System.Text.Encoding.Default);
                }


                // сортировка
                Hyperparameters temp;
                for (int i = 0; i < population_value - 1; i++)
                {
                    for (int j = i + 1; j < population_value; j++)
                    {
                        double i_value = Convert.ToDouble(population[i].getValueByName("expert_target_function").Replace('.', ','));
                        double j_value = Convert.ToDouble(population[j].getValueByName("expert_target_function").Replace('.', ','));
                        if (i_value < j_value || (double.IsNaN(i_value) && (!double.IsNaN(j_value))))
                        {
                            log(" [" + i.ToString() + "] <- [" + j.ToString() + "]: " + i_value + "<" + j_value, Color.Orchid);

                            string tempFolder = form1.pathPrefix + "Optimization\\" + name + "\\temp";
                            string path_to_i  = form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + i.ToString() + "]";
                            string path_to_j  = form1.pathPrefix + "Optimization\\" + name + "\\" + name + "[" + j.ToString() + "]";

                            temp          = Copy(population[i], path_to_i, tempFolder);
                            population[i] = Copy(population[j], path_to_j, path_to_i);
                            population[j] = Copy(temp, tempFolder, path_to_j);

                            population[i].setValueByName("report_path", form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[" + i.ToString() + "]");
                            population[j].setValueByName("report_path", form1.pathPrefix + "Optimization\\" + name + "\\" + expert.expertName + "[" + j.ToString() + "]");
                        }
                    }
                }
            }

            for (int i = 0; i < population_value; i++)
            {
                population[i].setValueByName("name", population[i].nodes[0].name() + "[" + i.ToString() + "]");
            }

            log("Обновление отображаемых параметров", Color.Lime);

            refreshEOTree();
            // A.draw(0, form1.picBox, form1, 15, 150);

            variableChangeMonitoring();

            variablesVisualizer.addPoint(Convert.ToDouble(population[0].getValueByName("expert_target_function").Replace('.', ',')), "best_individ");
            double averegeBest = 0;

            for (int i = 0; i < population_value; i++)
            {
                var t_f = Convert.ToDouble(population[i].getValueByName("expert_target_function").Replace('.', ','));

                if (i < population_value * elite_ratio)
                {
                    averegeBest += t_f;
                }

                variablesVisualizer.addPoint(t_f, " [" + population[i].getValueByName("code") + "]");
            }
            variablesVisualizer.addPoint((averegeBest / (population_value * elite_ratio)).ToString(), "averege best");

            variablesVisualizer.refresh();
        }