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
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
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
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
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
# for $x in doc("regions.xml")/regions/region/nation/name return <pair>{$x/../../regionkey}{$x}</pair> from algebraixlib.mathobjects import Set from algebraixlib.algebras.clans import project, cross_union from algebraixlib.algebras.sets import union region_key_nation_name_pairs_accumulator = Set() for region in regions: region_key = project(Set(region), 'regionkey') print('region_key:\n' + mo_to_str(region_key)) region_nations = region['nation'] print('region_nations:\n' + mo_to_str(region_nations)) region_nation_names = project(region_nations, 'name') print('region_nation_names:\n' + mo_to_str(region_nation_names)) region_key_nation_name_pairs = cross_union(region_key, region_nation_names) print('region_key_nation_name_pairs:\n' + mo_to_str(region_key_nation_name_pairs)) region_key_nation_name_pairs_accumulator = union(region_key_nation_name_pairs_accumulator, region_key_nation_name_pairs) print('region_key_nation_name_pairs_accumulator:\n' + mo_to_str(region_key_nation_name_pairs_accumulator)) print('region_key_nation_name_pairs_accumulator:\n' + mo_to_str(region_key_nation_name_pairs_accumulator)) # Get all regions with a nation named "UNITED STATES" # for $x in doc("regions.xml")/regions/region/nation[name="UNITED STATES"] return <name>$x/../name</name> from algebraixlib.mathobjects.couplet import Couplet us_nation_name = from_dict({'name': 'UNITED STATES'}) print('us_nation_name:\n' + mo_to_str(us_nation_name)) us_region_key_nation_name_pair = superstrict(region_key_nation_name_pairs_accumulator, us_nation_name) print('us_region_key_nation_name_pair:\n' + mo_to_str(us_region_key_nation_name_pair))
# for $x in doc("regions.xml")/regions/region/nation/name return <pair>{$x/../../regionkey}{$x}</pair> from algebraixlib.mathobjects import Set from algebraixlib.algebras.clans import project, cross_union from algebraixlib.algebras.sets import union region_key_nation_name_pairs_accumulator = Set() for region in regions: region_key = project(Set(region), 'regionkey') print('region_key:\n' + mo_to_str(region_key)) region_nations = region['nation'] print('region_nations:\n' + mo_to_str(region_nations)) region_nation_names = project(region_nations, 'name') print('region_nation_names:\n' + mo_to_str(region_nation_names)) region_key_nation_name_pairs = cross_union(region_key, region_nation_names) print('region_key_nation_name_pairs:\n' + mo_to_str(region_key_nation_name_pairs)) region_key_nation_name_pairs_accumulator = union( region_key_nation_name_pairs_accumulator, region_key_nation_name_pairs) print('region_key_nation_name_pairs_accumulator:\n' + mo_to_str(region_key_nation_name_pairs_accumulator)) print('region_key_nation_name_pairs_accumulator:\n' + mo_to_str(region_key_nation_name_pairs_accumulator)) # Get all regions with a nation named "UNITED STATES" # for $x in doc("regions.xml")/regions/region/nation[name="UNITED STATES"] return <name>$x/../name</name> from algebraixlib.mathobjects.couplet import Couplet us_nation_name = from_dict({'name': 'UNITED STATES'})
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