コード例 #1
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def check_blocks(_board):
    """Check each block.  If there is only one value missing..."""
    if VERBOSE:
        print("* check_blocks")

    board = get_filled_cells(_board)
    blocks = partition.partition(board, partial(by_keys, 'band', 'stack'))
    for block_clan in _SORT(blocks, key=partial(by_clan_keys, 'band', 'stack')):
        new_possible, conflict = get_block_candidates(block_clan, board)

        if new_possible.is_empty:
            continue
        if new_possible.cardinality == 1:
            _board = get_new_board(_board, new_possible)
            continue

        if block_clan.cardinality == GRID_SIZE - 2:
            # Knowing that the value in conflict can't be placed in the conflict cell
            # ..it must go in the other...
            first_choice = clans.superstrict(new_possible, project(conflict, 'value'))
            if first_choice.cardinality == 2:
                # place both values
                _board = get_new_board(_board, first_choice)
                continue

            # Remove the first choice for all_possible
            remaining_possible = sets.minus(new_possible, first_choice)

            # Knowing that first_choice goes in a row/col, remove other value from that cell
            first_rowcol = project(first_choice, 'row', 'col')

            # The remaining cell is the second choice
            second_choice = sets.minus(remaining_possible,
                                       clans.superstrict(remaining_possible, first_rowcol))

            new_cells = sets.union(first_choice, second_choice)
            _board = get_new_board(_board, new_cells)
            continue

        # Partition by value
        candidates = partition.partition(new_possible, partial(by_key, 'value'))
        for candidate in _SORT(candidates, key=partial(by_clan_key, 'value')):
            # If any value fits in only 1 cell, place it
            if candidate.cardinality == 1:
                # Remove band/stack
                new_cell = project(candidate, 'row', 'col', 'value')
                _board = get_new_board(_board, new_cell)

    return _board
コード例 #2
0
ファイル: sudoku.py プロジェクト: Libardo1/algebraixlib
def check_blocks(_board):
    """Check each block.  If there is only one value missing..."""
    if VERBOSE:
        print("* check_blocks")

    board = get_filled_cells(_board)
    blocks = partition.partition(board, partial(by_keys, 'band', 'stack'))
    for block_clan in _SORT(blocks, key=partial(by_clan_keys, 'band', 'stack')):
        new_possible, conflict = get_block_candidates(block_clan, board)

        if new_possible.is_empty:
            continue
        if new_possible.cardinality == 1:
            _board = get_new_board(_board, new_possible)
            continue

        if block_clan.cardinality == GRID_SIZE - 2:
            # Knowing that the value in conflict can't be placed in the conflict cell
            # ..it must go in the other...
            first_choice = clans.superstrict(new_possible, project(conflict, 'value'))
            if first_choice.cardinality == 2:
                # place both values
                _board = get_new_board(_board, first_choice)
                continue

            # Remove the first choice for all_possible
            remaining_possible = sets.minus(new_possible, first_choice)

            # Knowing that first_choice goes in a row/col, remove other value from that cell
            first_rowcol = project(first_choice, 'row', 'col')

            # The remaining cell is the second choice
            second_choice = sets.minus(remaining_possible,
                                       clans.superstrict(remaining_possible, first_rowcol))

            new_cells = sets.union(first_choice, second_choice)
            _board = get_new_board(_board, new_cells)
            continue

        # Partition by value
        candidates = partition.partition(new_possible, partial(by_key, 'value'))
        for candidate in _SORT(candidates, key=partial(by_clan_key, 'value')):
            # If any value fits in only 1 cell, place it
            if candidate.cardinality == 1:
                # Remove band/stack
                new_cell = project(candidate, 'row', 'col', 'value')
                _board = get_new_board(_board, new_cell)

    return _board
