Algorytm_genetyczny #3

Merged
s481832 merged 18 commits from Algorytm_genetyczny into master 2024-06-10 13:30:20 +02:00
5 changed files with 40 additions and 9 deletions
Showing only changes of commit 12c53aff35 - Show all commits

View File

@ -8,6 +8,7 @@ class Agent:
self.grid_size = grid_size
self.image= pygame.image.load(image_path)
self.image = pygame.transform.scale(self.image, (grid_size, grid_size))
self._food = 0
def draw(self, screen, grid_size):
# Obróć obrazek zgodnie z kierunkiem
@ -55,12 +56,28 @@ class Agent:
self.istate = (self.x, self.y, self.direction)
feed_animal(self, animals, goal)
take_food(self)
def feed_animal(self, animals, goal):
goal_x, goal_y = goal
if self.x == goal_x and self.y == goal_y:
for animal in animals:
if animal.x == goal_x and animal.y == goal_y:
if animal.feed() == 'True':
if animal._feed < self._food :
self._food -= animal._feed
animal._feed = 0
print(animal.name, "fed with", animal.food)
print(animal.name, "fed with", animal.food)
print("Current food level: ", self._food)
else:
animal._feed -= self._food
self._food = 0
print(animal.name, "fed with", animal.food)
print("Current food level: ", self._food)
def take_food(self):
house_x = 3
house_y = 1
if self.x == house_x and self.y == house_y:
if self._food == 0:
self._food = 15
print("Agent took food and current food level is", self._food)

10
draw.py
View File

@ -11,4 +11,12 @@ def draw_grid(const):
for y in range(0, const.GRID_HEIGHT * const.GRID_SIZE, const.GRID_SIZE):
for x in range(0, const.GRID_WIDTH * const.GRID_SIZE, const.GRID_SIZE):
rect = pygame.Rect(x, y, const.GRID_SIZE, const.GRID_SIZE)
pygame.draw.rect(const.screen, const.BLACK, rect, 1)
pygame.draw.rect(const.screen, const.BLACK, rect, 1)
def draw_house(const):
X = 2
Y = 0
image_path = 'images/house.png'
image_surface = pygame.image.load(image_path) # Wczytanie obrazka do obiektu Surface
scaled_image = pygame.transform.scale(image_surface, (const.GRID_SIZE * 2, const.GRID_SIZE * 2))
const.screen.blit(scaled_image, (X * const.GRID_SIZE, Y * const.GRID_SIZE))

BIN
images/house.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

16
main.py
View File

@ -11,7 +11,7 @@ from spawner import Spawner
from state_space_search import graphsearch, generate_cost_map
from terrain_obstacle import create_obstacles, draw_Terrain_Obstacles
from constants import Constants, init_pygame
from draw import draw_goal, draw_grid
from draw import draw_goal, draw_grid, draw_house
from season import draw_background, draw_season
const = Constants()
@ -89,6 +89,7 @@ def main():
draw_grid(const)
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
draw_house(const)
if not spawned:
@ -110,10 +111,15 @@ def main():
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal)
pygame.time.wait(200)
else:
animal = random.choice(Animals)
goal = (animal.x, animal.y)
draw_goal(const, goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
if agent._food != 0:
animal = random.choice(Animals)
goal = (animal.x, animal.y)
draw_goal(const, goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
else:
goal = (3,1)
draw_goal(const, goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
if __name__ == "__main__":
main()

View File

@ -21,7 +21,7 @@ class Spawner:
break
def spawn_terrain_obstacles(self, blocked1, blocked2, taken, grid_width, grid_height):
blocked1 = blocked1 | {(8,5),(3,10),(15,2),(26,2),(11,4),(15,7),(22,4),(24,10),(11,12),(19,12)}
blocked1 = blocked1 | {(2,0),(3,0),(2,1),(3,1),(8,5),(3,10),(15,2),(26,2),(11,4),(15,7),(22,4),(24,10),(11,12),(19,12)}
while True:
self.entity.x = random.randint(0, grid_width - 1)
self.entity.y = random.randint(0, grid_height - 1)