Example #1
0
        private BulkSolverStats InitializeStats()
        {
            BulkSolverStats stats = new BulkSolverStats();

            stats.RepositoryVersionHash = ShellExec("git describe --always --dirty");
            return(stats);
        }
Example #2
0
 private void AddSolutionToStats(BulkSolverStats stats, ISolver solver)
 {
     if (stats == null)
     {
         return;
     }
     lock (stats)
     {
         stats.TotalTime                   += solver.Solution.TimeElapsed;
         stats.NumberOfExamples            += 1;
         stats.NumberOfAcceptableSolutions += solver.Solution.CompletionScore >= 1 ? 1 : 0;
         stats.TasksStats.AddTasksStats(solver.Solution.TotalStats);
     }
 }
Example #3
0
        private void SerializeStats(BulkSolverStats stats, string statsPath)
        {
            FileInfo file = new FileInfo(statsPath);

            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }
            using (var writer = new StreamWriter(statsPath))
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.None,
                    Formatting       = Formatting.Indented,
                    TypeNameHandling = TypeNameHandling.Objects,
                };
                JsonSerializer ser = JsonSerializer.Create(settings);
                ser.Serialize(writer, stats);
                writer.Flush();
                writer.Close();
            }
        }
Example #4
0
        public void SolveEverything(Func <ISolver> solverMaker)
        {
            _stats            = InitializeStats();
            _categorizedStats = new Dictionary <string, BulkSolverStats>();
            var solveTasks = GenerateAllTasks(solverMaker);

            solveTasks.Shuffle(Random);
            if (ParallelExecution)
            {
                Parallel.ForEach(
                    solveTasks,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = MaxThreads,
                },
                    solveTask => solveTask());
            }
            else
            {
                foreach (var task in solveTasks)
                {
                    task();
                }
            }

            ISolver solver = solverMaker();

            _stats.Solver = solver;
            string statsPath = Path.Combine(SolutionsDirectory, solver.Description, "TotalStats.json");

            SerializeStats(_stats, statsPath);
            foreach (var categoryStats in _categorizedStats)
            {
                statsPath = Path.Combine(SolutionsDirectory, solver.Description, "CategorizedStats", $"{categoryStats.Key}.json");
                SerializeStats(categoryStats.Value, statsPath);
            }
        }
Example #5
0
 private Action GenerateSolveTask(string pathIn, string pathOut, Func <ISolver> solverMaker, int seed)
 {
     return(() =>
     {
         try
         {
             string category = TotalStatsCategories.FirstOrDefault(c => pathOut.Contains(c));
             BulkSolverStats categoryStats = null;
             if (!string.IsNullOrEmpty(category))
             {
                 lock (_categorizedStats)
                 {
                     if (!_categorizedStats.TryGetValue(category, out categoryStats))
                     {
                         categoryStats = InitializeStats();
                         _categorizedStats[category] = categoryStats;
                     }
                 }
             }
             var reader = new InstanceJsonSerializer
             {
                 Path = pathIn,
             };
             Instance instance = reader.DeserializeInstance();
             var solver = solverMaker();
             solver.Seed = seed;
             solver.Instance = instance;
             if (StartingSolutionsDirectory != null)
             {
                 if (solver is InstanceSolvers.Solvers.Evolutionary evo)
                 {
                     evo.Population = LoadStartingSolutions(SolutionsDirectory, pathIn, instance, evo.PopulationCount, solver);
                     evo.PopulationCount = evo.Population.Count;
                 }
                 else
                 {
                     solver.Solution = LoadStartingSolutions(SolutionsDirectory, pathIn, instance, 1, solver)[0];
                 }
             }
             IReporter reporter = new NullReporter();
             if (ReportProgrssToFile)
             {
                 reporter = new ScoreReporter();
             }
             reporter.Start();
             solver.Reporter = reporter;
             solver.Solve();
             InstanceJsonSerializer serializer = new InstanceJsonSerializer()
             {
                 Path = pathOut,
             };
             serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
             reporter.Save(Path.Combine(new FileInfo(pathOut).Directory.FullName, $"{Path.GetFileNameWithoutExtension(new FileInfo(pathOut).Name)}Report.csv"));
             Console.WriteLine($"Solution {pathOut} was generated, completion {solver.Solution.CompletionScore}, loss {solver.Solution.WeightedLoss}, time {solver.Solution.TimeElapsed.ToString(@"hh\:mm\:ss")}.");
             AddSolutionToStats(_stats, solver);
             AddSolutionToStats(categoryStats, solver);
         }
         catch (FileLoadException e)
         {
             Console.WriteLine(e.Message);
             Console.WriteLine(e.StackTrace);
         }
         //GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
         GC.Collect();
         GC.WaitForPendingFinalizers();
         GC.Collect();
         GC.WaitForPendingFinalizers();
     });
 }