예제 #1
0
        private static void Test()
        {
            int threshold = 400 * 1000 * 1000;

            while (true)
            {
                var           gsc     = new GameServerClient();
                TrainResponse problem = gsc.Train(TrainProblemType.Any, 15);
                Console.Out.WriteLine("==== TrainProblem: {0}", problem);

                Task <int> countTask = Task.Run(() =>
                {
                    IEnumerable <byte[]> trees = new BinaryBruteForcer(problem.operators).Enumerate(problem.size - 1);
                    return(trees.TakeWhile((t, i) => i < threshold).Count());
                });
                Stopwatch sw     = Stopwatch.StartNew();
                string    answer = ConcurrentWithoutShitAlphaProtocol.PostSolution(problem.id, problem.size,
                                                                                   problem.operators);
                sw.Stop();
                Console.Out.WriteLine("==== Solved, waiting for TreeSize...");
                countTask.Wait();
                Console.Out.WriteLine("==== SolvedIn: {0} ms, Answer: {1}, TreeSize: {2}", sw.ElapsedMilliseconds,
                                      answer,
                                      countTask.Result);
            }
        }
예제 #2
0
//        [TestCase(
//            "(lambda (x_16756) (shl1 (fold (plus 0 x_16756) 1 (lambda (x_16757 x_16758) (not (and x_16757 x_16758)))))",
//            12, new[] {"and", "fold", "not", "plus", "shl1"}, 68)]
//        [TestCase(
//            "(lambda (x_19856) (fold (xor (shr16 1) x_19856) x_19856 (lambda (x_19857 x_19858) (not (or x_19858 x_19857)))))"
//            , 12, new[] {"fold", "not", "or", "shr16", "xor"}, 68)]
//        [TestCase(
//            "(lambda (x_13969) (shr16 (shr1 (xor (if0 (and x_13969 (plus x_13969 x_13969)) 1 0) x_13969))))",
//            13, new[] {"and", "if0", "plus", "shr1", "shr16", "xor"}, 9999)]
        public void Test(string function, int size, string[] operations, int equalFormulas)
        {
            Expr formula = Expr.ParseFunction(function);
            var  random  = new Random();

            IEnumerable <byte[]> trees = new BinaryBruteForcer(operations).Enumerate(size - 1);

            ulong[] inputs  = Enumerable.Range(1, 256).Select(e => random.NextUInt64()).ToArray();
            ulong[] outputs = inputs.Select(i => formula.Eval(new Vars(i))).ToArray();
            trees = Guesser.Guess(trees, inputs, outputs);
            Assert.AreEqual(equalFormulas, trees.Count());
        }
예제 #3
0
        public static string PostSolution(string problemId, int size, string[] operations)
        {
            var gsc = new GameServerClient();

            log.DebugFormat("Trying to solve problem {0}...", problemId);
            var random = new Random();
            IEnumerable <byte[]> trees = new BinaryBruteForcer(operations).Enumerate(size - 1);

            ulong[] inputs = InputGenerator.Generate();

            List <ulong> outputs = gsc.Eval(problemId, inputs);

            log.Debug("Eval result for samples received");

            IEnumerable <byte[]> solutions = Guesser.Guesser.Guess(trees, inputs, outputs.ToArray());

            while (true)
            {
                log.Debug("Start find solution");
                byte[] solution = solutions.First();
                log.Debug("First solution finded, asking the guess...");

                string      formula     = String.Format("(lambda (x) {0})", solution.ToSExpr().Item1);
                WrongAnswer wrongAnswer = gsc.Guess(problemId, formula);

                log.Debug("Guess answer received");

                if (wrongAnswer == null)
                {
                    log.DebugFormat("Problem solved!!!. Problem Id: {0}", problemId);
                    return(formula);
                }

                log.Debug(string.Format("WrongAnswer received: {0}", wrongAnswer));

                solutions = AddCase(solutions, wrongAnswer);
            }
        }
예제 #4
0
        public void CuttingTest()
        {
            var            keys   = GenerateFirstSamples();
            ProblemsReader reader = ProblemsReader.CreateDefault();

            for (int i = 8; i < 14; ++i)
            {
                var problems =
                    reader.ReadProblems()
                    .Where(p => p.Size == i)
                    .SelectMany(
                        p => reader.ReadTrainProblems(p, true).Concat(reader.ReadTrainProblems(p, false)).Select(tp => Tuple.Create(p, tp)).Take(2))
                    .Take(10);
                foreach (var problemPair in problems)
                {
                    var tp       = problemPair.Item2;
                    var programm = Expr.ParseFunction(tp.challenge).GetUnified().ToBinExp().ToArray();

                    var values = keys.ToDictionary(k => k, k => programm.Eval(k));
                    Func <byte[], bool> check = tree => values.All(kv => tree.Eval(kv.Key) == kv.Value);

                    var solutions = new BinaryBruteForcer(new Mask(values.Values), tp.operators).Enumerate(tp.size - 1);
                    int count     = 0;
                    int cutted    = 0;
                    foreach (var solution in solutions)
                    {
                        ++count;
                        if (!check(solution))
                        {
                            ++cutted;
                        }
                    }
                    Console.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}", count, cutted, tp.size, tp));
                }
            }
        }