Compare commits
7 Commits
genetic_te
...
master
Author | SHA1 | Date | |
---|---|---|---|
ee864b16c7 | |||
|
71b2ecc23f | ||
|
936e0d7f4d | ||
749f87a428 | |||
74f58ec742 | |||
8a41a8e4f0 | |||
3dd6b8a9ee |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
__pycache__/drzewo_decyzyjne.cpython-310.pyc
Normal file
BIN
__pycache__/drzewo_decyzyjne.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
agent.py
2
agent.py
@ -47,7 +47,7 @@ class Agent(pygame.sprite.Sprite):
|
||||
self._layer = AGENT_LAYER
|
||||
self.damage = 50*self.level
|
||||
|
||||
self.artifact = True
|
||||
self.artifact = "tak"
|
||||
|
||||
def update(self):
|
||||
|
||||
|
2
astar.py
2
astar.py
@ -34,7 +34,7 @@ class Astar():
|
||||
def a_star(self, goal):
|
||||
path = []
|
||||
start = (self.g.agent.rect.x//TILE_SIZE, self.g.agent.rect.y//TILE_SIZE)
|
||||
print(start,goal)
|
||||
#print(start,goal)
|
||||
open_set = []
|
||||
heapq.heappush(open_set, (0, start)) # Priority queue with the start position
|
||||
came_from = {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
zdrowie_bohatera,moc_bohatera,moc_moba,lvl_wiekszy_bohater,mob_jest_strzelcem,zdrowie_moba,artefakt,akcja
|
||||
100,tak,tak,tak,tak,100,tak,zmien_kierunek
|
||||
100,tak,tak,tak,tak,100,tak,walcz
|
||||
100,tak,tak,tak,tak,100,nie,zmien_kierunek
|
||||
100,tak,tak,tak,tak,50,tak,zmien_kierunek
|
||||
100,tak,tak,tak,tak,50,nie,zmien_kierunek
|
150
drzewo_decyzyjne.py
Normal file
150
drzewo_decyzyjne.py
Normal file
@ -0,0 +1,150 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
class Tree():
|
||||
|
||||
|
||||
# Obliczanie entropii dla całego zbioru danych
|
||||
def oblicz_calkowita_entropie(self,dane_treningowe, etykieta, lista_klas):
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
calkowita_entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_treningowe[dane_treningowe[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = - (liczba_wystapien_klasy / liczba_wierszy) * np.log2(liczba_wystapien_klasy / liczba_wierszy)
|
||||
calkowita_entropia += entropia_klasy
|
||||
|
||||
return calkowita_entropia
|
||||
|
||||
|
||||
# Obliczanie entropii dla przefiltrowanego zbioru danych
|
||||
def oblicz_entropie(self,dane_wartosci_cechy, etykieta, lista_klas):
|
||||
liczba_wystapien_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = 0
|
||||
|
||||
if liczba_wystapien_klasy != 0:
|
||||
prawdopodobienstwo_klasy = liczba_wystapien_klasy / liczba_wystapien_cechy
|
||||
entropia_klasy = - prawdopodobienstwo_klasy * np.log2(prawdopodobienstwo_klasy)
|
||||
|
||||
entropia += entropia_klasy
|
||||
|
||||
return entropia
|
||||
|
||||
|
||||
# Obliczanie przyrostu informacji dla danej cechy
|
||||
def oblicz_przyrost_informacji(self,nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
unikalne_wartosci_cechy = dane_treningowe[nazwa_cechy].unique()
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
informacja_cechy = 0.0
|
||||
|
||||
for wartosc_cechy in unikalne_wartosci_cechy:
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
liczba_wystapien_wartosci_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia_wartosci_cechy = self.oblicz_entropie(dane_wartosci_cechy, etykieta, lista_klas)
|
||||
prawdopodobienstwo_wartosci_cechy = liczba_wystapien_wartosci_cechy / liczba_wierszy
|
||||
informacja_cechy += prawdopodobienstwo_wartosci_cechy * entropia_wartosci_cechy
|
||||
|
||||
return self.oblicz_calkowita_entropie(dane_treningowe, etykieta, lista_klas) - informacja_cechy
|
||||
|
||||
|
||||
# Znajdowanie najbardziej informatywnej cechy (cechy o najwyższym przyroście informacji)
|
||||
def znajdz_najbardziej_informatywna_ceche(self,dane_treningowe, etykieta, lista_klas):
|
||||
lista_cech = dane_treningowe.columns.drop(etykieta)
|
||||
# Etykieta nie jest cechą, więc ją usuwamy
|
||||
max_przyrost_informacji = -1
|
||||
najbardziej_informatywna_cecha = None
|
||||
|
||||
for cecha in lista_cech:
|
||||
przyrost_informacji_cechy = self.oblicz_przyrost_informacji(cecha, dane_treningowe, etykieta, lista_klas)
|
||||
|
||||
if max_przyrost_informacji < przyrost_informacji_cechy:
|
||||
max_przyrost_informacji = przyrost_informacji_cechy
|
||||
najbardziej_informatywna_cecha = cecha
|
||||
|
||||
return najbardziej_informatywna_cecha
|
||||
|
||||
|
||||
# Dodawanie węzła do drzewa
|
||||
def generuj_poddrzewo(self,nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
slownik_licznosci_wartosci_cechy = dane_treningowe[nazwa_cechy].value_counts(sort=False)
|
||||
drzewo = {}
|
||||
|
||||
for wartosc_cechy, liczba in slownik_licznosci_wartosci_cechy.items():
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
|
||||
przypisany_do_wezla = False
|
||||
for klasa in lista_klas:
|
||||
liczba_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
|
||||
if liczba_klasy == liczba:
|
||||
drzewo[wartosc_cechy] = klasa
|
||||
dane_treningowe = dane_treningowe[dane_treningowe[nazwa_cechy] != wartosc_cechy]
|
||||
przypisany_do_wezla = True
|
||||
if not przypisany_do_wezla:
|
||||
drzewo[wartosc_cechy] = "?"
|
||||
|
||||
return drzewo, dane_treningowe
|
||||
|
||||
|
||||
# Wykonywanie algorytmu ID3 i generowanie drzewa
|
||||
def generuj_drzewo(self,korzen, poprzednia_wartosc_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
if dane_treningowe.shape[0] != 0:
|
||||
najbardziej_informatywna_cecha = self.znajdz_najbardziej_informatywna_ceche(dane_treningowe, etykieta, lista_klas)
|
||||
drzewo, dane_treningowe = self.generuj_poddrzewo(najbardziej_informatywna_cecha, dane_treningowe, etykieta, lista_klas)
|
||||
nastepny_korzen = None
|
||||
|
||||
if poprzednia_wartosc_cechy is not None:
|
||||
korzen[poprzednia_wartosc_cechy] = dict()
|
||||
korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha]
|
||||
else:
|
||||
korzen[najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[najbardziej_informatywna_cecha]
|
||||
|
||||
for wezel, galezie in list(nastepny_korzen.items()):
|
||||
if galezie == "?":
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[najbardziej_informatywna_cecha] == wezel]
|
||||
self.generuj_drzewo(nastepny_korzen, wezel, dane_wartosci_cechy, etykieta, lista_klas)
|
||||
|
||||
|
||||
# Znajdowanie unikalnych klas etykiety i rozpoczęcie algorytmu
|
||||
def id3(self,nasze_dane, etykieta):
|
||||
dane_treningowe = nasze_dane.copy()
|
||||
drzewo = {}
|
||||
lista_klas = dane_treningowe[etykieta].unique()
|
||||
self.generuj_drzewo(drzewo, None, dane_treningowe, etykieta, lista_klas)
|
||||
return drzewo
|
||||
|
||||
|
||||
# Przewidywanie na podstawie drzewa
|
||||
def przewiduj(self,drzewo, instancja):
|
||||
if not isinstance(drzewo, dict):
|
||||
return drzewo
|
||||
else:
|
||||
korzen = next(iter(drzewo))
|
||||
wartosc_cechy = instancja[korzen]
|
||||
if wartosc_cechy in drzewo[korzen]:
|
||||
return self.przewiduj(drzewo[korzen][wartosc_cechy], instancja)
|
||||
else:
|
||||
return 'walcz'
|
||||
|
||||
def tree(self,przyklad):
|
||||
# Wczytywanie danych
|
||||
nasze_dane = pd.read_csv("data")
|
||||
|
||||
drzewo = self.id3(nasze_dane, 'akcja')
|
||||
|
||||
return self.przewiduj(drzewo, przyklad)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#print(przewiduj(drzewo, przyklad))
|
||||
#print(drzewo)
|
@ -1,148 +0,0 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
# Wczytywanie danych
|
||||
nasze_dane = pd.read_csv("drzewo_decyzyjne\data")
|
||||
|
||||
|
||||
# Obliczanie entropii dla całego zbioru danych
|
||||
def oblicz_calkowita_entropie(dane_treningowe, etykieta, lista_klas):
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
calkowita_entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_treningowe[dane_treningowe[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = - (liczba_wystapien_klasy / liczba_wierszy) * np.log2(liczba_wystapien_klasy / liczba_wierszy)
|
||||
calkowita_entropia += entropia_klasy
|
||||
|
||||
return calkowita_entropia
|
||||
|
||||
|
||||
# Obliczanie entropii dla przefiltrowanego zbioru danych
|
||||
def oblicz_entropie(dane_wartosci_cechy, etykieta, lista_klas):
|
||||
liczba_wystapien_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = 0
|
||||
|
||||
if liczba_wystapien_klasy != 0:
|
||||
prawdopodobienstwo_klasy = liczba_wystapien_klasy / liczba_wystapien_cechy
|
||||
entropia_klasy = - prawdopodobienstwo_klasy * np.log2(prawdopodobienstwo_klasy)
|
||||
|
||||
entropia += entropia_klasy
|
||||
|
||||
return entropia
|
||||
|
||||
|
||||
# Obliczanie przyrostu informacji dla danej cechy
|
||||
def oblicz_przyrost_informacji(nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
unikalne_wartosci_cechy = dane_treningowe[nazwa_cechy].unique()
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
informacja_cechy = 0.0
|
||||
|
||||
for wartosc_cechy in unikalne_wartosci_cechy:
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
liczba_wystapien_wartosci_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia_wartosci_cechy = oblicz_entropie(dane_wartosci_cechy, etykieta, lista_klas)
|
||||
prawdopodobienstwo_wartosci_cechy = liczba_wystapien_wartosci_cechy / liczba_wierszy
|
||||
informacja_cechy += prawdopodobienstwo_wartosci_cechy * entropia_wartosci_cechy
|
||||
|
||||
return oblicz_calkowita_entropie(dane_treningowe, etykieta, lista_klas) - informacja_cechy
|
||||
|
||||
|
||||
# Znajdowanie najbardziej informatywnej cechy (cechy o najwyższym przyroście informacji)
|
||||
def znajdz_najbardziej_informatywna_ceche(dane_treningowe, etykieta, lista_klas):
|
||||
lista_cech = dane_treningowe.columns.drop(etykieta)
|
||||
# Etykieta nie jest cechą, więc ją usuwamy
|
||||
max_przyrost_informacji = -1
|
||||
najbardziej_informatywna_cecha = None
|
||||
|
||||
for cecha in lista_cech:
|
||||
przyrost_informacji_cechy = oblicz_przyrost_informacji(cecha, dane_treningowe, etykieta, lista_klas)
|
||||
|
||||
if max_przyrost_informacji < przyrost_informacji_cechy:
|
||||
max_przyrost_informacji = przyrost_informacji_cechy
|
||||
najbardziej_informatywna_cecha = cecha
|
||||
|
||||
return najbardziej_informatywna_cecha
|
||||
|
||||
|
||||
# Dodawanie węzła do drzewa
|
||||
def generuj_poddrzewo(nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
slownik_licznosci_wartosci_cechy = dane_treningowe[nazwa_cechy].value_counts(sort=False)
|
||||
drzewo = {}
|
||||
|
||||
for wartosc_cechy, liczba in slownik_licznosci_wartosci_cechy.items():
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
|
||||
przypisany_do_wezla = False
|
||||
for klasa in lista_klas:
|
||||
liczba_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
|
||||
if liczba_klasy == liczba:
|
||||
drzewo[wartosc_cechy] = klasa
|
||||
dane_treningowe = dane_treningowe[dane_treningowe[nazwa_cechy] != wartosc_cechy]
|
||||
przypisany_do_wezla = True
|
||||
if not przypisany_do_wezla:
|
||||
drzewo[wartosc_cechy] = "?"
|
||||
|
||||
return drzewo, dane_treningowe
|
||||
|
||||
|
||||
# Wykonywanie algorytmu ID3 i generowanie drzewa
|
||||
def generuj_drzewo(korzen, poprzednia_wartosc_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
if dane_treningowe.shape[0] != 0:
|
||||
najbardziej_informatywna_cecha = znajdz_najbardziej_informatywna_ceche(dane_treningowe, etykieta, lista_klas)
|
||||
drzewo, dane_treningowe = generuj_poddrzewo(najbardziej_informatywna_cecha, dane_treningowe, etykieta, lista_klas)
|
||||
nastepny_korzen = None
|
||||
|
||||
if poprzednia_wartosc_cechy is not None:
|
||||
korzen[poprzednia_wartosc_cechy] = dict()
|
||||
korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha]
|
||||
else:
|
||||
korzen[najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[najbardziej_informatywna_cecha]
|
||||
|
||||
for wezel, galezie in list(nastepny_korzen.items()):
|
||||
if galezie == "?":
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[najbardziej_informatywna_cecha] == wezel]
|
||||
generuj_drzewo(nastepny_korzen, wezel, dane_wartosci_cechy, etykieta, lista_klas)
|
||||
|
||||
|
||||
# Znajdowanie unikalnych klas etykiety i rozpoczęcie algorytmu
|
||||
def id3(nasze_dane, etykieta):
|
||||
dane_treningowe = nasze_dane.copy()
|
||||
drzewo = {}
|
||||
lista_klas = dane_treningowe[etykieta].unique()
|
||||
generuj_drzewo(drzewo, None, dane_treningowe, etykieta, lista_klas)
|
||||
return drzewo
|
||||
|
||||
|
||||
# Przewidywanie na podstawie drzewa
|
||||
def przewiduj(drzewo, instancja):
|
||||
if not isinstance(drzewo, dict):
|
||||
return drzewo
|
||||
else:
|
||||
korzen = next(iter(drzewo))
|
||||
wartosc_cechy = instancja[korzen]
|
||||
if wartosc_cechy in drzewo[korzen]:
|
||||
return przewiduj(drzewo[korzen][wartosc_cechy], instancja)
|
||||
else:
|
||||
return 'walcz'
|
||||
|
||||
|
||||
drzewo = id3(nasze_dane, 'akcja')
|
||||
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'nie',
|
||||
'moc_moba': 'nie',
|
||||
'lvl_wiekszy_bohater': 'tak',
|
||||
'mob_jest_strzelcem': 'nie',
|
||||
'zdrowie_moba': '1',
|
||||
'artefakt': 'tak'}
|
||||
|
||||
print(przewiduj(drzewo, przyklad))
|
||||
print(drzewo)
|
52
genetic.py
52
genetic.py
@ -1,52 +0,0 @@
|
||||
# wygenerowanie populacji polozenia mobow na mapie - Archer, Infantry, Flower, Rocks, Grass, Blank
|
||||
# Fitness
|
||||
# Crossover
|
||||
# Mutation
|
||||
|
||||
|
||||
|
||||
import random
|
||||
import pygame
|
||||
|
||||
|
||||
TILE_SIZE = 64
|
||||
TYPES = ['A', 'I', "F", 'R', 'G', 'B']
|
||||
MAP_TILES_LIST = list()
|
||||
population = list()
|
||||
max_population = 168 - 1
|
||||
population_size = 50
|
||||
|
||||
class Genetic():
|
||||
|
||||
def generate_population():
|
||||
if population_size > 168:
|
||||
print("GENETIC: Podana populacja przekracza limit miejsc na mapie")
|
||||
pygame.quit()
|
||||
|
||||
for i in range(155):
|
||||
MAP_TILES_LIST.append(i)
|
||||
random.shuffle(MAP_TILES_LIST)
|
||||
if 14 in MAP_TILES_LIST:
|
||||
MAP_TILES_LIST.remove(14)
|
||||
if 131 in MAP_TILES_LIST:
|
||||
MAP_TILES_LIST.remove(131)
|
||||
|
||||
for i in range(population_size):
|
||||
tile = MAP_TILES_LIST.pop()
|
||||
type = random.choice(TYPES)
|
||||
object = (tile,type)
|
||||
population.append(object)
|
||||
|
||||
if type == 'A':
|
||||
TYPES.remove('A')
|
||||
if type == 'I':
|
||||
TYPES.remove('I')
|
||||
|
||||
print(population)
|
||||
return population
|
||||
|
||||
def get_cell_x_y_cord(x,y):
|
||||
cell_x = x // TILE_SIZE
|
||||
cell_y = y // TILE_SIZE
|
||||
return cell_x, cell_y
|
||||
|
705
main.py
705
main.py
@ -1,239 +1,466 @@
|
||||
|
||||
import pygame
|
||||
from config import *
|
||||
from agent import *
|
||||
from map_add_ons import *
|
||||
from mobs import *
|
||||
from bfs import *
|
||||
from nn import *
|
||||
from astar import *
|
||||
from genetic import *
|
||||
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
||||
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
self.running = True
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
|
||||
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
||||
|
||||
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
|
||||
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
||||
|
||||
pygame.display.set_caption('Gra-SI')
|
||||
|
||||
self.bfs = Bfs(self)
|
||||
self.nn = NeuralN()
|
||||
self.astar = Astar(self)
|
||||
self.genetic = Genetic()
|
||||
|
||||
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
self.obstacles = [[False for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
|
||||
|
||||
|
||||
|
||||
def new(self): # tworzy się nowa sesja grania
|
||||
self.all_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.archer_orks = pygame.sprite.LayeredUpdates()
|
||||
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.sauronL = pygame.sprite.LayeredUpdates()
|
||||
self.flowers = pygame.sprite.LayeredUpdates()
|
||||
self.little_rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.agent = Agent(self,1,1)
|
||||
self.sauron = Sauron(self, 1, 10)
|
||||
self.obstacles[1][10] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
||||
|
||||
|
||||
self.list_object = list()
|
||||
self.list_object = Genetic.generate_population()
|
||||
l = len(self.list_object)
|
||||
while l > 0:
|
||||
obj = self.list_object.pop()
|
||||
x,y = self.bfs.get_coordinates(obj[0])
|
||||
cell_x, cell_y = Genetic.get_cell_x_y_cord(x,y)
|
||||
l=l-1
|
||||
if obj[1] == 'A':
|
||||
self.archer_ork = Archer_ork(self,cell_x,cell_y)
|
||||
self.obstacles[cell_x][cell_y] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
||||
if obj[1] == 'I':
|
||||
self.infantry_ork = Infantry_ork(self,cell_x,cell_y)
|
||||
self.obstacles[cell_x][cell_y] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
||||
if obj[1] == 'F':
|
||||
self.flower = Health_flower(self,cell_x,cell_y)
|
||||
if obj[1] == 'R':
|
||||
self.rock = Rocks(self,cell_x,cell_y)
|
||||
self.obstacles[cell_x][cell_y] = True
|
||||
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
|
||||
if obj[1] == 'G':
|
||||
self.grass = Grass(self,cell_x,cell_y)
|
||||
self.cell_costs[cell_x][cell_y] = 5
|
||||
if obj[1] == 'B':
|
||||
continue
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def update(self):
|
||||
self.all_sprites.update()
|
||||
|
||||
|
||||
def events(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
pygame.quit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
||||
self.move_agent(self.astar.a_star(self.goal_pos))
|
||||
|
||||
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
mouse_presses = pygame.mouse.get_pressed()
|
||||
if mouse_presses[0]:
|
||||
|
||||
gx = self.archer_ork.x
|
||||
gy = self.archer_ork.y
|
||||
a_cell_x, a_cell_y = Genetic.get_cell_x_y_cord(gx,gy)
|
||||
gx2 = self.infantry_ork.x
|
||||
gy2 = self.infantry_ork.y
|
||||
i_cell_x, i_cell_y = Genetic.get_cell_x_y_cord(gx2,gy2)
|
||||
|
||||
|
||||
x = self.sauron.x
|
||||
y = self.sauron.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.sauron.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "SAURON"
|
||||
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać
|
||||
if prediction == "SAURON" and self.agent.level < 3:
|
||||
x = self.archer_ork.x,
|
||||
y = self.archer_ork.y,
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.archer_ork.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "ORK_ARCHER"
|
||||
elif prediction == "SAURON" and self.agent.level >= 3:
|
||||
self.obstacles[1][10] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
|
||||
elif prediction == "ORK_INFANTRY":
|
||||
self.obstacles[i_cell_x][i_cell_y] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
if self.agent.current_health < self.agent.max_health:
|
||||
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
x = self.sauron.x
|
||||
y = self.sauron.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.sauron.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "SAURON"
|
||||
elif prediction == "ORK_ARCHER":
|
||||
self.obstacles[a_cell_x][a_cell_y] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
if self.agent.current_health < self.agent.max_health:
|
||||
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
x = self.infantry_ork.x,
|
||||
y = self.infantry_ork.y,
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.infantry_ork.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "ORK_INFANTRY"
|
||||
|
||||
|
||||
|
||||
|
||||
def prediction_road(self,x,y,mob_image):
|
||||
mob_goal_cell = (self.bfs.get_cell_number(x,y))
|
||||
if self.bfs.get_up_cell(mob_goal_cell) == None:
|
||||
goal_cell = self.bfs.get_down_cell(mob_goal_cell)
|
||||
x,y = self.bfs.get_coordinates(goal_cell)
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
prediction = self.nn.predict(mob_image)
|
||||
else:
|
||||
goal_cell = self.bfs.get_up_cell(mob_goal_cell)
|
||||
x,y = self.bfs.get_coordinates(goal_cell)
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
prediction = self.nn.predict(mob_image)
|
||||
return prediction
|
||||
|
||||
def move_agent(self,path):
|
||||
print("PATH:::::",path)
|
||||
for cell_to_move in path:
|
||||
x, y = self.bfs.get_coordinates(cell_to_move)
|
||||
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||
if(self.bfs.get_cell_number(self.agent.rect.x,self.agent.rect.y)!=cell_to_move):
|
||||
if x > self.agent.rect.x:
|
||||
self.agent.direction = 0
|
||||
elif y > self.agent.rect.y:
|
||||
self.agent.direction = 1
|
||||
elif x < self.agent.rect.x:
|
||||
self.agent.direction = 2
|
||||
elif y < self.agent.rect.y:
|
||||
self.agent.direction = 3
|
||||
if self.agent.direction==0:
|
||||
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.x_change += TILE_SIZE
|
||||
elif self.agent.direction==1:
|
||||
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.y_change += TILE_SIZE
|
||||
elif self.agent.direction==2:
|
||||
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.x_change -= TILE_SIZE
|
||||
elif self.agent.direction==3:
|
||||
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.y_change -= TILE_SIZE
|
||||
|
||||
self.agent.rotate()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
print("Polozenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||
self.clock.tick(2)
|
||||
|
||||
def map(self): # tworzenie mapy
|
||||
self.clock.tick(FRAMERATE)
|
||||
for x in range(0, WIDTH, TILE_SIZE):
|
||||
for y in range(0, 768, TILE_SIZE):
|
||||
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
||||
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
||||
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
||||
self.flowers.draw(self.SCREEN)
|
||||
self.all_sprites.draw(self.SCREEN)
|
||||
self.rock_sprites.draw(self.SCREEN)
|
||||
self.grass_sprites.draw(self.SCREEN)
|
||||
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
||||
pygame.display.update()
|
||||
|
||||
def main(self):
|
||||
self.events()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
|
||||
g = Game()
|
||||
g.new()
|
||||
|
||||
while g.running:
|
||||
g.main()
|
||||
|
||||
import pygame
|
||||
from config import *
|
||||
from agent import *
|
||||
from map_add_ons import *
|
||||
from mobs import *
|
||||
from bfs import *
|
||||
from nn import *
|
||||
from astar import *
|
||||
import math
|
||||
import random
|
||||
from drzewo_decyzyjne import *
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
||||
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
self.running = True
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
|
||||
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
||||
|
||||
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
|
||||
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
||||
|
||||
pygame.display.set_caption('Gra-SI')
|
||||
|
||||
self.bfs = Bfs(self)
|
||||
self.nn = NeuralN()
|
||||
self.astar = Astar(self)
|
||||
self.tree = Tree()
|
||||
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
self.obstacles = [[False for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
|
||||
|
||||
|
||||
|
||||
def new(self): # tworzy się nowa sesja grania
|
||||
self.all_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.archer_orks = pygame.sprite.LayeredUpdates()
|
||||
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.sauronL = pygame.sprite.LayeredUpdates()
|
||||
self.flowers = pygame.sprite.LayeredUpdates()
|
||||
self.little_rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
#Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
|
||||
self.allpositions=[]
|
||||
self.allpositionsSet=set()
|
||||
while(len(self.allpositionsSet)<100):
|
||||
self.positions=[] #.append
|
||||
self.positionsSet=set() #.add
|
||||
for x in range(16):
|
||||
while len(self.positionsSet)<16:
|
||||
pos1=random.randint(0,12) #x
|
||||
pos2=random.randint(0,11) #y
|
||||
pom=(pos1,pos2)
|
||||
lenSetBefore=len(self.positionsSet)
|
||||
self.positionsSet.add(pom)
|
||||
lenSetAfter=len(self.positionsSet)
|
||||
if(lenSetAfter>lenSetBefore):
|
||||
self.positions.append(pom)
|
||||
AllPositionsSetB=len(self.allpositionsSet)
|
||||
self.allpositionsSet.add(tuple(self.positions))
|
||||
AllPositionsSetA=len(self.allpositionsSet)
|
||||
if(AllPositionsSetA>AllPositionsSetB):
|
||||
self.positions.append((1000))
|
||||
self.allpositions.append(self.positions)
|
||||
#print("TO SA KOLEJNE po randomowaniu")
|
||||
#print(self.allpositions)
|
||||
def sprawdz_powtorzenia(tablica):
|
||||
wystapienia = set()
|
||||
for element in tablica[:-1]:
|
||||
if tuple(element) in wystapienia:
|
||||
return True # Powtórzenie znalezione
|
||||
wystapienia.add(tuple(element))
|
||||
return False # Brak powtórzeń
|
||||
def ocena_tablicy(dane):
|
||||
for x in range(100):
|
||||
grade=0
|
||||
if(sprawdz_powtorzenia(dane[x])):
|
||||
dane[x][-1]=10000000
|
||||
else:
|
||||
x1,y1=dane[x][0]
|
||||
x2,y2=dane[x][1]
|
||||
x3,y3=dane[x][2]
|
||||
x4,y4=dane[x][3]
|
||||
x5,y5=dane[x][4]
|
||||
|
||||
|
||||
|
||||
r1=math.sqrt((x1 - x4)**2 + (y1 - y4)**2)
|
||||
r2=math.sqrt((x4 - x2)**2 + (y4 - y2)**2)
|
||||
r3=math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
|
||||
r4=math.sqrt((x3 - x5)**2 + (y3 - y5)**2)
|
||||
r5=math.sqrt((x5 - x4)**2 + (y5 - y4)**2)
|
||||
r12=math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
|
||||
r13=math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
|
||||
r14=math.sqrt((x1 - x5)**2 + (y1 - y5)**2)
|
||||
spr=math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
|
||||
spr1=math.sqrt((x2 - x4)**2 + (y2 - y4)**2)
|
||||
spr2=math.sqrt((x2 - x5)**2 + (y2 - y5)**2)
|
||||
spr3=math.sqrt((x3 - x4)**2 + (y3 - y4)**2)
|
||||
spr4=math.sqrt((x3 - x5)**2 + (y3 - y5)**2)
|
||||
spr5=math.sqrt((x4 - x5)**2 + (y4 - y5)**2)
|
||||
avg=(r1+r2+r3+r4+r5)/5
|
||||
grade=abs(r1-avg)+abs(r2-avg)+abs(r3-avg)+abs(r4-avg)+abs(r5-avg)
|
||||
if(r1<5):
|
||||
grade=grade+2
|
||||
if(r12<5):
|
||||
grade=grade+2
|
||||
if(r13<5):
|
||||
grade=grade+2
|
||||
if(r14<5):
|
||||
grade=grade+2
|
||||
if(spr<5 or spr1<5 or spr2<5 or spr3<5 or spr4<5 or spr5<5):
|
||||
grade=grade+5
|
||||
|
||||
|
||||
x6,y6=dane[x][-2]
|
||||
x7,y7=dane[x][-3]
|
||||
x8,y8=dane[x][-4]
|
||||
x9,y9=dane[x][-5]
|
||||
x0,y0=dane[x][-6]
|
||||
rock1=math.sqrt((x6 - x7)**2 + (y6 - y7)**2)
|
||||
rock2=math.sqrt((x6 - x8)**2 + (y6 - y8)**2)
|
||||
rock3=math.sqrt((x6 - x9)**2 + (y6 - y9)**2)
|
||||
rock4=math.sqrt((x6 - x0)**2 + (y6 - y0)**2)
|
||||
|
||||
rock5=math.sqrt((x7 - x8)**2 + (y7 - y8)**2)
|
||||
rock6=math.sqrt((x7 - x9)**2 + (y7 - y9)**2)
|
||||
rock7=math.sqrt((x7 - x0)**2 + (y7 - y0)**2)
|
||||
|
||||
rock8=math.sqrt((x8 - x9)**2 + (y8 - y9)**2)
|
||||
rock9=math.sqrt((x8 - x0)**2 + (y8 - y0)**2)
|
||||
|
||||
rock0=math.sqrt((x9 - x0)**2 + (y9 - y0)**2)
|
||||
if(rock1<2 or rock2<2 or rock3<2 or rock4<2 or rock5<2 or rock6<2 or rock7<2 or rock8<2 or rock9<2 or rock0<2):
|
||||
grade=grade+3
|
||||
|
||||
|
||||
grade=round(grade,2)*100
|
||||
dane[x][-1]=grade
|
||||
return dane
|
||||
def sort_tablicy(dane):
|
||||
posortowana_tablica = sorted(dane, key=lambda x: x[-1])
|
||||
return posortowana_tablica
|
||||
def generuj_pary(n):
|
||||
pary = []
|
||||
for i in range(1, n+1):
|
||||
for j in range(i+1, n+1):
|
||||
pary.append([i, j])
|
||||
return pary
|
||||
self.WszystkiePary=generuj_pary(7)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("WszystkiePary")
|
||||
#print(self.WszystkiePary)
|
||||
|
||||
def polacz_tablice(tablica1, tablica2,n):
|
||||
nowa_tablica = tablica1[:n] + tablica2[n:]
|
||||
return nowa_tablica
|
||||
self.positionsAfterGrade=ocena_tablicy(self.allpositions)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("")
|
||||
#print("Po ocenie ")
|
||||
#print(self.positionsAfterGrade)
|
||||
self.sortedAfterGrade=sort_tablicy(self.positionsAfterGrade)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("")
|
||||
#print("Po sortowaniu ")
|
||||
#print(self.sortedAfterGrade)
|
||||
n=100
|
||||
self.licznik=0
|
||||
|
||||
while(self.sortedAfterGrade[0][16]!=0 and self.licznik <n):
|
||||
#print("NUMER ITERACJI: "+str(self.licznik))
|
||||
#print("")
|
||||
#print("")
|
||||
|
||||
self.WynikKombinacji=[]
|
||||
pomWynikInt=0
|
||||
pomWszystkieParyInt=0
|
||||
|
||||
while(len(self.WynikKombinacji)<20 and pomWszystkieParyInt<21):
|
||||
|
||||
|
||||
gen1=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][0]-1]
|
||||
gen1[-1]=9966
|
||||
#print("gen1")
|
||||
#print(gen1)
|
||||
gen2=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][1]-1]
|
||||
gen2[-1]=9966
|
||||
#print("gen2")
|
||||
#print(gen2)
|
||||
rollKombinacja=random.randint(0,100)# chance 60%
|
||||
|
||||
#print("rollKombinacja:"+str(rollKombinacja))
|
||||
|
||||
if(rollKombinacja<61):
|
||||
KombInt=random.randint(1,4)
|
||||
#print("KombInt:"+str(KombInt))
|
||||
#print("Przed append")
|
||||
#print(self.WynikKombinacji)
|
||||
losujKtoGen1=random.randint(0,100)
|
||||
#print("")
|
||||
#print(losujKtoGen1)
|
||||
if(losujKtoGen1>50):
|
||||
self.WynikKombinacji.append(polacz_tablice(gen1,gen2,KombInt))
|
||||
else:
|
||||
self.WynikKombinacji.append(polacz_tablice(gen2,gen1,KombInt))
|
||||
#print("Po append")
|
||||
#print(self.WynikKombinacji)
|
||||
rollMutacja=random.randint(0,100)# chance 10%
|
||||
#print("rollMutacja:"+str(rollMutacja))
|
||||
if(rollMutacja<90):
|
||||
#print("rolowanie mutacji")
|
||||
MutacjaInt=random.randint(0,4)
|
||||
#print(MutacjaInt)
|
||||
xPoMutacji=random.randint(0,12) #x
|
||||
yPoMutacji=random.randint(0,11) #y
|
||||
#print("rolowanie x y")
|
||||
#print(xPoMutacji,yPoMutacji)
|
||||
self.WynikKombinacji[pomWynikInt][MutacjaInt]=[xPoMutacji,yPoMutacji]
|
||||
pomWynikInt=pomWynikInt+1
|
||||
pomWszystkieParyInt=pomWszystkieParyInt+1
|
||||
laczenieGeneracji=self.sortedAfterGrade[:(100-len(self.WynikKombinacji))]+self.WynikKombinacji
|
||||
#print("pewna czesc ")
|
||||
#print(self.sortedAfterGrade[:(100-len(self.WynikKombinacji))])
|
||||
#print("reszta ")
|
||||
#print("")
|
||||
#print(self.WynikKombinacji)
|
||||
|
||||
OcenaWszystkich=ocena_tablicy(laczenieGeneracji)
|
||||
#print("Przed DODANIEM GENERACJI")
|
||||
#print(self.sortedAfterGrade)
|
||||
#print("")
|
||||
#print("")
|
||||
self.sortedAfterGrade=sort_tablicy(OcenaWszystkich)
|
||||
#print("Po DODANIU GENERACJI")
|
||||
#print(self.sortedAfterGrade)
|
||||
self.licznik=self.licznik+1
|
||||
#self.sortedAfterGrade[0] najlepszy
|
||||
#print("")
|
||||
#print("")
|
||||
print(self.sortedAfterGrade)
|
||||
self.najlepszaGeneracja=self.sortedAfterGrade[0] #Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
|
||||
|
||||
|
||||
self.agent = Agent(self,self.najlepszaGeneracja[0][0],self.najlepszaGeneracja[0][1])
|
||||
self.archer_ork = Archer_ork(self,self.najlepszaGeneracja[1][0],self.najlepszaGeneracja[1][1])
|
||||
self.obstacles[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
||||
self.infantry_ork = Infantry_ork(self,self.najlepszaGeneracja[2][0],self.najlepszaGeneracja[2][1])
|
||||
self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
||||
|
||||
self.sauron = Sauron(self, self.najlepszaGeneracja[3][0], self.najlepszaGeneracja[3][1])
|
||||
self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
||||
self.flower = Health_flower(self, self.najlepszaGeneracja[4][0],self.najlepszaGeneracja[4][1])
|
||||
|
||||
for y in range (6):
|
||||
self.grass = Grass(self,self.najlepszaGeneracja[y+5][0],self.najlepszaGeneracja[y+5][1])
|
||||
self.cell_costs[self.najlepszaGeneracja[y+5][0]][self.najlepszaGeneracja[y+5][1]] = 5
|
||||
|
||||
for y in range(5):
|
||||
self.rock = Rocks(self,self.najlepszaGeneracja[y+11][0],self.najlepszaGeneracja[y+11][1])
|
||||
self.obstacles[self.najlepszaGeneracja[y+11][0]][self.najlepszaGeneracja[y+11][1]] = True
|
||||
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
|
||||
|
||||
|
||||
def update(self):
|
||||
self.all_sprites.update()
|
||||
|
||||
|
||||
def events(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
pygame.quit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.goal_cell = self.bfs.get_cell_number(self.flower.x, self.flower.y)
|
||||
self.move_agent(self.bfs.bfs(self.goal_cell))
|
||||
|
||||
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
mouse_presses = pygame.mouse.get_pressed()
|
||||
if mouse_presses[0]:
|
||||
|
||||
x = self.sauron.x
|
||||
y = self.sauron.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.sauron.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
#prediction = "SAURON"
|
||||
while True: #do poprawienia poprawne rozpoznawanie
|
||||
print("goal: ",goal)
|
||||
if prediction == "SAURON":
|
||||
if self.agent.level < self.sauron.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.sauron.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
else:
|
||||
x = self.archer_ork.x
|
||||
y = self.archer_ork.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.archer_ork.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "ORK_ARCHER"
|
||||
|
||||
elif prediction == "ORK_MELEE":
|
||||
if self.agent.level < self.infantry_ork.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.infantry_ork.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
if self.agent.current_health < self.agent.max_health:
|
||||
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
x = self.sauron.x
|
||||
y = self.sauron.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.sauron.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
#prediction = "SAURON"
|
||||
elif prediction == "ORK_ARCHER":
|
||||
if self.agent.level < self.archer_ork.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.archer_ork.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = False
|
||||
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
|
||||
if self.agent.current_health < self.agent.max_health:
|
||||
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
x = self.infantry_ork.x
|
||||
y = self.infantry_ork.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.infantry_ork.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
#prediction = "ORK_INFANTRY"
|
||||
|
||||
|
||||
|
||||
|
||||
def prediction_road(self,x,y,mob_image):
|
||||
mob_goal_cell = (self.bfs.get_cell_number(x,y))
|
||||
if self.bfs.get_up_cell(mob_goal_cell) == None:
|
||||
goal_cell = self.bfs.get_down_cell(mob_goal_cell)
|
||||
x,y = self.bfs.get_coordinates(goal_cell)
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
prediction = self.nn.predict(mob_image)
|
||||
else:
|
||||
goal_cell = self.bfs.get_up_cell(mob_goal_cell)
|
||||
x,y = self.bfs.get_coordinates(goal_cell)
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
prediction = self.nn.predict(mob_image)
|
||||
return prediction
|
||||
|
||||
def move_agent(self,path):
|
||||
print("PATH:::::",path)
|
||||
for cell_to_move in path:
|
||||
x, y = self.bfs.get_coordinates(cell_to_move)
|
||||
#print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||
if(self.bfs.get_cell_number(self.agent.rect.x,self.agent.rect.y)!=cell_to_move):
|
||||
if x > self.agent.rect.x:
|
||||
self.agent.direction = 0
|
||||
elif y > self.agent.rect.y:
|
||||
self.agent.direction = 1
|
||||
elif x < self.agent.rect.x:
|
||||
self.agent.direction = 2
|
||||
elif y < self.agent.rect.y:
|
||||
self.agent.direction = 3
|
||||
if self.agent.direction==0:
|
||||
##print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.x_change += TILE_SIZE
|
||||
elif self.agent.direction==1:
|
||||
##print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.y_change += TILE_SIZE
|
||||
elif self.agent.direction==2:
|
||||
##print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.x_change -= TILE_SIZE
|
||||
elif self.agent.direction==3:
|
||||
##print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||
self.agent.y_change -= TILE_SIZE
|
||||
|
||||
self.agent.rotate()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
#print("Polozenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||
self.clock.tick(2)
|
||||
|
||||
def map(self): # tworzenie mapy
|
||||
self.clock.tick(FRAMERATE)
|
||||
for x in range(0, WIDTH, TILE_SIZE):
|
||||
for y in range(0, 768, TILE_SIZE):
|
||||
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
||||
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
||||
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
||||
self.flowers.draw(self.SCREEN)
|
||||
self.all_sprites.draw(self.SCREEN)
|
||||
self.rock_sprites.draw(self.SCREEN)
|
||||
self.grass_sprites.draw(self.SCREEN)
|
||||
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
||||
pygame.display.update()
|
||||
|
||||
def main(self):
|
||||
self.events()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
|
||||
g = Game()
|
||||
g.new()
|
||||
|
||||
while g.running:
|
||||
g.main()
|
||||
|
14
mobs.py
14
mobs.py
@ -14,7 +14,7 @@ class Archer_ork(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.image_path = "./zdjecia/ORK_ARCHER/ork_lucznik.png"
|
||||
self.image_path = "./zdjecia/ORK_ARCHER/ork_archer (889).jpg"
|
||||
self.ARCHER_ORK_IMG = pygame.image.load(self.image_path)
|
||||
self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
|
||||
|
||||
@ -29,6 +29,8 @@ class Archer_ork(pygame.sprite.Sprite):
|
||||
self.level = 1
|
||||
self.damage = 50*self.level
|
||||
self.health = 50
|
||||
|
||||
self.archer = 'tak'
|
||||
|
||||
class Infantry_ork(pygame.sprite.Sprite):
|
||||
|
||||
@ -43,7 +45,7 @@ class Infantry_ork(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.image_path = "./zdjecia/ORK_MELEE/ork-piechota.png"
|
||||
self.image_path = "C:\\mobs_photos\\ork_melee (11).jpg"#sciezka do zmiany
|
||||
self.INFANTRY_ORK_IMG = pygame.image.load(self.image_path)
|
||||
self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64))
|
||||
|
||||
@ -59,6 +61,8 @@ class Infantry_ork(pygame.sprite.Sprite):
|
||||
self.damage = 50*self.level
|
||||
self.health = 100
|
||||
|
||||
self.archer = 'nie'
|
||||
|
||||
|
||||
class Sauron(pygame.sprite.Sprite):
|
||||
|
||||
@ -73,7 +77,7 @@ class Sauron(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.image_path = "./zdjecia/SAURON/sauron.png"
|
||||
self.image_path = "C:\\mobs_photos\\sauron (700).jpg"#sciezka do zmiany
|
||||
self.SAURON_IMG = pygame.image.load(self.image_path)
|
||||
self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64))
|
||||
|
||||
@ -87,4 +91,6 @@ class Sauron(pygame.sprite.Sprite):
|
||||
|
||||
self.level = 3
|
||||
self.damage = 50*self.level
|
||||
self.health = 150
|
||||
self.health = 150
|
||||
|
||||
self.archer = 'nie'
|
35
nn.py
35
nn.py
@ -11,8 +11,8 @@ import pathlib
|
||||
|
||||
|
||||
class NeuralN:
|
||||
# @staticmethod
|
||||
def predict(self,image_path):
|
||||
# @staticmethod
|
||||
def predict(self, image_path):
|
||||
data_dir = pathlib.Path('zdjecia')
|
||||
saved_model_path = pathlib.Path('trained_model.h5')
|
||||
class_names_path = pathlib.Path("class_names.pkl")
|
||||
@ -47,12 +47,6 @@ class NeuralN:
|
||||
image_size=(180, 180),
|
||||
batch_size=32)
|
||||
|
||||
# test_ds = tf.keras.utils.image_dataset_from_directory(
|
||||
# data_dir,
|
||||
# seed=123,
|
||||
# image_size=(180, 180),
|
||||
# batch_size=32)
|
||||
|
||||
class_names = train_ds.class_names
|
||||
print(class_names)
|
||||
|
||||
@ -77,7 +71,7 @@ class NeuralN:
|
||||
metrics=['accuracy'])
|
||||
model.summary()
|
||||
|
||||
epochs = 1
|
||||
epochs = 10
|
||||
history = model.fit(
|
||||
train_ds,
|
||||
validation_data=val_ds,
|
||||
@ -92,31 +86,24 @@ class NeuralN:
|
||||
probability_model = tf.keras.Sequential([model,
|
||||
tf.keras.layers.Softmax()])
|
||||
|
||||
#image_path = image
|
||||
image_path = pathlib.Path('zdjecia\ORK_ARCHER\ork_lucznik.png')
|
||||
# image_path = image
|
||||
#image_path = pathlib.Path('')
|
||||
image = Image.open(image_path)
|
||||
|
||||
# Preprocess the image
|
||||
|
||||
image = image.resize((180, 180)) # Resize to match the input size of the model
|
||||
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
||||
image_array = image_array / 255.0 # Normalize pixel values
|
||||
|
||||
# Add an extra dimension to the image array
|
||||
image_array = tf.expand_dims(image_array, 0)
|
||||
image_array = tf.expand_dims(image, 0)
|
||||
# Make the prediction
|
||||
predictions = probability_model.predict(image_array)
|
||||
|
||||
model = tf.keras.models.load_model("trained_model.h5")
|
||||
prediction = model.predict(image_array)
|
||||
# Convert the predictions to class labels
|
||||
predicted_label = class_names[predictions[0].argmax()]
|
||||
#actions = {
|
||||
# 'ORK_MELEE': 'fight',
|
||||
# 'ORK_ARCHER': 'change_dir',
|
||||
# 'SAURON': 'change_dir'
|
||||
#}
|
||||
|
||||
# Get the action for the predicted character
|
||||
#action = actions.get(predicted_label, 'unknown')
|
||||
predicted_label = class_names[prediction[0].argmax()]
|
||||
|
||||
# Print the predicted label
|
||||
print(predicted_label)
|
||||
return predicted_label#, action
|
||||
return predicted_label
|
||||
|
BIN
trained_model.h5
BIN
trained_model.h5
Binary file not shown.
Loading…
Reference in New Issue
Block a user