/// <summary> /// Pythonスクリプトの出力結果を読み込む /// </summary> /// <returns>string[]のリストで出力結果を返す</returns> public static List <string[]> GetResultList() { string result_filePath = MySettings.GetInstance().OutputResultCsvPath; var raw_result_list = new List <string[]>(); for (int count = 0; count <= 5; count++) { try { using (var sr = new StreamReader(result_filePath, System.Text.Encoding.GetEncoding("utf-8"))) { sr.ReadLine().Split(','); while (sr.EndOfStream == false) { raw_result_list.Add(sr.ReadLine().Split(',')); } Console.WriteLine("ok Success Get RawResultList"); return(raw_result_list); } } catch { System.Threading.Thread.Sleep(100); } } Console.WriteLine("no Failure Get RawResultList"); return(null); }
/// <summary> /// 過去のPythonスクリプトの出力結果を消去する /// </summary> /// <returns>消去に成功するとtrueを返す</returns> public static bool DeltaResults() { var result_path = MySettings.GetInstance().OutputResultCsvPath; var all_result_path = MySettings.GetInstance().OutputAllResultCsvPath; var flag_path = MySettings.GetInstance().OutputFlagPath; File.Delete(result_path); File.Delete(flag_path); System.Threading.Thread.Sleep(100); return(true); //string[] filePaths = Directory.GetFiles(result_path); // try { // foreach (string filePath in filePaths) { // File.SetAttributes(filePath, FileAttributes.Normal); // File.Delete(filePath); // } // Console.WriteLine("ok Delete Output Result"); // return true; // } catch (Exception) { // Console.WriteLine("no Failure Delete Output Result"); // return false; // } }
/// <summary> /// Pythonスクリプトを用いて複数対戦の評価をする /// </summary> /// <param name="path">今は指定の必要なし.将来的に動的に評価スクリプトを変化した場合に用いるとよい</param> /// <returns>評価が無事に終了するとtrue,失敗するとfalseを返す</returns> public static bool PythonPredictBranch(string path = "") { ErrorFlag = false; var work_path = MySettings.GetInstance().WorkingPath; if (path == "") { path = MySettings.GetInstance().PredictBranchScriptName; } if (StreamWriter.BaseStream.CanWrite) { StreamWriter.WriteLine(@"cd " + work_path); StreamWriter.WriteLine(@"python " + path + " ./data/" + MySettings.GetInstance().InputLearningCsvName); } bool exist_flag = false; while (!exist_flag) { exist_flag = File.Exists(MySettings.GetInstance().OutputFlagPath); System.Threading.Thread.Sleep(100); if (PythonProxy.ErrorFlag) { System.Threading.Thread.Sleep(100); return(false); } } return(true); }
/// <summary> /// Pythonの環境設定をする /// </summary> public static void StartUpPython() { Process = new System.Diagnostics.Process(); Process.StartInfo.UseShellExecute = false; Process.StartInfo.RedirectStandardInput = true; Process.StartInfo.RedirectStandardOutput = true; Process.StartInfo.RedirectStandardError = true; Process.OutputDataReceived += p_OutputDataReceived; Process.ErrorDataReceived += p_ErrorDataReceived; Process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec"); Process.StartInfo.CreateNoWindow = true; Process.Start(); Process.BeginOutputReadLine(); Process.BeginErrorReadLine(); StreamWriter = Process.StandardInput; if (StreamWriter.BaseStream.CanWrite) { StreamWriter.WriteLine(MySettings.GetInstance().AnacondaPath); StreamWriter.WriteLine(@"activate " + MySettings.GetInstance().AnacondaEnv); } }
static void Main(string[] args) { PythonProxy.StartUpPython(); //トーナメントに使うデータを読み込む var group = InOutput.ReadRaw(MySettings.GetInstance().InputLearningCsvPath).GroupBy(line => new { start = line.MyRouteList[0].RouteColumnDic["a0"], goal = line.MyRouteList[0].RouteColumnDic["g0"] }); var group_list = group.OrderByDescending(g => g.Count()).ToList(); var tournament_data = group.ToList().OrderByDescending(g => g.Count()).FirstOrDefault().ToList(); foreach (var group_data in group_list) { Console.WriteLine(group_data.Key); Console.WriteLine(group_data.Count()); new Rapid_Experiment().Execute(group_data.ToList()); } //new Simple_Experiment().Execute(); //new Rapid_Experiment().Execute(tournament_data); }
/// <summary> /// CargoAndRoute型のデータをcsvファイルに書き込む /// </summary> /// <param name="car_list">CargoAndRoute型のリスト</param> public static void WriteRaw(List <CargoAndRoutes> car_list) { var filepath = MySettings.GetInstance().OutputPredictCsvPath; List <string> column_list = new List <string>(); File.Delete(filepath); System.Threading.Thread.Sleep(100); column_list.Add("random"); column_list.Add("number"); column_list.Add("cor_number"); foreach (var cargo_column in car_list.First().MyCargo.CargoColumnDic.Keys) { column_list.Add(cargo_column); } foreach (var route in car_list.First().MyRouteList) { foreach (var route_column in route.RouteColumnDic.Keys) { column_list.Add(route_column); } } string sp = string.Empty; using (StreamWriter sw = new StreamWriter(filepath, false, Encoding.UTF8)) { foreach (var column in column_list) { sw.Write(sp + column); sp = ","; } sw.WriteLine(); foreach (var car in car_list) { var cargo_values = car.MyCargo.CargoColumnDic.Values; sp = string.Empty; for (int i = 0; i < 3; i++) { sw.Write(sp); sp = ","; } foreach (var cargo_value in cargo_values) { sw.Write(sp + cargo_value); sp = ","; } foreach (var route in car.MyRouteList) { foreach (var route_value in route.RouteColumnDic.Values) { sw.Write(sp + route_value); sp = ","; } } sw.WriteLine(); } } }
/// <summary> /// 学習データを貨物オブジェクト,経路オブジェクトに変換して登録する /// </summary> /// <param name="one_row"></param> /// <param name="columns"></param> public void Register(List <string> one_row, List <string> columns, int line_num) { var setting = MySettings.GetInstance(); this.ColumnID = int.Parse(one_row[1]); this.CorrectNumber = int.Parse(one_row[2]); List <string> cargo_str_list = new List <string>(); List <string> cargo_columns = new List <string>(); for (int i = setting.CargoColumDuration.Item1; i <= setting.CargoColumDuration.Item2; i++) { cargo_str_list.Add(one_row[i]); cargo_columns.Add(columns[i]); } this.MyCargo.Register(cargo_str_list, cargo_columns); List <string> route00_str_list = new List <string>(); List <string> route00_columns = new List <string>(); for (int i = setting.Route00_ColumDuration.Item1; i <= setting.Route00_ColumDuration.Item2; i++) { route00_str_list.Add(one_row[i]); route00_columns.Add(columns[i]); } var route00 = new Route(0, 0 + 4 * line_num); route00.Register(route00_str_list, route00_columns); this.MyRouteList.Add(route00); List <string> route01_str_list = new List <string>(); List <string> route01_columns = new List <string>(); for (int i = setting.Route01_ColumDuration.Item1; i <= setting.Route01_ColumDuration.Item2; i++) { route01_str_list.Add(one_row[i]); route01_columns.Add(columns[i]); } var route01 = new Route(1, 1 + 4 * line_num); route01.Register(route01_str_list, route01_columns); this.MyRouteList.Add(route01); List <string> route02_str_list = new List <string>(); List <string> route02_columns = new List <string>(); for (int i = setting.Route02_ColumDuration.Item1; i <= setting.Route02_ColumDuration.Item2; i++) { route02_str_list.Add(one_row[i]); route02_columns.Add(columns[i]); } var route02 = new Route(2, 2 + 4 * line_num); route02.Register(route02_str_list, route02_columns); this.MyRouteList.Add(route02); List <string> route03_str_list = new List <string>(); List <string> route03_columns = new List <string>(); for (int i = setting.Route03_ColumDuration.Item1; i <= setting.Route03_ColumDuration.Item2; i++) { route03_str_list.Add(one_row[i]); route03_columns.Add(columns[i]); } var route03 = new Route(3, 3 + 4 * line_num); route03.Register(route03_str_list, route03_columns); this.MyRouteList.Add(route03); }
public void Execute() { //トーナメントに使うデータを読み込む var tournament_data = InOutput.ReadRaw(MySettings.GetInstance().InputTournamentCsvPath); //貨物 var cargo = tournament_data[0].MyCargo; //トーナメント1試合目 //経路4つ var route10 = tournament_data[0].MyRouteList[0]; var route11 = tournament_data[0].MyRouteList[1]; var route12 = tournament_data[0].MyRouteList[2]; var route13 = tournament_data[0].MyRouteList[3]; //経路をまとめる var route_list_1 = new List <Route>(); route_list_1.Add(route10); route_list_1.Add(route11); route_list_1.Add(route12); route_list_1.Add(route13); //貨物と経路をまとめる var cargo_and_route_1 = new CargoAndRoutes(false); cargo_and_route_1.Register(cargo, route_list_1); //試合をする var win_route_1 = Predict.PredictBranchs(cargo_and_route_1); //トーナメント2試合目 //経路4つ var route20 = tournament_data[1].MyRouteList[0]; var route21 = tournament_data[1].MyRouteList[1]; var route22 = tournament_data[1].MyRouteList[2]; var route23 = tournament_data[1].MyRouteList[3]; //経路をまとめる var route_list_2 = new List <Route>(); route_list_2.Add(route20); route_list_2.Add(route21); route_list_2.Add(route22); route_list_2.Add(route23); //貨物と経路をまとめる var cargo_and_route_2 = new CargoAndRoutes(false); cargo_and_route_2.Register(cargo, route_list_2); //試合をする var win_route_2 = Predict.PredictBranchs(cargo_and_route_2); //トーナメント3試合目 //経路4つ var route30 = tournament_data[2].MyRouteList[0]; var route31 = tournament_data[2].MyRouteList[1]; var route32 = tournament_data[2].MyRouteList[2]; var route33 = tournament_data[2].MyRouteList[3]; //経路をまとめる var route_list_3 = new List <Route>(); route_list_3.Add(route30); route_list_3.Add(route31); route_list_3.Add(route32); route_list_3.Add(route33); //貨物と経路をまとめる var cargo_and_route_3 = new CargoAndRoutes(false); cargo_and_route_3.Register(cargo, route_list_3); //試合をする var win_route_3 = Predict.PredictBranchs(cargo_and_route_3); //トーナメント4試合目 //経路4つ var route40 = tournament_data[3].MyRouteList[0]; var route41 = tournament_data[3].MyRouteList[1]; var route42 = tournament_data[3].MyRouteList[2]; var route43 = tournament_data[3].MyRouteList[3]; //経路をまとめる var route_list_4 = new List <Route>(); route_list_4.Add(route40); route_list_4.Add(route41); route_list_4.Add(route42); route_list_4.Add(route43); //貨物と経路をまとめる var cargo_and_route_4 = new CargoAndRoutes(false); cargo_and_route_4.Register(cargo, route_list_4); //試合をする var win_route_4 = Predict.PredictBranchs(cargo_and_route_4); //トーナメント最終試合 //4試合の勝利経路をまとめる List <Route> win_route_list = new List <Route>(); win_route_list.AddRange(win_route_1); win_route_list.AddRange(win_route_2); win_route_list.AddRange(win_route_3); win_route_list.AddRange(win_route_4); //貨物と上の勝利経路4つをまとめる CargoAndRoutes car = new CargoAndRoutes(false); car.Register(cargo, win_route_list); //試合をする var final_win_route = Predict.PredictBranchs(car); Console.WriteLine($"最終勝利ルートは {final_win_route[0].PublicRouteID} です"); Console.ReadLine(); }