Testowanie A*
This commit is contained in:
parent
404e8783cb
commit
7f74fcf2fa
39
animals.py
Normal file
39
animals.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from elephant import Elephant
|
||||||
|
from giraffe import Giraffe
|
||||||
|
from penguin import Penguin
|
||||||
|
from parrot import Parrot
|
||||||
|
from bear import Bear
|
||||||
|
|
||||||
|
def create_animals():
|
||||||
|
giraffe1 = Giraffe(0, 0, adult=True)
|
||||||
|
giraffe2 = Giraffe(0, 0, adult=True)
|
||||||
|
giraffe3 = Giraffe(0, 0, adult=True)
|
||||||
|
giraffe4 = Giraffe(0, 0)
|
||||||
|
giraffe5 = Giraffe(0, 0)
|
||||||
|
bear1 = Bear(0, 0, adult=True)
|
||||||
|
bear2 = Bear(0, 0, adult=True)
|
||||||
|
bear3 = Bear(0, 0)
|
||||||
|
bear4 = Bear(0, 0)
|
||||||
|
bear5 = Bear(0, 0)
|
||||||
|
penguin1 = Penguin(0, 0)
|
||||||
|
penguin2 = Penguin(0, 0)
|
||||||
|
penguin3 = Penguin(0, 0)
|
||||||
|
penguin4 = Penguin(0, 0)
|
||||||
|
elephant1 = Elephant(0, 0, adult=True)
|
||||||
|
elephant2 = Elephant(0, 0, adult=True)
|
||||||
|
elephant3 = Elephant(0, 0)
|
||||||
|
elephant4 = Elephant(0, 0)
|
||||||
|
elephant5 = Elephant(0, 0)
|
||||||
|
parrot1 = Parrot(0, 0)
|
||||||
|
parrot2 = Parrot(0, 0)
|
||||||
|
parrot3 = Parrot(0, 0)
|
||||||
|
parrot4 = Parrot(0, 0)
|
||||||
|
parrot5 = Parrot(0, 0)
|
||||||
|
|
||||||
|
Animals = [giraffe1, giraffe2, giraffe3, giraffe4, giraffe5,
|
||||||
|
bear1, bear2, bear3, bear4, bear5,
|
||||||
|
elephant1, elephant2, elephant3, elephant4, elephant5,
|
||||||
|
penguin1, penguin2, penguin3, penguin4,
|
||||||
|
parrot1, parrot2, parrot3, parrot4, parrot5]
|
||||||
|
|
||||||
|
return Animals
|
27
enclosure.py
27
enclosure.py
@ -17,17 +17,17 @@ class Enclosure:
|
|||||||
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))
|
||||||
gate_x, gate_y = self.gate
|
gate_x, gate_y = self.gate
|
||||||
gate_x-=1
|
gate_x -= 1
|
||||||
gate_y-=1
|
gate_y -= 1
|
||||||
rect = pygame.Rect(gate_x * grid_size, gate_y * grid_size, grid_size, grid_size)
|
rect = pygame.Rect(gate_x * grid_size, gate_y * grid_size, grid_size, grid_size)
|
||||||
screen.blit(self.imageGate, (gate_x * grid_size, gate_y * grid_size))
|
screen.blit(self.imageGate, (gate_x * grid_size, gate_y * grid_size))
|
||||||
|
|
||||||
def draw(self,screen, grid_size):
|
def draw(self, screen, grid_size):
|
||||||
self.imageH = pygame.transform.scale(self.imageH, (grid_size, grid_size))
|
self.imageH = pygame.transform.scale(self.imageH, (grid_size, grid_size))
|
||||||
self.imageV = pygame.transform.scale(self.imageV, (grid_size, grid_size))
|
self.imageV = pygame.transform.scale(self.imageV, (grid_size, grid_size))
|
||||||
gate_x, gate_y = self.gate
|
gate_x, gate_y = self.gate
|
||||||
gate_x-=1
|
gate_x -= 1
|
||||||
gate_y-=1
|
gate_y -= 1
|
||||||
if self.x1 < self.x2:
|
if self.x1 < self.x2:
|
||||||
for i in range(self.x1, self.x2+1):
|
for i in range(self.x1, self.x2+1):
|
||||||
if (i, self.y1) != (gate_x, gate_y):
|
if (i, self.y1) != (gate_x, gate_y):
|
||||||
@ -38,4 +38,19 @@ class Enclosure:
|
|||||||
if (self.x1, j) != (gate_x, gate_y):
|
if (self.x1, j) != (gate_x, gate_y):
|
||||||
screen.blit(self.imageV, (self.x1 * grid_size, j * grid_size))
|
screen.blit(self.imageV, (self.x1 * grid_size, j * grid_size))
|
||||||
if (self.x2, j) != (gate_x, gate_y):
|
if (self.x2, j) != (gate_x, gate_y):
|
||||||
screen.blit(self.imageV, (self.x2 * grid_size, j * grid_size))
|
screen.blit(self.imageV, (self.x2 * grid_size, j * grid_size))
|
||||||
|
|
||||||
|
def create_enclosures():
|
||||||
|
fenceH = pygame.image.load('images/fenceHor.png')
|
||||||
|
fenceV = pygame.image.load('images/fenceVer.png')
|
||||||
|
gate = pygame.image.load('images/gate.png')
|
||||||
|
|
||||||
|
en1 = Enclosure(0,5, 9,11, (9,6),"hot", fenceH, fenceV, gate) # Lewa klatka
|
||||||
|
en2 = Enclosure(13,0, 29,3, (16,3), 'medium', fenceH, fenceV, gate) # Górna klatka
|
||||||
|
en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate) # Środkowa klatka
|
||||||
|
en4 = Enclosure(19,5, 31,11, (25,5),'hot', fenceH, fenceV, gate) # Prawa klatka
|
||||||
|
en5 = Enclosure(4,13, 28,16, (16,13),'cold', fenceH, fenceV, gate) # Dolna klatka
|
||||||
|
|
||||||
|
Enclosures = [en1, en2, en3, en4, en5]
|
||||||
|
|
||||||
|
return Enclosures
|
193
main.py
193
main.py
@ -1,18 +1,16 @@
|
|||||||
|
from enum import Enum
|
||||||
import random
|
import random
|
||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys
|
||||||
from elephant import Elephant
|
from animals import create_animals
|
||||||
from giraffe import Giraffe
|
|
||||||
from penguin import Penguin
|
|
||||||
from parrot import Parrot
|
|
||||||
from bear import Bear
|
|
||||||
from agent import Agent
|
from agent import Agent
|
||||||
from enclosure import Enclosure
|
from enclosure import Enclosure, create_enclosures
|
||||||
from spawner import Spawner
|
from spawner import Spawner
|
||||||
from state_space_search import graphsearch
|
from state_space_search import graphsearch
|
||||||
from terrain_obstacle import Terrain_Obstacle
|
from terrain_obstacle import Terrain_Obstacle
|
||||||
|
|
||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
GRID_SIZE = 50
|
GRID_SIZE = 50
|
||||||
GRID_WIDTH = 30
|
GRID_WIDTH = 30
|
||||||
@ -37,45 +35,9 @@ obstacles = set()
|
|||||||
animals_position = set()
|
animals_position = set()
|
||||||
terrain_obstacles_position = set()
|
terrain_obstacles_position = set()
|
||||||
|
|
||||||
# region Define the animals
|
Animals = create_animals()
|
||||||
giraffe1 = Giraffe(0, 0, adult=True)
|
Enclosures = create_enclosures()
|
||||||
giraffe2 = Giraffe(0, 0, adult=True)
|
|
||||||
giraffe3 = Giraffe(0, 0, adult=True)
|
|
||||||
giraffe4 = Giraffe(0, 0)
|
|
||||||
giraffe5 = Giraffe(0, 0)
|
|
||||||
bear1 = Bear(0, 0, adult=True)
|
|
||||||
bear2 = Bear(0, 0, adult=True)
|
|
||||||
bear3 = Bear(0, 0)
|
|
||||||
bear4 = Bear(0, 0)
|
|
||||||
bear5 = Bear(0, 0)
|
|
||||||
penguin1 = Penguin(0, 0)
|
|
||||||
penguin2 = Penguin(0, 0)
|
|
||||||
penguin3 = Penguin(0, 0)
|
|
||||||
penguin4 = Penguin(0, 0)
|
|
||||||
elephant1 = Elephant(0, 0, adult=True)
|
|
||||||
elephant2 = Elephant(0, 0, adult=True)
|
|
||||||
elephant3 = Elephant(0, 0)
|
|
||||||
elephant4 = Elephant(0, 0)
|
|
||||||
elephant5 = Elephant(0, 0)
|
|
||||||
parrot1 = Parrot(0, 0)
|
|
||||||
parrot2 = Parrot(0, 0)
|
|
||||||
parrot3 = Parrot(0, 0)
|
|
||||||
parrot4 = Parrot(0, 0)
|
|
||||||
parrot5 = Parrot(0, 0)
|
|
||||||
|
|
||||||
Animals = [giraffe1, giraffe2, giraffe3, giraffe4, giraffe5, bear1, bear2, bear3, bear4, bear5, elephant1, elephant2, elephant3, elephant4, elephant5, penguin1, penguin2, penguin3, penguin4, parrot1, parrot2, parrot3, parrot4, parrot5]
|
|
||||||
# endregion
|
|
||||||
|
|
||||||
# region Define Enclosures
|
|
||||||
# Enclosure (lewy_górny_x, lewy_górny_y, prawy_dolny_x, prawy_dolny_y, brama, klimat, fenceH, fenceV, gate_obrazek)
|
|
||||||
en1 = Enclosure(0,5, 9,11, (9,6),"hot", fenceH, fenceV, gate) # Lewa klatka
|
|
||||||
en2 = Enclosure(13,0, 29,3, (16,3), 'medium', fenceH, fenceV, gate) # Górna klatka
|
|
||||||
en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate) # Środkowa klatka
|
|
||||||
en4 = Enclosure(19,5, 31,11, (25,5),'hot', fenceH, fenceV, gate) # Prawa klatka
|
|
||||||
en5 = Enclosure(4,13, 28,16, (16,13),'cold', fenceH, fenceV, gate) # Dolna klatka
|
|
||||||
|
|
||||||
Enclosures = [en1, en2, en3, en4, en5]
|
|
||||||
# endregion
|
|
||||||
puddle1 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
puddle1 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||||
puddle2 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
puddle2 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||||
puddle3 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
puddle3 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||||
@ -158,8 +120,42 @@ def generate_obstacles():
|
|||||||
|
|
||||||
return obstacles
|
return obstacles
|
||||||
|
|
||||||
|
cost_map = {}
|
||||||
|
def generate_cost_map():
|
||||||
|
adult_animal_cost = 10
|
||||||
|
baby_animal_cost = 5
|
||||||
|
puddle_cost = 50
|
||||||
|
bush_cost = 20
|
||||||
|
wall_cost = 1000
|
||||||
|
for animal in Animals:
|
||||||
|
if animal.adult:
|
||||||
|
cost_map[(animal.x + 1, animal.y + 1)] = baby_animal_cost
|
||||||
|
cost_map[(animal.x + 1, animal.y)] = baby_animal_cost
|
||||||
|
cost_map[(animal.x, animal.y + 1)] = baby_animal_cost
|
||||||
|
cost_map[(animal.x, animal.y)] = adult_animal_cost
|
||||||
|
else:
|
||||||
|
cost_map[(animal.x, animal.y)] = baby_animal_cost
|
||||||
|
|
||||||
|
for terrain_obstacle in Terrain_Obstacles:
|
||||||
|
if terrain_obstacle.type == 'puddle':
|
||||||
|
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = puddle_cost
|
||||||
|
else:
|
||||||
|
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = bush_cost
|
||||||
|
|
||||||
|
for wall in Walls:
|
||||||
|
cost_map[wall[0], wall[1]] = wall_cost
|
||||||
|
# Inne pola z różnym kosztem
|
||||||
|
# cost_map[(x, y)] = cost_value
|
||||||
|
|
||||||
|
# region Fields Tests
|
||||||
available_fields_small = set()
|
available_fields_small = set()
|
||||||
available_fields_large = set()
|
available_fields_large = set()
|
||||||
|
|
||||||
|
WHITE = (255,255,255)
|
||||||
|
GREEN = (0, 255, 0)
|
||||||
|
YELLOW = (255, 255, 0)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
def generate_available_fields():
|
def generate_available_fields():
|
||||||
for enclosure in Enclosures:
|
for enclosure in Enclosures:
|
||||||
for x in range(enclosure.x1 + 1, enclosure.x2):
|
for x in range(enclosure.x1 + 1, enclosure.x2):
|
||||||
@ -170,21 +166,14 @@ def generate_available_fields():
|
|||||||
if x < enclosure.x2 - 1 and y < enclosure.y2 - 1:
|
if x < enclosure.x2 - 1 and y < enclosure.y2 - 1:
|
||||||
available_fields_large.add(field)
|
available_fields_large.add(field)
|
||||||
|
|
||||||
# region Fields Tests
|
|
||||||
WHITE = (255,255,255)
|
|
||||||
GREEN = (0, 255, 0)
|
|
||||||
YELLOW = (255, 255, 0)
|
|
||||||
BLACK = (0, 0, 0)
|
|
||||||
|
|
||||||
def draw_fields(fields, color):
|
def draw_fields(fields, color):
|
||||||
for field in fields:
|
for field in fields:
|
||||||
x, y = field
|
x, y = field
|
||||||
pygame.draw.rect(screen, color, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
pygame.draw.rect(screen, color, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
||||||
|
|
||||||
def main_fields_tests():
|
def available_fields_tests():
|
||||||
obstacles = generate_obstacles()
|
obstacles = generate_obstacles()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
screen.fill(WHITE)
|
screen.fill(WHITE)
|
||||||
|
|
||||||
@ -200,29 +189,6 @@ def main_fields_tests():
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
cost_map = {}
|
|
||||||
def generate_cost_map():
|
|
||||||
adult_animal_cost = 10
|
|
||||||
baby_animal_cost = 5
|
|
||||||
puddle_cost = 1000
|
|
||||||
bush_cost = 20
|
|
||||||
for animal in Animals:
|
|
||||||
if animal.adult:
|
|
||||||
cost_map[(animal.x + 1, animal.y + 1)] = baby_animal_cost
|
|
||||||
cost_map[(animal.x + 1, animal.y)] = baby_animal_cost
|
|
||||||
cost_map[(animal.x, animal.y + 1)] = baby_animal_cost
|
|
||||||
cost_map[(animal.x, animal.y)] = adult_animal_cost
|
|
||||||
else:
|
|
||||||
cost_map[(animal.x, animal.y)] = baby_animal_cost
|
|
||||||
for terrain_obstacle in Terrain_Obstacles:
|
|
||||||
if terrain_obstacle.type == 'puddle':
|
|
||||||
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = puddle_cost
|
|
||||||
else:
|
|
||||||
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = bush_cost
|
|
||||||
# Inne pola z różnym kosztem
|
|
||||||
# cost_map[(x, y)] = cost_value
|
|
||||||
|
|
||||||
|
|
||||||
# region Main Code
|
# region Main Code
|
||||||
def main():
|
def main():
|
||||||
initial_state = (0,0,'S')
|
initial_state = (0,0,'S')
|
||||||
@ -267,7 +233,7 @@ def main():
|
|||||||
goal = (animal.x, animal.y)
|
goal = (animal.x, animal.y)
|
||||||
|
|
||||||
# --- Zaznaczenie celu ---
|
# --- Zaznaczenie celu ---
|
||||||
pygame.draw.rect(screen, (255, 0, 0), (animal.x * GRID_SIZE, animal.y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
pygame.draw.rect(screen, RED, (animal.x * GRID_SIZE, animal.y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
pygame.time.delay(2000)
|
pygame.time.delay(2000)
|
||||||
# ------------------------
|
# ------------------------
|
||||||
@ -275,10 +241,75 @@ def main():
|
|||||||
actions = graphsearch(agent.istate, goal, GRID_WIDTH, GRID_HEIGHT, obstacles, cost_map)
|
actions = graphsearch(agent.istate, goal, GRID_WIDTH, GRID_HEIGHT, obstacles, cost_map)
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
if __name__ == "__main__":
|
Walls = []
|
||||||
DEBUG_MODE = False # Jeśli True to pokazuje dostępne pola
|
# region A* Test
|
||||||
|
from elephant import Elephant
|
||||||
|
puddle1 = Terrain_Obstacle(15,8,'puddle', puddle_image)
|
||||||
|
bush1 = Terrain_Obstacle(15,6,'bush', bush_image)
|
||||||
|
animal = Elephant(15, 10)
|
||||||
|
animal1 = Elephant(14, 10)
|
||||||
|
animal2 = Elephant(13, 10)
|
||||||
|
animal3 = Elephant(12, 10)
|
||||||
|
animal4 = Elephant(11, 10)
|
||||||
|
Animals = [animal, animal1, animal2, animal3, animal4]
|
||||||
|
Terrain_Obstacles = [puddle1, bush1]
|
||||||
|
|
||||||
if DEBUG_MODE:
|
empty_rows = [5, 7, 9]
|
||||||
main_fields_tests()
|
|
||||||
else:
|
def generate_test_walls():
|
||||||
|
for x in range(4,26):
|
||||||
|
for y in range(0, 15):
|
||||||
|
if y not in empty_rows:
|
||||||
|
Walls.append((x, y))
|
||||||
|
return Walls
|
||||||
|
|
||||||
|
def draw_test_walls():
|
||||||
|
for wall in generate_test_walls():
|
||||||
|
pygame.draw.rect(screen, BLACK, (wall[0] * GRID_SIZE, wall[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
||||||
|
|
||||||
|
def a_star_testing():
|
||||||
|
initial_state = (0, 7, 'E')
|
||||||
|
agent = Agent(initial_state, 'images/agent1.png', GRID_SIZE)
|
||||||
|
goal = (29, 7)
|
||||||
|
|
||||||
|
actions = []
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
generated = False
|
||||||
|
while True:
|
||||||
|
screen.fill(WHITE)
|
||||||
|
draw_grid()
|
||||||
|
draw_test_walls()
|
||||||
|
draw_Terrain_Obstacles()
|
||||||
|
draw_Animals()
|
||||||
|
if not generated:
|
||||||
|
generate_cost_map()
|
||||||
|
|
||||||
|
agent.draw(screen, GRID_SIZE)
|
||||||
|
pygame.draw.rect(screen, RED, (goal[0] * GRID_SIZE, goal[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
clock.tick(10)
|
||||||
|
|
||||||
|
if actions:
|
||||||
|
action = actions.pop(0)
|
||||||
|
agent.move(action, GRID_WIDTH, GRID_HEIGHT, obstacles, Animals, goal)
|
||||||
|
pygame.time.wait(100)
|
||||||
|
else:
|
||||||
|
actions = graphsearch(agent.istate, goal, GRID_WIDTH, GRID_HEIGHT, obstacles, cost_map)
|
||||||
|
# endregion
|
||||||
|
|
||||||
|
class DebugMode(Enum):
|
||||||
|
MAIN = 1
|
||||||
|
AVAILABLE_FIELDS = 2
|
||||||
|
A_STAR_TESTING = 3
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
debug_mode = DebugMode.A_STAR_TESTING
|
||||||
|
|
||||||
|
if debug_mode == DebugMode.MAIN:
|
||||||
main()
|
main()
|
||||||
|
elif debug_mode == DebugMode.AVAILABLE_FIELDS:
|
||||||
|
available_fields_tests()
|
||||||
|
elif debug_mode == DebugMode.A_STAR_TESTING:
|
||||||
|
a_star_testing()
|
@ -88,7 +88,8 @@ def current_cost(node, cost_map):
|
|||||||
while node[1] is not None: # Dopóki nie dojdziemy do korzenia
|
while node[1] is not None: # Dopóki nie dojdziemy do korzenia
|
||||||
_, parent, action = node
|
_, parent, action = node
|
||||||
# Dodaj koszt pola z mapy kosztów tylko jeśli akcja to "Forward"
|
# Dodaj koszt pola z mapy kosztów tylko jeśli akcja to "Forward"
|
||||||
if action == 'Go Forward':
|
# if action == 'Go Forward':
|
||||||
|
if True:
|
||||||
state, _, _ = node
|
state, _, _ = node
|
||||||
cost += cost_map.get(state[:2], DEFAULT_COST_VALUE) # Pobiera koszt przejścia przez dane pole, a jeśli koszt nie jest zdefiniowany to bierze wartość domyślną
|
cost += cost_map.get(state[:2], DEFAULT_COST_VALUE) # Pobiera koszt przejścia przez dane pole, a jeśli koszt nie jest zdefiniowany to bierze wartość domyślną
|
||||||
node = parent # Przejdź do rodzica
|
node = parent # Przejdź do rodzica
|
||||||
|
Loading…
Reference in New Issue
Block a user