예제 #1
0
 def testCollissionDetectedForSmallObjectsEvenIfCoordinatesNotIdentical(
         self):
     a = game.Ball('X')
     a.set_position(1.0, 1.0)
     b = game.Ball('O')
     b.set_position(1.2, 1.3)
     self.assertEqual(True, game.HaveObjectsCollided(a, b))
예제 #2
0
    def next_generation2(self):
        self.generation_number += 1
        # sort by fitness descending
        self.population.sort(key=lambda x: x.fitness, reverse=True)

        new_population = []

        # pick the 5% best jumpers from previous generation
        for i in range(int(self.size * 0.1)):
            new_ball = game.Ball(self.population[i].brain)
            new_population.append(new_ball)

        # choose randomly 75% of the previous generation and cross them
        for i in range(int(self.size * 0.75)):
            new_population.append(self.crossing(self.pick_parent(), self.pick_parent()))

        # choose randomly 15% of the previous generation
        for i in range(int(self.size * 0.1)):
            new_population.append(self.pick_parent())

        # 5% of the new generation is generated completly randomly
        for i in range(int(self.size * 0.05)):
            new_ball = game.Ball()
            new_population.append(new_ball)

        # mutate all the jumper of the new generation
        for elem in new_population:
            elem.brain.mutate()

        self.population = new_population
예제 #3
0
 def testTwoObjectsAliveThenDead(self):
     a, b = game.Ball('A'), game.Ball('B')
     a.set_health(-1)
     b.set_health(5)
     objects = [b, a]
     removed = game.RemoveDeadObjects(objects)
     self.assertEqual(set([a]), removed)
     self.assertEqual([b], objects)
예제 #4
0
 def testTwoConsecutiveDeadInARow(self):
     a, b, c = game.Ball('A'), game.Ball('B'), game.Ball('C')
     a.set_health(5)
     b.set_health(-5)
     c.set_health(-5)
     objects = [a, b, c]
     removed = game.RemoveDeadObjects(objects)
     self.assertEqual(set([b, c]), removed)
     self.assertEqual([a], objects)
예제 #5
0
 def testThreeObjectsAliveThenDeadTheAlive(self):
     a, b, c = game.Ball('A'), game.Ball('B'), game.Ball('C')
     a.set_health(5)
     b.set_health(-5)
     c.set_health(5)
     objects = [a, b, c]
     removed = game.RemoveDeadObjects(objects)
     self.assertEqual(set([b]), removed)
     self.assertEqual([a, c], objects)
예제 #6
0
    def testNoCollissionHealthStaysSame(self):
        a = game.Ball('X')
        a.set_position(1.0, 1.0)
        a.set_damage(1)
        b = game.Ball('O')
        b.set_position(5.0, 5.0)
        b_previous_health = b.health

        game.RunCollisionDetection([a, b])

        self.assertEqual(b_previous_health, b.health,
                         "B health should not change.")
예제 #7
0
 def testMoveInboundsReturnsTrue(self):
     height, width = 20, 30
     obj = game.Ball('*')
     obj.set_position(width / 2, height / 2)
     obj.set_direction(1.0, 0.0)
     obj.set_speed(1)
     self.assertTrue(obj.move(height, width))
예제 #8
0
 def testOneObjectAliveStays(self):
     a = game.Ball('X')
     a.set_health(1)
     objects = [a]
     removed = game.RemoveDeadObjects(objects)
     self.assertEqual(set([]), removed)
     self.assertEqual([a], objects)
예제 #9
0
 def testOneObjectDeadGetsRemoved(self):
     a = game.Ball('X')
     a.set_health(0)
     objects = [a]
     removed = game.RemoveDeadObjects(objects)
     self.assertEqual(set([a]), removed)
     self.assertEqual(0, len(objects), "Dead obj should have been removed.")
