public void PostSolutionsForSameProblems() { var repo = new ProblemsRepo(); var solutions = new Dictionary <int, Tuple <string, double, int> >(); foreach (var problemSpec in repo.GetAll().Where(p => repo.FindSolution(p.id) != null)) { var polygonsHashCode = problemSpec.GetPolygonsHashCode(); Tuple <string, double, int> prev; if (!solutions.TryGetValue(polygonsHashCode, out prev) || prev.Item2 < repo.GetProblemResemblance(problemSpec.id)) { solutions[polygonsHashCode] = Tuple.Create(repo.FindSolution(problemSpec.id), repo.GetProblemResemblance(problemSpec.id), problemSpec.id); } } foreach (var problemSpec in repo.GetAll()) { var polygonsHashCode = problemSpec.GetPolygonsHashCode(); Tuple <string, double, int> best; if (solutions.TryGetValue(polygonsHashCode, out best) && repo.GetProblemResemblance(problemSpec.id) < best.Item2) { Console.Write($"{problemSpec.id}->{best.Item3}: "); ProblemsSender.Post(new SolutionSpec(best.Item1), problemSpec.id, pack: false); Console.WriteLine(); } } }
public void Solve(int problemId) { var problemsRepo = new ProblemsRepo(); var problem = problemsRepo.Get(problemId); var poly = problem.Polygons.Single(); // var dx = (int) problem.Polygons.SelectMany(p => p.Vertices).Select(x => x.X.Denomerator).Max(); // var dy = (int) problem.Polygons.SelectMany(p => p.Vertices).Select(x => x.Y.Denomerator).Max(); // foreach (var x in Enumerable.Range(0, dx).Select(x => new Rational(x, dx))) // foreach (var y in Enumerable.Range(0, dy).Select(y => new Rational(y, dy))) { // var shift = new Vector(x, y); var shift = new Vector(0, 0); var initialSolution = SolutionSpec.CreateTrivial(v => v + shift); var solution = ConvexPolygonSolver.Solve(poly.GetConvexBoundary(), initialSolution); var packedSolution = solution.Pack(); var packedSolutionSize = packedSolution.Size(); var solutionSize = solution.Size(); Console.WriteLine($"{shift}: {solutionSize}; packed: {packedSolutionSize}"); if (packedSolutionSize <= 5000) { ProblemsSender.Post(packedSolution, problemId, false); // return; } } }
private static void PostSolution(int problemId, SolutionSpec solution) { try { var res = ProblemsSender.Post(solution, problemId); if (res == 1.0) { var repo = new ProblemsRepo(); var problemSpec = repo.Get(problemId); var problemSpecStr = problemSpec.ToString(); var toSend = repo.GetAllNotSolvedPerfectly().Where(p => p.ToString() == problemSpecStr).ToList(); foreach (var sameProb in toSend) { res = Math.Min(res, ProblemsSender.Post(solution, sameProb.id)); } MessageBox.Show($"Resemblance = 1.0. {toSend.Count} same problem. min resemblence = {res}"); } else { MessageBox.Show("Resemblance = " + res + " no same problems"); } } catch (Exception e) { MessageBox.Show(e.ToString(), e.Message); } }
private static void Solve(ProblemSpec problemSpec) { var originalities = new[] { 0.5 }; var mutex = new object(); var solutionFoundEvent = new ManualResetEvent(false); var threads = originalities .Select(coeff => { var thread = new Thread(() => { try { var solution = SolutionPacker.Pack(new ConstructorSolver(problemSpec).Work()); if (solution == null || solution.Size() > 5000 || !solution.AreFacetsValid()) { return; } double ps; lock (mutex) { Console.WriteLine(" posting... "); ps = ProblemsSender.Post(solution, problemSpec.id); Console.Write($" perfect score: {ps}"); } if (ps == 1.0) { solutionFoundEvent.Set(); } } catch (Exception e) { if (e is ThreadAbortException) { return; } Console.WriteLine($"Exception in ProjectionSolverRunner: {e}"); } }) { IsBackground = true }; thread.Start(); return(thread); }) .ToArray(); solutionFoundEvent.WaitOne(TimeSpan.FromSeconds(10)); foreach (var t in threads) { if (t.IsAlive) { t.Abort(); t.Join(); } } }
static void Main(string[] args) { if (args.Contains("--yura")) { ShowIntro("SolveWithProjectionSolverRunner"); for (var iteration = 0;; iteration++) { if (iteration > 0 || args.Contains("-d")) { DownloadNewProblems(); } Console.WriteLine("Solving..."); repo.GetAllNotSolvedPerfectly() .OrderBy(EstimateDifficulty) .AsParallel().WithDegreeOfParallelism(8) .ForAll(problemSpec => { Console.WriteLine($"Solving {problemSpec.id}..."); SolveWithProjectionSolverRunner(problemSpec); }); Console.WriteLine("Waiting 1 minute..."); Thread.Sleep(TimeSpan.FromMinutes(1)); } } else if (args.Contains("--convex")) { ShowIntro("ConvexPolygonSolver"); var newProblems = DownloadNewProblems(); ConvexPolygonSolver.SolveAll(newProblems); ConvexPolygonSolver.SolveAllNotSolvedPerfectly(); } else { ShowIntro("DumbSolver"); var newProblems = DownloadNewProblems(); Console.Out.WriteLine($"newProblems.Count: {newProblems.Count}"); var noSolutionProblems = repo.GetAllNotSolvedPerfectly().Where(x => repo.FindResponse(x.id) == null).Skip(15).ToList(); Console.Out.WriteLine($"noSolutionProblems.Count: {noSolutionProblems.Count}"); var sw = Stopwatch.StartNew(); for (var i = 0; i < noSolutionProblems.Count; i++) { var problem = noSolutionProblems[i]; Console.Write($"{sw.Elapsed:c} Problem {problem.id:0000} ({i:0000}/{noSolutionProblems.Count:0000}) "); var solution = TryGetInitialSolution(problem); if (solution != null) { ProblemsSender.Post(solution, problem.id); } Console.WriteLine(); } } }
private static double SolveAndSendInternal(int taskNumber, double originality) { var problemSpec = new ProblemsRepo().Get(taskNumber); var solutionSpec = Solve(problemSpec, originality); if (solutionSpec == null) { return(0); } return(ProblemsSender.Post(solutionSpec, taskNumber)); }
public void DoSomething_WhenSomething2(int problemId) { var repo = new ProblemsRepo(); var problemSpec = repo.Get(problemId); var sourcePoints = new List <Vector>(); var destPoints = new List <Vector>(); var facets = new List <Facet>(); for (int iX = 0; iX <= 31; iX++) { for (int iY = 0; iY <= 6; iY++) { sourcePoints.Add(new Vector(iX / (Rational)31, iY / (Rational)6)); if (iY % 2 == 0) { if (iX % 2 == 0) { destPoints.Add("0,0"); } else { destPoints.Add("1/31,0"); } } else if (iX % 2 == 0) { destPoints.Add("0,1/6"); } else { destPoints.Add("1/31,1/6"); } } } for (int iX = 0; iX < 31; iX++) { for (int iY = 0; iY < 6; iY++) { facets.Add(new Facet(iX * 7 + iY, iX * 7 + 1 + iY, (iX + 1) * 7 + iY + 1, (iX + 1) * 7 + iY)); } } var solution = new SolutionSpec(sourcePoints.ToArray(), facets.ToArray(), destPoints.ToArray()); Console.Out.WriteLine($"size: {solution.Size()}; packed: {solution.Pack().Size()}"); Console.Out.WriteLine($"facets: {solution.Facets.Length}; sourcePoints: {solution.SourcePoints.Length}; destPoints: {solution.DestPoints.Length}"); //solution.CreateVisualizerForm().ShowDialog(); var post = ProblemsSender.Post(solution, problemSpec.id); Console.Out.WriteLine(post); }
public static void SolveAll(List <ProblemSpec> problems) { var sw = Stopwatch.StartNew(); for (var i = 0; i < problems.Count; i++) { var problem = problems[i]; Console.Write($"{sw.Elapsed:c} Problem {problem.id:0000} ({i:0000}/{problems.Count:0000}) "); var solution = TrySolveSingleProblem(problem); if (solution != null) { ProblemsSender.Post(solution, problem.id, storeSolutionWithSameResemblance: true); } else { Console.ForegroundColor = ConsoleColor.Yellow; Console.Out.Write($" no solution found "); Console.ResetColor(); } Console.WriteLine(); } }
private static void SolveWithProjectionSolverRunner(ProblemSpec problemSpec) { var thread = new Thread(() => { try { var solution = UltraSolver.AutoSolve(problemSpec); if (solution == null || solution.Size() > 5000 || !solution.AreFacetsValid()) { return; } Console.WriteLine(" posting... "); var ps = ProblemsSender.Post(solution, problemSpec.id); Console.Write($" perfect score: {ps}"); } catch (Exception e) { if (e is ThreadAbortException) { return; } Console.WriteLine($"Exception in ProjectionSolverRunner: {e}"); } }) { IsBackground = true }; thread.Start(); if (!thread.Join(TimeSpan.FromSeconds(25))) { thread.Abort(); thread.Join(); } }
public void DoSomething_WhenSomething(int problemId, string s887, string s112, bool toCorner) { var repo = new ProblemsRepo(); var problemSpec = repo.Get(problemId); var v887 = (Rational)s887; var v112 = (Rational)s112; var vRate = (1 - v112) / v887; vRate.Denomerator.Should().Be(1, $"rate={vRate.Numerator}/{vRate.Denomerator}"); var rate = (int)vRate.Numerator; var rateOddity = 1 - rate % 2; var sourcePoints = new List <Vector>(); var destPoints = new List <Vector>(); var facets = new List <Facet>(); for (int iX = 0; iX <= rate; iX++) { for (int iY = 0; iY <= rate; iY++) { sourcePoints.Add(new Vector(iX * v887, iY * v887)); if ((iX + iY) % 2 == 0) { destPoints.Add(new Vector(v887, v887)); } else if (iX % 2 == rateOddity) { destPoints.Add(new Vector(0, v887)); } else { destPoints.Add(new Vector(v887, 0)); } } } for (int iX = 0; iX < rate; iX++) { for (int iY = 0; iY < rate; iY++) { if ((iX + iY) % 2 == 0) { facets.Add(new Facet(iX * (rate + 1) + iY, iX * (rate + 1) + iY + 1, iX * (rate + 1) + iY + rate + 1)); facets.Add(new Facet(iX * (rate + 1) + iY + 1, iX * (rate + 1) + iY + rate + 1, iX * (rate + 1) + iY + rate + 2)); } else { facets.Add(new Facet(iX * (rate + 1) + iY, iX * (rate + 1) + iY + 1, iX * (rate + 1) + iY + rate + 2)); facets.Add(new Facet(iX * (rate + 1) + iY, iX * (rate + 1) + iY + rate + 2, iX * (rate + 1) + iY + rate + 1)); } } } for (int iX = 0; iX <= rate; iX++) { sourcePoints.Add(new Vector(iX * v887, 1)); destPoints.Add(new Vector(iX % 2 == rateOddity ? 0 : v887, v887 - v112)); } for (int iY = 0; iY <= rate; iY++) { sourcePoints.Add(new Vector(1, iY * v887)); destPoints.Add(new Vector(v887 - v112, iY % 2 == rateOddity ? 0 : v887)); } for (int iX = 0; iX < rate; iX++) { var start = (rate + 1) * (rate + 1); facets.Add(new Facet(start + iX, start + iX + 1, (iX + 2) * (rate + 1) - 1, (iX + 1) * (rate + 1) - 1)); } for (int iY = 0; iY < rate; iY++) { var start = (rate + 1) * (rate + 2); var start2 = (rate + 1) * rate; facets.Add(new Facet(start + iY, start + iY + 1, start2 + iY + 1, start2 + iY)); } sourcePoints.Add(new Vector(1, 1)); destPoints.Add(new Vector(v887 - v112, v887 - v112)); facets.Add(new Facet((rate + 1) * (rate + 1) - 1, (rate + 1) * (rate + 2) - 1, sourcePoints.Count - 1, sourcePoints.Count - 2)); var solution = new SolutionSpec(sourcePoints.ToArray(), facets.ToArray(), destPoints.ToArray()); if (toCorner) { solution = solution.Reflect(new Segment(new Vector(v887, 0), new Vector(0, v887))); } Console.Out.WriteLine($"size: {solution.Size()}; packed: {solution.Pack().Size()}"); Console.Out.WriteLine($"facets: {solution.Facets.Length}; sourcePoints: {solution.SourcePoints.Length}; destPoints: {solution.DestPoints.Length}"); //solution.CreateVisualizerForm().ShowDialog(); var post = ProblemsSender.Post(solution, problemSpec.id); Console.Out.WriteLine(post); // var v775 = (Rational)"479887358674775"; // var rational = v887*5 + 112; // var rational = (denominator - numerator)/112; // var rational = v775/112; // rational.Reduce(); // Console.Out.WriteLine($"{rational.Numerator}/{rational.Denomerator}"); // Console.Out.WriteLine(2399436793374547); //Console.Out.WriteLine((double)(Rational)"479887358674775/2399436793374547"); }