PEP 8 reformat + generate random data for mine_characteristics

This commit is contained in:
egotd 2022-05-19 10:42:46 +02:00
parent d2820d872b
commit 213e6ce076
5 changed files with 386 additions and 398 deletions

View File

@ -15,7 +15,6 @@ class BFS:
new_nodes = [] new_nodes = []
neighbours_list = self.agent.sensor(current_position[0], current_position[1]) neighbours_list = self.agent.sensor(current_position[0], current_position[1])
if current_position[2] == 180: # jesli patrzy na polnoc if current_position[2] == 180: # jesli patrzy na polnoc
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']: if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
if neighbours_list[0][1] == 'grass': if neighbours_list[0][1] == 'grass':
@ -134,8 +133,6 @@ class BFS:
return new_nodes return new_nodes
# fringe = struktura danych przeszowyjąca wierchowki do odwiedzenia # fringe = struktura danych przeszowyjąca wierchowki do odwiedzenia
# explored = lista odwiedzonych stanow # explored = lista odwiedzonych stanow
# position_at_beginning = stan poczatkowy # position_at_beginning = stan poczatkowy
@ -149,25 +146,22 @@ class BFS:
self.window.pause(True) self.window.pause(True)
position_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy position_at_beginning = [self.agent.position_x, self.agent.position_y,
self.agent.rotation_degrees] # x, y, gdzie_patczy
final_action_list = [] # lista co ma robic zeby dojechac do miny final_action_list = [] # lista co ma robic zeby dojechac do miny
root = node.Node(None, None, position_at_beginning, 2) # parent, action, position, cost root = node.Node(None, None, position_at_beginning, 2) # parent, action, position, cost
heapq.heappush(fringe, (0, root)) # add first node to fringe heapq.heappush(fringe, (0, root)) # add first node to fringe
while len(fringe) != 0: # poki sa wezly do odwiedzenia(na fringe) while len(fringe) != 0: # poki sa wezly do odwiedzenia(na fringe)
if len(fringe) == 0: if len(fringe) == 0:
return False return False
# get node from fringe # get node from fringe
tmp_node = heapq.heappop(fringe) # tuple(priority, node) tmp_node = heapq.heappop(fringe) # tuple(priority, node)
tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy
# jesli tmp node to goaltest # jesli tmp node to goaltest
if tmp_node_position[:2] == goaltest: if tmp_node_position[:2] == goaltest:
print('Find') print('Find')
@ -182,8 +176,6 @@ class BFS:
explored.append(tmp_node[1]) # add node to array of visited nodes explored.append(tmp_node[1]) # add node to array of visited nodes
neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij
print(neighbours_list_of_our_node) print(neighbours_list_of_our_node)
@ -193,7 +185,6 @@ class BFS:
notInFringe = True # false if node in fringe notInFringe = True # false if node in fringe
notInExplored = True # false if node in explored notInExplored = True # false if node in explored
p = manhattan(node_[1], goaltest) + node_[2] p = manhattan(node_[1], goaltest) + node_[2]
# wyznacza jaki jest priorytet nastempnika # wyznacza jaki jest priorytet nastempnika
# manchaten from node_ + cost of way from tmp_ode to node_ # manchaten from node_ + cost of way from tmp_ode to node_
@ -204,20 +195,20 @@ class BFS:
for fringeNode in fringe: # isc po wszystkich wezlach ktore juz sa w fringe for fringeNode in fringe: # isc po wszystkich wezlach ktore juz sa w fringe
# jesli nasz wezel juz jest w fringe # jesli nasz wezel juz jest w fringe
if fringeNode[1].get_position()[0] == node_[1][0] and fringeNode[1].get_position()[1] == node_[1][1] and fringeNode[1].get_position()[2] == node_[1][2]: if fringeNode[1].get_position()[0] == node_[1][0] and fringeNode[1].get_position()[1] == node_[1][
1] and fringeNode[1].get_position()[2] == node_[1][2]:
notInFringe = False notInFringe = False
priority_in_fringe = fringeNode[0] priority_in_fringe = fringeNode[0]
# number of element in fringe # number of element in fringe
index_of_node_in_fringe = counter index_of_node_in_fringe = counter
counter = counter + 1 counter = counter + 1
for exploredNode in explored: # isc po wszystkich wezlach z listy explored for exploredNode in explored: # isc po wszystkich wezlach z listy explored
# jesli nasz wezel juz jest w explored # jesli nasz wezel juz jest w explored
if exploredNode.get_position()[0] == node_[1][0] and exploredNode.get_position()[1] == node_[1][1] and exploredNode.get_position()[2] == node_[1][2]: if exploredNode.get_position()[0] == node_[1][0] and exploredNode.get_position()[1] == node_[1][
1] and exploredNode.get_position()[2] == node_[1][2]:
notInExplored = False notInExplored = False
# if node not in fringe and not in explored # if node not in fringe and not in explored
if notInFringe and notInExplored: if notInFringe and notInExplored:
x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # parent, action, state_array, cost x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # parent, action, state_array, cost
@ -229,7 +220,5 @@ class BFS:
tmp[0] = p tmp[0] = p
tmp[1] = x tmp[1] = x
fringe[index_of_node_in_fringe] = tuple(tmp) fringe[index_of_node_in_fringe] = tuple(tmp)
self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]],self.agent.current_map.tile_size, self.agent.current_map, self.agent) self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]],
self.agent.current_map.tile_size, self.agent.current_map, self.agent)

