<commit_before>﻿using GeneticSharp.Domain.Fitnesses;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QuantConnect.Util;
using System;
using System.IO;
using System.Reflection;

namespace Optimization
{

    public class Program
    {

        #region Declarations
        static StreamWriter _writer;
        static OptimizerConfiguration _config;
        static object _writerLock;
        #endregion

        public static void Main(string[] args)
        {
            _config = LoadConfig();
            string path = _config.ConfigPath;
            System.IO.File.Copy(path, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"), true);

            path = _config.AlgorithmLocation;
            if (!string.IsNullOrEmpty(path))
            {
                System.IO.File.Copy(path, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(path)), true);
                string pdb = path.Replace(Path.GetExtension(path), ".pdb");
                if (File.Exists(pdb))
                {
                    System.IO.File.Copy(pdb, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(pdb)), true);
                }
            }

            _writerLock = new object();

            using (_writer = System.IO.File.AppendText("optimizer.txt"))
            {
                AppDomainManager.Initialize(_config);

                OptimizerFitness instance = (OptimizerFitness)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName, false, BindingFlags.Default, null,
                    new[] { _config }, null, null);

                var manager = new GeneticManager(_config, instance, new LogManager());
                manager.Start();
            }

            Console.ReadKey();
        }

        public static void Output(string line, params object[] format)
        {
            Output(string.Format(line, format));
        }

        public static void Output(string line)
        {
            lock (_writerLock)
            {
                _writer.Write(DateTime.Now.ToString("u"));
                _writer.Write(" ");
                _writer.Write(line);
                _writer.Write(_writer.NewLine);
            }
            _writer.Flush();
            Console.WriteLine(line);
        }

        private static OptimizerConfiguration LoadConfig()
        {
            using (StreamReader file = File.OpenText("optimization.json"))
            {
                var document = (JObject)JsonConvert.DeserializeObject(file.ReadToEnd());
                return JsonConvert.DeserializeObject<OptimizerConfiguration>(document["optimizer"].ToString());
            }
        }

    }
}<commit_msg>Change to accept config path on command line<commit_after>﻿using GeneticSharp.Domain.Fitnesses;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QuantConnect.Util;
using System;
using System.IO;
using System.Reflection;

namespace Optimization
{

    public class Program
    {

        #region Declarations
        static StreamWriter _writer;
        static OptimizerConfiguration _config;
        static object _writerLock;
        #endregion

        public static void Main(string[] args)
        {
            _config = LoadConfig(args);
            string path = _config.ConfigPath;
            System.IO.File.Copy(path, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"), true);

            path = _config.AlgorithmLocation;
            if (!string.IsNullOrEmpty(path))
            {
                System.IO.File.Copy(path, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(path)), true);
                string pdb = path.Replace(Path.GetExtension(path), ".pdb");
                if (File.Exists(pdb))
                {
                    System.IO.File.Copy(pdb, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(pdb)), true);
                }
            }

            _writerLock = new object();

            using (_writer = System.IO.File.AppendText("optimizer.txt"))
            {
                AppDomainManager.Initialize(_config);

                OptimizerFitness instance = (OptimizerFitness)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName, false, BindingFlags.Default, null,
                    new[] { _config }, null, null);

                var manager = new GeneticManager(_config, instance, new LogManager());
                manager.Start();
            }

            Console.ReadKey();
        }

        public static void Output(string line, params object[] format)
        {
            Output(string.Format(line, format));
        }

        public static void Output(string line)
        {
            lock (_writerLock)
            {
                _writer.Write(DateTime.Now.ToString("u"));
                _writer.Write(" ");
                _writer.Write(line);
                _writer.Write(_writer.NewLine);
            }
            _writer.Flush();
            Console.WriteLine(line);
        }

        private static OptimizerConfiguration LoadConfig(string[] args)
        {
            string path = "optimization.json";
            if (args != null && args.Length > 0 && !string.IsNullOrEmpty(args[0]))
            {
                path = args[0];
            }

            using (StreamReader file = File.OpenText(path))
            {
                var document = (JObject)JsonConvert.DeserializeObject(file.ReadToEnd());
                return JsonConvert.DeserializeObject<OptimizerConfiguration>(document["optimizer"].ToString());
            }
        }

    }
}
{'fraction_non_alphanumeric': 0.10255500354861603, 'fraction_numerical': 0.0005322924059616749, 'mean_word_length': 1.9192128430864837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5180630', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1392, 'n_words': 269}