コード例 #1
0
def test_exact_cover_with_solution(array_data):
    rowcount = len(array_data)
    actual = get_exact_cover(array_data)
    assert actual.size > 0
    assert actual.size <= rowcount
    assert all(actual < rowcount)
    check_cover = array_data[actual, :].sum(axis=0)
    assert all(check_cover == 1)
コード例 #2
0
def test_single_row_exact_cover():
    """
        ArthurKantor's 2nd test case
    """
    data = np.array([[1, 1, 1]], dtype=np.int32)
    expected = np.array([0])
    actual = get_exact_cover(data)
    np.testing.assert_array_equal(actual, expected)
コード例 #3
0
def test_simple_exact_cover():
    """
        ArthurKantor's 1st test case
    """
    data = np.array([[1, 0, 0], [0, 1, 1]], dtype=np.int32)
    expected = np.array([0, 1])
    actual = get_exact_cover(data)
    np.testing.assert_array_equal(actual, expected)
コード例 #4
0
ファイル: sudoku.py プロジェクト: moygit/exact_cover_np
 def solve(self):
     # NOTES:
     # 1.  No error-checking; data is clean at this point.
     # 2.  We translate the puzzle once into the constraint matrix below, and then we
     #     translate again into a sparse matrix in get_exact_cover.  This duplicates
     #     some work, but it allows get_exact_cover to be a more generally applicable
     #     function.
     cover = ec.get_exact_cover(self._constraint_matrix)
     solution = None
     if cover != [] and ((cover[0] != 0) or (cover[1] != 0)):  # an array of 0's implies no solution.
         solution = Sudoku(self._size)
         solution._sudo = self._translate_exact_cover_into_sudoku(cover)
     return solution
コード例 #5
0
ファイル: sudoku.py プロジェクト: jwg4/exact_cover_np
 def solve(self):
     # NOTES:
     # 1.  No error-checking; data is clean at this point.
     # 2.  We translate the puzzle once into the constraint matrix below, and then we
     #     translate again into a sparse matrix in get_exact_cover.  This duplicates
     #     some work, but it allows get_exact_cover to be a more generally applicable
     #     function.
     logging.debug(self._constraint_matrix)
     cover = ec.get_exact_cover(self._constraint_matrix)
     logging.debug(cover)
     solution = None
     if cover.size > 0 and ((cover[0] != 0) or (cover[1] != 0)):  # an array of 0's implies no solution.
         solution = Sudoku(self._size)
         solution._sudo = self._translate_exact_cover_into_sudoku(cover)
     return solution
コード例 #6
0
    def createExactCover(self, board):
        """ Creates an matrix such that it is equal to the exact cover board. """
        try:
            import exact_cover_np as ec
        except ImportError as error:
            print("ERROR FOUND: ", error)
            print("")
            print(
                "Most likely you did not install the exact_cover_np package. Please install it and try again."
            )
            print("")
            quit()

        S = np.array(board)
        exact_cover = ec.get_exact_cover(S)
        return exact_cover
