feat and fix

This commit is contained in:
MlodyJacky 2024-05-26 10:12:10 +02:00
parent b5e69bcc97
commit 997564080e
7 changed files with 86 additions and 113 deletions

84
app.py
View File

@ -9,67 +9,76 @@ from classes.agent import Agent
from collections import deque from collections import deque
import threading import threading
import time import time
import random
pygame.init() pygame.init()
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT)) window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
pygame.display.set_caption("Game Window") pygame.display.set_caption("Game Window")
table_coords = [(4, 4), (4, prefs.GRID_SIZE-5), (prefs.GRID_SIZE-5, 4), (prefs.GRID_SIZE-5, prefs.GRID_SIZE-5)]
def initBoard(): def initBoard():
wall_probability = 0.001
global cells global cells
cells = [] cells = []
for i in range(prefs.GRID_SIZE): for i in range(prefs.GRID_SIZE):
row = [] row = []
for j in range(prefs.GRID_SIZE): for j in range(prefs.GRID_SIZE):
cell = Cell(i, j, 1) waga = random.choices([1, 4, 5], weights=[0.7, 0.1, 0.1], k=1)[0]
cell = Cell(i, j, waga)
if (i, j) not in table_coords:
if waga == 5:
cell.prepareTexture("sprites/plama.png")
if waga == 4:
cell.prepareTexture("sprites/dywan.png")
if random.random() < wall_probability:
cell.prepareTexture("sprites/wall.png")
cell.blocking_movement = True
# Wybierz kolor dla płytki na podstawie jej położenia # Wybierz kolor dla płytki na podstawie jej położenia
if i == 0 or i == prefs.GRID_SIZE - 1 or j == 0 or j == prefs.GRID_SIZE - 1:
color = (100, 20, 20)
elif i == 1 or i == prefs.GRID_SIZE - 2 or j == 1 or j == prefs.GRID_SIZE - 2:
color = (20, 100, 20)
elif i == 2 or i == prefs.GRID_SIZE - 3 or j == 2 or j == prefs.GRID_SIZE - 3:
color = (20, 20, 100)
else:
color = (150, 200, 200)
cell.color = color
row.append(cell) row.append(cell)
cells.append(row) cells.append(row)
# Test # Test
# Na potrzeby prezentacji tworzę sobie prostokątne ściany na które nie da się wejść # Na potrzeby prezentacji tworzę sobie prostokątne ściany na które nie da się wejść
x1 = 3 # x1 = 3
y1 = 6 # y1 = 6
for i in range(x1, x1+4): # for i in range(x1, x1+4):
for j in range(y1, y1+2): # for j in range(y1, y1+2):
# cells[i][j].prepareTexture("sprites/wall.png")
# cells[i][j].blocking_movement = True
for i in range(prefs.GRID_SIZE):
for j in range(prefs.GRID_SIZE):
if i == 0 or j==0 or j==prefs.GRID_SIZE-1 or (i == prefs.GRID_SIZE-1 and j != 17):
cells[i][j].prepareTexture("sprites/wall.png") cells[i][j].prepareTexture("sprites/wall.png")
cells[i][j].blocking_movement = True cells[i][j].blocking_movement = True
cells[6][4].interactableItem = BeerKeg(cells[6][4], "Beer Keg") cells[6][6].interactableItem = BeerKeg(cells[6][6], "Beer Keg")
cells[4][10].interactableItem = CoffeMachine(cells[4][10], "Coffe Machine") cells[4][10].interactableItem = CoffeMachine(cells[4][10], "Coffe Machine")
cells[9][10].interactableItem = Table(cells[9][10], "Table")
cells[8][2].interactableItem = Table(cells[8][2], "Table")
cells[6][2].interactableItem = Table(cells[6][2], "Table")
cells[4][2].interactableItem = Table(cells[4][2], "Table")
for cell in cells: cells[4][4].interactableItem = Table(cells[4][4], "Table")
for cel in cell: cells[4][prefs.GRID_SIZE-5].interactableItem = Table(cells[4][prefs.GRID_SIZE-5], "Table")
cel.waga = Agent.get_cost((cel.X, cel.Y), cells) cells[prefs.GRID_SIZE-5][4].interactableItem = Table(cells[prefs.GRID_SIZE-5][4], "Table")
cells[prefs.GRID_SIZE-5][prefs.GRID_SIZE-5].interactableItem = Table(cells[prefs.GRID_SIZE-5][prefs.GRID_SIZE-5], "Table")
cells[9][9].waga = 2
cells[9][8].waga = 10
cells[8][8].waga = 10
cells[prefs.SPAWN_POINT[0]+1][prefs.SPAWN_POINT[1]].waga = 100
cells[prefs.SPAWN_POINT[0]][prefs.SPAWN_POINT[1]-1].waga = 100
cells[9][7].waga = 2 # cells[9][9].waga = 2
cells[10][6].waga = 2 # cells[9][8].waga = 10
cells[7][7].waga = 2 # cells[8][8].waga = 10
# cells[prefs.SPAWN_POINT[0]+1][prefs.SPAWN_POINT[1]].waga = 100
# cells[prefs.SPAWN_POINT[0]][prefs.SPAWN_POINT[1]-1].waga = 100
# cells[9][7].waga = 2
# cells[10][6].waga = 2
# cells[7][7].waga = 2
def draw_grid(window, cells, agent): def draw_grid(window, cells, agent):
for i in range(prefs.GRID_SIZE): for i in range(prefs.GRID_SIZE):
for j in range(prefs.GRID_SIZE): for j in range(prefs.GRID_SIZE):
cell = cells[i][j] cells[i][j].update(window)
color = cell.color
pygame.draw.rect(window, cell.color, (i*prefs.CELL_SIZE, j*prefs.CELL_SIZE, prefs.CELL_SIZE, prefs.CELL_SIZE))
if(cells[i][j].interactableItem): if(cells[i][j].interactableItem):
cells[i][j].interactableItem.update(window) cells[i][j].interactableItem.update(window)
if(not cells[i][j].blocking_movement): if(not cells[i][j].blocking_movement):
@ -84,7 +93,7 @@ def draw_grid(window, cells, agent):
initBoard() initBoard()
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells) agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
target_x, target_y = 9, 11 target_x, target_y = 18, 18
def watekDlaSciezkiAgenta(): def watekDlaSciezkiAgenta():
time.sleep(3) time.sleep(3)
@ -137,13 +146,6 @@ while running:
watek.daemon = True watek.daemon = True
watek.start() watek.start()
if keys[K_g]:
path, cost = agent.astar((target_x, target_y), start_cost=0)
print("Shortest path:", path)
print("Total cost:", cost)
watek = threading.Thread(target=watekDlaSciezkiAgenta)
watek.daemon = True
watek.start()
if pygame.key.get_pressed()[pygame.K_e]: if pygame.key.get_pressed()[pygame.K_e]:

