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.imageV = imageV
self.imageGate = imageGate
self.animals = set()
def gatebuild(self, screen, 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')
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
en3 = Enclosure(11, 5, 16, 11, (12, 5), (16, 8), 'cold', fenceH, fenceV, gate) # Środkowa klatka
en4 = Enclosure(19, 5, 31, 11, (23, 5), (25, 11), 'hot', fenceH, fenceV, gate) # Prawa klatka
en5 = Enclosure(4, 13, 28, 16, (12, 13), (20, 13), 'cold', fenceH, fenceV, gate) # Dolna klatka
en2 = Enclosure(4, 13, 28, 16, (12, 13), (20, 13), 'cold', fenceH, fenceV, gate) # Dolna klatka
en3 = 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(13, 0, 29, 3, (16, 3), (27, 3), 'medium', fenceH, fenceV, gate) # Górna klatka
Enclosures = [en1, en2, en3, en4, en5]

35
main.py
View File

@ -78,6 +78,12 @@ def main():
clock = pygame.time.Clock()
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:
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -91,16 +97,14 @@ def main():
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
draw_house(const)
if not spawned:
spawn_all_animals()
spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals:
animal._feed = 0
# animal._feed = random.randint(0, 10)
# animal._feed = 0
animal._feed = random.randint(0, 10)
spawned = True
draw_Animals(Animals, const)
@ -114,11 +118,28 @@ def main():
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const)
pygame.time.wait(200)
else:
if agent._dryfood != 0 and agent._wetfood != 0 :
animal = random.choice(Animals)
goal = (animal.x, animal.y)
if agent._dryfood > 1 and agent._wetfood > 1 :
if not goals_to_compare_list:
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)
# 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)
else:
goal = (3,1)
draw_goal(const, goal)

View File

@ -1,6 +1,8 @@
import time
import pygame
DAY_LENGTH = 90 # Długość dnia w sekundach
def draw_night(const):
overlay = pygame.Surface(const.WINDOW_SIZE)
overlay.fill((0, 0, 0))
@ -14,4 +16,4 @@ def change_time(const):
if current_time >= const.TIME_CHANGE:
# Zmieniamy porę dnia
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
enclosure = random.choice(self.enclosures)
enclosure.animals.add(self.entity) # Przydzielenie zwierzęcia do wybiegu
while True:
if self.entity.adult:
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:
if animal.adult:
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, 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, animal.y + 1)] = adult_animal_cost
cost_map[(animal.x, animal.y)] = adult_animal_cost
else:
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