コード例 #7
0
def main(inputList, poem, plainListOfSyllables):
    OMINOE_SIZE = []
    OMINOE_BOOLS = []
    POEM_SIZE = 60
    iterations = 1
    start = time.time()
    #OMINOE_SIZE2 == OMINOE_SIZE1 if you only want one type
    #format of inputFile is:
    #iterations poem_size number_of_ominoe_sizes ominoe_size_0 ... ominoe_size_n add_padding_boolean_0 ... add_padding_boolean_n
    user_specify = inputList
    print("input data: ")
    print(inputList)

    try:
        TILINGS_TO_PRINT = int(user_specify[0])
        POEM_SIZE = int(user_specify[1])
        for i in range(int(user_specify[2])):
            OMINOE_SIZE.append(int(user_specify[3+i]))
            OMINOE_BOOLS.append(int(user_specify[3+int(user_specify[2])+i]))

    except:
        print("ERROR")
        sys.exit(1)

    #error checking
    if 6 in OMINOE_SIZE and OMINOE_BOOLS[OMINOE_SIZE.index(6)] == 1:
        print("ERROR")
        sys.exit(1)

    bucketsHashMap = {}
    #myFile = open("poem.txt")
    #otherFile = open("howILoveThee.txt")
    #otherFile = open("syllablizedPoemOneSyllablePerWord.txt")
    #otherFile = open("syllablizedPoem.txt")

    #No punctuation in words!
    #listOfWords = readInRawPoem(myFile)
    listOfSyls = plainListOfSyllables #readInSyllablePoem(otherFile)
    masterListOfSyllables = makeMasterListOfSyllables(listOfSyls)
    listOfSylNodes = makeSylNodes(poem) #makeSylNodes(listOfSyls)

    for index, j in enumerate(OMINOE_SIZE):
        for i, node in enumerate(listOfSylNodes):
            myOminoe = ominoe(j)
            myOminoe.reachableIndices+= listOfReachableIndices(i, POEM_SIZE)
            extendOminoe(myOminoe, i, listOfSylNodes, POEM_SIZE)
            expandInAllDirections(myOminoe, listOfSylNodes, POEM_SIZE)

    #let's take valid tiles from the ominoe objects and sort them
    sortedListOfTiles = []
    for validTile in listOfTiles:
        sortedListOfTiles.append(validTile.getIndicesInOminoe())
    builtListOfSimpleOminoes = buildSimpleOminoes(sortedListOfTiles)

    print("Number of tiles constructed: " + str(len(sortedListOfTiles)))
    placeIntoBuckets(builtListOfSimpleOminoes)
    print("number of different ominoe shapes:",len(buckets))

    validCount = 0
    sortedListOfTilesCopy = copy.deepcopy(sortedListOfTiles)
    sortedListOfTiles = []
    notUniqueCount = 0
    print("size of bucket: ")

    for qqq in range(TILINGS_TO_PRINT):
        cover = []
        numPadding = len(buckets)
        if qqq == 0:
            sortedListOfTiles = []
            bucketAddition = POEM_SIZE
            for i,bucket in enumerate(buckets):
                if len(buckets[i][0].tile) in OMINOE_SIZE:
                    if OMINOE_BOOLS[OMINOE_SIZE.index(len(buckets[i][0].tile))] == 1:
                        print(OMINOE_SIZE.index(len(buckets[i][0].tile)))
                        for ominoeObject in bucket:
                            tempTile = ominoeObject.tile
                            tempTile.append(bucketAddition)
                            sortedListOfTiles.append(tempTile)
                        bucketAddition +=1
                    else:
                        for ominoeObject in bucket:
                            sortedListOfTiles.append(ominoeObject.tile)

            rangeIncrease = bucketAddition-POEM_SIZE

        random.shuffle(sortedListOfTiles)
        true = 0
        listOfUsableInputs = []
        for tile in sortedListOfTiles:
            referenceTile = np.zeros(POEM_SIZE+rangeIncrease)
            for index in tile:
                referenceTile[index] = 1
            listOfUsableInputs.append(referenceTile)
####################################################################
        S = np.array(listOfUsableInputs, dtype='int32')
        end = time.time()
        print("time to finish pre-process: "+str(end - start))
        start = time.time()
        cover = ec.get_exact_cover(S)
        end = time.time()
        print("number of tiles: "+str(len(listOfUsableInputs)))
        print("time to finish exact cover: "+str(end - start))
####################################################################
        finalList = []
        for i in cover:
            finalList.append(sortedListOfTiles[i])

        if uniqueCover(finalList):
            check = np.zeros(POEM_SIZE+rangeIncrease)
            for tile in finalList:
                for val in tile:
                    check[val]+=1
            valid = True
            for i, val in enumerate(check):
                if val != 1:
                    valid = False
            if valid or not valid:
                validCount+=1
            else:
                print("invalid tiling")
                #sys.exit(1)

            finalListToDisplay = []
            boardTile = np.empty(POEM_SIZE,  dtype='|S6')
            boardTile.flatten()
            for index, val in enumerate(boardTile):
                boardTile[index] = ''
            letterIndex = 0
            print(finalList)
            for index,polyomino in enumerate(finalList):
                for index2, value in enumerate(polyomino):
                    if value >= POEM_SIZE:
                        finalList[index].remove(value)
            return finalList

            for indexT, tile in enumerate(finalList):
                for index in tile:
                    if index < POEM_SIZE:
                        boardTile[index] = ascii_uppercase[letterIndex]
                letterIndex+=1

            boardTile = np.reshape(boardTile,(POEM_SIZE/10,10))
            print("iteration " + str(qqq) + ": ")
            print(boardTile)
        else:
            notUniqueCount +=1
            qqq-=1
    print("number of non-uniques hit (not displayed above, all of the above ARE unique): " + str(notUniqueCount))
コード例 #8
0
def test_exact_cover(array_data):
    rowcount = len(array_data)
    actual = get_exact_cover(array_data)
    assert actual.size <= rowcount
    assert all(actual < rowcount)
コード例 #9
0
def test_exact_cover_without_solution(array_data):
    rowcount = len(array_data)
    actual = get_exact_cover(array_data)
    assert actual.size == 0
コード例 #10
0
def test_exact_cover():
    data = np.array([[1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 1]],
                    dtype=np.int32)
    expected = np.array([0, 1, 3])
    actual = get_exact_cover(data)
    np.testing.assert_array_equal(actual, expected)
コード例 #11
0
def test_complex_exact_cover_problem():
    data = np.array(polyomino_problem, dtype=np.int32)
    actual = get_exact_cover(data)
    assert actual.shape == (13, )