コード例 #3
0
def fill_lefts(rel: 'P(M x M)',
               renames: 'P(M x M)',
               _checked=True) -> 'P(M x M)':
    r"""Return the left components in ``rel`` that are missing in ``renames`` as a diagonal
    unioned with ``renames``.

    The purpose is to create a :term:`relation` that can be used with the :term:`composition`
    operation to change (rename) one or more :term:`left component`\s and leave the rest alone.

    :param rel: The :term:`relation` that provides the full :term:`left set`.
    :param renames: A relation where the :term:`right component`\s are meant to be
        :term:`composition` 'origins' and the :term:`left component`\s composition 'targets'.
    :return: A relation that contains all members of ``renames`` unioned with a :term:`diagonal`
        that consists of all left components in ``rel`` that are missing in ``renames``.
    """
    if _checked:
        if not is_member(rel):
            return _undef.make_or_raise_undef2(rel)
        if not is_member(renames):
            return _undef.make_or_raise_undef2(renames)
    else:
        assert is_member_or_undef(rel)
        assert is_member_or_undef(renames)
        if rel is _undef.Undef() or renames is _undef.Undef():
            return _undef.make_or_raise_undef(2)
    missing_lefts = _sets.minus(get_lefts(rel, _checked=False),
                                get_rights(renames, _checked=False),
                                _checked=False)
    diag_missing_lefts = diag(*missing_lefts, _checked=False)
    result = _sets.union(renames, diag_missing_lefts, _checked=False)
    assert result.cached_is_relation
    return result
コード例 #4
0
ファイル: relations.py プロジェクト: edwardt/algebraixlib
def fill_lefts(rel: 'P(M x M)', renames: 'P(M x M)', _checked=True) -> 'P(M x M)':
    r"""Return the left components in ``rel`` that are missing in ``renames`` as a diagonal
    unioned with ``renames``.

    The purpose is to create a :term:`relation` that can be used with the :term:`composition`
    operation to change (rename) one or more :term:`left component`\s and leave the rest alone.

    :param rel: The :term:`relation` that provides the full :term:`left set`.
    :param renames: A relation where the :term:`right component`\s are meant to be
        :term:`composition` 'origins' and the :term:`left component`\s composition 'targets'.
    :return: A relation that contains all members of ``renames`` unioned with a :term:`diagonal`
        that consists of all left components in ``rel`` that are missing in ``renames``.
    """
    if _checked:
        if not is_member(rel):
            return _undef.make_or_raise_undef2(rel)
        if not is_member(renames):
            return _undef.make_or_raise_undef2(renames)
    else:
        assert is_member_or_undef(rel)
        assert is_member_or_undef(renames)
        if rel is _undef.Undef() or renames is _undef.Undef():
            return _undef.make_or_raise_undef(2)
    missing_lefts = _sets.minus(get_lefts(rel, _checked=False),
                                get_rights(renames, _checked=False), _checked=False)
    diag_missing_lefts = diag(*missing_lefts, _checked=False)
    result = _sets.union(renames, diag_missing_lefts, _checked=False)
    assert result.cached_is_relation
    return result
コード例 #5
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def check_values(_board):
    """Look for values where only one is missing.  If there is only one missing, then there is
    only one cell where adding the value would not cause a duplicate in a row or column.  Fill
    in those cells if they exist."""
    if verbose:
        print("* check_values")
    board = get_filled_cells(_board)

    new_cells = Set()
    value_clans = partition.partition(board, partial(by_key, 'value'))
    for value_clan in _sort(value_clans, key=partial(by_clan_key, 'value')):
        # If there is only 1 missing value..fill in the cell
        if value_clan.cardinality == GRID_SIZE-1:
            # Get the set of rows and cols containing value
            occupied_rows = project(value_clan, 'row')
            occupied_cols = project(value_clan, 'col')
            # Get the entire set of rows and cols based on the occupied rows and cols
            occupied = clans.superstrict(_board, sets.union(occupied_rows, occupied_cols))
            # Remove all occupied rows to get the only candidate row_col left
            row_col = sets.minus(_board, occupied)
            value = project(value_clan, 'value')
            new_cells = sets.union(new_cells, clans.cross_union(row_col, value))
    if new_cells:
        return get_new_board(_board, new_cells)
    return _board