예제 #10
0
    def crossing(self, elem1, elem2):
        w11 = elem1.brain.first_weights_matrix.reshape(1, elem1.brain.input_nodes * elem1.brain.hidden_nodes)
        w21 = elem1.brain.second_weights_matrix.reshape(1, elem1.brain.hidden_nodes * elem1.brain.output_nodes)
        b11 = elem1.brain.first_bias.reshape(1, elem1.brain.hidden_nodes)
        b21 = elem1.brain.second_bias.reshape(1, elem1.brain.output_nodes)

        w12 = elem2.brain.first_weights_matrix.reshape(1, elem2.brain.input_nodes * elem2.brain.hidden_nodes)
        w22 = elem2.brain.second_weights_matrix.reshape(1, elem2.brain.hidden_nodes * elem2.brain.output_nodes)
        b12 = elem2.brain.first_bias.reshape(1, elem2.brain.hidden_nodes)
        b22 = elem2.brain.second_bias.reshape(1, elem2.brain.output_nodes)

        # create new matrices for the child's Neural Network
        nw1 = []
        nw2 = []
        nb1 = []
        nb2 = []

        for i in range(elem1.brain.input_nodes * elem1.brain.hidden_nodes):
            p = random.randint(0,1)
            if p == 0:
                nw1.append(w11.item(i))
            else:
                nw1.append(w12.item(i))

        for i in range(elem1.brain.hidden_nodes * elem1.brain.output_nodes):
            p = random.randint(0,1)
            if p == 0:
                nw2.append(w21.item(i))
            else:
                nw2.append(w22.item(i))

        for i in range(elem1.brain.hidden_nodes):
            p = random.randint(0,1)
            if p == 0:
                nb1.append(b11.item(i))
            else:
                nb1.append(b12.item(i))

        for i in range(elem1.brain.output_nodes):
            p = random.randint(0,1)
            if p == 0:
                nb2.append(b21.item(i))
            else:
                nb2.append(b22.item(i))

        nw1 = asarray(nw1).reshape(elem1.brain.hidden_nodes,elem1.brain.input_nodes)
        nw2 = asarray(nw2).reshape(elem1.brain.output_nodes,elem1.brain.hidden_nodes)
        nb1 = asarray(nb1).reshape(elem1.brain.hidden_nodes,1)
        nb2 = asarray(nb2).reshape(elem1.brain.output_nodes,1)

        child = game.Ball(neural_net.Neural_Network(elem1.brain.input_nodes, elem1.brain.hidden_nodes, elem1.brain.output_nodes))
        child.brain.first_weights_matrix = nw1
        child.brain.second_weights_matrix = nw2
        child.brain.first_bias = nb1
        child.brain.second_bias = nb2
        return child
예제 #11
0
 def testMoveOutsideWithoutBounceStrategyReturnsFalse(self):
     height, width = 20, 30
     obj = game.Ball('*')
     # Place object at rightmost edge, and speeding to right so at next move
     # it leaves the boundaries
     obj.set_position(width - 1, height / 2)
     obj.set_speed(1)
     obj.set_direction(1.0, 0.0)
     obj.set_edge_strategy(game.EdgeStrategy.DISAPPEAR)
     self.assertFalse(obj.move(height, width))
예제 #12
0
 def testMoveOutsideWithBounceStrategyCausesBounce(self):
     height, width = 20, 30
     obj = game.Ball('*')
     # Place object at rightmost edge, and speeding to right so at next move
     # it leaves the boundaries
     obj.set_position(width - 1, height / 2)
     obj.set_speed(1)
     obj.set_direction(1.0, 0.0)
     obj.set_edge_strategy(game.EdgeStrategy.BOUNCE)
     self.assertGreater(obj.x_speed, 0, 'Should be moving right initially.')
     object_still_in_game = obj.move(height, width)
     self.assertTrue(object_still_in_game)
     self.assertLess(obj.x_speed, 0,
                     'Should be moving left after bouncing.')
예제 #13
0
    def testShowsRightNumberOfHealthBars(self):
        player = game.Ball('*')
        player_health_max = 3
        player_health_obj = game.MakePlayerHealthObject(player_health_max)

        player.health = 0
        game.UpdatePlayerHealth(player, player_health_obj)
        self.assertEqual('   ', player_health_obj.strings[0])

        player.health = 1
        game.UpdatePlayerHealth(player, player_health_obj)
        self.assertEqual('X  ', player_health_obj.strings[0])

        player.health = 2
        game.UpdatePlayerHealth(player, player_health_obj)
        self.assertEqual('XX ', player_health_obj.strings[0])

        player.health = 3
        game.UpdatePlayerHealth(player, player_health_obj)
        self.assertEqual('XXX', player_health_obj.strings[0])
예제 #14
0
 def MakeTwoCollidingObjects(self):
     a = game.Ball('X')
     a.set_position(1.0, 1.0)
     b = game.Ball('O')
     b.set_position(1.0, 1.0)
     return a, b
예제 #15
0
 def testObjectsFarAwayNoCollission(self):
     a = game.Ball('X')
     a.set_position(1.0, 1.0)
     b = game.Ball('O')
     b.set_position(5.0, 5.0)
     self.assertEqual(False, game.HaveObjectsCollided(a, b))
예제 #16
0
 def __init__(self, size):
     for i in range(size):
         self.population.append(game.Ball())
     self.size = size
     self.best = self.population[0]