コード例 #1
0
def lcv_values(sudoku, i, j, domainMat):
    """

    :param sudoku:
    :param i: line index of selected var
    :param j: column index of selected var
    :param domainMat: Mat[x][y] = set -> set hold domain of var (x,y)
    :return: ordered list of var's ( var == (i,j) ) values in order (according to heuristic logic) of selection
    """
    valueSet = domainMat[i][j]  # must not be empty
    if valueSet == set():
        return None
    orderedList = [-1] * (hanidokuFactor + 1)
    for value in valueSet:
        sudoku[i][j] = value
        counter = 0

        toDiscardFromLine = notInRange(sudoku, getLineByIndex(i))
        lineDomains = {}
        for var in getLineByIndex(i):
            if var == (i, j) or sudoku[var[0]][var[1]] != 0:
                continue
            lineDomains[var] = copy.deepcopy(domainMat[var[0]][var[1]])
            lineDomains[var] -= {value}
            lineDomains[var] -= toDiscardFromLine
            counter += len(domainMat[var[0]][var[1]] - lineDomains[var])

        toDiscardFromIncline = notInRange(sudoku, getInclineByVar(i,j))
        inclineDomains = {}
        for var in getInclineByVar(i,j):
            if var == (i, j) or sudoku[var[0]][var[1]] != 0:
                continue
            inclineDomains[var] = copy.deepcopy(domainMat[var[0]][var[1]])
            inclineDomains[var] -= {value}
            inclineDomains[var] -= toDiscardFromIncline
            counter += len(domainMat[var[0]][var[1]] - inclineDomains[var])

        toDiscardFromDecline = notInRange(sudoku, getDeclineByVar(i, j))
        declineDomains = {}
        for var in getDeclineByVar(i, j):
            if var == (i, j) or sudoku[var[0]][var[1]] != 0:
                continue
            declineDomains[var] = copy.deepcopy(domainMat[var[0]][var[1]])
            declineDomains[var] -= {value}
            declineDomains[var] -= toDiscardFromDecline
            counter += len(domainMat[var[0]][var[1]] - declineDomains[var])

        sudoku[i][j] = 0
        orderedList[value] = counter

    answer = []
    for k in valueSet:
        minIndex = nonNegativeMinIndex(orderedList)
        answer.append(minIndex)
        orderedList[minIndex] = -1

    return answer
コード例 #2
0
def inputSudokuIsVaild(sudoku):
    if sudokuDimentionsCorrect(sudoku) is False:
        print("dimentions are wrong")
        return False
    for i in range(0, len(sudoku)):
        indicesLine = getLineByIndex(i)
        line = indicesArrayToValuesArray(sudoku, indicesLine)
        if arrIsValid(line) is False:
            print("line: ",i, " is Wrong")
            return False

    for i in range(0,hanidokuFactor):
        indicesIncline = getInclineByIndex(i)
        incline = indicesArrayToValuesArray(sudoku, indicesIncline)
        if arrIsValid(incline) is False:
            print("incline: ",i, " is Wrong")
            return False

    for i in range(0,hanidokuFactor):
        indicesDecline = getDeclineByIndex(i)
        decline = indicesArrayToValuesArray(sudoku, indicesDecline)
        if arrIsValid(decline) is False:
            print("decline: ",i, " is Wrong")
            return False

    return True
コード例 #3
0
def sudoku_is_correct(sudoku):
    if sudoku_is_complete(sudoku) is False:
        return False
    numOfLines = len(sudoku)
    numOfInclines = hanidokuFactor
    numOfDeclines = hanidokuFactor

    for i in range(numOfLines):
        if allDifferent(sudoku, getLineByIndex(i)) is False or continuousArray(sudoku, getLineByIndex(i)) is False:
            return False
    for i in range(numOfInclines):
        if allDifferent(sudoku, getInclineByIndex(i)) is False or continuousArray(sudoku, getInclineByIndex(i)) is False:
            return False
    for i in range(numOfDeclines):
        if allDifferent(sudoku, getDeclineByIndex(i)) is False or continuousArray(sudoku, getDeclineByIndex(i)) is False:
            return False

    return True
