PEP 8 reformat + generate random data for mine_characteristics
This commit is contained in:
parent
d2820d872b
commit
213e6ce076
@ -15,7 +15,6 @@ class BFS:
|
||||
new_nodes = []
|
||||
neighbours_list = self.agent.sensor(current_position[0], current_position[1])
|
||||
|
||||
|
||||
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] == 'grass':
|
||||
@ -134,8 +133,6 @@ class BFS:
|
||||
|
||||
return new_nodes
|
||||
|
||||
|
||||
|
||||
# fringe = struktura danych przeszowyjąca wierchowki do odwiedzenia
|
||||
# explored = lista odwiedzonych stanow
|
||||
# position_at_beginning = stan poczatkowy
|
||||
@ -149,25 +146,22 @@ class BFS:
|
||||
|
||||
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
|
||||
root = node.Node(None, None, position_at_beginning, 2) # parent, action, position, cost
|
||||
|
||||
heapq.heappush(fringe, (0, root)) # add first node to fringe
|
||||
|
||||
|
||||
while len(fringe) != 0: # poki sa wezly do odwiedzenia(na fringe)
|
||||
|
||||
|
||||
if len(fringe) == 0:
|
||||
return False
|
||||
|
||||
|
||||
# get node from fringe
|
||||
tmp_node = heapq.heappop(fringe) # tuple(priority, node)
|
||||
tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy
|
||||
|
||||
|
||||
# jesli tmp node to goaltest
|
||||
if tmp_node_position[:2] == goaltest:
|
||||
print('Find')
|
||||
@ -182,8 +176,6 @@ class BFS:
|
||||
|
||||
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
|
||||
print(neighbours_list_of_our_node)
|
||||
|
||||
@ -193,7 +185,6 @@ class BFS:
|
||||
notInFringe = True # false if node in fringe
|
||||
notInExplored = True # false if node in explored
|
||||
|
||||
|
||||
p = manhattan(node_[1], goaltest) + node_[2]
|
||||
# wyznacza jaki jest priorytet nastempnika
|
||||
# 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
|
||||
# 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
|
||||
priority_in_fringe = fringeNode[0]
|
||||
# number of element in fringe
|
||||
index_of_node_in_fringe = counter
|
||||
counter = counter + 1
|
||||
|
||||
|
||||
for exploredNode in explored: # isc po wszystkich wezlach z listy 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
|
||||
|
||||
|
||||
# if node not in fringe and not in explored
|
||||
if notInFringe and notInExplored:
|
||||
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[1] = x
|
||||
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)
|
||||
|
@ -7,37 +7,17 @@ from numpy import random
|
||||
class DecisionTrees:
|
||||
|
||||
def return_predict(self):
|
||||
# Header looks like:
|
||||
# header = ['Size(bigger_more_difficult)', 'Year(older_more_difficult)', 'Protection_from_defuse',
|
||||
# 'Meters_under_the_ground', 'Random_detonation_chance', 'Detonation_power_in_m',
|
||||
# 'Decision']
|
||||
|
||||
# read data
|
||||
|
||||
df = pd.read_csv("D:\\1 Python projects\Saper\data\db.txt")
|
||||
|
||||
# print data
|
||||
# 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
|
||||
config = {'algorithm': 'ID3'}
|
||||
# create decision tree
|
||||
@ -46,5 +26,19 @@ class DecisionTrees:
|
||||
# print predict
|
||||
# print(chef.predict(model, [1, 2022, 0, 0, 0, 10]))
|
||||
|
||||
predict = normalized_data_for_predict[random.randint(0, 199)]
|
||||
return chef.predict(model, predict)
|
||||
size = random.randint(1, 10)
|
||||
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)
|
||||
|
@ -5,6 +5,7 @@ from classes import decisionTrees
|
||||
|
||||
pygame.mixer.init()
|
||||
|
||||
|
||||
# mina
|
||||
class Mine:
|
||||
position_x: int
|
||||
@ -143,7 +144,8 @@ class Map:
|
||||
elif ok and rng < 2 and not (i < 2 and j < 3):
|
||||
matrix[i].append(0) # piasek
|
||||
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)
|
||||
# self.cliffs.append(cliff)
|
||||
cliff = Cliff(j + 1, i, self.tile_size, rand_rotate, type=0)
|
||||
@ -173,7 +175,8 @@ class Map:
|
||||
# narysowanie na ekranie terenu
|
||||
for i in range(len(self.terrain_matrix)):
|
||||
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):
|
||||
for mine in self.mines:
|
||||
@ -181,6 +184,7 @@ class Map:
|
||||
for cliff in self.cliffs:
|
||||
cliff.draw(self.window.window)
|
||||
|
||||
|
||||
# broń
|
||||
class Weapon:
|
||||
size: int
|
||||
@ -191,6 +195,7 @@ class Weapon:
|
||||
weight: float
|
||||
weapon_type: int
|
||||
|
||||
|
||||
# baza operacji
|
||||
class Base:
|
||||
position_x: int
|
||||
@ -201,6 +206,7 @@ class Base:
|
||||
weapon_level: int
|
||||
weapons = []
|
||||
|
||||
|
||||
# schron
|
||||
class Shelter:
|
||||
position_x: int
|
||||
@ -208,6 +214,7 @@ class Shelter:
|
||||
size: int
|
||||
image: pygame.surface.Surface
|
||||
|
||||
|
||||
# składowisko
|
||||
class Dumpster:
|
||||
position_x: int
|
||||
@ -232,7 +239,6 @@ class NPC:
|
||||
weight: float
|
||||
|
||||
|
||||
|
||||
# saper
|
||||
class Minesweeper:
|
||||
size: int
|
||||
@ -251,7 +257,6 @@ class Minesweeper:
|
||||
ability = 1
|
||||
max_carried_weight = 5.0
|
||||
|
||||
|
||||
def __init__(self, position_x=0, position_y=0, size=64):
|
||||
self.position_x = position_x
|
||||
self.position_y = position_y
|
||||
@ -293,7 +298,6 @@ class Minesweeper:
|
||||
if self.current_map.terrain_matrix[self.position_y][self.position_x] < 5:
|
||||
self.speed = 1
|
||||
|
||||
|
||||
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)
|
||||
window.blit(self.rotated_image, position_on_screen)
|
||||
@ -320,7 +324,6 @@ class Minesweeper:
|
||||
self.rotation_degrees = dirr
|
||||
self.rotated_image = pygame.transform.rotate(self.image, dirr)
|
||||
|
||||
|
||||
def move(self, dir: int = -1):
|
||||
# południe - 0
|
||||
# wschód - 90
|
||||
@ -391,7 +394,6 @@ class Minesweeper:
|
||||
return
|
||||
for mine in self.current_map.mines:
|
||||
if (self.position_x, self.position_y) == (mine.position_x, mine.position_y):
|
||||
|
||||
tree = decisionTrees.DecisionTrees()
|
||||
|
||||
decision = tree.return_predict()
|
||||
|
@ -1,5 +1,6 @@
|
||||
import pygame
|
||||
|
||||
|
||||
class Window:
|
||||
window: None
|
||||
width: int
|
||||
|
2
main.py
2
main.py
@ -1,6 +1,7 @@
|
||||
# pygame - biblioteka do symulacji graficznych
|
||||
from multiprocessing import freeze_support
|
||||
import os
|
||||
|
||||
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
|
||||
import pygame
|
||||
# system - klasy związane z pygame
|
||||
@ -22,6 +23,7 @@ FPS = 60
|
||||
# włączenie tekstu
|
||||
pygame.font.init()
|
||||
|
||||
|
||||
def main():
|
||||
if MUSIC:
|
||||
pygame.mixer.init()
|
||||
|
Loading…
Reference in New Issue
Block a user