Esempio n. 1
0
        public void PostSolutionsForSameProblems3()
        {
            var repo      = new ProblemsRepo();
            var solutions = new Dictionary <int, Tuple <string, double, int> >();

            Console.Out.WriteLine($"calculating");
            foreach (var problemSpec in repo.GetAll().Where(p => repo.FindSolution(p.id) != null))
            {
                var perms            = GetAllPerms(problemSpec);
                var polygonsHashCode = perms.Select(p => p.MoveToOrigin().GetPolygonsHashCode()).ToList().Min();
                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 perms            = GetAllPerms(problemSpec);
                var polygonsHashCode = perms.Select(p => p.MoveToOrigin().GetPolygonsHashCode()).ToList().Min();
                Tuple <string, double, int> best;
                if (solutions.TryGetValue(polygonsHashCode, out best))
                {
                    if (repo.GetProblemResemblance(problemSpec.id) < best.Item2)
                    {
                        Console.Out.Write($"{problemSpec.id} -> {best.Item3}: ");
                        Console.Out.Write($"{repo.GetProblemResemblance(problemSpec.id)} -> {best.Item2}");
                        Console.Out.WriteLine();
                    }
                }
//					ProblemsSender.Post(new SolutionSpec(best.Item1), problemSpec.id, pack: false);
            }
        }
 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);
     }
 }
Esempio n. 3
0
        public void EvaluateAll()
        {
            var    problems = new ProblemsRepo().GetAll();
            double sum1     = 0;
            double sum2     = 0;

            foreach (var p in problems)
            {
                //Console.WriteLine("Problem: " + p.id);
                var s1 = solver.SolveMovingInitialSquare(p);
                var s2 = solver.SolveMovingFoldedSquare(p);
                //Console.WriteLine(s2);
                var score1 = Eval(p, s1);
                var score2 = Eval(p, s2);
                sum1 += score1;
                sum2 += Math.Max(score1, score2);
                if (score2 > score1)
                {
                    Console.WriteLine(apiClient.PostSolution(p.id, s2));
                }
                Thread.Sleep(800);
            }
            Console.WriteLine(sum1);
            Console.WriteLine(sum2);
        }
Esempio n. 4
0
 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;
         }
     }
 }
Esempio n. 5
0
        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();
                }
            }
        }
Esempio n. 6
0
        public void SubmitSolution()
        {
            var problemsRepo = new ProblemsRepo();
            var goodTasks    = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 38, 39, 40, 41, 42, 46 };
//			var goodTasks = new[] { 13 };
            var apiClient = new ApiClient();

            foreach (var p in goodTasks)
            {
                try
                {
                    Console.WriteLine($"!solving: {p}");
                    var problemSpec  = problemsRepo.Get(p);
                    var solutionSpec = UltraSolver.AutoSolve(problemSpec);
                    var postSolution = apiClient.PostSolution(p, solutionSpec);
                    Console.WriteLine(postSolution);
                    problemsRepo.PutSolution(p, solutionSpec);
                    problemsRepo.PutResponse(p, postSolution);
                }
                catch (Exception e)
                {
                    problemsRepo.PutSolution(p, e.ToString());
                }
                Thread.Sleep(1000);
            }
        }
Esempio n. 7
0
        public void CalcImperfectScore()
        {
            var snapshotJson = new ProblemsRepo().GetSnapshot(new ApiClient());
            var v            = snapshotJson.Problems.Where(p => p.Ranking.All(r => r.resemblance != 1.0))
                               .Sum(p => p.SolutionSize / (1 + p.Ranking.Length));

            Console.WriteLine(v);
        }
Esempio n. 8
0
        public void TestMove(int problemId)
        {
            Console.WriteLine($"problemId = {problemId}");
            var spec  = new ProblemsRepo().Get(problemId);
            var score = EvaluateProblem(spec);

            Console.WriteLine($"Score = {score}");
        }
Esempio n. 9
0
        public void CalcImperfectScore2()
        {
            var repo = new ProblemsRepo();
            var c    = repo.GetAllNotSolvedPerfectly().Count();

            Console.WriteLine(c);
            Console.WriteLine(repo.GetAll().Count());
        }
Esempio n. 10
0
        public void DoSomething_WhenSomething()
        {
            var problemSpec = new ProblemsRepo().Get(16);

            problemSpec.CreateVisualizerForm().ShowDialog();
            var res = new ImperfectSolver().SolveMovingInitialSquare(problemSpec);

            res.CreateVisualizerForm().ShowDialog();
        }
Esempio n. 11
0
        public static void SolveAndShow(int taskNumber)
        {
            var problemSpec = new ProblemsRepo().Get(taskNumber);
            var solver      = Solve2(problemSpec, 0);

            if (solver != null)
            {
                SolverMaker.Visualize(solver);
            }
        }