コード例 #6
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def check_values(_board):
    """Look for values where only one is missing.  If there is only one missing, then there is
    only one cell where adding the value would not cause a duplicate in a row or column.  Fill
    in those cells if they exist."""
    if VERBOSE:
        print("* check_values")
    board = get_filled_cells(_board)

    new_cells = Set()
    value_clans = partition.partition(board, partial(by_key, 'value'))
    for value_clan in _SORT(value_clans, key=partial(by_clan_key, 'value')):
        # If there is only 1 missing value..fill in the cell
        if value_clan.cardinality == GRID_SIZE - 1:
            # Get the set of rows and cols containing value
            occupied_rows = project(value_clan, 'row')
            occupied_cols = project(value_clan, 'col')
            # Get the entire set of rows and cols based on the occupied rows and cols
            occupied = clans.superstrict(_board, sets.union(occupied_rows, occupied_cols))
            # Remove all occupied rows to get the only candidate row_col left
            row_col = sets.minus(_board, occupied)
            value = project(value_clan, 'value')
            new_cells = sets.union(new_cells, clans.cross_union(row_col, value))
    if new_cells:
        return get_new_board(_board, new_cells)
    return _board
コード例 #7
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def get_missing_rowcols(block_clan):
    band, stack = by_clan_keys('band', 'stack', block_clan)

    # Get block defined by band, stack
    full_block_clan = clans.superstrict(BANDS_STACKS,
                                        clans.from_dict({'band': band, 'stack': stack}))
    # Get missing rows/cols from the block
    target_rowcols = sets.minus(project(full_block_clan, 'row', 'col'),
                                project(block_clan, 'row', 'col'))
    return target_rowcols
コード例 #8
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def get_missing_rowcols(block_clan):
    band, stack = by_clan_keys('band', 'stack', block_clan)

    # Get block defined by band, stack
    full_block_clan = clans.superstrict(BANDS_STACKS,
                                        clans.from_dict({'band': band, 'stack': stack}))
    # Get missing rows/cols from the block
    target_rowcols = sets.minus(project(full_block_clan, 'row', 'col'),
                                project(block_clan, 'row', 'col'))
    return target_rowcols
コード例 #9
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def get_new_board(board, new_cells):
    if verbose:
        for cell in new_cells:
            row = cell('row').value
            col = cell('col').value
            value = cell('value').value
            print("*** value %d goes in Row %d, Col %d" % (value, row, col))

    cell_filter = project(new_cells, 'row', 'col')
    old_cells = clans.superstrict(board, cell_filter)
    new_board = sets.minus(board, old_cells)

    bands_stacks = clans.superstrict(BANDS_STACKS, cell_filter)
    new_cells = clans.functional_cross_union(new_cells, bands_stacks)

    new_board = sets.union(new_board, new_cells)
    # if verbose:
    #     print(get_string(new_board))
    assert len(new_board) == GRID_SIZE*GRID_SIZE
    return new_board
コード例 #10
0
ファイル: relations.py プロジェクト: jdcasi/algebraixlib
def fill_lefts(rel: 'P(M x M)', renames: 'P(M x M)', _checked=True) -> 'P(M x M)':
    """Return the left components in ``rel`` that are missing in ``renames`` as a diagonal
    unioned with ``renames``.

    :param rel: The :term:`relation` that provides the full :term:`left set`.
    :param renames: A relation where the :term:`right component`\s are meant to be
        :term:`composition` 'origins' and the :term:`left component`\s composition 'targets'.
    :return: A relation that contains all members of ``lefts`` unioned with a :term:`diagonal`
        that consists of all left components in ``rel`` that are missing in ``lefts``.
    """
    if _checked:
        if not is_member(rel):
            return _make_or_raise_undef()
        if not is_member(renames):
            return _make_or_raise_undef()
    else:
        assert is_member(rel)
        assert is_member(renames)
    missing_lefts = _sets.minus(get_lefts(rel, _checked=False),
                                get_rights(renames, _checked=False), _checked=False)
    diag_missing_lefts = diag(*missing_lefts)
    return _sets.union(renames, diag_missing_lefts, _checked=False).cache_is_relation(True)
コード例 #11
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def get_new_board(board, new_cells):
    if VERBOSE:
        for cell in new_cells:
            row = cell('row').value
            col = cell('col').value
            value = cell('value').value
            print("*** value %d goes in Row %d, Col %d" % (value, row, col))

    cell_filter = project(new_cells, 'row', 'col')
    old_cells = clans.superstrict(board, cell_filter)
    new_board = sets.minus(board, old_cells)

    band = new_cells['band']
    if not band:
        # print("missing band")
        bands_stacks = clans.superstrict(BANDS_STACKS, cell_filter)
        new_cells = clans.cross_functional_union(new_cells, bands_stacks)

    new_board = sets.union(new_board, new_cells)
    # if VERBOSE:
    #     print(get_string(new_board))
    assert len(new_board) == GRID_SIZE * GRID_SIZE
    return new_board
コード例 #12
0
ファイル: sudoku.py プロジェクト: Libardo1/algebraixlib
def get_block_candidates(block_clan, board):
    # Get the set of missing values...see if any can be placed due to row/col information
    values_clan = get_missing_values(block_clan)

    # Get the set of missing values...see if any can be placed due to row/col information
    target_rowcols = get_missing_rowcols(block_clan)

    if block_clan.cardinality == GRID_SIZE - 1:
        new_cells = clans.cross_union(target_rowcols, values_clan)
        return new_cells, Set()

    # Need cross union values with rows
    rows_clan = project(target_rowcols, 'row')
    cols_clan = project(target_rowcols, 'col')
    possible_rows_values = clans.cross_union(values_clan, rows_clan)
    possible_cols_values = clans.cross_union(values_clan, cols_clan)

    possible_rows_cols_values = sets.union(possible_rows_values, possible_cols_values)

    # The occupied_clan is the row/col/value set that is a conflict for values
    occupied_clan = clans.superstrict(board, possible_rows_cols_values)

    # If there are no conflicts then no cells can be placed
    if occupied_clan.is_empty:
        return Set(), Set()

    all_possible = clans.cross_union(values_clan, target_rowcols).cache_functional(CacheStatus.IS)

    # Get the set of conflicts...conflicting row/value + col/value
    conflict = sets.union(
        clans.superstrict(all_possible, project(occupied_clan, 'value', 'col')),
        clans.superstrict(all_possible, project(occupied_clan, 'value', 'row')))

    # Remove the conflicts from all_possible
    new_possible = sets.minus(all_possible, conflict)
    return new_possible, conflict
コード例 #13
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def get_block_candidates(block_clan, board):
    # Get the set of missing values...see if any can be placed due to row/col information
    values_clan = get_missing_values(block_clan)

    # Get the set of missing values...see if any can be placed due to row/col information
    target_rowcols = get_missing_rowcols(block_clan)

    if block_clan.cardinality == GRID_SIZE - 1:
        new_cells = clans.cross_union(target_rowcols, values_clan)
        return new_cells, Set()

    # Need cross union values with rows
    rows_clan = project(target_rowcols, 'row')
    cols_clan = project(target_rowcols, 'col')
    possible_rows_values = clans.cross_union(values_clan, rows_clan)
    possible_cols_values = clans.cross_union(values_clan, cols_clan)

    possible_rows_cols_values = sets.union(possible_rows_values, possible_cols_values)

    # The occupied_clan is the row/col/value set that is a conflict for values
    occupied_clan = clans.superstrict(board, possible_rows_cols_values)

    # If there are no conflicts then no cells can be placed
    if occupied_clan.is_empty:
        return Set(), Set()

    all_possible = clans.cross_union(values_clan, target_rowcols).cache_functional(CacheStatus.IS)

    # Get the set of conflicts...conflicting row/value + col/value
    conflict = sets.union(
        clans.superstrict(all_possible, project(occupied_clan, 'value', 'col')),
        clans.superstrict(all_possible, project(occupied_clan, 'value', 'row')))

    # Remove the conflicts from all_possible
    new_possible = sets.minus(all_possible, conflict)
    return new_possible, conflict
