示例#1
0
        public string Solve(string problemId, int size, string[] ops, Func <List <ulong>, List <ulong>, IEnumerable <byte[]> > getGuesses)
        {
            log.DebugFormat("Solving problem {0}: size={1}, ops={2}", problemId, size, string.Join(", ", ops));

            var wallTime      = Stopwatch.StartNew();
            var correctValues = WithTimer(() => gsc.Eval(problemId, args), "gsc.Eval()");

            var nextGuessSw = Stopwatch.StartNew();
            int c           = 0;

            foreach (var guess in getGuesses(args, correctValues))
            {
                c++;
                if (c > 10000000 && c < 10000000 + 1000)
                {
                    Console.WriteLine(guess.Printable());
                }
                if (!IsGood(guess, correctValues))
                {
                    continue;
                }
                log.DebugFormat("findNextGuess() took: {0} ms, WS: {1}", nextGuessSw.ElapsedMilliseconds, Environment.WorkingSet);

                var program     = FormatProgram(guess);
                var wrongAnswer = WithTimer(() => gsc.Guess(problemId, program), "gsc.Guess()");
                if (wrongAnswer == null)
                {
                    log.DebugFormat("SOLVED: {0}, TimeTaken: {1} ms, WS: {2}", problemId, wallTime.ElapsedMilliseconds, Environment.WorkingSet);
                    return(program);
                }
                log.DebugFormat("WRONG_ANSWER: {0}, WS: {1}", wrongAnswer, Environment.WorkingSet);
                args.Add(wrongAnswer.Arg);
                correctValues.Add(wrongAnswer.CorrectValue);

                if (wallTime.Elapsed.TotalSeconds > 300)
                {
                    log.DebugFormat("TIME_LIMIT: {0}, WS: {1}", wallTime.ElapsedMilliseconds, Environment.WorkingSet);

                    /*if (wallTime.Elapsed.TotalSeconds > 500)
                     *      return null;*/
                }

                nextGuessSw.Reset();
            }

            log.DebugFormat("SOLUTION_NOT_FOUND: {0}, WS: {1}", wallTime.ElapsedMilliseconds, Environment.WorkingSet);
            return(null);
        }
示例#2
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);
            }
        }
示例#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();

            List <ulong> inputs  = Enumerable.Range(1, 256).Select(e => random.NextUInt64()).ToList();
            List <ulong> outputs = gsc.Eval(problemId, inputs).ToList();

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

            IEnumerable <byte[]> trees = new SmartGenerator(inputs, outputs, operations).Enumerate(size - 1, size - 1, false);

            int tasksCount = 4;
            IEnumerable <byte[][]>   chunckedTrees          = Chuncked(trees, 1 * 1024 * 1024);
            IEnumerable <byte[][][]> chunckedTreesPerWorker = Chuncked(chunckedTrees, tasksCount);
            var results = new byte[0][];
            IEnumerator <byte[][][]> enumerator = chunckedTreesPerWorker.GetEnumerator();

            enumerator.MoveNext();

            var sw = Stopwatch.StartNew();

            while (true)
            {
                if (sw.Elapsed.TotalSeconds > 400)
                {
                    throw new InvalidOperationException(string.Format("TIME_LIMIT: {0} sec", sw.Elapsed.TotalSeconds));
                }

                byte[][][] treesPerWorkerChunk = enumerator.Current;
                var        tasks = new List <Task <byte[][]> >(tasksCount);
                log.Debug("Starting creating tasks");
                foreach (var treesChunk in treesPerWorkerChunk)
                {
                    byte[][]        treeToCheck = treesChunk;
                    Task <byte[][]> task        = Task.Factory.StartNew(() => FilterTrees(treeToCheck, inputs, outputs));
                    tasks.Add(task);
                }
                log.Debug("Finished created tasks and starts new chunk");
                enumerator.MoveNext();
                log.Debug("Finished new chunk");
                Task.WaitAll(tasks.ToArray <Task>());

                List <byte[][]> tasksResults = Results(tasks);
                log.DebugFormat("All tasks completed, {0} results", results.Length);
                byte[] solution;
                while ((solution = GetSolution(tasksResults)) != null)
                {
                    log.Debug("First solution finded, asking the guess...");
                    var         s           = solution.SkipWhile(b => b == 16).ToArray();
                    string      formula     = String.Format("(lambda (x) {0})", s.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));
                    inputs       = inputs.Concat(new[] { wrongAnswer.Arg }).ToList();
                    outputs      = outputs.Concat(new[] { wrongAnswer.CorrectValue }).ToList();
                    tasksResults =
                        tasksResults.Select(
                            r =>
                            FilterTrees(r, new List <ulong> {
                        wrongAnswer.Arg
                    }, new List <ulong> {
                        wrongAnswer.CorrectValue
                    }))
                        .ToList();
                }
            }
        }