private void writeResultToFile(GridModel grid, int targetScore) { if (resultFileName != null) { try { SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); PrintWriter writer = new PrintWriter(new FileWriter(resultFileName)); writer.println("#"); writer.println( String.format( "# Grid format: %dx%dx%d", grid.getSize(), grid.getSize(), grid.getPatternStats().getPatterns().size())); writer.println(String.format("# Grid score: %d/%d", bestScore, targetScore)); writer.println("# Time: " + df.format(new Date())); writer.println("# Elapsed: " + durationToString(getElapsedTime())); writer.println("# Host: " + InetAddress.getLocalHost().getHostName()); writer.println("# Thread: " + Thread.currentThread().getName()); writer.println("#"); writer.println(); writer.println(bestSolution.toQuadString()); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
public boolean submitSolution(GridModel grid, int gridScore) { int targetScore = grid.countConnections(); synchronized (this) { // Computes stats periodically if (stats != null) { stats.recordScore(gridScore); } // Register solution if better than the current best if (bestScore < gridScore) { grid.copyTo(bestSolution); bestScore = gridScore; logMessage(String.format("Best score: %3d/%d", bestScore, targetScore)); writeResultToFile(grid, targetScore); notifyListeners(bestScore); } } if (!solutionFound && gridScore == targetScore) { solutionFound = true; logMessage("Solution found !"); } return solutionFound; }
@Test public void testSolving() { GridModel grid = new GridModel(4); GridFiller filler = new GridFiller(grid); filler.fillRandom(grid.getSize() * 3 / 2); grid.shuffle(); ClusterManager cluster = new ClusterManager(new NullLog()); GridModel solutionGrid = new GridModel(); AStarSolverMkI solver = new AStarSolverMkI(grid, solutionGrid, cluster); solver.run(); assertTrue(solutionGrid.isComplete()); }
public void actionPerformed(ActionEvent e) { gridModel.reset(); }
private static void computeStats( List<String> solvers, List<String> paths, int samples, int sizeMin, int sizeMax) throws InterruptedException, ExecutionException { // Grid cpuGrid = GridFactory.start(); for (int gridsize = sizeMin; gridsize <= sizeMax; gridsize++) { int patterns = (int) (gridsize * 1.5); Collector reference = null; for (String solverName : solvers) { List<String> pathsQueue = new ArrayList<String>(); if (SolverFactory.isSolverPathSensitive(solverName)) { pathsQueue.addAll(paths); } else { pathsQueue.add(null); } for (String pathName : pathsQueue) { String collectorName = computeCollectorName(solverName, pathName, gridsize, patterns); Collector collector = new Collector(collectorName, reference); if (reference == null) { reference = collector; } // ExecutorService executor = // cpuGrid.newGridExecutorService(); ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); Set<Future<Collector>> collectors = new LinkedHashSet<Future<Collector>>(); for (int sample = 0; sample < samples; sample++) { GridModel grid = new GridModel(gridsize); GridFiller filler = new GridFiller(grid); filler.fillRandom(patterns); grid.shuffle(); SolverMeter solverMeter = new SolverMeter(solverName, pathName, grid); collectors.add(executor.submit(solverMeter)); } for (Future<Collector> futureCollector : collectors) { collector.recordExecutions(futureCollector.get()); } executor.shutdown(); while (!executor.isTerminated()) { try { executor.awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e) { // Do nothing, keep looping } } collector.displayResults(); } } System.out.println(); } }
public boolean submitSolution(GridModel grid) { int gridScore = grid.countPairs(); return submitSolution(grid, gridScore); }
public void actionPerformed(ActionEvent e) { StringSelection stringSelection = new StringSelection(gridModel.toQuadString()); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(stringSelection, this); }