Exemple #1
0
def test_cage():
    from random import choice, randint

    sheeps = [Sheep('white'), Sheep('black')]
    wolves = [Wolf('gray'), Wolf('black'), Wolf('brown'), Wolf('white')]
    snakes = [Snake('black'), Snake('golden'), Snake('green')]
    parrots = [Parrot('yello/blue'), Parrot('red')]
    species = [sheeps, wolves, snakes, parrots]
    small_cages = [SmallCage(n) for n in range(randint(3, 10))]
    big_cages = [BigCage(n) for n in range(randint(2, 4))]
    # -- SMAL CAGE TEST --
    for cage in small_cages:
        for num_animals in range(randint(0, 5)):
            try:
                cage.add_animals(choice(choice(species)))
            except CageFull:
                pass
    for cage in small_cages:
        print(cage)
    # -- BIG CAGE TEST --
    for cage in big_cages:
        for num_animals in range(randint(7, 12)):
            try:
                cage.add_animals(choice(choice(species)))
            except CageFull:
                pass
    for cage in big_cages:
        print(cage)
 def __init__(self,
              sheep_move_dist=0.5,
              wolf_move_dist=1.0,
              step=54,
              scale=1.0):
     self.step = step
     self.scale = scale
     self.wolf = Wolf(wolf_move_dist * self.step)
     self.flock = []
     self.distances = []
     self.turn_result = {}
     self.results = []
     self.turn = 0
Exemple #3
0
 def new_wolf(self):
     Wolf.Wolf(self.caller.win.world, self.x, self.y, Breeding.wolf, Strength.wolf)
     self.draw_self()
     self.root.destroy()
 def add_wolf(self):
     self.simulation.wolf = Wolf(cfg.config['wolf_move_dist']*self.step, cfg.config['canvas_side']/2, cfg.config['canvas_side']/2)
     self.paint_animal(cfg.config['canvas_side']/2, cfg.config['canvas_side']/2, cfg.config['wolf_color'])
class Simulation:
    def __init__(self,
                 sheep_move_dist=0.5,
                 wolf_move_dist=1.0,
                 step=54,
                 scale=1.0):
        self.step = step
        self.scale = scale
        self.wolf = Wolf(wolf_move_dist * self.step)
        self.flock = []
        self.distances = []
        self.turn_result = {}
        self.results = []
        self.turn = 0

    def add_sheep(self, x, y):
        sheep = Sheep(cfg.config["sheep_move_dist"] * self.step,
                      len(self.flock), x, y)
        self.flock.append(sheep)

    def update_scale(self, multiplier):
        self.wolf.move_dist *= multiplier
        self.wolf.pos_x *= multiplier
        self.wolf.pos_y *= multiplier
        for i in range(len(self.flock)):
            self.flock[i].move_dist *= multiplier
            self.flock[i].pos_x *= multiplier
            self.flock[i].pos_y *= multiplier
        self.scale *= multiplier
        self.step *= multiplier

    def go_step(self):
        if self.get_alive_sheep_amount() != 0:
            self.let_flock_flee()
            self.calculate_distances_between_wolf_and_flock()
            nearest_sheep = self.get_nearest_alive_sheep()
            distance_from_nearest_sheep = nearest_sheep[1]
            if (distance_from_nearest_sheep < self.wolf.move_dist):
                self.let_wolf_eat(nearest_sheep[0])
                self.alive_sheep_amount = self.get_alive_sheep_amount()
            else:
                self.let_wolf_chase(nearest_sheep)

            self.turn += 1
            self.write_turn_result()
            # print(self.turn_result)
            self.results.append(self.turn_result)
        else:
            print("Baco, nie ma łowiecek")

    def let_flock_flee(self):
        for id in range(0, len(self.flock)):
            self.flock[id].move()

    def let_wolf_eat(self, nearest_sheep):
        self.wolf.eat(nearest_sheep)

    def let_wolf_chase(self, nearest_sheep):
        self.wolf.chase(nearest_sheep)

    def calculate_distances_between_wolf_and_flock(self):
        self.distances = []
        for id in range(0, len(self.flock)):
            distance = 0.0
            distance = math.sqrt(
                math.pow(self.wolf.pos_x - self.flock[id].pos_x, 2) +
                math.pow(self.wolf.pos_y - self.flock[id].pos_y, 2))
            self.distances.append([self.flock[id], distance])

    def get_nearest_alive_sheep(self):
        alive_sheep = [sheep for sheep in self.distances if sheep[0].is_alive]
        if self.get_alive_sheep_amount() != 0:
            return min(alive_sheep, key=lambda x: x[1])
        else:
            return None

    def is_any_alive_sheep(self):
        result = False if not self.get_alive_sheep_amount() else True
        return result

    def get_alive_sheep_amount(self):
        x = 0
        for i in range(len(self.flock)):
            if self.flock[i].is_alive:
                x += 1
        return x

    def get_eaten_sheep_amount(self):
        cnt = 0
        for sheep in self.flock:
            if not sheep.is_alive:
                cnt += 1
        return cnt

    def write_turn_result(self):
        self.turn_result = {
            "turn_no": self.turn,
            "wolf_pos": (self.wolf.pos_x, self.wolf.pos_y),
            "sheep_positions": [],
            "sheep_move_dist": self.flock[0].move_dist,
            "wolf_move_dist": self.wolf.move_dist,
            "sheeps": len(self.flock)
        }
        for i in range(len(self.flock)):
            self.turn_result["sheep_positions"].append((
                self.flock[i].pos_x,
                self.flock[i].pos_y) if self.flock[i].is_alive else None)
