示例#1
0
 def test_put(self):
     genes = Genes()
     genes.put(18)
     self.assertEqual(11, len(genes))
     self.assertEqual(10, genes.last_index)
     self.assertEqual(0, genes.pointer)
     self.assertEqual(18, genes.genes[10])
示例#2
0
 def __init__(self, location, world):
     self.energy = randint(50, 90)
     self.direction = rand_direction()
     self.genes = Genes()
     self.location = location
     self.world = world
     self.generation = 0
     self.generations_alive = 0
示例#3
0
 def test_shift_pointer(self):
     genes = Genes()
     genes.genes = np.arange(0, 64)
     genes.pointer = 64
     genes.last_index = 64
     genes.put_index = 0
     genes.put_with_point(1)
     self.assertEqual(0, genes.pointer)
     self.assertEqual(64, genes.last_index)
     self.assertEqual(0, genes.put_index)
示例#4
0
 def test_shift_pointer_cycle_5(self):
     genes = Genes()
     self.assertEqual(0, genes.pointer)
     genes.shift_pointer(1)
     self.assertEqual(1, genes.pointer)
     genes.shift_pointer(10)
     self.assertEqual(1, genes.pointer)
示例#5
0
 def test_move_index_cycle_2(self):
     genes = Genes()
     self.assertEqual(0, genes.pointer)
     genes.move_pointer(15)
     self.assertEqual(5, genes.pointer)
示例#6
0
 def test_shift_pointer_cycle_3(self):
     genes = Genes()
     genes.put(1)
     self.assertEqual(0, genes.pointer)
     genes.shift_pointer(15)
     self.assertEqual(4, genes.pointer)
示例#7
0
 def test_shift_pointer_cycle_2(self):
     genes = Genes()
     self.assertEqual(0, genes.pointer)
     genes.shift_pointer(15)
     self.assertEqual(5, genes.pointer)
示例#8
0
 def test_get_next(self):
     genes = Genes()
     gene = genes.get_next()
     self.assertIn(gene, range(0, 33))
示例#9
0
 def test_mutate(self):
     genes = Genes()
     genes.put_with_point(65)
     self.assertEqual(len(genes), 11)
     genes.mutate()
     self.assertEqual(len(genes), 11)
示例#10
0
 def test_put_with_point(self):
     genes = Genes()
     genes.put_with_point(65)
     self.assertEqual(10, genes.pointer)
     self.assertEqual(65, genes.get_next())
     self.assertEqual(11, genes.put_index)
示例#11
0
 def test_move_index_cycle_3(self):
     genes = Genes()
     self.assertEqual(0, genes.pointer)
     genes.put(1)
     genes.move_pointer(1)
     self.assertEqual(1, genes.pointer)
示例#12
0
 def test_init(self):
     genes = Genes()
     self.assertEqual(10, len(genes))
     self.assertEqual(0, genes.pointer)
     self.assertEqual(9, genes.last_index)
示例#13
0
class Bot:
    def __init__(self, location, world):
        self.energy = randint(50, 90)
        self.direction = rand_direction()
        self.genes = Genes()
        self.location = location
        self.world = world
        self.generation = 0
        self.generations_alive = 0

    def execute_commands(self):
        for i in range(10):
            current_command = self.genes.get_next()
            if is_move_command(current_command):
                self.move(current_command)
                break
            elif is_peek_command(current_command):
                self.peek(current_command % 8)
                break
            elif is_lookup_command(current_command):
                self.lookup(current_command % 16)
            elif is_rotate_command(current_command):
                self.rotate(current_command % 24)
            else:
                self.genes.shift_pointer(current_command)
        self.energy -= 1

    def move(self, command):
        new_cell, new_location = self.get_new_cell_from_nell_location(command)
        if new_cell == CellType.EMPTY:
            self.world.put_cell(new_location, self)
            self.world.update_cell(self.location, CellType.EMPTY)
            self.location = new_location
        elif new_cell == CellType.FOOD:
            self.world.update_cell(new_location, self)
            self.world.update_cell(self.location, CellType.EMPTY)
            self.location = new_location
            self.energy += 11
        elif new_cell == CellType.POISON:
            self.energy = 0
            self.world.update_cell(self.location, CellType.POISON)
        # elif new_cell == CellType.WALL:
        #     self.genes.put_with_point(8 + command + 1)
        # elif isinstance(new_cell, Bot):
        #     self.genes.put_with_point(8 + command + 1)
        #     self.energy += 2

    def peek(self, command):
        new_cell, peek_location = self.get_new_cell_from_nell_location(command)
        # if new_cell == CellType.EMPTY:
        #     self.genes.put_with_point(8 + command + 1)
        if new_cell == CellType.FOOD:
            self.world.update_cell(peek_location, CellType.EMPTY)
            self.energy += 11
        elif new_cell == CellType.POISON:
            self.world.update_cell(peek_location, CellType.EMPTY)
            self.energy += 11
        # elif new_cell == CellType.WALL:
        #     self.genes.put_with_point(8 + command + 1)
        elif isinstance(new_cell, Bot):
            # self.genes.put_with_point(8 + command + 1)
            self.energy += 2

    def lookup(self, command):
        lookup_cell, lookup_location = self.get_new_cell_from_nell_location(
            command)
        # if lookup_cell == CellType.EMPTY:
        #     self.genes.put_with_point(8 + command + 1)
        if lookup_cell == CellType.FOOD:
            self.genes.put_with_point(8 + command)
        elif lookup_cell == CellType.POISON:
            self.genes.put_with_point(8 + command)
        # elif lookup_cell == CellType.WALL:
        #     self.genes.put_with_point(8 + command + 1)
        # elif isinstance(lookup_cell, Bot):
        #     self.genes.put_with_point(8 + command + 1)

    def rotate(self, command):
        self.direction = get_direction(self.direction, command + 1)

    def get_new_cell_from_nell_location(self, command):
        direction_vector = get_direction_vector(self.direction, command)
        new_location = self.location.transform(direction_vector)
        new_cell = self.world.get_cell(new_location)
        return new_cell, new_location

    def is_alive(self):
        return self.energy > 0

    def clone(self):
        clone = Bot(None, self.world)
        clone.genes = self.genes.clone()
        clone.generation = self.generation
        return clone

    def mutate(self):
        self.genes.mutate()
        self.generation = 0