public void run() { for (int it = 0; it < 20; it++) { currentHypercube = new List <TargetFunctionPoint>(); iteration(); // поиск максимума TargetFunctionPoint maxQ = new TargetFunctionPoint(null, 0); for (int i = 0; i < currentHypercube.Count; i++) { variablesVisualizer.addPoint(currentHypercube[i].Q, " [" + i.ToString() + "]"); if (maxQ.Q < currentHypercube[i].Q) { maxQ = currentHypercube[i]; } } variablesVisualizer.addPoint(maxQ.Q, "max Q"); form1.log(getReportLine(maxQ), Color.Magenta); bool isOptimumInCenter = true; for (int i = 0; i < P.nodes.Count; i++) { if (P.nodes[i].getAttributeValue("variable") == "numerical") { string maxQVariableValue = maxQ.h.nodes[i].getValue(); string variableCenter = getVariableCenter(P.nodes[i]); if (maxQVariableValue != variableCenter) { isOptimumInCenter = false; } } } if (isOptimumInCenter) { //если максимум в центре - сжатие form1.log("Сжатие", Color.Cyan); variablesVisualizer.markLast("Сжатие", "max Q"); for (int i = 0; i < P.nodes.Count; i++) { if (P.nodes[i].getAttributeValue("variable") == "numerical") { string newMin = ""; string newMax = ""; if (P.nodes[i].getAttributeValue("max").Contains('.') || P.nodes[i].getAttributeValue("max").Contains(',')) { double min_double = double.Parse(P.nodes[i].getAttributeValue("min").Replace('.', ',')); double max_double = double.Parse(P.nodes[i].getAttributeValue("max").Replace('.', ',')); // сужение интервала if (min_double + ((max_double - min_double) / 4) > 0) { newMin = (min_double + ((max_double - min_double) / 4)).ToString().Replace(',', '.'); } else { newMin = min_double.ToString().Replace(',', '.'); } newMax = (max_double - ((max_double - min_double) / 4)).ToString().Replace(',', '.'); } else { int min_int = int.Parse(P.nodes[i].getAttributeValue("min")); int max_int = int.Parse(P.nodes[i].getAttributeValue("max")); // сужение интервала if (min_int + ((max_int - min_int) / 4) >= 2) { newMin = (min_int + ((max_int - min_int) / 4)).ToString(); } else { newMin = min_int.ToString(); } newMax = (max_int - ((max_int - min_int) / 4)).ToString(); } P.nodes[i].setAttribute("min", newMin); P.nodes[i].setAttribute("max", newMax); } } } else { //иначе - перемещение центра в точку максимума form1.log("Перемещение из " + getReportLine(lastP) + " в " + getReportLine(maxQ), Color.Cyan); variablesVisualizer.markLast("Перемещение", "max Q"); for (int i = 0; i < P.nodes.Count; i++) { if (P.nodes[i].getAttributeValue("variable") == "numerical") { string newMin = ""; string newMax = ""; if (P.nodes[i].getAttributeValue("max").Contains('.') || P.nodes[i].getAttributeValue("max").Contains(',')) { double min_double = double.Parse(P.nodes[i].getAttributeValue("min").Replace('.', ',')); double max_double = double.Parse(P.nodes[i].getAttributeValue("max").Replace('.', ',')); double value = double.Parse(maxQ.h.nodes[i].getValue().Replace('.', ',')); // перемещение центра if ((value - ((max_double - min_double) / 2)) > 0) { newMin = (value - ((max_double - min_double) / 2)).ToString().Replace(',', '.'); } else { newMin = min_double.ToString().Replace(',', '.'); } newMax = (value + ((max_double - min_double) / 2)).ToString().Replace(',', '.'); } else { int min_int = int.Parse(P.nodes[i].getAttributeValue("min")); int max_int = int.Parse(P.nodes[i].getAttributeValue("max")); int value = int.Parse(maxQ.h.nodes[i].getValue()); // перемещение центра if (value - ((max_int - min_int) / 2) >= 2) { newMin = (value - ((max_int - min_int) / 2)).ToString(); } else { newMin = min_int.ToString(); } newMax = (value + ((max_int - min_int) / 2)).ToString(); } P.nodes[i].setAttribute("min", newMin); P.nodes[i].setAttribute("max", newMax); } } } lastP = maxQ; variablesVisualizer.refresh(); } }
public void TEST() { mainThread = System.Threading.Thread.CurrentThread; double balance_BTC = 0; double balance_USD = 100; double lot = 0.001; Hyperparameters order_book; var api = new ExmoApi("k", "s"); string responce = ""; string s = ""; vis.addParameter("BTC_USD", Color.White, 1000); vis.parameters[0].showLastNValues = true; vis.enableGrid = true; vis.parameters[0].window = 360; //наименьшая цена, по которой можно покупать vis.parameters[0].functions.Add(new Function("bid_top", Color.Blue)); //наивысшая цена, по которой можно продать vis.parameters[0].functions.Add(new Function("ask_top", Color.Red)); expert = new Expert("Expert_Flex", this); algorithm = new BidAsk(this, "BidAsk"); algorithm.Open(@"E:\Anton\Desktop\MAIN\Экспертная система\Экспертная система\Алгоритмы прогнозирования\BidAsk\h.json"); expert.AddAlgorithm(algorithm); int window = int.Parse(algorithm.h.getValueByName("window_size")); int step = 60000; string[] input; double[] predictions = null; List <string> rawInput = new List <string>(); // List<string> spreads = new List<string>(); rawInput.Add("<DATE_TIME>;<bid_top>;<bid_quantity>;<bid_amount>;<ask_top>;<ask_quantity>;<ask_amount>"); //ЗАПУСК СКРИПТОВ ПОТОЧНОГО ПРОГНОЗИРОВНИЯ Task[] RunTasks = new Task[expert.algorithms.Count]; foreach (Algorithm algorithm in expert.algorithms) { RunTasks[expert.algorithms.IndexOf(algorithm)] = Task.Run(() => algorithm.runGetPredictionScript()); } //ОЖИДАНИЕ ЗАВЕРШЕНИЯ ЗАПУСКА foreach (var task in RunTasks) { task.Wait(); } List <double> positions = new List <double>(); while (true) { vis.parameters[0].window = 20; try { responce = api.ApiQuery("order_book", new Dictionary <string, string> { { "pair", "BTC_USD" } }); if (responce.Length > 400) { responce = responce.Substring(0, 400); } if (!responce.Contains("maintenance")) { order_book = new Hyperparameters(responce, this); //<bid_top>;<bid_quantity>;<bid_amount>;<ask_top>;<ask_quantity>;<ask_amount> s = DateTime.Now.ToString() + ';' + order_book.nodes[0].getAttributeValue("bid_top") + ';' + order_book.nodes[0].getAttributeValue("bid_quantity") + ';' + order_book.nodes[0].getAttributeValue("bid_amount") + ';' + order_book.nodes[0].getAttributeValue("ask_top") + ';' + order_book.nodes[0].getAttributeValue("ask_quantity") + ';' + order_book.nodes[0].getAttributeValue("ask_amount"); s = s.Replace('.', ','); double bid_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_top").Replace('.', ',')); double ask_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_top").Replace('.', ',')); /* * double bid_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_quantity").Replace('.', ',')); * double ask_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_quantity").Replace('.', ',')); * * double bid_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_amount").Replace('.', ',')); * double ask_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_amount").Replace('.', ','));*/ vis.addPoint(bid_top, "bid_top"); vis.addPoint(ask_top, "ask_top"); vis.refresh(); // log(s); //запись входных данных File.AppendAllText("BTC_USD_exmo.txt", s); rawInput.Add(s); // spreads.Add(String.Format("{0:0.#####}", bid_top - ask_top)); showInpOutp.fillTextBox1(rawInput); //+1 для заголовка, +1 для нормализации i/(i-1) if (rawInput.Count < window + 2) { File.AppendAllText("BTC_USD_exmo.txt", "\n"); } else { input = expert.prepareDataset(rawInput.ToArray(), "<DATE_TIME>", true); //шапка должна оставаться на месте, поэтому 1, а не 0 rawInput.RemoveAt(1); showInpOutp.fillTextBox2(input); if (input.Length == window + 1) { predictions = expert.getCommitteePrediction(input); } else { log("input.Length =/= window+1; input.Length = " + input.Length.ToString()); } string action = expert.getDecision(predictions); if (action == "error") { log("action = error", Color.Red); } if (action == "buy") { if (balance_USD > ask_top * lot) { balance_BTC = balance_BTC + lot; balance_USD = balance_USD - (ask_top * lot); positions.Add(ask_top); log("BUY", Color.Blue); log(ask_top.ToString()); log(" USD:" + balance_USD.ToString()); log(" BTC:" + balance_BTC.ToString()); vis.parameters[0].functions[2].points[vis.parameters[0].functions[1].points.Count - 1].mark = "BUY‾‾‾‾‾‾‾‾"; } else { // log("Баланс USD: " + balance_USD.ToString() + " Невозможно купить " + (bid_top * lot).ToString() + " BTC"); action += " (fail)"; } } for (int i = 0; i < positions.Count; i++) { if (positions[i] < bid_top) { action = "sell"; positions.RemoveAt(i); break; } } if (action == "sell") { if (bid_top != 0) { if (balance_BTC >= lot) { balance_BTC = balance_BTC - lot; balance_USD = balance_USD + (bid_top * lot); log("SELL", Color.Red); log(bid_top.ToString()); log(" USD:" + balance_USD.ToString()); log(" BTC:" + balance_BTC.ToString()); vis.parameters[0].functions[1].points[vis.parameters[0].functions[1].points.Count - 1].mark = "SELL‾‾‾‾‾‾‾‾"; } else { // log("Баланс BTC: " + balance_BTC.ToString() + " Невозможно продать " + (lot).ToString() + " BTC"); action += " (fail)"; } } else { log("bid_top почему-то равно нулю!", Color.Red); action += " (fail)"; } } log(DateTime.Now.ToString() + " " + action); //запись состояния балансов и последнего действия File.AppendAllText("BTC_USD_exmo.txt", balance_BTC.ToString() + ';' + balance_USD.ToString() + ';' + action + "\n"); } } else { log("Технические работы", Color.Red); } } catch (Exception e) { log(e.Message, Color.Red); // log(result); } Thread.Sleep(step); } }
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(); }