View File

@ -7,37 +7,17 @@ from numpy import random
class DecisionTrees: class DecisionTrees:
def return_predict(self): def return_predict(self):
# Header looks like:
# header = ['Size(bigger_more_difficult)', 'Year(older_more_difficult)', 'Protection_from_defuse', # header = ['Size(bigger_more_difficult)', 'Year(older_more_difficult)', 'Protection_from_defuse',
# 'Meters_under_the_ground', 'Random_detonation_chance', 'Detonation_power_in_m', # 'Meters_under_the_ground', 'Random_detonation_chance', 'Detonation_power_in_m',
# 'Decision'] # 'Decision']
# read data # read data
df = pd.read_csv("D:\\1 Python projects\Saper\data\db.txt") df = pd.read_csv("D:\\1 Python projects\Saper\data\db.txt")
# print data # print data
# print(df.head()) # print(df.head())
lines = []
with open('D:\\1 Python projects\Saper\data\db.txt') as f:
line = f.readline()
for i in range(0, 200):
line = f.readline()
line = line.rstrip()
line = line.replace(",detonate", "")
line = line.replace(",defuse", "")
lines.append(line)
ss = []
for line in lines:
ss.append(line.split(","))
normalized_data_for_predict = []
for i in ss:
normalized_data_for_predict.append(list(map(int, i)))
print(normalized_data_for_predict)
# ID3 config # ID3 config
config = {'algorithm': 'ID3'} config = {'algorithm': 'ID3'}
# create decision tree # create decision tree
@ -46,5 +26,19 @@ class DecisionTrees:
# print predict # print predict
# print(chef.predict(model, [1, 2022, 0, 0, 0, 10])) # print(chef.predict(model, [1, 2022, 0, 0, 0, 10]))
predict = normalized_data_for_predict[random.randint(0, 199)] size = random.randint(1, 10)
return chef.predict(model, predict) year = random.randint(1941, 2022)
protection = 0
if year >= 2000:
protection = random.choice([1, 0, 1])
m_under_the_ground = random.randint(0, 10)
detonation_chance = random.randint(0, 100)
detonation_power_in_m = random.randint(0, 10)
detonation_power_in_m = detonation_power_in_m - m_under_the_ground
if detonation_power_in_m <= 0:
detonation_power_in_m = 0
mine_characteristics = [size, year, protection, m_under_the_ground, detonation_chance, detonation_power_in_m]
print(mine_characteristics)
return chef.predict(model, mine_characteristics)

View File

