Beispiel #1
0
        private bool IsRibbon(ProblemSpec prob)
        {
            var ds        = prob.Segments.Select(s => s.Direction).ToList();
            var diagCount = ds.Count(d => d.X == d.Y || d.X == -d.Y);

            return(diagCount == 2 && ds.All(d => d.X == 0 || d.Y == 0 || d.X == d.Y || d.X == -d.Y));
        }
Beispiel #2
0
        private static PointProjectionSolver Solve2(ProblemSpec problemSpec, double originality)
        {
            var solver      = SolverMaker.CreateSolver(problemSpec);
            var ribbonWidth = RibbonIndicator.GetRibbonWidth(problemSpec);

            return(SolverMaker.Solve(solver, ribbonWidth.HasValue ? ribbonWidth.Value : 1, originality));
        }
Beispiel #3
0
        public ProblemSpec Get(int id)
        {
            var s = ProblemSpec.Parse(File.ReadAllText(GetFilename(id)));

            s.id = id;
            return(s);
        }
Beispiel #4
0
        private static double EvaluateProblem(ProblemSpec spec)
        {
            //var solution = solver.SolveMovingInitialSquare(spec);
            var solution = solver.SolveMovingAndRotatingInitialSquare(spec, 10);
            var score    = SolutionEvaluator.EvaluateX(spec, solution, 100);

            return(score);
        }
Beispiel #5
0
        private static Bitmap MakeBitmap(Painter painter, ProblemSpec problemSpec)
        {
            var size   = 300;
            var bitmap = new Bitmap(size, size);

            painter.Paint(Graphics.FromImage(bitmap), size, problemSpec);
            return(bitmap);
        }
Beispiel #6
0
        private static Vector GetShift(ProblemSpec problem)
        {
            var vs    = problem.Polygons.SelectMany(p => p.Vertices).ToList();
            var minX  = vs.Select(p => p.X).Min();
            var minY  = vs.Select(p => p.Y).Min();
            var shift = new Vector(minX, minY);

            return(shift);
        }
Beispiel #7
0
        public SolutionSpec SolveMovingFoldedSquare(ProblemSpec problem)
        {
            var shift  = GetShift(problem);
            var half   = "0,0 0,1 1,1 1,0 0,1/2 1,1/2".ToPoints();
            var dest   = "0,0 0,0 1,0 1,0 0,1/2 1,1/2".ToPoints().Select(v => v + shift).ToArray();
            var facets = new[] { new Facet(0, 4, 5, 3), new Facet(4, 1, 2, 5) };

            return(new SolutionSpec(half, facets, dest));
        }
        public static Form CreateVisualizerForm(this ProblemSpec problem)
        {
            var     form    = new Form();
            Painter painter = new Painter();

            form.Paint +=
                (sender, args) => painter.Paint(args.Graphics, Math.Min(form.ClientSize.Height, form.ClientSize.Width), problem);
            form.Text = "Problem";
            return(form);
        }
        public static SolutionSpec TrySolveInBestShot(ProblemSpec problem, Polygon convexPolygon)
        {
            var solution = EnumerateInitialSolutions(convexPolygon)
                           .Take(20)
                           .Select(x => Solve(convexPolygon, x, TimeSpan.FromSeconds(1)))
                           .OrderByDescending(x => SolutionEvaluator.EvaluateX(problem, x, dpi: 200))
                           .FirstOrDefault();

            return(solution);
        }
Beispiel #10
0
        public void Paint(Graphics g, int size, ProblemSpec spec)
        {
            PaintBackground(g, size);
            g.ScaleTransform(size / 1.5f, size / 1.5f);

            var spec2 = spec.MoveToOrigin();

            PaintPolygons(g, spec2.Polygons);
            PaintSegments(g, spec2.Segments);
        }
        public static SolutionSpec TrySolveInOneShot(ProblemSpec problem, Polygon convexPolygon)
        {
            SolutionSpec solution        = null;
            var          initialSolution = TryGetInitialSolution(problem, convexPolygon);

            if (initialSolution != null)
            {
                solution = Solve(convexPolygon, initialSolution);
            }
            return(solution);
        }
