2020-06-15 12:48:24 +02:00
|
|
|
import pygame, sys
|
|
|
|
from traktor import Traktor
|
2020-04-28 21:08:11 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from tensorflow.keras.preprocessing import image
|
|
|
|
from tensorflow.keras.models import load_model
|
|
|
|
from tensorflow.keras.applications import MobileNetV2
|
|
|
|
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
|
|
|
|
import numpy as np
|
|
|
|
import os, random
|
|
|
|
from algorytm_genetyczny import Gen
|
|
|
|
import drzewo_decyzyjne_n as drzewko
|
|
|
|
from drzewo_decyzyjne_o import Tree
|
|
|
|
|
|
|
|
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
2020-04-28 21:08:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Game(object):
|
2020-06-15 12:48:24 +02:00
|
|
|
|
2020-04-28 21:08:11 +02:00
|
|
|
def __init__(self):
|
2020-06-15 12:48:24 +02:00
|
|
|
|
|
|
|
self.gen = Gen(self)
|
|
|
|
|
|
|
|
# nat
|
|
|
|
self.nawadnianie = []
|
|
|
|
|
|
|
|
# ola
|
|
|
|
self.stan = []
|
2020-04-28 21:08:11 +02:00
|
|
|
|
|
|
|
self.fields = []
|
2020-06-15 12:48:24 +02:00
|
|
|
|
|
|
|
self.neighbours()
|
|
|
|
|
|
|
|
# planszę przedstawiam za pomocą punktów 25 pól (współrzędne środka)
|
|
|
|
self.punkty_pola = [[22, 22], [22, 166], [22, 310], [22, 454], [22, 598], # pierwsza kolumna
|
|
|
|
[166, 22], [166, 166], [166, 310], [166, 454], [166, 598], # druga kolumna
|
|
|
|
[310, 22], [310, 166], [310, 310], [310, 454], [310, 598], # trzecia kolumna
|
|
|
|
[454, 22], [454, 166], [454, 310], [454, 454], [454, 598], # czwarta kolumna
|
|
|
|
[598, 22], [598, 166], [598, 310], [598, 454], [598, 598]] # 5 kolumna
|
|
|
|
|
|
|
|
# tworzę listę etykiet warzyw, które mogą pojawić się na polu
|
|
|
|
self.warzywa_etykiety = []
|
|
|
|
|
|
|
|
self.warzywa_etykiety_sciezka = './warzywa_etykiety'
|
|
|
|
|
|
|
|
# losowo przypisuję do każdego pola etykietę
|
|
|
|
for _ in self.punkty_pola:
|
|
|
|
self.warzywa_etykiety.append(
|
|
|
|
'./warzywa_etykiety/' + random.choice(os.listdir(self.warzywa_etykiety_sciezka)))
|
|
|
|
|
|
|
|
# ładuję model sieci neutronowej
|
|
|
|
self.model = load_model('neural_model.h5')
|
|
|
|
|
|
|
|
|
2020-04-28 21:08:11 +02:00
|
|
|
pygame.init()
|
2020-06-15 12:48:24 +02:00
|
|
|
self.res = (720, 720)
|
2020-04-28 21:08:11 +02:00
|
|
|
self.screen = pygame.display.set_mode(self.res)
|
2020-06-15 12:48:24 +02:00
|
|
|
|
|
|
|
self.player = Traktor(self)
|
|
|
|
|
|
|
|
self.rozpoznawanie_planszy()
|
|
|
|
self.randomize_field()
|
|
|
|
|
|
|
|
# gen
|
|
|
|
# sadzonki do zasadzenia
|
|
|
|
self.sadzonka = self.gen.algorytm()
|
|
|
|
|
|
|
|
# nat
|
|
|
|
drzewko.build_tree(drzewko.training_data)
|
|
|
|
|
|
|
|
# ola
|
|
|
|
self.tree = Tree()
|
|
|
|
self.tree.Algorithm()
|
|
|
|
|
2020-04-28 21:08:11 +02:00
|
|
|
|
|
|
|
while True:
|
2020-06-15 12:48:24 +02:00
|
|
|
|
|
|
|
pole = int(self.player.y // 144 * 5 + self.player.x // 144)
|
|
|
|
|
2020-04-28 21:08:11 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2020-06-15 12:48:24 +02:00
|
|
|
pygame.quit()
|
2020-04-28 21:08:11 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_UP:
|
|
|
|
self.player.y -= 144
|
|
|
|
if self.player.y < 0:
|
|
|
|
self.player.y = 0
|
|
|
|
elif event.key == pygame.K_DOWN:
|
|
|
|
self.player.y += 144
|
|
|
|
print(pole)
|
|
|
|
if self.player.y > 720:
|
|
|
|
self.player.y = 598
|
|
|
|
elif event.key == pygame.K_RIGHT:
|
|
|
|
self.player.x += 144
|
|
|
|
if self.player.x > 720:
|
|
|
|
self.player.x = 598
|
|
|
|
elif event.key == pygame.K_LEFT:
|
|
|
|
self.player.x -= 144
|
|
|
|
if self.player.x < 0:
|
|
|
|
self.player.x = 0
|
|
|
|
elif event.key == pygame.K_SPACE:
|
|
|
|
# sieci neuronowe
|
|
|
|
# dla obecnego położenia klienta rozpoznaję jakie rośnie tam warzywo
|
|
|
|
obecne_polozenie_agenta = [self.player.x, self.player.y]
|
|
|
|
self.player.obecne_pole = self.punkty_pola.index(obecne_polozenie_agenta)
|
|
|
|
temp = self.player.obecne_pole
|
|
|
|
img_path = self.warzywa_etykiety[temp]
|
|
|
|
img = image.load_img(img_path, target_size=(224, 224))
|
|
|
|
x = image.img_to_array(img)
|
|
|
|
x = np.expand_dims(x, axis=0)
|
|
|
|
x = preprocess_input(x)
|
|
|
|
plt.imshow(img)
|
|
|
|
preds = self.model.predict(x)
|
|
|
|
preds = np.asarray(preds)
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
elif event.key == pygame.K_s:
|
|
|
|
# algorytm genetyczny
|
|
|
|
# dla każdego pustego pola sadzę warzywo, które najlepiej się przyjmie względem sąsiadujących
|
|
|
|
if pole in self.gen.index_gen:
|
|
|
|
self.fields[pole] = self.sadzonka[self.gen.index_gen.index(pole)]
|
|
|
|
print('zasadzono', self.sadzonka[self.gen.index_gen.index(pole)])
|
|
|
|
self.gen.index_gen.remove(pole)
|
|
|
|
elif event.key == pygame.K_p:
|
|
|
|
# drzewo decyzyjne
|
|
|
|
# dla każdego pola decyduje czy należy je podlać
|
|
|
|
wynik = drzewko.finalAnswer(self.nawadnianie[pole], drzewko.my_tree)
|
|
|
|
if (wynik == 's'):
|
|
|
|
print('należy podlać pole')
|
|
|
|
else:
|
|
|
|
print('nie podlewać pola ')
|
|
|
|
elif event.key == pygame.K_g:
|
|
|
|
# drzewo decyzyjne
|
|
|
|
# dla danego pola sprawdza stan gleby
|
|
|
|
s = self.stan[pole]
|
|
|
|
print(self.tree.Solution(s))
|
|
|
|
|
|
|
|
pygame.display.update()
|
2020-04-28 21:08:11 +02:00
|
|
|
|
|
|
|
self.screen.fill((0, 0, 0))
|
|
|
|
self.draw()
|
|
|
|
pygame.display.flip()
|
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
# plansza
|
2020-04-28 21:08:11 +02:00
|
|
|
def draw(self):
|
2020-06-15 12:48:24 +02:00
|
|
|
self.screen.fill((108, 142, 191))
|
2020-04-28 21:08:11 +02:00
|
|
|
self.draw_field()
|
|
|
|
self.draw_net()
|
|
|
|
self.player.draw()
|
|
|
|
pygame.display.update()
|
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
# krata
|
2020-04-28 21:08:11 +02:00
|
|
|
def draw_net(self):
|
|
|
|
color = (255, 255, 255)
|
|
|
|
for i in range(1, 5):
|
|
|
|
krat = int(720 / 5 * i)
|
|
|
|
# linia pozioma
|
|
|
|
pygame.draw.line(self.screen, color, (0, krat), (720, krat), 1)
|
|
|
|
# linia pionowa
|
|
|
|
pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1)
|
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
def draw_field(self):
|
|
|
|
for x in range(25):
|
|
|
|
self.screen.fill((255, 234, 234), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1)))
|
|
|
|
|
|
|
|
#sąsiedzi
|
|
|
|
def neighbours(self):
|
|
|
|
self.neighbours = list(range(25))
|
|
|
|
self.neighbours[0] = [1, 5]
|
|
|
|
self.neighbours[4] = [3, 9]
|
|
|
|
self.neighbours[20] = [15, 21]
|
|
|
|
self.neighbours[24] = [19, 23]
|
|
|
|
for x in range(1, 4):
|
|
|
|
self.neighbours[x] = [x - 1, x + 5, x + 1]
|
|
|
|
for x in range(5, 16, 5):
|
|
|
|
self.neighbours[x] = [x - 5, x + 1, x + 5]
|
|
|
|
for x in range(9, 20, 5):
|
|
|
|
self.neighbours[x] = [x - 5, x - 1, x + 5]
|
|
|
|
for x in range(21, 24):
|
|
|
|
self.neighbours[x] = [x - 1, x - 5, x + 1]
|
|
|
|
for x in [6, 7, 8, 11, 12, 13, 16, 17, 18]:
|
|
|
|
self.neighbours[x] = [x - 5, x - 1, x + 1, x + 5]
|
|
|
|
|
|
|
|
# rozpoznawanie warzyw na każdym polu
|
|
|
|
def rozpoznawanie_planszy(self):
|
|
|
|
for _ in range(25):
|
|
|
|
temp = _
|
|
|
|
img_path = self.warzywa_etykiety[temp]
|
|
|
|
img = image.load_img(img_path, target_size=(224, 224))
|
|
|
|
x = image.img_to_array(img)
|
|
|
|
x = np.expand_dims(x, axis=0)
|
|
|
|
x = preprocess_input(x)
|
|
|
|
|
|
|
|
preds = self.model.predict(x)
|
|
|
|
preds = np.asarray(preds)
|
|
|
|
|
|
|
|
szacunek = preds.max()
|
|
|
|
ind = np.where(preds == szacunek)
|
|
|
|
|
|
|
|
if ind == 0:
|
|
|
|
self.fields.append('rzodkiewa')
|
|
|
|
if ind == 1:
|
|
|
|
self.fields.append('papryka')
|
|
|
|
if ind == 2:
|
|
|
|
self.fields.append('pomidor')
|
|
|
|
if ind == 3:
|
|
|
|
self.fields.append('marchew')
|
|
|
|
if ind == 4:
|
|
|
|
self.fields.append('salata')
|
|
|
|
if ind == 5:
|
|
|
|
self.fields.append('pietrucha')
|
|
|
|
if ind == 6:
|
|
|
|
self.fields.append('puste')
|
|
|
|
|
|
|
|
# generowanie dodatkowych własności dla każdego pola w oparciu o rosnącą tam roślinę
|
2020-04-28 21:08:11 +02:00
|
|
|
def randomize_field(self):
|
2020-06-15 12:48:24 +02:00
|
|
|
chwasty_list = list(range(1, 11))
|
|
|
|
waga_ch = [0.3, 0.2, 0.15, 0.1, 0.05, 0.05, 0.05, 0.04, 0.03, 0.03]
|
|
|
|
podlanie_list = list(range(1, 11))
|
|
|
|
waga_po = [0.02, 0.05, 0.05, 0.05, 0.05, 0.08, 0.1, 0.15, 0.15, 0.3]
|
|
|
|
ph_list = list(range(1, 11))
|
|
|
|
waga_ph = [0.01, 0.01, 0.02, 0.03, 0.07, 0.37, 0.4, 0.05, 0.03, 0.01]
|
2020-04-28 21:08:11 +02:00
|
|
|
for x in range(25):
|
|
|
|
temp = []
|
|
|
|
# nasiona
|
2020-06-15 12:48:24 +02:00
|
|
|
temp1 = []
|
|
|
|
temp1.append(random.choices(chwasty_list, waga_ch)[0])
|
|
|
|
woda = random.choices(podlanie_list, waga_po)[0]
|
|
|
|
temp1.append(woda)
|
|
|
|
temp1.append(random.choices(ph_list, waga_ph)[0])
|
|
|
|
|
|
|
|
self.stan.append(temp1)
|
|
|
|
|
2020-05-20 11:42:28 +02:00
|
|
|
# ile dni temu pole bylo podlewane
|
|
|
|
temp.append(random.randrange(7))
|
2020-06-15 12:48:24 +02:00
|
|
|
ind = 2
|
2020-06-10 11:11:45 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
temp.append(ind)
|
|
|
|
#co ile dni nalezy podlewac dana uprawe
|
|
|
|
if self.fields[x] == "rzodkiewa":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(4)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "papryka":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(3)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "pomidor":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(2)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "marchew":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(5)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "sałata":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(5)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "pietruszka":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(1)
|
2020-06-15 12:48:24 +02:00
|
|
|
elif self.fields[x] == "puste":
|
2020-05-20 11:42:28 +02:00
|
|
|
temp.append(6)
|
2020-04-28 21:08:11 +02:00
|
|
|
else:
|
|
|
|
temp.append(0)
|
2020-06-14 16:29:16 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
# czy ma padac
|
|
|
|
pada = random.choice(['t', 'n'])
|
|
|
|
temp.append(pada)
|
2020-06-10 11:11:45 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
# kiedy padalo
|
|
|
|
kiedyPada = random.randrange(9)
|
|
|
|
temp.append(kiedyPada)
|
2020-06-14 16:29:16 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
# woda
|
|
|
|
if woda <= 5:
|
|
|
|
temp.append('s')
|
2020-06-14 16:29:16 +02:00
|
|
|
else:
|
2020-06-15 12:48:24 +02:00
|
|
|
temp.append('n')
|
2020-04-28 21:08:11 +02:00
|
|
|
|
2020-06-15 12:48:24 +02:00
|
|
|
self.nawadnianie.append(temp)
|
2020-04-28 21:08:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Game()
|