限制我的方法的执行时间

时间:2017-08-31 14:15:41

标签: java sudoku

以下是我的 Sudoku的强力代码

Traceback (most recent call last):
  File "setup.py", line 88, in <module>
    setup(**configuration(top_path='').todict())
  File "setup.py", line 82, in configuration
    maybe_cythonize_extensions(top_path, config)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/_build_utils/__init__.py", line 84, in maybe_cythonize_extensions
    config.ext_modules = cythonize(config.ext_modules)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 908, in cythonize
    aliases=aliases)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 778, in create_extension_list
    for file in nonempty(sorted(extended_iglob(filepattern)), "'%s' doesn't match any files" % filepattern):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 124, in nonempty
    raise ValueError(error_msg)
ValueError: '_check_build.pyx' doesn't match any files

要求是将public abstract class SudokuBoard { protected int ROWS = 9; protected int COLS = 9; int solutionsCounter; double startTime; double endTime; String[] data = new String[8]; int puzzleNum = countTotalRows(); // data accessors public abstract int get(int r, int c); public abstract void set(int r, int c, int v); // specific constraints checker, returns true even if the values are not complete abstract boolean isRowCompatible(int r, int c); abstract boolean isColCompatible(int r, int c); abstract boolean isBoxCompatible(int r, int c); // returns true if element S[r,c] is compatible, even if some values arount it are not filled public boolean isCompatible(int r, int c) { for (int i=0; i<ROWS; i++) for (int j=0; j<COLS; j++) if(! (isRowCompatible(r, c) && isColCompatible(r, c) && isBoxCompatible(r, c))) return false; return true; } // this is the one called to solve the sudoku public void solve() { //convert to seconds startTime = System.nanoTime() / 1000000000.0; solve(1,1); } // function to incorporate clues public void incorporateClues(int[] clues) { for (int i=0; i<clues.length; i++) set(clues[i]/100, (clues[i]%100)/10, clues[i]%10); } // the recursive backtracking function that does the hardwork void solve(int r, int c) { while (((System.nanoTime() / 1000000000.0) - startTime) < 10) { System.out.println("Time: " + ((System.nanoTime() / 1000000000.0) - startTime)); if (r<=9 && c<=9) { if (get(r,c) == 0) { for (int v=1; v<=COLS; v++) { set(r,c,v); if (isCompatible(r,c)) solve((c==9)?(r+1):r, (c==9)?1:(c+1)); } set(r, c, 0); } else solve((c==9)?(r+1):r, (c==9)?1:(c+1)); } else { solutionsCounter = solutionsCounter + 1; //convert to seconds endTime = System.nanoTime() / 1000000000.0; // print(); } } } // sample display function void print() { for(int i=1; i<=ROWS; i++) { for (int j=1; j<=COLS; j++) System.out.print(get(i,j)); System.out.println(); } System.out.println("count: " + solutionsCounter); } void saveData (String[] data) throws java.io.IOException { try { java.io.BufferedWriter outfile = new java.io.BufferedWriter(new java.io.FileWriter("15-clue_results.csv", true)); for (int i = 0; i < data.length; i++) { outfile.write(String.valueOf(data[i])); outfile.append(','); } outfile.append('\n'); outfile.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } static int countTotalRows () { int count = 0; try { java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.FileReader("15-clue_results.csv")); String input; while((input = bufferedReader.readLine()) != null) { count = count + 1; } } catch (java.io.IOException e) { e.printStackTrace(); } return count; } public static void main(String []arg) { int numClues; try { java.io.BufferedReader csvFile = new java.io.BufferedReader(new java.io.FileReader("clue_set")); String dataRow; while ((dataRow = csvFile.readLine()) != null) { SudokuBoard board = new SB_IntMatrix(); String[] stringSet = new String[15]; int[] PUZZLE1 = new int[15]; board.puzzleNum = board.puzzleNum + 1; stringSet = dataRow.split(" "); for (int i = 0; i < stringSet.length; i++) { PUZZLE1[i] = Integer.parseInt(stringSet[i]); } board.incorporateClues(PUZZLE1); for (int i = 0; i < 1; i++) { board.solutionsCounter = 0; board.solve(); board.data[0] = Integer.toString(board.puzzleNum); board.data[1] = dataRow; board.data[2] = Integer.toString(board.solutionsCounter); board.data[3 + i] = Double.toString(board.endTime - board.startTime); } try { board.saveData(board.data); } catch (java.io.IOException e) { e.printStackTrace(); } } csvFile.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } } 的解决时间限制为仅1小时。

为此,我尝试将其置于while循环solve(int r, int c)内。数字10只是测试代码。

据我所知,我在main方法中只循环了5次但是,它总是重置为0并且永远不会停止并超过我在main中的循环限制。

2 个答案:

答案 0 :(得分:2)

您应该使用Future

final ExecutorService executor = Executors.newFixedThreadPool(4);

final Future<Boolean> future = executor.submit(() -> {
    // Call solve here
    return true;
});

future.get(60, TimeUnit.MINUTES); // Blocks

答案 1 :(得分:1)

您可以执行以下操作: 初始化开始日期:

LocalDateTime startDateTime = LocalDateTime.now();

并检查是否已经过了1小时:

LocalDateTime toDateTime = LocalDateTime.now();
if (Duration.between(startDateTime, toDateTime).toHours() > 0) {
// stop the execution
}