def test_mutation(): class TestChromosome(VectorChromosome): GENES = VectorChromosome.GENES + ( ('test', 1, 0, 1), ) vc = TestChromosome(11) vc = vc.copy() def init_and_mutate(): vc.test = 0.5 vc.mut_rate = np.arange(0., 1.1, 0.1) vc.mut_std = np.arange(0.1, 1.2, 0.1) vc.mutate() num_iter = 1000 totals = np.ndarray(vc.test.shape + tuple([num_iter])) for i in xrange(num_iter): init_and_mutate() totals[..., i] = vc.test stds = np.std(totals, 1) prev = stds[0] eq_f(prev, 0) for std in stds[1:]: assert std - prev > -0.1 prev = std
def test_ops(): av = VectorAgent((10,)) av.speed = np.arange(0, 1, 0.1) av.speed[av.speed < 0.5] = 0 av.speed = av.speed.clip(0, 0.8) eq_f(av[1].speed, 0.0) eq_f(av[4].speed, 0.0) eq_f(av[5].speed, 0.5) eq_f(av[8].speed, 0.8) eq_f(av[9].speed, 0.8)
def test_population(): population = setup() env = population.env a = population._agents env.fetch_positions() assert env.X.shape == (100, 2) np.testing.assert_array_equal(a.x, env.X[:, 0]) np.testing.assert_array_equal(a.y, env.X[:, 1]) a.move() env.fetch_positions() assert (a[0].x, a[0].y) == (1.0, 0.0) assert (a[-1].x, a[-1].y) == (0.0, 79.0) env.calc_distances() dist = env.dist np.testing.assert_array_equal(dist.diagonal(), 0) a0a1_dist_sq = (a[0].x - a[1].x) ** 2 + (a[0].y - a[1].y) ** 2 eq_f(dist[0, 1], a0a1_dist_sq) assert env.near_lists[0, 0] == 1 eq_f(env.near_dist[0, 0], a0a1_dist_sq) eq_f(env.near_dist[1, 0], a0a1_dist_sq) eq_f(env.near_dist[0, 1], a0a1_dist_sq * 4) env.calc_cell_pos() env.food.simulate() env.food._food[:] = FOOD_MAX env.food._food[0, 0] = 0 a.health = 10 a[:50].eating = True a.eat_grass() assert np.all(a[:2].health == 10) assert np.all(a[2:50].health == 210) assert np.all(a[50:].health == 10) a.simulate()
def test_rotate(): av = VectorAgent((8,)) av.ax = 0.0 av.ay = -1.0 av.rotate(np.arange(0, 2 * math.pi, 2 * math.pi / 8)) eq_f(av[1].ax, math.sqrt(0.5)) eq_f(av[1].ay, -math.sqrt(0.5)) eq_f(av[2].ax, 1) eq_f(av[2].ay, 0) eq_f(av[3].ax, math.sqrt(0.5)) eq_f(av[3].ay, math.sqrt(0.5)) eq_f(av[4].ax, 0) eq_f(av[4].ay, 1) eq_f(av[6].ax, -1) eq_f(av[6].ay, 0)