Skip to content
Snippets Groups Projects
Commit 2311fc3d authored by Mata Seb's avatar Mata Seb
Browse files

Commit rendu Algorithmie series 1 et 2 + rapport

parent 0aaed9db
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -217,6 +217,4 @@ public class Puzzle {
}
return this;
}
}
}
\ No newline at end of file
......@@ -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
No preview for this file type
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
File deleted
File deleted
......@@ -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();
}
/**
......
......@@ -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
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment