Agent sprawdza klatki po kolei

This commit is contained in:
s481832 2024-05-12 23:05:28 +02:00
parent 041fdf41db
commit d86809f940
6 changed files with 41 additions and 15 deletions

View File

@ -14,6 +14,7 @@ class Enclosure:
self.imageH = imageH self.imageH = imageH
self.imageV = imageV self.imageV = imageV
self.imageGate = imageGate self.imageGate = imageGate
self.animals = set()
def gatebuild(self, screen, grid_size): def gatebuild(self, screen, grid_size):
self.imageGate = pygame.transform.scale(self.imageGate, (grid_size, grid_size)) self.imageGate = pygame.transform.scale(self.imageGate, (grid_size, grid_size))
@ -55,10 +56,10 @@ def create_enclosures():
gate = pygame.image.load('images/gate.png') gate = pygame.image.load('images/gate.png')
en1 = Enclosure(0, 5, 9, 11, (9, 6), (4, 11), "hot", fenceH, fenceV, gate) # Lewa klatka en1 = Enclosure(0, 5, 9, 11, (9, 6), (4, 11), "hot", fenceH, fenceV, gate) # Lewa klatka
en2 = Enclosure(13, 0, 29, 3, (16, 3), (27, 3), 'medium', fenceH, fenceV, gate) # Górna klatka en2 = Enclosure(4, 13, 28, 16, (12, 13), (20, 13), 'cold', fenceH, fenceV, gate) # Dolna klatka
en3 = Enclosure(11, 5, 16, 11, (12, 5), (16, 8), 'cold', fenceH, fenceV, gate) # Środkowa klatka en3 = Enclosure(19, 5, 31, 11, (23, 5), (25, 11), 'hot', fenceH, fenceV, gate) # Prawa klatka
en4 = Enclosure(19, 5, 31, 11, (23, 5), (25, 11), 'hot', fenceH, fenceV, gate) # Prawa klatka en4 = Enclosure(11, 5, 16, 11, (12, 5), (16, 8), 'cold', fenceH, fenceV, gate) # Środkowa klatka
en5 = Enclosure(4, 13, 28, 16, (12, 13), (20, 13), 'cold', fenceH, fenceV, gate) # Dolna klatka en5 = Enclosure(13, 0, 29, 3, (16, 3), (27, 3), 'medium', fenceH, fenceV, gate) # Górna klatka
Enclosures = [en1, en2, en3, en4, en5] Enclosures = [en1, en2, en3, en4, en5]

35
main.py
View File

@ -78,6 +78,12 @@ def main():
clock = pygame.time.Clock() clock = pygame.time.Clock()
spawned = False spawned = False
# Lista zawierająca klatki do odwiedzenia
enclosures_to_visit = Enclosures.copy()
current_enclosure_index = -1 # Indeks bieżącej klatki
actions_to_compare_list = [] # Lista zawierająca ścieżki do porównania
goals_to_compare_list = list() # Lista zawierająca cele do porównania
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -92,15 +98,13 @@ def main():
draw_gates(Enclosures, const) draw_gates(Enclosures, const)
draw_house(const) draw_house(const)
if not spawned: if not spawned:
spawn_all_animals() spawn_all_animals()
spawn_obstacles() spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles) cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals: for animal in Animals:
animal._feed = 0 # animal._feed = 0
# animal._feed = random.randint(0, 10) animal._feed = random.randint(0, 10)
spawned = True spawned = True
draw_Animals(Animals, const) draw_Animals(Animals, const)
@ -114,11 +118,28 @@ def main():
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const) agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const)
pygame.time.wait(200) pygame.time.wait(200)
else: else:
if agent._dryfood != 0 and agent._wetfood != 0 : if agent._dryfood > 1 and agent._wetfood > 1 :
animal = random.choice(Animals) if not goals_to_compare_list:
goal = (animal.x, animal.y) current_enclosure_index = (current_enclosure_index + 1) % len(enclosures_to_visit)
current_enclosure = enclosures_to_visit[current_enclosure_index]
for animal in current_enclosure.animals:
goal = (animal.x, animal.y)
goals_to_compare_list.append(goal)
actions_to_compare = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
actions_to_compare_list.append((actions_to_compare, goal))
chosen_path_and_goal = min(actions_to_compare_list, key=lambda x: len(x[0]))
goal = chosen_path_and_goal[1]
draw_goal(const, goal) draw_goal(const, goal)
# Usuń wybrany element z listy
actions_to_compare_list.remove(chosen_path_and_goal)
goals_to_compare_list.remove(goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map) actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
else: else:
goal = (3,1) goal = (3,1)
draw_goal(const, goal) draw_goal(const, goal)

View File

@ -1,6 +1,8 @@
import time import time
import pygame import pygame
DAY_LENGTH = 90 # Długość dnia w sekundach
def draw_night(const): def draw_night(const):
overlay = pygame.Surface(const.WINDOW_SIZE) overlay = pygame.Surface(const.WINDOW_SIZE)
overlay.fill((0, 0, 0)) overlay.fill((0, 0, 0))
@ -14,4 +16,4 @@ def change_time(const):
if current_time >= const.TIME_CHANGE: if current_time >= const.TIME_CHANGE:
# Zmieniamy porę dnia # Zmieniamy porę dnia
const.IS_NIGHT = not const.IS_NIGHT # Jeśli było dzień, teraz będzie noc, i odwrotnie const.IS_NIGHT = not const.IS_NIGHT # Jeśli było dzień, teraz będzie noc, i odwrotnie
const.TIME_CHANGE = current_time + 60 # Ustawiamy czas na kolejną zmianę za 1 minutę const.TIME_CHANGE = current_time + DAY_LENGTH

View File

@ -9,6 +9,8 @@ class Spawner:
# Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia # Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia
enclosure = random.choice(self.enclosures) enclosure = random.choice(self.enclosures)
enclosure.animals.add(self.entity) # Przydzielenie zwierzęcia do wybiegu
while True: while True:
if self.entity.adult: if self.entity.adult:
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2-2) self.entity.x = random.randint(enclosure.x1+1, enclosure.x2-2)

View File

@ -111,9 +111,9 @@ def generate_cost_map(Animals, Terrain_Obstacles, cost_map={}):
for animal in Animals: for animal in Animals:
if animal.adult: if animal.adult:
cost_map[(animal.x + 1, animal.y + 1)] = adult_animal_cost # cost_map[(animal.x + 1, animal.y + 1)] = adult_animal_cost
cost_map[(animal.x + 1, animal.y)] = adult_animal_cost # cost_map[(animal.x + 1, animal.y)] = adult_animal_cost
cost_map[(animal.x, animal.y + 1)] = adult_animal_cost # cost_map[(animal.x, animal.y + 1)] = adult_animal_cost
cost_map[(animal.x, animal.y)] = adult_animal_cost cost_map[(animal.x, animal.y)] = adult_animal_cost
else: else:
cost_map[(animal.x, animal.y)] = baby_animal_cost cost_map[(animal.x, animal.y)] = baby_animal_cost

BIN
tree.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 745 KiB

After

Width:  |  Height:  |  Size: 669 KiB