Esempio n. 12
0
        public void FindRects()
        {
            var repo = new ProblemsRepo();
            var sq   = repo.GetAllNotSolvedPerfectly().Where(p => p.Segments.Length == 4);

            foreach (var p in sq)
            {
                Console.WriteLine(p.id);
            }
        }
Esempio n. 13
0
        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));
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
        public void VisualiseSolution()
        {
            var problemsRepo = new ProblemsRepo();
            var problemSpec  = problemsRepo.Get(1170);

            Console.WriteLine("problem");
            Console.WriteLine(problemSpec);
            problemSpec.CreateVisualizerForm().ShowDialog();
            var solutionSpec = UltraSolver.AutoSolve(problemSpec);

            solutionSpec.CreateVisualizerForm().ShowDialog();
            Console.WriteLine("solution");
            Console.WriteLine(solutionSpec);
        }
Esempio n. 16
0
        public void SubmitSolutions(int problemId)
        {
            ImperfectSolver solver = new ImperfectSolver();

            Console.WriteLine($"problemId = {problemId}");
            var repo     = new ProblemsRepo();
            var spec     = repo.Get(problemId);
            var solution = solver.SolveMovingInitialSquare(spec);
            var res      = apiClient.PostSolution(problemId, solution);

            Console.WriteLine(res);
            repo.PutSolution(problemId, solution);
            repo.PutResponse(problemId, res);
            Thread.Sleep(1000);
        }
Esempio n. 17
0
        public void SolveRibbons()
        {
            var repo         = new ProblemsRepo();
            var snapshotJson = repo.GetSnapshot(new ApiClient());
            var ribbons      = repo.GetAllNotSolvedPerfectly().Where(IsRibbon);
            var sum          = 0d;

            foreach (var ribbon in ribbons)
            {
                var desc = snapshotJson.Problems.First(p => p.Id == ribbon.id);
                sum += desc.ExpectedScore();
                Console.WriteLine(ribbon.id + " Owner = " + desc.Owner + " Exp = " + desc.ExpectedScore());
            }
            Console.WriteLine(sum);
        }
Esempio n. 18
0
        public void DoSomething_WhenSomething()
        {
            var repo      = new ProblemsRepo();
            var apiClient = new ApiClient();

            foreach (var problemSpec in repo.GetAll()
                     .Where(x => repo.GetProblemResemblance(x.id) == 1.0)
                     .Reverse().Take(3))
            {
                Console.Out.WriteLine(problemSpec.id);
                var solutionSpec = ConvexPolygonSolver.TrySolveSingleProblem(problemSpec);
                if (solutionSpec != null)
                {
                    var response = apiClient.PostSolution(problemSpec.id, solutionSpec.Normalize().Pack());
                    Console.Out.WriteLine(response);
                }
            }
        }
Esempio n. 19
0
        public void ProblemsRating()
        {
            var api         = new ApiClient();
            var repo        = new ProblemsRepo();
            var snapshot    = api.GetLastSnapshot();
            var totalConvex = 0.0;

            foreach (var p in snapshot.Problems.OrderByDescending(p => p.ExpectedScore()))
            {
                var spec          = repo.Find(p.Id);
                var expectedScore = p.ExpectedScore();
                var convex        = spec == null ? null : (spec.Polygons.Length == 1 && spec.Polygons[0].IsConvex() && spec.Polygons[0].GetSignedSquare() > 0).ToString();
                Console.WriteLine($"id={p.Id} size={p.SolutionSize} expected={expectedScore} isconvex={convex ?? "Unknown"}");
                if (convex != null)
                {
                    totalConvex += expectedScore;
                }
            }
            Console.WriteLine($"Total convex: {totalConvex}");
        }
