コード例 #1
0
        public void Optimize()
        {
            var config = JsonConvert.DeserializeObject <DynastyConfiguration>(_file.File.ReadAllText("dynasty.json"));

            OptimizerConfiguration current = null;
            var settings = new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            for (var i = config.StartDate; i <= config.EndDate; i = i.AddDays(config.DurationDays).AddHours(config.DurationHours))
            {
                if (current == null)
                {
                    current = JsonConvert.DeserializeObject <OptimizerConfiguration>(_file.File.ReadAllText(_configFilename));
                }

                current.StartDate = i;
                current.EndDate   = i.AddDays(config.DurationDays).AddHours(config.DurationHours);

                string json = JsonConvert.SerializeObject(current, settings);

                _file.File.WriteAllText(_configFilename, json);

                var info = new ProcessStartInfo("Optimization.exe", _configFilename)
                {
                    RedirectStandardOutput = true,
                    UseShellExecute        = false
                };

                _process.Start(info);
                string output;
                FixedSizeQueue <string> queue = new FixedSizeQueue <string>(2);
                while ((output = _process.ReadLine()) != null)
                {
                    queue.Enqueue(output);
                    //Console.WriteLine(output);

                    if (queue.First() == GeneManager.Termination)
                    {
                        _logWrapper.Info($"For period: {current.StartDate} {current.EndDate}");
                        _logWrapper.Info(queue.Dequeue());
                        _logWrapper.Info(queue.Dequeue());
                        string optimal = queue.Dequeue();
                        _logWrapper.Info(optimal);

                        if (config.WalkForward)
                        {
                            var split = optimal.Split(',');

                            for (int ii = 0; ii < split.Length; ii++)
                            {
                                string[] pair = split[ii].Split(':');
                                var      gene = current.Genes.SingleOrDefault(g => g.Key == pair[0].Trim());

                                decimal parsedDecimal;
                                int     parsedInt;
                                if (int.TryParse(pair[1].Trim(), out parsedInt))
                                {
                                    gene.ActualInt = parsedInt;
                                }
                                else if (decimal.TryParse(pair[1].Trim(), out parsedDecimal))
                                {
                                    gene.ActualDecimal = parsedDecimal;
                                }
                                else
                                {
                                    throw new Exception($"Unable to parse optimal gene from range {current.StartDate} {current.EndDate}");
                                }
                            }
                        }
                        _process.Kill();
                    }
                }
            }
        }