Beispiel #12
0
        private static SolutionSpec Solve(ProblemSpec problemSpec, double originality)
        {
            var solver       = SolverMaker.CreateSolver(problemSpec);
            var ribbonWidth  = RibbonIndicator.GetRibbonWidth(problemSpec);
            var simpleSolver = SolverMaker.Solve(solver, ribbonWidth.HasValue ? ribbonWidth.Value : 1, originality);

            if (simpleSolver != null)
            {
                return(SolutionSpecBuilder.BuildSolutionByRibbonGraph(simpleSolver.Projection));
            }
            return(null);
        }
        public void MovableToOrigin()
        {
            var x = ProblemSpec.Parse(@"1
4
1267650600228229401496703205376,1267650600228229401496703205376
1267650600228229401496703205377,1267650600228229401496703205376
1267650600228229401496703205377,1267650600228229401496703205377
1267650600228229401496703205376,1267650600228229401496703205377
4
1267650600228229401496703205376,1267650600228229401496703205376 1267650600228229401496703205376,1267650600228229401496703205377
1267650600228229401496703205377,1267650600228229401496703205376 1267650600228229401496703205377,1267650600228229401496703205377
1267650600228229401496703205376,1267650600228229401496703205376 1267650600228229401496703205377,1267650600228229401496703205376
1267650600228229401496703205376,1267650600228229401496703205377 1267650600228229401496703205377,1267650600228229401496703205377").MoveToOrigin();
        }
Beispiel #14
0
        public void PaintOne(int index)
        {
            var painter  = new Painter();
            var filename = index.ToString("000") + ".spec.txt";
            var content  = File.ReadAllText(Path.Combine(Paths.ProblemsDir(), filename));
            var spec     = ProblemSpec.Parse(content).MoveToOrigin();

            Console.WriteLine(spec.ToString());
            var bmp  = MakeBitmap(painter, spec);
            var file = Path.Combine(Paths.ProblemsDir(), filename + ".bmp");

            bmp.Save(file);
            Process.Start(file);
        }
Beispiel #15
0
        public SolutionSpec SolveMovingAndRotatingInitialSquare(ProblemSpec problem, int dpi = 10)
        {
            var shift = GetShift(problem);
            var ts    = from dx in Range(0, 1, dpi)
                        from dy in Range(0, 1, dpi)
                        let finalShift = shift.Move(dx, dy)
                                         from x in Range(0, 1, dpi * 2)
                                         select(Func <Vector, Vector>)(v => v.Rotate(x) + finalShift);
            var transform =
                ts.Select(t => Tuple.Create(t, SolutionEvaluator.EvaluateX(problem, SolutionSpec.CreateTrivial(t), dpi * 2)))
                .OrderByDescending(t => t.Item2)
                .FirstOrDefault()?.Item1;

            return(SolutionSpec.CreateTrivial(transform));
        }
Beispiel #16
0
        public void EvaluateX(string problem, string solution, double expectedResult, double precision)
        {
            var problemPolygons = problem.Split('|').Select(x => new Polygon(x.Split(' ').Select(Vector.Parse).ToArray())).ToArray();
            var problemSpec     = new ProblemSpec(problemPolygons, new Segment[0]);

            var solutionPolygons = solution.Split('|').Select(x => new Polygon(x.Split(' ').Select(Vector.Parse).ToArray())).ToArray();
            var solutionPoints   = solutionPolygons.SelectMany(x => x.Vertices).Distinct().ToArray();
            var pointToIndex     = solutionPoints.Select((p, i) => new { p, i }).ToDictionary(x => x.p, x => x.i);
            var facets           = solutionPolygons.Select(x => new Facet(x.Vertices.Select(v => pointToIndex[v]).ToArray())).ToArray();
            var solutionSpec     = new SolutionSpec(solutionPoints, facets, solutionPoints);

            var evaluation = SolutionEvaluator.EvaluateX(problemSpec, solutionSpec, 100);

            Console.Out.WriteLine(evaluation);
            evaluation.Should().BeApproximately(expectedResult, precision);
        }
        public static SolutionSpec TrySolveSingleProblem(ProblemSpec problem)
        {
            Polygon convexPolygon;
            var     positivePolygon = problem.Polygons.Single(x => x.GetSignedSquare() > 0);

            if (positivePolygon.IsConvex())
            {
                convexPolygon = positivePolygon;
                Console.Write("CONVEX ");
            }
            else
            {
                convexPolygon = positivePolygon.GetConvexBoundary();
                Console.Write("NOT_CONVEX ");
            }
            return(TrySolveInOneShot(problem, convexPolygon));
        }
        public static SolutionSpec TryGetInitialSolution(ProblemSpec problem, Polygon problemPolygon, TimeSpan?timeout = null)
        {
            timeout = timeout ?? TimeSpan.FromSeconds(10);
            SolutionSpec initialSolution = null;
            var          t = new Thread(() => { initialSolution = GetInitialSolutionByLongestRationalEdge(problemPolygon) ?? new ImperfectSolver().SolveMovingAndRotatingInitialSquare(problem); })
            {
                IsBackground = true
            };

            t.Start();
            if (!t.Join(timeout.Value))
            {
                t.Abort();
                t.Join();
                Console.Write($"Failed to get initial solution in {timeout}! Skipping");
            }
            return(initialSolution);
        }
Beispiel #19
0
        public void PaintProblems()
        {
            var painter = new Painter();
            var ps      =
                from i in Enumerable.Range(1, int.MaxValue)
                let filename = i.ToString("000") + ".spec.txt"
                               let filepath = Path.Combine(Paths.ProblemsDir(), filename)
                                              select filepath;

            foreach (var path in ps.TakeWhile(File.Exists))
            {
                Console.WriteLine($"writing {path}");
                var content = File.ReadAllText(path);
                var spec    = ProblemSpec.Parse(content);
                var bmp     = MakeBitmap(painter, spec);
                bmp.Save(path + ".bmp");
            }
        }
Beispiel #20
0
        private List <ProblemSpec> GetAllPerms(ProblemSpec source)
        {
            var result = new List <ProblemSpec>();

            result.Add(source);
            for (int i = 0; i < 3; i++)
            {
                source = Rotate90(source);
                result.Add(source);
            }
            source = ReflectProblem(source, "0,0 1,1");
            for (int i = 0; i < 4; i++)
            {
                source = Rotate90(source);
                result.Add(source);
            }
            return(result);
        }
Beispiel #21
0
        private ProblemListItem CreateItem(ProblemSpec problem)
        {
            var res = new ProblemListItem()
            {
                Id   = problem.id,
                Spec = problem
            };
            var resp = repo.FindResponse(problem.id);

            if (resp != null)
            {
                var json = Read(resp);
                res.OurResemblance = json.resemblance;
            }
            if (problemsJson.ContainsKey(problem.id))
            {
                res.json          = problemsJson[problem.id];
                res.ExpectedScore = problemsJson[problem.id].ExpectedScore();
            }
            return(res);
        }
        public void BeParsable()
        {
            var         input = @"1
4
0,0
1,0
1/2,1/2
0,1/2
5
0,0 1,0
1,0 1/2,1/2
1/2,1/2 0,1/2
0,1/2 0,0
0,0 1/2,1/2";
            ProblemSpec spec  = ProblemSpec.Parse(input);

            Console.WriteLine(spec);
            spec.Polygons.Length.Should().Be(1);
            spec.Segments.Length.Should().Be(5);
            spec.ToString().Should().Be(input);
        }
Beispiel #23
0
        public static double Evaluate(ProblemSpec problemSpec, SolutionSpec solutionSpec, int dpi)
        {
            var problemPolygons = problemSpec.Polygons;
            var destPoints      = solutionSpec.DestPoints.ToList();
            var allPoints       = problemPolygons.SelectMany(x => x.Vertices).Concat(destPoints).ToList();
            var minX            = allPoints.Min(x => x.X);
            var minY            = allPoints.Min(x => x.Y);
            var maxX            = allPoints.Max(x => x.X);
            var maxY            = allPoints.Max(x => x.Y);

            var positivePolygons = problemPolygons.Where(p => p.GetSignedSquare() >= 0).ToList();
            var negativePolygons = problemPolygons.Where(p => p.GetSignedSquare() < 0).ToList();
            var solutionPolygons = solutionSpec.Facets.Select(x => new Polygon(x.Vertices.Select(v => destPoints[v]).ToArray())).ToList();

            var deltaX       = (maxX - minX) / dpi;
            var deltaY       = (maxY - minY) / dpi;
            int intersection = 0;
            int union        = 0;

            for (var x = minX; x < maxX; x += deltaX)
            {
                for (var y = minY; y < maxY; y += deltaY)
                {
                    var p          = new Vector(x, y);
                    var inNegative = negativePolygons.Any(poly => p.GetPositionToPolygon(poly) == PointToPolygonPositionType.Inside);
                    var inPositive = positivePolygons.Any(poly => p.GetPositionToPolygon(poly) != PointToPolygonPositionType.Outside);
                    var inProblem  = inPositive && !inNegative;
                    var inSolution = solutionPolygons.Any(poly => p.GetPositionToPolygon(poly) != PointToPolygonPositionType.Outside);
                    if (inProblem && inSolution)
                    {
                        intersection++;
                    }
                    if (inProblem || inSolution)
                    {
                        union++;
                    }
                }
            }
            return((double)intersection / union);
        }
Beispiel #24
0
        public static Rational?GetRibbonWidth(ProblemSpec spec)
        {
            var solver = SolverMaker.CreateSolver(spec);

            return(GetRibbonWidth(solver));
        }
Beispiel #25
0
 public static SolutionSpec AutoSolve(ProblemSpec problemSpec)
 {
     return(Solve(problemSpec, 0.3));
 }
 public void SetData(Polygon[] polygons, Segment[] segments)
 {
     this.Problem = new ProblemSpec(polygons, segments);
     panel1.Invalidate();
 }
Beispiel #27
0
 private static double Eval(ProblemSpec p, SolutionSpec s)
 {
     return(SolutionEvaluator.EvaluateX(p, s, 100));
 }
Beispiel #28
0
 private static ProblemSpec ReflectProblem(ProblemSpec source, Segment segment)
 {
     return(new ProblemSpec(source.Polygons.Select(v => v.Reflect(segment)).ToArray(), new Segment[0]));
 }
Beispiel #29
0
 private static ProblemSpec Rotate90(ProblemSpec source)
 {
     return(ReflectProblem(ReflectProblem(source, "1/2,0 1/2,1"), "0,0 1,1"));
 }
Beispiel #30
0
 private void ListOnSelectedValueChanged(object sender, EventArgs eventArgs)
 {
     problem = ((ProblemListItem)list.SelectedItem).Spec;
     problemPanel.Invalidate();
 }