Exemple #1
0
        public void SnapshotRestoredSolutionIdenticalToOriginal()
        {
            var file         = Properties.Resources.week_DS_D_DH_inst;
            var deserializer = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance instance = deserializer.DeserializeInstance();

            deserializer.Reader = new StreamReader(new MemoryStream(Properties.Resources.week_DS_D_DH_sol), Encoding.UTF8);
            Solution solution = deserializer.DeserializeSolution(instance);

            solution.GradingFunction = new Scorer();
            solution.GradingFunction.AssesSolution(solution);
            string path1 = @"results\hourSolScored.json";
            string path2 = @"results\hourSolScored2.json";
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = path1,
            };

            serializer.SerializeSolution(solution, SolutionSerializationMode.DebugFull);
            Solution solution2 = solution.TakeSnapshot();

            solution2.RestoreStructures();
            solution2.GradingFunction.AssesSolution(solution2);
            serializer.Path = path2;
            serializer.SerializeSolution(solution2, SolutionSerializationMode.DebugFull);
            Assert.AreEqual(GetFileHash(path1), GetFileHash(path2));
        }
Exemple #2
0
        public void GreedyFastHeuristicSolveDay3ChannelInstance()
        {
            var file   = Properties.Resources.day_DS_D_DH_inst;
            var reader = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance            instance = reader.DeserializeInstance();
            GreedyFastHeuristic solver   = new GreedyFastHeuristic()
            {
                Instance         = instance,
                Seed             = 10,
                ScoringFunction  = new Scorer(),
                MaxOverfillUnits = 1,
            };

            solver.Solve();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\day_DS_D_DH_sol_greedyfastheur.json"
            };

            serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
            var taskStats = solver.Solution.AdOrdersScores;

            solver.ScoringFunction.AssesSolution(solver.Solution);
            Assert.IsTrue(taskStats.Values.Sum(d => d.SelfIncompatibilityConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.SelfSpacingConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.OwnerConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.BreakTypeConflictsProportion) == 0);
        }
Exemple #3
0
        public void LocalRandomSolveHour3ChannelInstance()
        {
            var file   = Properties.Resources.hour_DS_D_DH_inst;
            var reader = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance         instance     = reader.DeserializeInstance();
            RandomFastSolver randomSolver = new RandomFastSolver()
            {
                Instance = instance,
                Seed     = 10,
            };
            LocalSearch solver = new LocalSearch()
            {
                Instance            = instance,
                Solution            = randomSolver.Solution,
                PropagateRandomSeed = true,
                Seed              = 10,
                ScoringFunction   = new Scorer(),
                StopWhenCompleted = true,
                TimeLimit         = new TimeSpan(0, 0, 60),
            };

            solver.InitialSolvers.Add(randomSolver);
            solver.Solve();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\hour_DS_D_DH_sol_localrandom.json"
            };

            serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
        }
        public void GenerateSolution(SolutionSerializationMode serializationMode = SolutionSerializationMode.Bare)
        {
            if (DataSource == null)
            {
                DataSource = new StreamReader(SourcePath);
            }
            InstanceLoader.Reader = DataSource;
            Instance instance = InstanceLoader.LoadInstanceFile();

            InstanceConverter.Instance = instance;
            InstanceConverter.ConvertToProblem();
            Solution solution = new Solution()
            {
                Instance = instance,
            };

            solution.GenerateSolutionFromRealData();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer();

            if (OutputFilename == null)
            {
                OutputFilename = Path.GetFileNameWithoutExtension(SourcePath) + ".json";
            }
            serializer.Path = OutputFilename;
            FileInfo file = new FileInfo(OutputFilename);

            file.Directory.Create();
            serializer.SerializeSolution(solution, serializationMode);
        }
        private void GradeSolution(string instancePath, string solutionPath, string pathOut)
        {
            var deserializer = new InstanceJsonSerializer
            {
                Path = instancePath,
            };
            Instance instance = deserializer.DeserializeInstance();

            deserializer.Path   = solutionPath;
            deserializer.Reader = null;
            Solution solution = deserializer.DeserializeSolution(instance);

            solution.GradingFunction = new Scorer();
            solution.GradingFunction.AssesSolution(solution);
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = pathOut,
            };

            serializer.SerializeSolution(solution, SolutionSerializationMode.DebugTaskData);
        }
Exemple #6
0
        public void RandomFastSolverSolveWeek3ChannelInstance()
        {
            var file   = Properties.Resources.week_DS_D_DH_inst;
            var reader = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance         instance = reader.DeserializeInstance();
            RandomFastSolver solver   = new RandomFastSolver()
            {
                Instance        = instance,
                Seed            = 10,
                ScoringFunction = new Scorer(),
            };

            solver.Solve();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\week_DS_D_DH_sol_randomfast.json"
            };

            serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
        }
Exemple #7
0
        public void GreedyHeuristicSolveDay3ChannelInstance()
        {
            var file   = Properties.Resources.day_DS_D_DH_inst;
            var reader = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance instance = reader.DeserializeInstance();
            ImprovingInsertsHeuristic solver = new ImprovingInsertsHeuristic()
            {
                Instance        = instance,
                Seed            = 10,
                ScoringFunction = new Scorer(),
            };

            solver.Solve();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\day_DS_D_DH_sol_greedyheur.json"
            };

            serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
        }
Exemple #8
0
        public void StartsHeuristicSolveDay3ChannelInstance()
        {
            var file   = Properties.Resources.day_DS_D_DH_inst;
            var reader = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance            instance       = reader.DeserializeInstance();
            GreedyFastHeuristic fastRandomHeur = new GreedyFastHeuristic()
            {
                Seed             = 10,
                MaxOverfillUnits = 1,
            };
            BeginingsHeuristic solver = new BeginingsHeuristic()
            {
                Seed                   = 10,
                ScoringFunction        = new Scorer(),
                Instance               = instance,
                PropagateRandomSeed    = true,
                MaxBreakExtensionUnits = 30,
            };

            solver.InitialSolvers.Add(fastRandomHeur);
            solver.Solve();
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\day_DS_D_DH_sol_startsheur.json"
            };

            serializer.SerializeSolution(solver.Solution, SolutionSerializationMode.DebugTaskData);
            var taskStats = solver.Solution.AdOrdersScores;

            Assert.IsTrue(taskStats.Values.Sum(d => d.SelfIncompatibilityConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.SelfSpacingConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.OwnerConflictsProportion) == 0);
            Assert.IsTrue(taskStats.Values.Sum(d => d.BreakTypeConflictsProportion) == 0);
        }
Exemple #9
0
        public void GradeWeek3ChannelSolutionFromSavedFiles()
        {
            var file         = Properties.Resources.week_DS_D_DH_inst;
            var deserializer = new InstanceJsonSerializer
            {
                Reader = new StreamReader(new MemoryStream(file), Encoding.UTF8)
            };
            Instance instance = deserializer.DeserializeInstance();

            deserializer.Reader = new StreamReader(new MemoryStream(Properties.Resources.week_DS_D_DH_sol), Encoding.UTF8);
            Solution solution = deserializer.DeserializeSolution(instance);

            solution.GradingFunction = new Scorer();
            solution.GradingFunction.AssesSolution(solution);
            InstanceJsonSerializer serializer = new InstanceJsonSerializer()
            {
                Path = @"results\week_DS_D_DH_sol_scored.json"
            };

            serializer.SerializeSolution(solution, SolutionSerializationMode.DebugTaskData);

            Assert.IsNotNull(instance);
            Assert.IsNotNull(solution);
        }
Exemple #10
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();
     });
 }