Esempio n. 1
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. 2
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);
            }
        }
Esempio n. 3
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);
                    }
                }