Commits (2)
*.png
*.pyc
......@@ -22,4 +22,8 @@ il faut d\'e9j\'e0 choisir quelle case on teste : \
- le mieux serait de faire celle qui a le plus de voisins, mais c\'92est long de calculer les voisins de toutes\
- on pourrait avoir une matrice avec le nombre de voisins de chacune des cases, ce ne serait pas si long de la mettre \'e0 jour \'e0 chaque fois, d\'92autant plus qu\'92on aurait en plus \'e0 le faire que quand un nouveau chiffre est trouv\'e9 \
\
ensuite c\'92est pas tr\'e8s compliqu\'e9, il suffit de parcourir tous les voisins}
\ No newline at end of file
ensuite c\'92est pas tr\'e8s compliqu\'e9, il suffit de parcourir tous les voisins\
\
SITES : \
http://aishack.in\
http://aishack.in/tutorials/sudoku-grabber-opencv-detection/}
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf830
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 \
\
il faut v\'e9rifier si un nbr ne peut \'eatre que dans une unit\'e9 (aka ligne, colonne, block ?)\
si c\'92est le cas il faut le virer de la tempGrid\
\
ne pouvoir \'eatre que dans un block pour une ligne = les seules possibilit\'e9s pour le nbr sont dans la k-ieme ligne du block\
\
Juste lignes pour le moment \
on parcourt chaque bloc : \
pour chacune des trois lignes on regarde si un des 9 nombres n\'92est pas pr\'e9sent dans les deux autres lignes\
si c\'92est le cas on modifie\
\
meilleure m\'e9thode : \
pour chaque chiffre (inconnu), on regarde l\'92ensemble des couples de coordonnes au sein du bloc ou on peut le trouver \
il faut noter dans un tableau toutes les ordonn\'e9es dans le bloc, dans un autre routes les abscisses dans le bloc\
on va obtenir qqch de la forme : (1,2) et (2) par ex. donc si la longueur est de 1 on peut conclure\
\
}
\ No newline at end of file
This diff is collapsed.
......@@ -10,13 +10,16 @@ import time
#pour enlever les 0 au debut et fin : np.trim_zeros
# il faudrait changer xIndex et yIndex en hIndex et vIndex...
#Reflechir à si il faut améliorer 2outOf3
#implementer la methode du site avec le nombre qui ne peut etre que dans un bloc...
#on dirait qu'il est aussi bon que moi...
#pour accelerer :
#modifier la fonction isFinished
#modifier update ?
#pour le rendre plus puissant :
#regarder les techniques qui utilisent les blocs identiques
class Grid (object) :
......@@ -245,6 +248,18 @@ class Grid (object) :
tileToModify = myGrid.getTile(lineToStudy, 3*blockToStudy+2)
tileToModify.modifyValue(i)
def checkToNarrowAllNeighbors (self) :
""" Launches the website method for all the blocks
Doesn't return anything
Works but doesn't help on the difficult grids
Slows down the easy grids
"""
for k in range (3) :
for i in range (3) :
currentBlock = self.getBlock(k,i)
currentBlock.checkToNarrowNeighbors()
class Block (object) :
def __init__ (self, block, xIndex, yIndex) :
......@@ -267,6 +282,72 @@ class Block (object) :
return (True, positionOfZero, valueToPut)
return (False, -1, -1)
def checkToNarrowNeighbors (self) :
""" Uses the technique seen on the website to narrow possibilities for the lines and columns nearby
For all unfound numbers, it checks if it is necessarly in the block (respectively to lines and columns)
Doesn't return anything
"""
foundNumbers = np.trim_zeros(np.sort((self.block.flatten())))
unfoundNumbers = []
for k in range (1, 10):
if k not in foundNumbers :
unfoundNumbers.append(k)
unfoundNumbers = np.array(unfoundNumbers)
for k in unfoundNumbers :
#now we have to check if k can only be in a line or a columns
#ATTENTION : bordel entre la notation XY et la convention mathématique
possibilitiesInLine = [] #list of the lines numbers in which k is a possibility
possibilitiesInColumn = [] #list of the columns numbers in which k is a possibility
for i in range (3) :
for j in range (3) :
currentTileX = 3*self.yIndex+i
currentTileY = 3*self.xIndex+j
currentPossibilities = myGrid.tempGrid[currentTileX, currentTileY]
if k in currentPossibilities :
if currentTileX not in possibilitiesInLine :
possibilitiesInLine.append(currentTileX)
if currentTileY not in possibilitiesInColumn :
possibilitiesInColumn.append(currentTileY)
if len(possibilitiesInLine) == 1 : #it means this number has to be in this block, respectively to the line
#before modifying anything, we must verify if the number has been found in the neighbor blocks.
# In that case, it is useless to proceed (ENCORE A FAIRE)
lineToStudyIndex = possibilitiesInLine[0]
for i in range(9) :
if i not in [3*self.xIndex, 3*self.xIndex+1, 3*self.xIndex+2] : #in that case we're not in the block, so we must remove k from tempGrid
currentTilePossibilities = myGrid.tempGrid[lineToStudyIndex, i]
currentTileNewPossibilities = []
#the problem is that we must keep the possibilities in order
for j in currentTilePossibilities :
if j != k :
currentTileNewPossibilities.append(j)
if len(currentTileNewPossibilities) == 8 :
currentTileNewPossibilities.append(0)
myGrid.tempGrid[lineToStudyIndex, i] = currentTileNewPossibilities
if len(possibilitiesInColumn) == 1 : #it means this number has to be in this block, respectively to the column
#before modifying anything, we must verify if the number has been found in the neighbor blocks.
# In that case, it is useless to proceed (ENCORE A FAIRE)
columnToStudyIndex = possibilitiesInColumn[0]
for i in range(9) :
if i not in [3*self.yIndex, 3*self.yIndex+1, 3*self.yIndex+2] : #in that case we're not in the block, so we must remove k from tempGrid
currentTilePossibilities = myGrid.tempGrid[i, columnToStudyIndex]
currentTileNewPossibilities = []
#the problem is that we must keep the possibilities in order
for j in currentTilePossibilities :
if j != k :
currentTileNewPossibilities.append(j)
if len(currentTileNewPossibilities) == 8 :
currentTileNewPossibilities.append(0)
myGrid.tempGrid[i, columnToStudyIndex] = currentTileNewPossibilities
class Line (object) :
def __init__ (self, line, index) :
......@@ -565,7 +646,7 @@ TEST_GRID_6 = np.array([[ 0, 5, 0, 6, 0, 0, 0, 0, 0],
###############################################################
TEST_GRID = TEST_GRID_2
TEST_GRID = TEST_GRID_6
METHOD = 1 #1 = smart, 2 = bourrin
myGrid = Grid(copy.deepcopy(TEST_GRID))
......@@ -578,7 +659,9 @@ if METHOD == 1 :
# myGrid.initNeighborsNbrGrid()
# print("Number of neighbors", sum(sum(myGrid.neighborsNbrGrid))) #it can be a good indicator of difficulty
#this first inital "naive" search helps in most cases
# a 2-out-3-search doesnt speed up the algorithm :(
# this first inital "naive" search helps in most cases
for k in range (9) :
for i in range (9) :
myGrid.getTile(k,i).evaluate()
......@@ -589,6 +672,7 @@ if METHOD == 1 :
aTileChanged = False
mostPromising = myGrid.findMostPromising()
promisingCounter = 0
#myGrid.checkToNarrowAllNeighbors() #works but slows everything
while not(aTileChanged) and (myGrid.isFinished() != True) and (promisingCounter < 81) :
numberOfPassagesInLoop += 1
xIndex = int(mostPromising[0,promisingCounter])
......@@ -626,7 +710,20 @@ if METHOD == 2 :
if compteur > 1 :
someTile.narrowPossibilities()
if METHOD == 3 :
for k in range (9) :
for i in range (9) :
someTile = myGrid.getTile(k,i)
someTile.evaluate()
for k in range (9) :
for i in range (9) :
someTile = myGrid.getTile(k,i)
someTile.narrowPossibilities()
someBlock = myGrid.getBlock(0,1)
print(someBlock.block)
someBlock.checkToNarrowNeighbors()
endTime = time.time()
print(myGrid)
......