コード例 #14
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def check_blocks(_board):
    """Check each block.  If there is only one value missing..."""
    if verbose:
        print("* check_blocks")

    board = get_filled_cells(_board)
    blocks = partition.partition(board, partial(by_keys, 'band', 'stack'))
    for block_clan in _sort(blocks, key=partial(by_clan_keys, 'band', 'stack')):
        # Get the set of missing values...see if any can be placed due to row/col information
        values_clan = get_missing_values(block_clan)

        # Get the set of missing values...see if any can be placed due to row/col information
        target_rowcols = get_missing_rowcols(block_clan)

        if block_clan.cardinality == GRID_SIZE-1:
            new_cells = clans.cross_union(target_rowcols, values_clan)
            _board = get_new_board(_board, new_cells)
            continue

        # Need cross union values with rows
        rows_clan = project(target_rowcols, 'row')
        cols_clan = project(target_rowcols, 'col')
        possible_rows_values = clans.cross_union(values_clan, rows_clan)
        possible_cols_values = clans.cross_union(values_clan, cols_clan)

        possible_rows_cols_values = sets.union(possible_rows_values, possible_cols_values)

        # The occupied_clan is the row/col/value set that is a conflict for values
        occupied_clan = project(clans.superstrict(board, possible_rows_cols_values),
                                'value', 'row', 'col')

        # If there are no conflicts then no cells can be placed
        if occupied_clan.is_empty:
            continue

        all_possible = clans.cross_union(values_clan, target_rowcols).cache_is_left_functional(True)
        for rel in all_possible:
            rel.cache_is_left_functional(True)

        # Get the set of conflicts...conflicting row/value + col/value
        conflict = sets.union(
            clans.superstrict(all_possible, project(occupied_clan, 'value', 'col')),
            clans.superstrict(all_possible, project(occupied_clan, 'value', 'row')))

        # Remove the conflicts from all_possible
        new_possible = sets.minus(all_possible, conflict)

        if block_clan.cardinality == GRID_SIZE-2:
            # Knowing that the value in conflict can't be placed in the conflict cell
            # ..it must go in the other...
            first_choice = clans.superstrict(new_possible, project(conflict, 'value'))
            if first_choice.cardinality == 2:
                # place both values
                _board = get_new_board(_board, first_choice)
                continue

            # Remove the first choice for all_possible
            remaining_possible = sets.minus(new_possible, first_choice)

            # Knowing that first_choice goes in a row/col, remove other value from that cell
            first_rowcol = project(first_choice, 'row', 'col')

            # The remaining cell is the second choice
            second_choice = sets.minus(remaining_possible,
                                       clans.superstrict(remaining_possible, first_rowcol))

            new_cells = sets.union(first_choice, second_choice)
            _board = get_new_board(_board, new_cells)
            continue

        # Partition by value
        candidates = partition.partition(new_possible, partial(by_key, 'value'))
        for candidate in _sort(candidates, key=partial(by_clan_key, 'value')):
            # If any value fits in only 1 cell, place it
            if candidate.cardinality == 1:
                # Remove band/stack
                new_cell = project(candidate, 'row', 'col', 'value')
                _board = get_new_board(_board, new_cell)
    return _board
