diff --git a/Algo/.DS_Store b/Algo/.DS_Store index 408f5f8f500df78a07c64f36a57b28ec7327f156..a955722a6808fc2b6ca148a91048dd649c5d034c 100644 Binary files a/Algo/.DS_Store and b/Algo/.DS_Store differ diff --git a/Algo/Serie1/.DS_Store b/Algo/Serie1/.DS_Store index 03e249df7848bab8655577c2a5977c362d775cf2..fee490bd384c59351bfe3d4534d22e0a7762d6df 100644 Binary files a/Algo/Serie1/.DS_Store and b/Algo/Serie1/.DS_Store differ diff --git a/Algo/Serie1/Puzzle.java b/Algo/Serie1/Puzzle.java index 5989d3fb78e6e24dc80d280c6012fa9c3dc8a00a..776005712cfa1e5c11118a506bd24de6c3769ed0 100644 --- a/Algo/Serie1/Puzzle.java +++ b/Algo/Serie1/Puzzle.java @@ -217,6 +217,4 @@ public class Puzzle { } return this; } -} - - +} \ No newline at end of file diff --git a/Algo/Serie1/main.java b/Algo/Serie1/main.java index 47b66f9ddd5d46f6ac8a4badd0ee36a64b02decf..983c9e9e1ab66d5c8265e83fb367bb3e756ee636 100644 --- a/Algo/Serie1/main.java +++ b/Algo/Serie1/main.java @@ -221,28 +221,20 @@ public class Main { showPath(heuristicSearchByPlacement(proftest, finalState)); showPath(heuristicSearchByDistance(proftest, finalState)); showPath(improvedBlindSearch(proftest, finalState)); - }else{ Puzzle toTest = new Puzzle(finalState, null); for(int i = 0; i<moves; i++){ - double rnd = Math.random(); - if(rnd < 0.25) - toTest = toTest.moveUp(); - else if(rnd < 0.5) - toTest = toTest.moveRight(); - else if(rnd < 0.75) - toTest = toTest.moveDown(); - else - toTest = toTest.moveLeft(); - } - System.out.println("Test for "+ moves +" moves"); - if(moves<13) + toTest.moveInNewPositions(); + toTest = toTest.getNexts().get(0); + System.out.println("Test for "+ (i+1) +" moves"); + if(i<13) blindSearch(toTest.getState(), finalState); - if(moves<20) + if(i<20) improvedBlindSearch(toTest.getState(), finalState); heuristicSearchByDistance(toTest.getState(), finalState); heuristicSearchByPlacement(toTest.getState(), finalState); System.out.println(); + } } } } \ No newline at end of file diff --git a/Algo/Serie2/.DS_Store b/Algo/Serie2/.DS_Store index c28d982446d53523d5bac83e5c8302f5f72c7720..c2487b74ed11c662cfc928962570450f55750ece 100644 Binary files a/Algo/Serie2/.DS_Store and b/Algo/Serie2/.DS_Store differ diff --git a/Algo/Serie2/Counter.java b/Algo/Serie2/Counter.java new file mode 100644 index 0000000000000000000000000000000000000000..7f6be50daaab1a81a9b743ea5044e93fd81ee13e --- /dev/null +++ b/Algo/Serie2/Counter.java @@ -0,0 +1,17 @@ + +public class Counter { + int count; + + public Counter(){ + this.count = 0; + } + + public int getCount(){ return this.count; } + + public void increment(){ this.count += 1; } + + public void decrement(){ this.count -=1; } + + public void reset(){ this.count = 0; } + +} \ No newline at end of file diff --git a/Algo/Serie2/Main.class b/Algo/Serie2/Main.class deleted file mode 100644 index 23fe0514432daa4348517433722f69a742e586e7..0000000000000000000000000000000000000000 Binary files a/Algo/Serie2/Main.class and /dev/null differ diff --git a/Algo/Serie2/Sudoku.class b/Algo/Serie2/Sudoku.class deleted file mode 100644 index 16c60d8f656cc1773b09755e037cf1aeffdbe421..0000000000000000000000000000000000000000 Binary files a/Algo/Serie2/Sudoku.class and /dev/null differ diff --git a/Algo/Serie2/Sudoku.java b/Algo/Serie2/Sudoku.java index c61cb718328bab52ce64b12090244f4694eb5ec7..d8b5af257c86a93031a42480478f459ef327b0c1 100644 --- a/Algo/Serie2/Sudoku.java +++ b/Algo/Serie2/Sudoku.java @@ -9,98 +9,130 @@ public class Sudoku { } /** - * Solves the sudoku first by reading order, then with a constraint heurisctic which shows to be slightly more efficient + * handles the output and the time mesure of the process + * calls the function that solves the sudoku by reading order */ public void solve(){ - int[][] tmp = copyTab(this.tab); + Counter backtracksCounter = new Counter(); + Counter assignementsBeforeBacktrackCounter = new Counter(); System.out.println("initial state : "); this.print(); System.out.println("Solving in reading order..."); long startTimer = System.currentTimeMillis(); - trackSolution(getBlankSpaces(), 0, 0, 0); - System.out.println("time: " +((double)(System.currentTimeMillis()-startTimer))/1000 +"s"); + trackSolution(getBlankSpaces(), 0, backtracksCounter, assignementsBeforeBacktrackCounter); + System.out.println("Solution found in "+((double)(System.currentTimeMillis()-startTimer))/1000 +"s ! + Assigements before backtrack: " + + assignementsBeforeBacktrackCounter.getCount()+" | Tracks: "+backtracksCounter.getCount()); + this.print(); + } + + /** + * handles the output and the time mesure of the process + * calls the function that solves the sudoku jumping to the most constraint case (the one with less possibilities) + */ + public void smartSolve(){ + Counter backtracksCounter = new Counter(); + Counter assignementsBeforeBacktrackCounter = new Counter(); + System.out.println("initial state : "); this.print(); - this.tab = tmp; System.out.println("Solving by most constraint cases order..."); - startTimer = System.currentTimeMillis(); - trackSolution(getBlankSpacesByMostConstraint(), 0, 0, 0); - System.out.println("time: " +((double)(System.currentTimeMillis()-startTimer))/1000 +"s"); + long startTimer = System.currentTimeMillis(); + int[] mostConstraintCell = getMostConstraintCell(); + trackSolutionButSmart(mostConstraintCell[1], mostConstraintCell[0], backtracksCounter, assignementsBeforeBacktrackCounter); + System.out.println("Solution found in " +((double)(System.currentTimeMillis()-startTimer))/1000 +"s ! Assigements before backtrack: " + + assignementsBeforeBacktrackCounter.getCount()+ " | Tracks: "+backtracksCounter.getCount()); this.print(); } /** - * finds a solution to the sudoku by backtracking + * finds a solution to the sudoku by trying all the solutions backtracking */ - private boolean trackSolution(ArrayList<int[]> list, int index, int assignmentCount ,int trackCount){ + private boolean trackSolution(ArrayList<int[]> list, int index, Counter backtracksCount, Counter assignementsBeforeBacktrackCount){ + int[] cell = list.get(index); + //tries playing all possibilities from 1 to the size of the tab (9) for(int i = 1; i<=this.tab.length; i++){ - if(trackCount == 0) - assignmentCount++; - if(play(i, list.get(index)[0],list.get(index)[1])){ - if(index==list.size()-1){ - System.out.println("Solution found! Assigements before backtrack: "+assignmentCount+" | Tracks: "+trackCount); + //if the play is possible + if(play(i, cell[0],cell[1])){ + //counts the number of assignements before the first backtrack + if(backtracksCount.getCount()==0) + assignementsBeforeBacktrackCount.increment(); + //are there still blank spaces to fill + if(getBlankSpaces().size() == 0) return true; - } else{ - if(trackSolution(list, index+1, assignmentCount, trackCount)) + //calls the function again with the next case to play in + if(trackSolution(list, index+1, backtracksCount, assignementsBeforeBacktrackCount)) return true; else{ - trackCount++; + //if the program must go back, deletes the changes, and increments the backtrack counts + backtracksCount.increment(); play(0, list.get(index)[0],list.get(index)[1]); } } } } + //if there was no solution + return false; + } + + private boolean trackSolutionButSmart(int x, int y, Counter backtracksCount, Counter assignementsBeforeBacktrackCount){ + ArrayList<Integer> possibilities = getConstraint(x,y); + //tries playing all the possible plays for this cell + for(int i = 0; i<possibilities.size(); i++){ + //plays the possibility at the index given + play(possibilities.get(i), y, x); + //counts the assignements before the first backtrack + if(backtracksCount.getCount()==0) + assignementsBeforeBacktrackCount.increment(); + //are there still blank spaces to fill + if(getBlankSpaces().size()== 0) + return true; + else{ + //calls the function again for the most constraint cell in the sudoku + int[] mostConstraintCell = getMostConstraintCell(); + if(trackSolutionButSmart(mostConstraintCell[1], mostConstraintCell[0], backtracksCount, assignementsBeforeBacktrackCount)) + return true; + else{ + //if the program must go back, deletes the changes and increments the backtrack counts + backtracksCount.increment(); + play(0, y, x); + } + } + } return false; } /** - * gets a list of indexes of blanspaces in the sudoku + * gets the index of the most constraint cell */ - private ArrayList<int[]> getBlankSpaces(){ - ArrayList<int[]> blankSpaces = new ArrayList(); - int[] blankSpaceIndex; - for(int i = 0; i<this.tab.length; i++){ - for(int j = 0; j<this.tab.length; j++){ - if(this.tab[j][i] == 0){ - blankSpaceIndex = new int[2]; - blankSpaceIndex[0] = i; - blankSpaceIndex[1] = j; - blankSpaces.add(blankSpaceIndex); - } + private int[] getMostConstraintCell(){ + ArrayList<int[]> list = getBlankSpaces(); + int[] mostConstraintCell = list.get(0); + int constraint = getConstraint(list.get(0)[1],list.get(0)[0]).size(); + for(int i = 1; i<list.size(); i++){ + int x = getConstraint(list.get(i)[1], list.get(i)[0]).size(); + if(x < constraint){ + constraint = x; + mostConstraintCell = list.get(i); } } - return blankSpaces; + return mostConstraintCell; } /** - * gets a list of indexes of blankspaces in the sudoku in the order of constraints + * gets a list of indexes of blanspaces in the sudoku */ - private ArrayList<int[]> getBlankSpacesByMostConstraint(){ + private ArrayList<int[]> getBlankSpaces(){ ArrayList<int[]> blankSpaces = new ArrayList(); int[] blankSpaceIndex; for(int i = 0; i<this.tab.length; i++){ for(int j = 0; j<this.tab.length; j++){ if(this.tab[j][i] == 0){ - blankSpaceIndex = new int[3]; + blankSpaceIndex = new int[2]; blankSpaceIndex[0] = i; blankSpaceIndex[1] = j; - blankSpaceIndex[2] = getConstraint(i, j); - if(blankSpaces.size() == 0) - blankSpaces.add(blankSpaceIndex); - else{ - for(int k=0; k<blankSpaces.size(); k++){ - if(blankSpaceIndex[2]<blankSpaces.get(k)[2]){ - blankSpaces.add(k, blankSpaceIndex); - break; - } - if(k == blankSpaces.size()-1){ - blankSpaces.add(blankSpaceIndex); - break; - } - } - } + blankSpaces.add(blankSpaceIndex); } - } + } } return blankSpaces; } @@ -108,7 +140,7 @@ public class Sudoku { /** * gets the constraint level of a case */ - public int getConstraint(int x, int y){ + public ArrayList<Integer> getConstraint(int x, int y){ ArrayList<Integer> possibilities = new ArrayList<>(); for(int i = 1; i<= this.tab.length; i++){ possibilities.add(i); @@ -123,7 +155,7 @@ public class Sudoku { if(possibilities.size()==0) break; } - return possibilities.size(); + return possibilities; } /** @@ -181,7 +213,6 @@ public class Sudoku { System.out.println(); System.out.println(); } - System.out.println(); } /** diff --git a/Algo/Serie2/main.java b/Algo/Serie2/main.java index d115a98b14a79d2cd5c743b9879b7da287106e41..95afd9ea6d15c0c0225a23fb7ed07fd5a6cd3f03 100644 --- a/Algo/Serie2/main.java +++ b/Algo/Serie2/main.java @@ -62,12 +62,17 @@ public class Main { public static void main(String[] args) { //tests de Sudoku à partir des fichiers - Sudoku profSudoku = new Sudoku(getTabFromFile("profSudoku.txt")); - Sudoku emptySudoku = new Sudoku(getTabFromFile("Sudoku0.txt")); - //Sudoku DeathSudoku = new Sudoku(getTabFromFile("DeathSudoku.txt")); - - profSudoku.solve(); - emptySudoku.solve(); + //Sudoku profSudoku = new Sudoku(getTabFromFile("profSudoku.txt")); + //Sudoku emptySudoku = new Sudoku(getTabFromFile("Sudoku0.txt")); + Sudoku DeathSudoku = new Sudoku(getTabFromFile("DeathSudoku.txt")); + //profSudoku.play(9,0,1); + //profSudoku.print(); + DeathSudoku.solve(); + DeathSudoku = new Sudoku(getTabFromFile("DeathSudoku.txt")); + DeathSudoku.smartSolve(); + //emptySudoku.solve(); + //emptySudoku = new Sudoku(getTabFromFile("Sudoku0.txt")); + //emptySudoku.smartSolve(); //sudoku2.solve(); } } \ No newline at end of file diff --git a/Algo/rapport.docx b/Algo/rapport.docx index 0bb29ef674e483842348e7d968c90ac9e00101cc..eac40277e9cbd313d90357e98cfee577bf0181bb 100644 Binary files a/Algo/rapport.docx and b/Algo/rapport.docx differ