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); }
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); }
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)); }
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); }
private static double Eval(ProblemSpec p, SolutionSpec s) { return(SolutionEvaluator.EvaluateX(p, s, 100)); }