def test_getting_columns(self): initial_board = numpy.arange(81) initial_board.shape = (9, 9) board = Board(initial_board) assert (board.get_column(1) == range(1, 81, 9)).all() assert (board.get_column(3) == range(3, 81, 9)).all()
def test_getting_rows(self): initial_board = numpy.arange(81) initial_board.shape = (9, 9) board = Board(initial_board) assert (board.get_row(1) == range(9, 18)).all() assert (board.get_row(3) == range(27, 36)).all()
class TestElimination(unittest.TestCase): """ Verify constraint propagation. """ def setUp(self): self.board = Board("board.txt") self.board.set_color(0, 2, 'R') self.this_cell = self.board.get(0, 2) def test_set_color(self): self.assertEquals('R', self.this_cell.proposal) def test_set_color_failures(self): with self.assertRaisesRegexp(Exception, "already eliminated"): self.board.set_color(0, 2, 'R') with self.assertRaisesRegexp(Exception, "already eliminated"): self.board.set_color(0, 2, 'G') def test_row_elimination(self): for c in self.board.iter_row(0): self.assertFalse( 'R' in c.possibilities ) def test_column_elimination(self): for c in self.board.iter_col(2): self.assertFalse( 'R' in c.possibilities ) def test_shared_height_elimination(self): for c, _row, _col in self.board.iter(): if c.height == self.this_cell.height: self.assertFalse( 'R' in c.possibilities )
class TestBoard(unittest.TestCase): """ Verify board utilities. """ def setUp(self): self.board = Board("board.txt") def test_iter_row(self): self.assertEquals( 6, len(list(self.board.iter_row(0))) ) def test_iter_col(self): self.assertEquals( 6, len(list(self.board.iter_col(0))) )
def test_getting_squares(self): initial_board = numpy.arange(81) initial_board.shape = (9, 9) board = Board(initial_board) print(initial_board) square00 = [[0, 1, 2], [9, 10, 11], [18, 19, 20]] square12 = [[33, 34, 35], [42, 43, 44], [51, 52, 53]] assert (board.get_square(0, 0) == square00).all() assert (board.get_square(1, 2) == square12).all()
def cross(self, p1, p2): board = Board() squares_num = p1.board.shape()[0] / 3 * p2.board.shape()[1] / 3 split = random.randint(0, squares_num) for i in range(split): row_num, col_num = board.get_square_indices(i) board.set_square(row_num, col_num, p1.board.get_square(row_num, col_num)) for i in range(split, p1.board.shape()[1]): row_num, col_num = board.get_square_indices(i) board.set_square(row_num, col_num, p1.board.get_square(row_num, col_num)) board.invariants = InvariantsMerger.merge_invariants(p1, p2, self.__area_function(board.get_square_indices(split))) genotype = BoardGenotype(board) return genotype
def setUp(self): self.ELITE_COEFF = 1 self.DROP_OUT_COEFF = 1 self.CROSSOVER = 1 self.population_size = 1 self.board = Board() self.generation = Generation(self.population_size, self.board, self.ELITE_COEFF, self.DROP_OUT_COEFF, self.CROSSOVER, dummy_crossover_function) self.sample_no = 0 self.DEFAULT_FITNESS = 10000
def cross(self, p1, p2): board = Board() split = random.randint(0, p1.board.shape()[1]) for i in range(split): board.set_column(i, p1.board.get_column(i)) for i in range(split, p1.board.shape()[1]): board.set_column(i, p2.board.get_column(i)) board.invariants = InvariantsMerger.merge_invariants(p1, p2, self.__area_function(split)) genotype = BoardGenotype(board) return genotype
def test_if_board_has_the_correct_size(self): board = Board() assert board.shape() == (9, 9)
from solver.board import Board # b = Board('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..') # b = Board('4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # b = Board('.....7.....56.34...1.....6.68..1..3....8.2....2..6..54.9.....1...12.43.....7.....') # b = Board('.1.2.......7.6..4..6...7.3...9.2.48.63..8..72.72.4.5...8.4...5..4..1.8.......3.2.') b = Board( '.3.58...49...4.....6.29.75..8....2....36125....1....3..58.64.7.....2...53...51.2.' ) b.solve()
def setUp(self): self.board = Board("board.txt") self.board.set_color(0, 2, 'R') self.this_cell = self.board.get(0, 2)
def setUp(self): self.board = Board("board.txt")