コード例 #4
0
def calculateDomainOfVar(sudoku,i,j):
    toDiscard = set()
    for val in sudoku[i]:
        if val != 0:
            toDiscard.add(val)

    for var in getInclineByVar(i,j):
        if sudoku[var[0]][var[1]] != 0:
            toDiscard.add(sudoku[var[0]][var[1]])

    for var in getDeclineByVar(i,j):
        if sudoku[var[0]][var[1]] != 0:
            toDiscard.add(sudoku[var[0]][var[1]])

    toDiscard = toDiscard.union(notInRange(sudoku, getLineByIndex(i)))
    toDiscard = toDiscard.union(notInRange(sudoku, getInclineByVar(i,j)))
    toDiscard = toDiscard.union(notInRange(sudoku, getDeclineByVar(i,j)))

    return set(range(1, hanidokuFactor + 1)) - toDiscard
コード例 #5
0
def calculateDomainSetMat(sudoku):
    """

    :param sudoku:
    :return: matrix mat with hanidoku dimensions such that:
        if sudoku[i][j] has value -> mat[i][j] == -1
        if sudoku[i][j] empty ->  mat[i][j] == {n : n is a legitimate consistent value to assign to sudoku[i][j]}
    """
    domainSetMat = createDomainSetMat(sudoku)

    # calculate the number already in use in each line and discard them from the domains of vars in said line
    for i in range(len(sudoku)):
        toDiscard = set()
        for val in sudoku[i]:
            if val != 0:
                toDiscard.add(val)
        for j in range(len(domainSetMat[i])):
            if type(domainSetMat[i][j]) is set:
                domainSetMat[i][j] -= toDiscard

    # calculate the number already in use in each incline and discard them from the domains of vars in said line
    for i in range(hanidokuFactor):  # hanidokuFacor == number of inclines
        toDiscard = set()
        for var in getInclineByIndex(i):
            if sudoku[var[0]][var[1]] != 0:
                toDiscard.add(sudoku[var[0]][var[1]])
        for var in getInclineByIndex(i):
            if type(domainSetMat[var[0]][var[1]]) is set:
                domainSetMat[var[0]][var[1]] -= toDiscard

    # calculate the number already in use in each decline and discard them from the domains of vars in said line
    for i in range(hanidokuFactor):  # hanidokuFacor == number of declines
        toDiscard = set()
        for var in getDeclineByIndex(i):
            if sudoku[var[0]][var[1]] != 0:
                toDiscard.add(sudoku[var[0]][var[1]])
        for var in getDeclineByIndex(i):
            if type(domainSetMat[var[0]][var[1]]) is set:
                domainSetMat[var[0]][var[1]] -= toDiscard

    # Discarding numbers that cannot be assigned in each line due to not inRange
    for i in range(len(sudoku)):
        toDiscard = notInRange(sudoku, getLineByIndex(i))
        for j in range(len(domainSetMat[i])):
            if type(domainSetMat[i][j]) is set:
                domainSetMat[i][j] -= toDiscard

    # Discarding numbers that cannot be assigned into each incline due to not inRange
    for i in range(hanidokuFactor):  # hanidokuFacor == number of inclines
        toDiscard = notInRange(sudoku, getInclineByIndex(i))
        for var in getInclineByIndex(i):
            if type(domainSetMat[var[0]][var[1]]) is set:
                domainSetMat[var[0]][var[1]] -= toDiscard

    # Discarding numbers that cannot be assigned into each decline due to not inRange
    for i in range(hanidokuFactor):  # hanidokuFacor == number of declines
        toDiscard = notInRange(sudoku, getDeclineByIndex(i))
        for var in getDeclineByIndex(i):
            if type(domainSetMat[var[0]][var[1]]) is set:
                domainSetMat[var[0]][var[1]] -= toDiscard

    return domainSetMat