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); } }
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(); } } }
public Solver() { rnd = new Random(); gsc = new GameServerClient(); args = new ulong[] { 0, 1, ulong.MaxValue, ulong.MinValue }.Concat(Enumerable.Range(1, 256).Select(e => rnd.NextUInt64())).Take(256).ToList(); }