static void Main(string[] args) { String mainLogName = string.Format("{0:yyyy-MM-dd_hh-mm-ss}log.txt", DateTime.Now); StreamWriter mainLogStream = new StreamWriter(mainLogName); IList <Gen> _generations = LoadGens(); if (_generations == null) { _generations = new List <Gen> { new Gen() }; } int generationNumber = 0; while (true) { generationNumber++; mainLogStream.WriteLine(String.Format("Поколение №{0}", generationNumber)); Console.WriteLine("Тестирование поколения №{0}", generationNumber); foreach (Gen gen in _generations) { gen.Tag = string.Format("{0:yyyy-MM-dd_hh-mm-ss}", DateTime.Now); String evoName = String.Format(evoTemplate, gen.Tag); String logName = String.Format(logTemplate, gen.Tag); String evoPath = Path.Combine(path, evoName); String logPath = Path.Combine(path, logName); String json = gen.ToJSON(); using (StreamWriter writer = new StreamWriter(evoPath)) { writer.Write(json); } IList <HangleProcess> _runnedProcesses = new List <HangleProcess>(); List <HangleProcess> _completedProcesses = new List <HangleProcess>(); int prevCompletedProcessesCount = -1; while (_completedProcesses.Count < HangleRounds) { IList <HangleProcess> _thisTurnCompletedProcesses = new List <HangleProcess>(); foreach (HangleProcess hProcess in _runnedProcesses) { if (hProcess.IsCompleted) { _thisTurnCompletedProcesses.Add(hProcess); } } foreach (HangleProcess hProcess in _thisTurnCompletedProcesses) { _runnedProcesses.Remove(hProcess); if (hProcess.Result != null)// && (hProcess.Result.oppScore > 3 || (hProcess.Result.myScore == 0 && hProcess.Result.oppScore == 0))) { _completedProcesses.Add(hProcess); } } while (_runnedProcesses.Count < MaxHangleThreads && _runnedProcesses.Count + _completedProcesses.Count < HangleRounds) { _runnedProcesses.Add(new HangleProcess(gen)); } if (prevCompletedProcessesCount != _completedProcesses.Count) { Console.Write("\r{0}/{1}", _completedProcesses.Count, HangleRounds); prevCompletedProcessesCount = _completedProcesses.Count; } Thread.Sleep(10); } Console.WriteLine(); int summ = 0; int summStable = 0; int summOpp = 0; int summStableOpp = 0; _completedProcesses.Sort(); for (int i = 0; i < _completedProcesses.Count; i++) { ScoreData score = _completedProcesses[i].Result; summ += score.myScore; summOpp += score.oppScore; if (i >= CutSideSize && i < _completedProcesses.Count - CutSideSize) { summStable += score.myScore; summStableOpp += score.oppScore; } } gen.Score.myScore = summStable; gen.Score.oppScore = summStableOpp; Console.WriteLine("Score: {0}/{1} : {2}/{3} {4}", summ, summOpp, summStable, summStableOpp, gen.Score.CompareScore); } IList <Gen> newGenerations = new List <Gen>(); _generations = _generations.OrderByDescending(x => x.Score).ToList(); for (int i = 0; i < _generations.Count; i++) { Gen entity = _generations[i]; mainLogStream.WriteLine(String.Format("{0}/{1} ({2}) очков. Тэг: {3} Данные: {4}", entity.Score.myScore, entity.Score.oppScore, entity.Score.CompareScore, entity.Tag, entity.ToJSON())); if (i == 2 && _generations.Count > 3) { mainLogStream.WriteLine("---Неудачники---"); } } mainLogStream.Flush(); while (_generations.Count > 3) { _generations.RemoveAt(_generations.Count - 1); } //Сохранение генов-победителей в файл SaveGens(_generations); foreach (Gen entity in _generations) { newGenerations.Add(entity); for (int i = 0; i < 5; i++) { newGenerations.Add(entity.Mutate()); } } _generations = newGenerations; } //Console.ReadLine(); /* * String runCmd = String.Format(formatString); * Process p =new Process(); * p.StartInfo.UseShellExecute = false; * p.StartInfo.RedirectStandardOutput = true; * p.StartInfo.FileName = path; * p.Start(); * * String output = p.StandardOutput.ReadToEnd(); * p.WaitForExit(); */ }
private void Process() { String evoName = String.Format(Program.evoTemplate, _gen.Tag); String logName = String.Format(Program.logTemplate, _gen.Tag + "_" + _id); String runCmd = String.Format(Program.runTemplate, Program.path, evoName, logName); String evoPath = Path.Combine(Program.path, evoName); String logPath = Path.Combine(Program.path, logName); Process p = new Process(); p.StartInfo.FileName = "CMD.exe"; p.StartInfo.Arguments = runCmd; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start(); //p.WaitForExit(); while (!p.HasExited && _waitIterations++ < MAX_ITERATIONS) { Thread.Sleep(40); } if (_waitIterations >= MAX_ITERATIONS) { _isCompleted = true; return; } String log = ""; if (!File.Exists(logPath)) { _isCompleted = true; return; } using (StreamReader reader = new StreamReader(logPath)) { log = reader.ReadToEnd(); } try { if (log.IndexOf("abort") > -1 || log.Length < 100 || log.IndexOf("valuation") == -1) { if (File.Exists(logPath)) { File.Delete(logPath); } _isCompleted = true; return; } int scriptFirstAppear = log.IndexOf("script"); int remoteFirstAppear = log.IndexOf("remote"); int ourIndex = scriptFirstAppear < remoteFirstAppear ? 0 : 1; int score2Index = log.LastIndexOf("score"); int score1Index = log.LastIndexOf("score", score2Index - 1); int score1EndIndex = 0; int score2EndIndex = 0; score1Index = log.IndexOf("\n", score1Index + 1); score1Index = log.IndexOf("\n", score1Index + 1); score1EndIndex = log.IndexOf("\n", score1Index + 1); score2Index = log.IndexOf("\n", score2Index + 1); score2Index = log.IndexOf("\n", score2Index + 1); score2EndIndex = log.IndexOf("\n", score2Index + 1); String score1String = log.Substring(score1Index, score1EndIndex - score1Index); String score2String = log.Substring(score2Index, score2EndIndex - score2Index); int score1 = Int32.Parse(score1String); int score2 = Int32.Parse(score2String);// Int32.Parse(log.Substring(startIndex, endIndex - startIndex)); int ourScore = ourIndex == 0 ? score1 : score2; int oppScore = ourIndex == 0 ? score2 : score1; ScoreData score = new ScoreData { myScore = ourScore, oppScore = oppScore }; _result = score; if (File.Exists(logPath)) { File.Delete(logPath); } } catch (Exception exp) { Console.WriteLine(exp.Message); } _isCompleted = true; }