@ -5,6 +5,7 @@ from classes import decisionTrees
pygame.mixer.init() pygame.mixer.init()
# mina # mina
class Mine: class Mine:
position_x: int position_x: int
@ -143,7 +144,8 @@ class Map:
elif ok and rng < 2 and not (i < 2 and j < 3): elif ok and rng < 2 and not (i < 2 and j < 3):
matrix[i].append(0) # piasek matrix[i].append(0) # piasek
rand_rotate = 0 # randrange(4)*90 rand_rotate = 0 # randrange(4)*90
if rand_rotate==0 and j+3<self.tiles_x and not (i==0 or i==self.tiles_y-1 or j==0 or j==self.tiles_x-1): if rand_rotate == 0 and j + 3 < self.tiles_x and not (
i == 0 or i == self.tiles_y - 1 or j == 0 or j == self.tiles_x - 1):
cliff = Cliff(j, i, self.tile_size, rand_rotate, type=2) cliff = Cliff(j, i, self.tile_size, rand_rotate, type=2)
# self.cliffs.append(cliff) # self.cliffs.append(cliff)
cliff = Cliff(j + 1, i, self.tile_size, rand_rotate, type=0) cliff = Cliff(j + 1, i, self.tile_size, rand_rotate, type=0)
@ -173,7 +175,8 @@ class Map:
# narysowanie na ekranie terenu # narysowanie na ekranie terenu
for i in range(len(self.terrain_matrix)): for i in range(len(self.terrain_matrix)):
for j in range(len(self.terrain_matrix[i])): for j in range(len(self.terrain_matrix[i])):
self.window.window.blit(self.tile_palette[self.terrain_matrix[i][j]], (self.tile_size*j, self.tile_size*i)) self.window.window.blit(self.tile_palette[self.terrain_matrix[i][j]],
(self.tile_size * j, self.tile_size * i))
def draw_objects(self): def draw_objects(self):
for mine in self.mines: for mine in self.mines:
@ -181,6 +184,7 @@ class Map:
for cliff in self.cliffs: for cliff in self.cliffs:
cliff.draw(self.window.window) cliff.draw(self.window.window)
# broń # broń
class Weapon: class Weapon:
size: int size: int
@ -191,6 +195,7 @@ class Weapon:
weight: float weight: float
weapon_type: int weapon_type: int
# baza operacji # baza operacji
class Base: class Base:
position_x: int position_x: int
@ -201,6 +206,7 @@ class Base:
weapon_level: int weapon_level: int
weapons = [] weapons = []
# schron # schron
class Shelter: class Shelter:
position_x: int position_x: int
@ -208,6 +214,7 @@ class Shelter:
size: int size: int
image: pygame.surface.Surface image: pygame.surface.Surface
# składowisko # składowisko
class Dumpster: class Dumpster:
position_x: int position_x: int
@ -232,7 +239,6 @@ class NPC:
weight: float weight: float
# saper # saper
class Minesweeper: class Minesweeper:
size: int size: int
@ -251,7 +257,6 @@ class Minesweeper:
ability = 1 ability = 1
max_carried_weight = 5.0 max_carried_weight = 5.0
def __init__(self, position_x=0, position_y=0, size=64): def __init__(self, position_x=0, position_y=0, size=64):
self.position_x = position_x self.position_x = position_x
self.position_y = position_y self.position_y = position_y
@ -293,7 +298,6 @@ class Minesweeper:
if self.current_map.terrain_matrix[self.position_y][self.position_x] < 5: if self.current_map.terrain_matrix[self.position_y][self.position_x] < 5:
self.speed = 1 self.speed = 1
def draw(self, window, delta: float): def draw(self, window, delta: float):
position_on_screen = (self.size * self.position_x + self.offset_x, self.size * self.position_y + self.offset_y) position_on_screen = (self.size * self.position_x + self.offset_x, self.size * self.position_y + self.offset_y)
window.blit(self.rotated_image, position_on_screen) window.blit(self.rotated_image, position_on_screen)
@ -320,7 +324,6 @@ class Minesweeper:
self.rotation_degrees = dirr self.rotation_degrees = dirr
self.rotated_image = pygame.transform.rotate(self.image, dirr) self.rotated_image = pygame.transform.rotate(self.image, dirr)
def move(self, dir: int = -1): def move(self, dir: int = -1):
# południe - 0 # południe - 0
# wschód - 90 # wschód - 90
@ -391,7 +394,6 @@ class Minesweeper:
return return
for mine in self.current_map.mines: for mine in self.current_map.mines:
if (self.position_x, self.position_y) == (mine.position_x, mine.position_y): if (self.position_x, self.position_y) == (mine.position_x, mine.position_y):
tree = decisionTrees.DecisionTrees() tree = decisionTrees.DecisionTrees()
decision = tree.return_predict() decision = tree.return_predict()

View File

@ -1,5 +1,6 @@
import pygame import pygame
class Window: class Window:
window: None window: None
width: int width: int

View File

@ -1,6 +1,7 @@
# pygame - biblioteka do symulacji graficznych # pygame - biblioteka do symulacji graficznych
from multiprocessing import freeze_support from multiprocessing import freeze_support
import os import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame import pygame
# system - klasy związane z pygame # system - klasy związane z pygame
@ -22,6 +23,7 @@ FPS = 60
# włączenie tekstu # włączenie tekstu
pygame.font.init() pygame.font.init()
def main(): def main():
if MUSIC: if MUSIC:
pygame.mixer.init() pygame.mixer.init()