def game_loop(size=10, initial_p_alive=0.1, n_iters=1000, p_stasis=1., p_overpopulation=0., p_underpopulation=0., p_reproduction=1.): X = (torch.rand(size, size) < initial_p_alive).long().cuda() states = [] ts_alive = [] for i in range(n_iters): X = step_gol(X, p_stasis=p_stasis, p_overpopulation=p_overpopulation, p_underpopulation=p_underpopulation, p_reproduction=p_reproduction) write_gol(X) encoded_state = utils.encode(X[:4, :2].reshape(-1).cpu(), b=2) states.append(encoded_state) ts_alive.append(X.sum().cpu().numpy()) return active_info(states, k=2), np.mean(ts_alive)
def test_decode_encode(self): for i in range(81): state = decode(i, b=3, n=4) self.assertEqual(i, encode(state, b=3))
def test_encode(self): self.assertEqual(0, encode([0], b=2)) self.assertEqual(1, encode([1], b=2)) with self.assertRaises(InformError): encode([2], b=2) self.assertEqual(0, encode([0], b=3)) self.assertEqual(1, encode([1], b=3)) self.assertEqual(2, encode([2], b=3)) with self.assertRaises(InformError): encode([3], b=3) self.assertEqual(0, encode([0, 0], b=2)) self.assertEqual(1, encode([0, 1], b=2)) self.assertEqual(2, encode([1, 0], b=2)) self.assertEqual(3, encode([1, 1], b=2)) with self.assertRaises(InformError): encode([0, 2], b=2) with self.assertRaises(InformError): encode([2, 0], b=2) self.assertEqual(0, encode([0, 0], b=3)) self.assertEqual(1, encode([0, 1], b=3)) self.assertEqual(2, encode([0, 2], b=3)) self.assertEqual(3, encode([1, 0], b=3)) self.assertEqual(4, encode([1, 1], b=3)) self.assertEqual(5, encode([1, 2], b=3)) self.assertEqual(6, encode([2, 0], b=3)) self.assertEqual(7, encode([2, 1], b=3)) self.assertEqual(8, encode([2, 2], b=3)) with self.assertRaises(InformError): encode([0, 3], b=3) with self.assertRaises(InformError): encode([3, 0], b=3)
def test_encode_too_large(self): with self.assertRaises(InformError): encode(np.zeros(32), b=2) with self.assertRaises(InformError): encode(np.zeros(16), b=4)
def test_encode_bad_base(self): with self.assertRaises(InformError): encode([0, 0, 1], b=-1) with self.assertRaises(InformError): encode([0, 0, 1], b=1)
def test_encode_empty(self): with self.assertRaises(ValueError): encode([], b=2)