Exemple #6
0
 def __spawn(self):
     tiles = self.width * self.height
     x = 0
     while x < tiles * Antelope.Antelope.density:
         f = self.empty_field()
         Antelope.Antelope(self, f.x, f.y, Breeding.antelope,
                           Strength.antelope)
         x += 1
     x = 0
     while x < tiles * Cyber_sheep.Cyber_sheep.density:
         f = self.empty_field()
         Cyber_sheep.Cyber_sheep(self, f.x, f.y, Breeding.cyber_sheep,
                                 Strength.cyber_sheep)
         x += 1
     x = 0
     while x < tiles * Fox.Fox.density:
         f = self.empty_field()
         Fox.Fox(self, f.x, f.y, Breeding.fox, Strength.fox)
         x += 1
     x = 0
     while x < tiles * Sheep.Sheep.density:
         f = self.empty_field()
         Sheep.Sheep(self, f.x, f.y, Breeding.sheep, Strength.sheep)
         x += 1
     x = 0
     while x < tiles * Turtle.Turtle.density:
         f = self.empty_field()
         Turtle.Turtle(self, f.x, f.y, Breeding.turtle, Strength.turtle)
         x += 1
     x = 0
     while x < tiles * Wolf.Wolf.density:
         f = self.empty_field()
         Wolf.Wolf(self, f.x, f.y, Breeding.wolf, Strength.wolf)
         x += 1
     x = 0
     while x < tiles * Belladonna.Belladonna.density:
         f = self.empty_field()
         Belladonna.Belladonna(self, f.x, f.y, Breeding.belladonna,
                               Strength.belladonna)
         x += 1
     x = 0
     while x < tiles * Grass.Grass.density:
         f = self.empty_field()
         Grass.Grass(self, f.x, f.y, Breeding.grass, Strength.grass)
         x += 1
     x = 0
     while x < tiles * Dandelion.Dandelion.density:
         f = self.empty_field()
         Dandelion.Dandelion(self, f.x, f.y, Breeding.dandelion,
                             Strength.dandelion)
         x += 1
     x = 0
     while x < tiles * Guarana.Guarana.density:
         f = self.empty_field()
         Guarana.Guarana(self, f.x, f.y, Breeding.guarana, Strength.guarana)
         x += 1
     x = 0
     while x < tiles * Hogweed.Hogweed.density:
         f = self.empty_field()
         Hogweed.Hogweed(self, f.x, f.y, Breeding.hogweed, Strength.hogweed)
         x += 1
     f = self.empty_field()
     Human.Human(self, f.x, f.y, Breeding.human, Strength.human)
Exemple #7
0
#%%
from animals import Wolf, Sheep
from space import Grid
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random
from celluloid import Camera
import custom_utils as utils

np.random.seed(1234)

grid = Grid(100, 100)

## initialize agents
for i in range(5):
    new_wolf = Wolf(grid)

for i in range(20):
    new_sheep = Sheep(grid)

## run simulation and get GIF saved
print("AGENTS before running")
print(len(grid.agent_population.values()))

utils.simulate(grid, output_path='test2.gif')

print("AGENTS after running")
print(len(grid.agent_population.values()))