View File

@ -18,8 +18,8 @@ class Agent:
self.multiplier = 1 self.multiplier = 1
self.direction = 0 self.direction = 0
self.directionPOM = 0 self.directionPOM = 0
self.xPOM = 5 self.xPOM = x
self.yPOM = 5 self.yPOM = y
self.g_scores = {} self.g_scores = {}
self.textures = [ self.textures = [
@ -270,74 +270,12 @@ class Agent:
return [] return []
#Algorytm astar
def moveto(self,x,y):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[x][y].blocking_movement:
self.current_cell = self.cells[x][y]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
print("Agent moved to x,y: ",x,y)
else:
print("Agent cannot move to this direction")
def get_cost(cell, cells):
x, y = cell[0], cell[1]
if x == 0 or x == len(cells) - 1 or y == 0 or y == len(cells[0]) - 1:
return 15 # Koszt dla pól na krawędziach
elif x == 1 or x == len(cells) - 2 or y == 1 or y == len(cells[0]) - 2:
return 10 # Koszt dla pól drugiego rzędu i przedostatniego oraz drugiej kolumny i przedostatniej
elif x == 2 or x == len(cells) - 3 or y == 2 or y == len(cells[0]) - 3:
return 5 # Koszt dla pól trzeciego rzędu i trzeciego od końca oraz trzeciej kolumny i trzeciej od końca
else:
return 1
def heuristic(self, current, target): def heuristic(self, current, target):
# Manhattan distance heuristic # Manhattan distance heuristic
dx = abs(current[0] - target[0]) dx = abs(current[0] - target[0])
dy = abs(current[1] - target[1]) dy = abs(current[1] - target[1])
return dx + dy return dx + dy
def priority(self, state, target):
# Oblicza priorytet dla danego stanu
g_score = self.g_score[state]
h_score = self.heuristic(state, target)
return g_score + h_score
def astar(self, target, start_cost=0):
if not isinstance(target, tuple) or len(target) != 2:
raise ValueError("Target must be a tuple of two elements (x, y).")
open_list = [(start_cost, (self.current_cell.X, self.current_cell.Y, self.directionPOM))]
came_from = {}
g_score = {(self.current_cell.X, self.current_cell.Y, self.directionPOM): start_cost}
while open_list:
_, current = heapq.heappop(open_list)
if isinstance(current, int):
raise ValueError("Current must be a tuple of three elements (x, y, direction).")
x, y, _ = current # Unpack the current tuple
if (x, y) == target: # Check if the current cell's coordinates match the target
path = []
while current in came_from:
path.append((current[0], current[1])) # Append only coordinates (x, y) to the path
current = came_from[current]
path = path[::-1] # Reverse the path
cost = g_score[(x, y, self.directionPOM)] # Retrieve the cost from the g_score dictionary
return path, cost
for neighbor in self.get_neighbors(self.cells[x][y], self.cells):
neighbor_coords = (neighbor.X, neighbor.Y, self.directionPOM) # Convert neighbor cell to tuple
tentative_g_score = g_score[current] + self.get_cost(neighbor_coords)
if tentative_g_score < g_score.get(neighbor_coords, float('inf')):
came_from[neighbor_coords] = current
g_score[neighbor_coords] = tentative_g_score
f_score = tentative_g_score + self.heuristic(neighbor_coords, target)
heapq.heappush(open_list, (f_score, neighbor_coords))
return [], float('inf')

33
decision_tree Normal file
View File

@ -0,0 +1,33 @@
digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname="helvetica"] ;
edge [fontname="helvetica"] ;
0 [label=<Tattoo &le; 1.5<br/>entropy = 1.045<br/>samples = 135<br/>value = [39.0, 1.0, 1.0, 93.0, 1.0]<br/>class = No >, fillcolor="#9190f0"] ;
1 [label=<Hair &le; 1.5<br/>entropy = 1.185<br/>samples = 66<br/>value = [28, 0, 1, 36, 1]<br/>class = No >, fillcolor="#d6d5fa"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label=<Balding &le; 1.5<br/>entropy = 0.678<br/>samples = 23<br/>value = [2, 0, 0, 20, 1]<br/>class = No >, fillcolor="#5855e9"] ;
1 -> 2 ;
3 [label=<entropy = 0.0<br/>samples = 13<br/>value = [0, 0, 0, 13, 0]<br/>class = No >, fillcolor="#3c39e5"] ;
2 -> 3 ;
4 [label=<entropy = 1.157<br/>samples = 10<br/>value = [2, 0, 0, 7, 1]<br/>class = No >, fillcolor="#8583ef"] ;
2 -> 4 ;
5 [label=<Wrinkles &le; 1.5<br/>entropy = 1.096<br/>samples = 43<br/>value = [26, 0, 1, 16, 0]<br/>class = Yes>, fillcolor="#f5d0b6"] ;
1 -> 5 ;
6 [label=<entropy = 0.976<br/>samples = 22<br/>value = [9, 0, 0, 13, 0]<br/>class = No >, fillcolor="#c3c2f7"] ;
5 -> 6 ;
7 [label=<entropy = 0.857<br/>samples = 21<br/>value = [17, 0, 1, 3, 0]<br/>class = Yes>, fillcolor="#eb9d65"] ;
5 -> 7 ;
8 [label=<Balding &le; 1.5<br/>entropy = 0.739<br/>samples = 69<br/>value = [11.0, 1.0, 0.0, 57.0, 0.0]<br/>class = No >, fillcolor="#6462ea"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
9 [label=<Glasses &le; 1.5<br/>entropy = 0.323<br/>samples = 34<br/>value = [2, 0, 0, 32, 0]<br/>class = No >, fillcolor="#4845e7"] ;
8 -> 9 ;
10 [label=<entropy = 0.567<br/>samples = 15<br/>value = [2, 0, 0, 13, 0]<br/>class = No >, fillcolor="#5a57e9"] ;
9 -> 10 ;
11 [label=<entropy = 0.0<br/>samples = 19<br/>value = [0, 0, 0, 19, 0]<br/>class = No >, fillcolor="#3c39e5"] ;
9 -> 11 ;
12 [label=<Outfit &le; 1.5<br/>entropy = 0.997<br/>samples = 35<br/>value = [9, 1, 0, 25, 0]<br/>class = No >, fillcolor="#8785ef"] ;
8 -> 12 ;
13 [label=<entropy = 1.272<br/>samples = 16<br/>value = [7, 1, 0, 8, 0]<br/>class = No >, fillcolor="#e9e9fc"] ;
12 -> 13 ;
14 [label=<entropy = 0.485<br/>samples = 19<br/>value = [2, 0, 0, 17, 0]<br/>class = No >, fillcolor="#5350e8"] ;
12 -> 14 ;
}

View File

@ -72,5 +72,5 @@ print("\nNew client:")
print(new_client_df) print(new_client_df)
print("Prediction:", prediction[0]) print("Prediction:", prediction[0])
graph = graphviz.Source(dot_data) #graph = graphviz.Source(dot_data)
graph.render("decision_tree", format='png') #graph.render("decision_tree", format='png')

View File

@ -1,9 +1,9 @@
WIDTH = 600 WIDTH = 1000
HEIGHT = WIDTH HEIGHT = WIDTH
GRID_SIZE = 12 GRID_SIZE = 20
CELL_SIZE = WIDTH // GRID_SIZE CELL_SIZE = WIDTH // GRID_SIZE
SPAWN_POINT = (5, 5) SPAWN_POINT = (5, 5)
COLORS = [(100, 20, 20), (20, 100, 20), (20, 20, 100),(150, 200, 200)] COLORS = [(190, 190, 190),(180,180,180)]

BIN
sprites/dywan.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
sprites/plama.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB