public void Temp([Values(3)] int problemId) { var problem = ProblemSolutionFactory.LoadProblem($"FR{problemId:D3}"); //var assembler = new GreedyPartialSolver(problem.TargetMatrix, new ThrowableHelperFast(problem.TargetMatrix)); //var disassembler = new InvertorDisassembler(new GreedyPartialSolver(problem.SourceMatrix, new ThrowableHelperFast(problem.SourceMatrix)), problem.SourceMatrix); //var solver = new SimpleReassembler(disassembler, assembler); var commonPart = problem.SourceMatrix.Intersect(problem.TargetMatrix); commonPart = new ComponentTrackingMatrix(commonPart).GetGroundedVoxels(); File.WriteAllBytes(Path.Combine(FileHelper.ProblemsDir, "FR666_tgt.mdl"), commonPart.Save()); File.WriteAllBytes(Path.Combine(FileHelper.ProblemsDir, "FR666_src.mdl"), commonPart.Save()); var solver = new GreedyPartialSolver(problem.SourceMatrix, commonPart, new ThrowableHelperFast(commonPart, problem.SourceMatrix)); List <ICommand> commands = new List <ICommand>(); try { foreach (var command in solver.Solve()) { commands.Add(command); } //commands.AddRange(solver.Solve()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e); throw; } finally { Console.WriteLine(commands.Take(5000).ToDelimitedString("\n")); var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, "FR666"), bytes); } }
public void Disassemble() { var problem = ProblemSolutionFactory.LoadProblem("FD120"); //var solver = new InvertorDisassembler(new DivideAndConquer(problem.SourceMatrix, true), problem.SourceMatrix); //var solver = new InvertorDisassembler(new GreedyPartialSolver(problem.SourceMatrix, new Matrix(problem.R), new ThrowableHelperFast(problem.SourceMatrix)), problem.SourceMatrix); var solver = new InvertorDisassembler(new HorizontalSlicer(problem.SourceMatrix, 6, 6, true), problem.SourceMatrix); //var solver = new HorizontalSlicer(problem.SourceMatrix, 6, 6, true); List <ICommand> commands = new List <ICommand>(); try { commands.AddRange(solver.Solve()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e); throw; } finally { var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, problem.Name), bytes); } var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); new Interpreter(state).Run(commands); }
public void DisassembleAndPost([Values(4)] int problemId) { var problem = ProblemSolutionFactory.LoadProblem($"FD{problemId:D3}"); //var solution = ProblemSolutionFactory.blockDeconstructor; var solution = ProblemSolutionFactory.CreateInvertingDisassembler(ProblemSolutionFactory.CreateSlicerAssembler(6, 6)); Evaluate(problem, solution, postToElastic: true); }
public void Test2() { var problem = ProblemSolutionFactory.LoadProblem("FA001"); var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); var plan = new GenPlanBuilder(state).CreateGenPlan(); var sorted = new GenPlanSorter(plan, state.R).Sort(); sorted.ToHashSet().SetEquals(plan).Should().BeTrue(); }
public void AssembleAndPost([Values(11)] int problemId) { var problem = ProblemSolutionFactory.LoadProblem($"FA{problemId:D3}"); var solution = new Solution { Name = "special", Solver = p => { var state = new DeluxeState(p.SourceMatrix, p.TargetMatrix); return(new Solver(state, new AssembleFA11(state))); } }; Evaluate(problem, solution, postToElastic: true); }
public void Test() { var tasks = ProblemSolutionFactory.GetTasks(); int replicaCount = 40; int[] buckets = new int[replicaCount]; foreach (var task in tasks) { //Console.Out.WriteLine(task.Problem.Name); //var b = xxHash.CalculateHash(Encoding.UTF8.GetBytes(task.Problem.Name + task.Solution.Name)) % replicaCount; var b = (uint)(task.Problem.Name + task.Solution.Name).GetHashCode() % replicaCount; buckets[b]++; } foreach (var b in buckets) { Console.Out.WriteLine(b); } }
public void Reassemble([Values(75)] int problemId) { var problem = ProblemSolutionFactory.LoadProblem($"FR{problemId:D3}"); //var assembler = new GreedyPartialSolver(problem.TargetMatrix, new ThrowableHelperFast(problem.TargetMatrix)); //var disassembler = new InvertorDisassembler(new GreedyPartialSolver(problem.SourceMatrix, new ThrowableHelperFast(problem.SourceMatrix)), problem.SourceMatrix); var solver = new SimpleReassembler( new InvertorDisassembler(ProblemSolutionFactory.CreateSlicer6x6(problem.SourceMatrix), problem.SourceMatrix), ProblemSolutionFactory.CreateSlicer6x6(problem.TargetMatrix), problem.SourceMatrix, problem.TargetMatrix ); //var solver = new SmartReassembler( // problem.SourceMatrix, // problem.TargetMatrix, // (s, t) => new InvertorDisassembler(new GreedyPartialSolver(s, t, new ThrowableHelperFast(t, s)), s, t), // (s, t) => new GreedyPartialSolver(t, s, new ThrowableHelperFast(s, t)) // ); List <ICommand> commands = new List <ICommand>(); try { foreach (var command in solver.Solve()) { commands.Add(command); } //commands.AddRange(solver.Solve()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e); throw; } finally { Console.WriteLine(commands.Take(5000).ToDelimitedString("\n")); var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, problem.Name), bytes); } var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); new Interpreter(state).Run(commands); }
//[Timeout(30000)] public void Assemble() { var problem = ProblemSolutionFactory.LoadProblem("FA011"); //var solver = new DivideAndConquer(problem.SourceMatrix, false); var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); var solver = new Solver(state, new AssembleFA11(state)); List <ICommand> commands = new List <ICommand>(); try { commands.AddRange(solver.Solve()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e); throw; } finally { var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, problem.Name), bytes); } }
public void AssembleKung() { var problem = ProblemSolutionFactory.LoadProblem("FA060"); var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); var solver = new Solver(state, new ParallelGredyFill(state, state.Bots.First())); List <ICommand> commands = new List <ICommand>(); try { commands.AddRange(solver.Solve()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {problem.Name}", e); throw; } finally { var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(FileHelper.SolutionsDir, problem.Name), bytes); } Console.Out.WriteLine(state.Energy); }
public void Run(IPluginContext <HoustonRunnerProperties> context) { const string elasticUrl = "http://efk2-elasticsearch9200.efk2.10.217.14.7.xip.io"; const string elasticIndex = "testruns"; var replicaNumber = context.Info.Replica.ReplicaNumber; var replicaCount = context.Info.Replica.ReplicationFactor; if (replicaNumber == 0 || replicaCount == 0) { replicaNumber = 1; replicaCount = 1; } context.Log.Info($"Replica # {replicaNumber} of {replicaCount}: preparing..."); var client = new ElasticClient(new ConnectionSettings(new Uri(elasticUrl)).DisableDirectStreaming().DefaultIndex(elasticIndex)); var tasks = ProblemSolutionFactory.GetTasks() .Where(t => t.Solution.ProblemPrioritizer(t.Problem) != ProblemPriority.DoNotSolve) .ToArray(); var selectedTasks = tasks .Where(task => ((uint)(task.Problem.Name + task.Solution.Name).GetHashCode()) % replicaCount == replicaNumber - 1) .ToArray(); context.Log.Info($"Replica # {replicaNumber} of {replicaCount}: " + $"running {selectedTasks.Length} of {tasks.Length} tasks"); var completeTasksCounter = 0; selectedTasks.OrderBy(st => st.Solution.ProblemPrioritizer(st.Problem)) .ForEach(task => { var solution = task.Solution; var result = new TaskRunMeta { StartedAt = DateTime.UtcNow, TaskName = task.Problem.Name, SolverName = solution.Name, RunningHostName = Environment.MachineName }; context.Log.Info($"Task {result.TaskName} " + $"with solver {result.SolverName} " + $"at {result.RunningHostName}: " + $"starting..."); try { var timer = Stopwatch.StartNew(); var solver = solution.Solver(task.Problem); var commands = new List <ICommand>(); var started = new ManualResetEvent(false); ExceptionDispatchInfo exceptionDispatchInfo = null; //task.Problem.Type var total = (task.Problem.TargetMatrix?.Weight + task.Problem.SourceMatrix?.Weight) ?? 0; int done = 0; var timeout = Stopwatch.StartNew(); var runThread = new Thread(() => { try { var solverCommands = solver.Solve(); foreach (var command in solverCommands) { started.Set(); commands.Add(command); if (command is Fill) { Interlocked.Increment(ref done); } } } catch (Exception exception) { exceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception); started.Set(); } }); runThread.Start(); if (!started.WaitOne(context.Properties.SolverStartTimeout)) { runThread.Abort(); runThread.Join(); throw new TimeoutException("Solve start timeout expired"); } while (!runThread.Join(context.Properties.SolverTimeoutMeasureInterval)) { var localDone = Interlocked.CompareExchange(ref done, 0, 0); if (localDone == 0) { throw new TimeoutException("Solver didn't fill any cells"); } var estimatedTotalTime = TimeSpan.FromTicks(timeout.Elapsed.Ticks * total / localDone); if (estimatedTotalTime > context.Properties.SolverTimeout) { throw new TimeoutException($"Solver total time estimation {estimatedTotalTime} exceeds limit {context.Properties.SolverTimeout}"); } } exceptionDispatchInfo?.Throw(); var state = new DeluxeState(task.Problem.SourceMatrix, task.Problem.TargetMatrix); new Interpreter(state).Run(commands); result.SecondsSpent = (int)timer.Elapsed.TotalSeconds; result.EnergySpent = state.Energy; //result.EnergyHistory = state.EnergyHistory; result.Solution = CommandSerializer.Save(commands.ToArray()).SerializeSolutionToString(); result.IsSuccess = true; } catch (Exception e) { context.Log.Warn($"Unhandled exception in solver for {task.Problem.FileName}"); result.IsSuccess = false; result.ExceptionInfo = $"{e.Message}\n{e.StackTrace}"; } context.Log.Info($"Task {result.TaskName} " + $"with solver {result.SolverName} " + $"at {result.RunningHostName}: " + $"completed in {result.SecondsSpent}s"); completeTasksCounter++; context.Log.Info($"Tasks complete: {completeTasksCounter} of {selectedTasks.Length} for this worker"); var indexingResult = client.IndexDocument(result); var tryCount = 1; while (!indexingResult.IsValid && tryCount < 10) { context.Log.Warn($"Failed to insert task {result.TaskName} into Elastic on {tryCount} try (success was {result.IsSuccess})"); context.Log.Warn(indexingResult.DebugInformation); Thread.Sleep(TimeSpan.FromSeconds(10)); indexingResult = client.IndexDocument(result); tryCount++; } if (!indexingResult.IsValid) { context.Log.Error($"TOTALLY FAILED to insert task {result.TaskName} into Elastic on {tryCount} try (success was {result.IsSuccess})"); context.Log.Error(indexingResult.DebugInformation); } }); context.Log.Info("Sleeping forever, all tasks done"); Thread.Sleep(int.MaxValue); }
public TraceResult Trace(string file, int startTick = 0, int count = 2000) { var problemName = file.Split("-")[0]; var problem = ProblemSolutionFactory.CreateProblem($"../data/problemsF/{problemName}_tgt.mdl"); var solution = CommandSerializer.Load(System.IO.File.ReadAllBytes($"../data/solutions/{problemName}.nbt")); var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); var queue = new Queue <ICommand>(solution); var results = new List <TickResult>(); var filledVoxels = new HashSet <Vec>(state.GetFilledVoxels()); var tickIndex = 0; try { var newFilledVoxels = new List <Vec>(state.GetFilledVoxels()); var newClearedVoxels = new List <Vec>(); var interpreter = new Interpreter(state); while (queue.Any() && tickIndex < startTick + count) { interpreter.Tick(queue); foreach (var vec in interpreter.LastChangedCells) { if (state.Matrix[vec]) { if (!filledVoxels.Contains(vec)) { newFilledVoxels.Add(vec); filledVoxels.Add(vec); } } else if (filledVoxels.Contains(vec)) { newClearedVoxels.Add(vec); filledVoxels.Remove(vec); } } if (tickIndex >= startTick) { results.Add(new TickResult { filled = newFilledVoxels.Select(v => new[] { v.X, v.Y, v.Z }).ToArray(), cleared = newClearedVoxels.Select(v => new[] { v.X, v.Y, v.Z }).ToArray(), bots = state.Bots .Select(x => new[] { x.Position.X, x.Position.Y, x.Position.Z }) .ToArray(), tickIndex = tickIndex }); newFilledVoxels.Clear(); } tickIndex++; } } catch (Exception e) { var arr = results.ToArray(); return(new TraceResult { Exception = e.ToString(), R = problem.R, startTick = startTick, totalTicks = arr.Length - 1, Ticks = arr }); } var ticks = results.ToArray(); return(new TraceResult { R = problem.R, startTick = startTick, totalTicks = ticks.Length - 1, Ticks = ticks }); }