Esempio n. 20
0
        public void PostSolutionsForSameProblems2()
        {
            var repo          = new ProblemsRepo();
            var directories   = Directory.GetDirectories(@"c:\Work\icfpc\other\icfp-2016\data\problems\", "*");
            var otherProblems = directories.Select(x =>
            {
                int result;
                if (int.TryParse(Path.GetFileName(x), out result))
                {
                    return(result);
                }
                return(-1);
            }).Where(x => x != -1);

            foreach (var otherProblem in otherProblems)
            {
                if (repo.FindResponse(otherProblem) == null)
                {
                    Console.Out.WriteLine(otherProblem);
                }
            }
        }
Esempio n. 21
0
        public void GetTriangles()
        {
            /*
             * 5987
             * 5988
             * 5990
             * 6100
             *
             * 16/9709 по X и по Y
             * 1077/9709 Ч ширина и высота треугольника
             * точка 1,1 Ч это вершина с пр¤мым углом
             *
             */

            var repo         = new ProblemsRepo();
            var snapshotJson = repo.GetSnapshot(new ApiClient());
            var ps           = snapshotJson.Problems.Where(p => p.Owner == "149");

            foreach (var problem in ps)
            {
                var pr     = repo.Get(problem.Id);
                var points = pr.Points.Distinct().ToList();
                if (points.Count == 7)
                {
                    var w     = points.Max(p => p.X);
                    var h     = points.Max(p => p.Y);
                    var minW1 = points.Select(p => p.X).Where(ww => ww > 0).Min();
                    var minW2 = points.Select(p => w - p.X).Where(ww => ww > 0).Min();
                    var minH1 = points.Select(p => p.Y).Where(hh => hh > 0).Min();
                    var minH2 = points.Select(p => h - p.Y).Where(hh => hh > 0).Min();
                    var minW  = minW1 > minW2 ? minW2 : minW1;
                    var minH  = minH1 > minH2 ? minH2 : minH1;

                    Console.WriteLine($"{problem.Id}, \"{w}\", \"{h}\", \"{minW}\", \"{minH}\", {minW1 < minW2}");
                }
            }
        }
Esempio n. 22
0
        public void CalculateOurScore()
        {
            var repo      = new ProblemsRepo();
            var sn        = repo.GetSnapshot(null);
            var ourScore1 = 0.0;
            var ourScore2 = 0.0;
            var ourScore3 = 0.0;

            foreach (var pr in sn.Problems)
            {
                var solutions = pr.Ranking.Count(r => r.resemblance == 1.0);
                var part      = pr.SolutionSize / (solutions + 1.0);
                var myR       = repo.GetProblemResemblance(pr.Id);
                if (myR == 1.0)
                {
                    ourScore1 += part;
                }
                else
                {
                    var rSum = pr.Ranking.Where(r => r.resemblance != 1.0).Sum(r => r.resemblance);
                    ourScore2 += part * myR / rSum;
                }
                if (pr.Owner == "89")
                {
                    ourScore3 += (5000 - pr.SolutionSize) / Math.Max(6, solutions + 1.0);
                }
            }
            Console.WriteLine(ourScore1);
            Console.WriteLine(ourScore2);
            Console.WriteLine(ourScore3);
            Console.WriteLine(ourScore1 + ourScore2 + ourScore3);
            var prCount       = repo.GetAll().Count();
            var noSolvedCount = repo.GetAllNotSolvedPerfectly().Count();

            Console.WriteLine(prCount - noSolvedCount);
            Console.WriteLine(prCount);
        }
Esempio n. 23
0
        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");
        }
Esempio n. 24
0
        public static void Run()
        {
            var pr                   = new ProblemsRepo();
            var problemSpecs         = pr.GetAllProblemSpecContentAndId().ToList();
            var specSolutionResponse = problemSpecs.ToDictionary(p => p.Item2, p => Tuple.Create(
                                                                     p.Item1,                  //spec
                                                                     pr.FindSolution(p.Item2), //sln
                                                                     pr.FindResponse(p.Item2), //resp
                                                                     p.Item2
                                                                     ));
            var sameSpecs = new Dictionary <string, List <Tuple <string, string, string, int> > >();

            foreach (var pair in specSolutionResponse)
            {
                if (!sameSpecs.ContainsKey(pair.Value.Item1))
                {
                    sameSpecs[pair.Value.Item1] = new List <Tuple <string, string, string, int> >();
                }
                sameSpecs[pair.Value.Item1].Add(pair.Value);
            }
            var toSolve = new Dictionary <int, List <int> >();

            foreach (var taskInfos in sameSpecs)
            {
                var sln = "";
                var id  = 0;
                foreach (var taskInfo in taskInfos.Value)
                {
                    if (sln != "" || taskInfo.Item3 != null && !taskInfo.Item3.Contains("\"resemblance\":1.0"))
                    {
                        continue;
                    }
                    sln = taskInfo.Item3;
                    id  = taskInfo.Item4;
                }
                if (sln == "")
                {
                    continue;
                }
                var list = taskInfos.Value
                           .Where(taskInfo => taskInfo.Item3 == null || !taskInfo.Item3.Contains("\"resemblance\":1.0"))
                           .Select(taskInfo => taskInfo.Item4)
                           .ToList();
                if (list.Count != 0)
                {
                    toSolve[id] = list;
                }
            }

            var client = new ApiClient();

            foreach (var kvp in toSolve)
            {
                if (kvp.Value.Count == 0)
                {
                    continue;
                }
                var sln = pr.FindSolution(kvp.Key);
                if (sln == null)
                {
                    continue;
                }
                foreach (var id in kvp.Value)
                {
                    try
                    {
                        var response = client.PostSolution(id, sln);
                        pr.PutResponse(id, response);
                        pr.PutSolution(id, sln);
                        Console.WriteLine(JObject.Parse(response)["resemblance"].Value <double>());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine();
                        Console.WriteLine(e);
                        Console.WriteLine(0);
                    }
                }