コード例 #15
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def check_rows(_board):
    """Look for rows where there is only one missing value.  If any are found fill in the missing
    value.  Look for rows where there are two missing values.  If either missing value is blocked
    by the same value in the candidate row, col, or block then the other value can be placed in
    the blocked cell.  The other value can be placed in the other cell.  Look for rows with more
    than two missing values.  Check each empty cell to see only one of the missing values can be
    placed in it.  Check each value to see if there is only one cell where it can be placed."""
    if verbose:
        print("* check_rows")
    board = get_filled_cells(_board)

    all_rows_clans = partition.partition(board, partial(by_key, 'row'))
    for row_clan in _sort(all_rows_clans, key=partial(by_clan_key, 'row')):
        row = project(row_clan, 'row')
        board_row = clans.superstrict(_board, row)
        values_clan = get_missing_values(row_clan)

        if row_clan.cardinality == GRID_SIZE-1:
            # Row is missing only 1 value, remove row_clan from the board leaving target row_col
            row_col = sets.minus(board_row, row_clan)
            new_cells = clans.cross_union(row_col, values_clan)
            _board = get_new_board(_board, new_cells)
            continue

        # Get the set of candidate col/value pairs
        row_possible = clans.cross_union(values_clan,
                                         project(sets.minus(board_row, row_clan), 'col'))

        if row_clan.cardinality == GRID_SIZE-2:

            # The occupied_clan is the col/value pair that is a conflict for each col/value
            occupied_clan = project(clans.superstrict(board, row_possible), 'col', 'value')

            # If there are no conflicts neither value can be placed without checking entire board
            if not occupied_clan.is_empty:
                # ..remove occupied_clan col/value pairs from all possible
                new_possible = sets.minus(row_possible, occupied_clan)

                if new_possible.cardinality == 2:
                    # Of the 4 possibilities (2 values * 2 cols), 2 were removed, place remaining
                    new_cells = clans.cross_union(row, new_possible)
                    _board = get_new_board(_board, new_cells)
                    continue

                # 3 of the possibilities remain...
                occupied_col = project(occupied_clan, 'col')

                # Remove the occupied_col choices to get the first col/value pair
                col_value1 = clans.superstrict(new_possible, occupied_col)

                occupied_val = project(col_value1, 'value')

                # Remove the occupied_val choices to get the second col/value pair
                col_value2 = sets.minus(new_possible, clans.superstrict(new_possible, occupied_val))

                new_cells = clans.cross_union(row, col_value1)
                new_cells = sets.union(new_cells, clans.cross_union(row, col_value2))
                _board = get_new_board(_board, new_cells)
                continue

        # The occupied_clan is the row/col/value set that could be a conflict for values
        occupied_clan = clans.superstrict(board, values_clan)

        # If there are no conflicts then no cells can be placed
        if occupied_clan.is_empty:
            continue

        # Add row to row_possible for remaining checks
        all_possible = clans.cross_union(row_possible, row)

        # Get the set of conflicts...conflicting row/value + col/value
        conflict = sets.union(
            clans.superstrict(all_possible,
                              project(occupied_clan, 'value', 'col')),
            clans.superstrict(all_possible,
                              project(occupied_clan, 'value', 'row')))

        # Remove the conflicts from all_possible
        new_possible = sets.minus(all_possible, conflict)

        if new_possible.is_empty:
            continue  # All possible may have been excluded due to row/col conflicts

        # Otherwise...need to check for block (band+stack) conflicts too!!
        # ...if value exists in same block as element of all_possible

        # Add band/stack
        new_targets = clans.superstrict(BANDS_STACKS, project(new_possible, 'row', 'col'))
        new_possible3 = clans.functional_cross_union(new_targets, new_possible)
        occupied_clan2 = occupied_clan

        # Remove block (band+stack) conflicts
        new_possible4a = sets.minus(project(new_possible3, 'value', 'band', 'stack'),
                                    project(occupied_clan2, 'value', 'band', 'stack'))
        new_possible4 = clans.superstrict(new_possible3, new_possible4a)

        # Partition by row/col
        placed = 0
        candidates = partition.partition(new_possible4, partial(by_keys, 'row', 'col'))
        for candidate in _sort(candidates, key=partial(by_clan_key, 'col')):
            # If any row/col has only 1 candidate, place it
            if candidate.cardinality == 1:
                # Remove band/stack
                cell = project(candidate, 'row', 'col', 'value')
                _board = get_new_board(_board, cell)
                placed += 1

        if placed:
            continue

        # Partition by value
        candidates = partition.partition(new_possible4, partial(by_key, 'value'))
        for candidate in _sort(candidates, key=partial(by_clan_key, 'value')):
            # If any value fits in only 1 cell, place it
            if candidate.cardinality == 1:
                # Remove band/stack
                cell = project(candidate, 'row', 'col', 'value')
                _board = get_new_board(_board, cell)
    return _board
コード例 #16
0
ファイル: sudoku.py プロジェクト: jdcasi/algebraixlib
def get_missing_values(clan):
    """Get remaining values from passed in clan by subtracting it from from all possible values."""
    values_clan = project(clan, 'value')
    return sets.minus(BLOCK_VALUES_CLAN, values_clan)
コード例 #17
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def get_missing_values(clan):
    """Get remaining values from passed in clan by subtracting it from from all possible values."""
    values_clan = project(clan, 'value')
    return sets.minus(BLOCK_VALUES_CLAN, values_clan)
