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); }
// old code from the past private void DumpNoPath(Vec target) { var all = whatToFill.Cast <bool>().Count(b => b); var done = Commands.Count(c => c is Fill); var bytes = CommandSerializer.Save(Commands.ToArray()); File.WriteAllBytes($@"c:\temp\020_.nbt", bytes); var s = ""; for (int y = 0; y < R; y++) { for (int z = 0; z < R; z++) { for (int x = R - 1; x >= 0; x--) { s += state.Get(new Vec(x, y, z)) ? "X" : "."; } s += "\r\n"; } s += "===r\n"; } File.WriteAllText(@"c:\1.txt", s); throw new InvalidOperationException($"Couldn't find path from {pos} to {target}; all={all}; done={done}; Commands.Count={Commands.Count}; {string.Join("; ", Commands.Take(20))}"); }
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 TestOnSamleTraces([ValueSource(nameof(GetModels))] string filename) { var content = File.ReadAllBytes(filename); var commands = CommandSerializer.Load(content); var newContent = CommandSerializer.Save(commands); Assert.AreEqual(content, newContent); }
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); }
public void Solve() { var problemsDir = FileHelper.ProblemsDir; var resultsDir = FileHelper.SolutionsDir; var allProblems = Directory.EnumerateFiles(problemsDir, "*.mdl"); var problems = allProblems.Select(p => { var matrix = Matrix.Load(File.ReadAllBytes(p)); return(new { m = matrix, p, weight = matrix.Voxels.Cast <bool>().Count(b => b) }); }).Take(10).ToList(); problems.Sort((p1, p2) => p1.weight.CompareTo(p2.weight)); Log.For(this).Info(string.Join("\r\n", problems.Select(p => p.p))); Parallel.ForEach(problems, p => { var R = p.m.R; //var solver = new GreedyPartialSolver(p.m.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(p.m)); var solver = new GreedyPartialSolver(p.m.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelperFast(p.m)); //var solver = new DivideAndConquer(p.m); //var solverName = "div-n-conq"; var solverName = "greedy-fst"; ICommand[] commands = null; try { commands = solver.Solve().ToArray(); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {Path.GetFileName(p.p)}", e); return; } var solutionEnergy = GetSolutionEnergy(p.m, commands, p.p); var bytes = CommandSerializer.Save(commands); File.WriteAllBytes(GetSolutionPath(resultsDir, p.p, solverName, solutionEnergy), bytes); }); }
public TaskRunMeta Run(Problem problem, Solution solution) { var result = new TaskRunMeta { StartedAt = DateTime.UtcNow, TaskName = problem.Name, SolverName = solution.Name, }; var timer = Stopwatch.StartNew(); var solver = solution.Solver(problem); var commands = solver.Solve().ToArray(); var state = new DeluxeState(problem.SourceMatrix, problem.TargetMatrix); new Interpreter(state).Run(commands); result.SecondsSpent = (int)timer.Elapsed.TotalSeconds; result.EnergySpent = state.Energy; result.Solution = CommandSerializer.Save(commands).SerializeSolutionToString(); result.IsSuccess = true; return(result); }
//[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); }
//[Timeout(30000)] public void SolveOne( [Values(122)] int problemId //[ValueSource(nameof(Problems))] int problemId ) { var problemsDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../../data/problemsF"); var resultsDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../../data/solutions"); var problemFile = Path.Combine(problemsDir, $"FD{problemId.ToString().PadLeft(3, '0')}_src.mdl"); var matrix = Matrix.Load(File.ReadAllBytes(problemFile)); var R = matrix.R; //var solver = new GreedyPartialSolver(matrix.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(matrix), new BottomToTopBuildingAround()); var solver = new GreedyPartialSolver(matrix.Voxels, new bool[R, R, R], new Vec(0, 0, 0), new ThrowableHelper(matrix), new NearToFarBottomToTopBuildingAround()); //var solver = new DivideAndConquer(matrix, true); List <ICommand> commands = new List <ICommand>(); try { var sw = Stopwatch.StartNew(); commands.AddRange(solver.Solve().TakeWhile(x => sw.Elapsed.TotalSeconds < 20)); Console.WriteLine(GreedyPartialSolver.candidatesCount.ToDetailedString()); } catch (Exception e) { Log.For(this).Error($"Unhandled exception in solver for {Path.GetFileName(problemFile)}", e); throw; } finally { var bytes = CommandSerializer.Save(commands.ToArray()); File.WriteAllBytes(GetSolutionPath(resultsDir, problemFile), bytes); } var solutionEnergy = GetSolutionEnergy(matrix, commands.ToArray(), problemFile); Console.WriteLine(solutionEnergy); Console.WriteLine(ThrowableHelper.opt.ToDetailedString()); }
public void DoRealTest(Func <Matrix, IAmSolver> solverFactory, Func <IAmSolver, List <ICommand>, Matrix, long> energyFactory, string scopeName, string algoName, [NotNull] string filename) { var sw = Stopwatch.StartNew(); var startTime = DateTime.Now; Console.WriteLine(filename); var content = File.ReadAllBytes(filename); var model = Matrix.Load(content); var solver = solverFactory(model); var shortname = Path.GetFileNameWithoutExtension(filename); List <ICommand> commands = new List <ICommand>(); long energy; var targetDirectory = "failed"; try { energy = energyFactory(solver, commands, model); targetDirectory = "res"; } finally { File.WriteAllBytes($"C:\\workspace\\icfpc\\{targetDirectory}\\{shortname}-test.nbt", CommandSerializer.Save(commands.ToArray())); } sw.Stop(); if (targetDirectory == "failed") { return; } var testResult = new ElasticTestResult { TestName = shortname, TimeSpent = sw.Elapsed, Energy = energy, StartTime = startTime, AlgoVersion = algoName, ScopeName = scopeName, }; const string elasticUrl = "http://efk2-elasticsearch9200.efk2.10.217.14.7.xip.io"; var client = new ElasticClient(new ConnectionSettings(new Uri(elasticUrl)).DisableDirectStreaming().DefaultMappingFor <ElasticTestResult>(x => x.IndexName("localrunresults"))); client.IndexDocument(testResult); var searchResponse = client.Search <ElasticTestResult>(s => s.Size(10000).Query(q => q.Bool(b => b.Filter(bs => bs.Term(t => t.Field("testName.keyword").Value(shortname)))))); var results = (searchResponse?.Documents?.ToList() ?? new List <ElasticTestResult>()).Where(d => d.ScopeName == scopeName).ToList(); var minEnergyRes = results.OrderBy(x => x.Energy).FirstOrDefault() ?? testResult; var a = results.Where(x => x.AlgoVersion != testResult.AlgoVersion).OrderBy(x => x.Energy).FirstOrDefault(); if (minEnergyRes.Energy < energy) { Assert.Warn($"Not the best energy ({minEnergyRes.Energy} < {energy} in {minEnergyRes.AlgoVersion})"); } else if (a?.Energy > energy) { Assert.Pass($"New best energy ({a.Energy * 1.0 / energy:##.00} : {a.Energy} > {energy} - prev in {a.AlgoVersion})"); } Console.WriteLine($"Energy: {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); }