Esempio n. 1
0
        public void Temp([Values(3)] int problemId)
        {
            var problem = ProblemSolutionFactory.LoadProblem($"FR{problemId:D3}");
            //var assembler = new GreedyPartialSolver(problem.TargetMatrix, new ThrowableHelperFast(problem.TargetMatrix));
            //var disassembler = new InvertorDisassembler(new GreedyPartialSolver(problem.SourceMatrix, new ThrowableHelperFast(problem.SourceMatrix)), problem.SourceMatrix);
            //var solver = new SimpleReassembler(disassembler, assembler);
            var commonPart = problem.SourceMatrix.Intersect(problem.TargetMatrix);

            commonPart = new ComponentTrackingMatrix(commonPart).GetGroundedVoxels();

            File.WriteAllBytes(Path.Combine(FileHelper.ProblemsDir, "FR666_tgt.mdl"), commonPart.Save());
            File.WriteAllBytes(Path.Combine(FileHelper.ProblemsDir, "FR666_src.mdl"), commonPart.Save());
            var             solver   = new GreedyPartialSolver(problem.SourceMatrix, commonPart, new ThrowableHelperFast(commonPart, problem.SourceMatrix));
            List <ICommand> commands = new List <ICommand>();

            try
            {
                foreach (var command in solver.Solve())
                {
                    commands.Add(command);
                }
                //commands.AddRange(solver.Solve());
            }
            catch (Exception e)
            {
                Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e);
                throw;
            }
            finally
            {
                Console.WriteLine(commands.Take(5000).ToDelimitedString("\n"));
                var bytes = CommandSerializer.Save(commands.ToArray());
                File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, "FR666"), bytes);
            }
        }
Esempio n. 2
0
        public void Solve()
        {
            var problemsDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../../data/problemsF");
            var p           = Directory.EnumerateFiles(problemsDir, "*.mdl").Single(x => x.Contains("007"));
            var mtrx        = Matrix.Load(File.ReadAllBytes(p));
            var R           = mtrx.R;
            var solver      = new GreedyPartialSolver(mtrx.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(mtrx));

            solver.Solve();
        }
Esempio n. 3
0
        public void Solve()
        {
            var problemsDir = FileHelper.ProblemsDir;
            var resultsDir  = FileHelper.SolutionsDir;
            var allProblems = Directory.EnumerateFiles(problemsDir, "*.mdl");
            var problems    = allProblems.Select(p =>
            {
                var matrix = Matrix.Load(File.ReadAllBytes(p));
                return(new { m = matrix, p, weight = matrix.Voxels.Cast <bool>().Count(b => b) });
            }).Take(10).ToList();

            problems.Sort((p1, p2) => p1.weight.CompareTo(p2.weight));
            Log.For(this).Info(string.Join("\r\n", problems.Select(p => p.p)));
            Parallel.ForEach(problems, p =>
            {
                var R = p.m.R;
                //var solver = new GreedyPartialSolver(p.m.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(p.m));
                var solver = new GreedyPartialSolver(p.m.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelperFast(p.m));
                //var solver = new DivideAndConquer(p.m);
                //var solverName = "div-n-conq";
                var solverName      = "greedy-fst";
                ICommand[] commands = null;
                try
                {
                    commands = solver.Solve().ToArray();
                }
                catch (Exception e)
                {
                    Log.For(this).Error($"Unhandled exception in solver for {Path.GetFileName(p.p)}", e);
                    return;
                }

                var solutionEnergy = GetSolutionEnergy(p.m, commands, p.p);

                var bytes = CommandSerializer.Save(commands);
                File.WriteAllBytes(GetSolutionPath(resultsDir, p.p, solverName, solutionEnergy), bytes);
            });
        }
Esempio n. 4
0
        //[Timeout(30000)]
        public void SolveOne(
            [Values(122)] int problemId
            //[ValueSource(nameof(Problems))] int problemId
            )
        {
            var problemsDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../../data/problemsF");
            var resultsDir  = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../../data/solutions");
            var problemFile = Path.Combine(problemsDir, $"FD{problemId.ToString().PadLeft(3, '0')}_src.mdl");
            var matrix      = Matrix.Load(File.ReadAllBytes(problemFile));
            var R           = matrix.R;
            //var solver = new GreedyPartialSolver(matrix.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(matrix), new BottomToTopBuildingAround());
            var solver = new GreedyPartialSolver(matrix.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(matrix), new NearToFarBottomToTopBuildingAround());
            //var solver = new DivideAndConquer(matrix, true);
            List <ICommand> commands = new List <ICommand>();

            try
            {
                var sw = Stopwatch.StartNew();
                commands.AddRange(solver.Solve().TakeWhile(x => sw.Elapsed.TotalSeconds < 20));
                Console.WriteLine(GreedyPartialSolver.candidatesCount.ToDetailedString());
            }
            catch (Exception e)
            {
                Log.For(this).Error($"Unhandled exception in solver for {Path.GetFileName(problemFile)}", e);
                throw;
            }
            finally
            {
                var bytes = CommandSerializer.Save(commands.ToArray());
                File.WriteAllBytes(GetSolutionPath(resultsDir, problemFile), bytes);
            }

            var solutionEnergy = GetSolutionEnergy(matrix, commands.ToArray(), problemFile);

            Console.WriteLine(solutionEnergy);
            Console.WriteLine(ThrowableHelper.opt.ToDetailedString());
        }