コード例 #18
0
ファイル: sudoku.py プロジェクト: tomyc/algebraixlib
def check_rows(_board, try_harder=0):
    """Look for rows where there is only one missing value.  If any are found fill in the missing
    value.  Look for rows where there are two missing values.  If either missing value is blocked
    by the same value in the candidate row, col, or block then the other value can be placed in
    the blocked cell.  The other value can be placed in the other cell.  Look for rows with more
    than two missing values.  Check each empty cell to see only one of the missing values can be
    placed in it.  Check each value to see if there is only one cell where it can be placed."""
    if VERBOSE:
        print("* check_rows")
    board = get_filled_cells(_board)

    all_rows_clans = partition.partition(board, partial(by_key, 'row'))
    for row_clan in _SORT(all_rows_clans, key=partial(by_clan_key, 'row')):
        row = project(row_clan, 'row')
        board_row = clans.superstrict(_board, row)
        values_clan = get_missing_values(row_clan)

        if row_clan.cardinality == GRID_SIZE - 1:
            # Row is missing only 1 value, remove row_clan from the board leaving target row_col
            row_col = sets.minus(board_row, row_clan)
            new_cells = clans.cross_union(row_col, values_clan)
            _board = get_new_board(_board, new_cells)
            try_harder = 0
            continue

        # Get the set of candidate col/value pairs
        row_possible = clans.cross_union(values_clan,
                                         project(sets.minus(board_row, row_clan), 'col'))

        if row_clan.cardinality == GRID_SIZE - 2:

            # The occupied_clan is the col/value pair that is a conflict for each col/value
            occupied_clan = project(clans.superstrict(board, row_possible), 'col', 'value')

            # If there are no conflicts neither value can be placed without checking entire board
            if not occupied_clan.is_empty:
                # ..remove occupied_clan col/value pairs from all possible
                new_possible = sets.minus(row_possible, occupied_clan)

                if new_possible.cardinality == 2:
                    # Of the 4 possibilities (2 values * 2 cols), 2 were removed, place remaining
                    new_cells = clans.cross_union(row, new_possible)
                    _board = get_new_board(_board, new_cells)
                    try_harder = 0
                    continue

                # 3 of the possibilities remain...
                occupied_col = project(occupied_clan, 'col')

                # Remove the occupied_col choices to get the first col/value pair
                col_value1 = clans.superstrict(new_possible, occupied_col)

                occupied_val = project(col_value1, 'value')

                # Remove the occupied_val choices to get the second col/value pair
                col_value2 = sets.minus(new_possible, clans.superstrict(new_possible, occupied_val))

                new_cells = clans.cross_union(row, col_value1)
                new_cells = sets.union(new_cells, clans.cross_union(row, col_value2))
                _board = get_new_board(_board, new_cells)
                try_harder = 0
                continue

        # The occupied_clan is the row/col/value set that could be a conflict for values
        occupied_clan = clans.superstrict(board, values_clan)

        # If there are no conflicts then no cells can be placed
        if occupied_clan.is_empty:
            continue

        # Add row to row_possible for remaining checks
        all_possible = clans.cross_union(row_possible, row)

        # Get the set of conflicts...conflicting row/value + col/value
        conflict = sets.union(
            clans.superstrict(all_possible,
                              project(occupied_clan, 'value', 'col')),
            clans.superstrict(all_possible,
                              project(occupied_clan, 'value', 'row')))

        # Remove the conflicts from all_possible
        new_possible = sets.minus(all_possible, conflict)

        if new_possible.is_empty:
            continue  # All possible may have been excluded due to row/col conflicts

        # Otherwise...need to check for block (band+stack) conflicts too!!
        # ...if value exists in same block as element of all_possible

        # Add band/stack
        new_targets = clans.superstrict(BANDS_STACKS, project(new_possible, 'row', 'col'))
        new_possible3 = clans.cross_functional_union(new_targets, new_possible)
        occupied_clan2 = occupied_clan

        # Remove block (band+stack) conflicts
        new_possible4a = sets.minus(project(new_possible3, 'value', 'band', 'stack'),
                                    project(occupied_clan2, 'value', 'band', 'stack'))
        new_possible4 = clans.superstrict(new_possible3, new_possible4a)

        while True:
            candidates_updated = False
            # Partition by row/col
            placed = 0
            candidates = partition.partition(new_possible4, partial(by_keys, 'row', 'col'))
            for candidate in _SORT(candidates, key=partial(by_clan_key, 'col')):
                # If any row/col has only 1 candidate, place it
                if candidate.cardinality == 1:
                    # Remove band/stack
                    _board = get_new_board(_board, candidate)
                    try_harder = 0
                    placed += 1
            if placed:
                break

            # Partition by value
            candidates = partition.partition(new_possible4, partial(by_key, 'value'))
            for candidate in _SORT(candidates, key=partial(by_clan_key, 'value')):
                # If any value fits in only 1 cell, place it
                if candidate.cardinality == 1:
                    # Remove band/stack
                    _board = get_new_board(_board, candidate)
                    try_harder = 0
                else:  # If any value must be placed elsewhere, remove as candidate for this cell
                    if try_harder:
                        value = project(candidate, 'value')
                        # If this row of a sibling block must contain this value...
                        blocks = partition.partition(candidate, partial(by_keys, 'band', 'stack'))
                        if blocks.cardinality > 1:
                            for block_clan in _SORT(blocks,
                                                    key=partial(by_clan_keys, 'band', 'stack')):
                                block = project(block_clan, 'band', 'stack')
                                board_block = clans.superstrict(board, block)
                                if board_block.is_empty:
                                    continue

                                new_possible, conflict = get_block_candidates(board_block, board)
                                new_possible_value = clans.superstrict(new_possible, value)

                                if new_possible_value['row'].cardinality == 1:
                                    # Value must be placed in this block
                                    # ...other block candidates can be removed
                                    remove = sets.minus(candidate, block_clan)
                                    new_possible4 = sets.minus(new_possible4, remove)
                                    candidates_updated = True
            if not candidates_updated or not try_harder:
                break
    return _board
コード例 #19
0
    def test_minus(self):
        self._check_wrong_argument_types_binary(minus)

        result = minus(_set1, _set2)
        self.assertEqual(result, _set1m2)
コード例 #20
0
nums = Set(1, 2, 3, 4, 5)
for elem in nums:
    print(elem, " ")
print(1 in nums)
print(7 in nums)

# Sets can be unioned, intersected, set-minused. Relations such as is_subset and is_superset are
# defined.
a = Set(1, 2)
b = Set(2, 3)

import algebraixlib.algebras.sets as sets

print("union(a, b) = {}".format(sets.union(a, b)))
print("intersect(a, b) = {}".format(sets.intersect(a, b)))
print("minus(a, b) = {}".format(sets.minus(a, b)))
print("is_subset(a, b) = {}".format(sets.is_subset_of(a, b)))
print("is_superset(a, {{1}}) = {}".format(sets.is_superset_of(a, Set(1))))

# We can use a Couplet to model a single truth, such as 'sky'->'blue' or 'name'->'jeff'. By
# collecting multiple Couplets together in a set, we form a mathematical model of a data record.
# This data structure, called a binary relation (abbreviated from hereon as simply 'relation'), is
# the fundamental data type in a Data Algebra program.
record_relation = Set(Couplet('id', 123), Couplet('name', 'jeff'),
                      Couplet('loves', 'math'), Couplet('loves', 'code'))
print(record_relation)

# Some relations, specify a function from left to right. This is the case when every left
# value maps to exactly one right value. Such a relation is called "left functional".
# Likewise, a relation can be said to be "right functional" when every right value maps
# to exactly one left value.
コード例 #21
0
    def test_minus(self):
        self._check_wrong_argument_types_binary(minus)

        result = minus(_set1, _set2)
        self.assertEqual(result, _set1m2)
コード例 #22
0
ファイル: hello_world.py プロジェクト: jdcasi/algebraixlib
nums = Set(1, 2, 3, 4, 5)
for elem in nums:
    print(elem, " ")
print(1 in nums)
print(7 in nums)

# Sets can be unioned, intersected, set-minused. Relations such as is_subset and is_superset are
# defined.
a = Set(1, 2)
b = Set(2, 3)

import algebraixlib.algebras.sets as sets

print("union(a, b) = {}".format(sets.union(a, b)))
print("intersect(a, b) = {}".format(sets.intersect(a, b)))
print("minus(a, b) = {}".format(sets.minus(a, b)))
print("is_subset(a, b) = {}".format(sets.is_subset_of(a, b)))
print("is_superset(a, {{1}}) = {}".format(sets.is_superset_of(a, Set(1))))

# We can use a Couplet to model a single truth, such as 'blue'^'sky' or 'jeff'^'name'. By collecting
# multiple Couplets together in a set, we form a mathematical model of a data record. This data
# structure, called a binary relation (abbreviated from hereon as simply 'relation'), is the
# fundamental data type in a Data Algebra program.
record_relation = Set(Couplet('id', 123), Couplet('name', 'jeff'), Couplet('loves', 'math'),
                      Couplet('loves', 'code'))
print(record_relation)

# Some relations, specify a function from left to right. This is the case when every left
# value maps to exactly one right value. Such a relation is called "left functional".
# Likewise, a relation can be said to be "right functional" when every right value maps
# to exactly one left value.