Zaktualizuj 'Traktorek/ran.py'
This commit is contained in:
parent
8818539c6c
commit
580fbd9d2e
308
Traktorek/ran.py
308
Traktorek/ran.py
@ -1,58 +1,157 @@
|
||||
import pygame
|
||||
import sys
|
||||
import random
|
||||
import NataliaWisniewskaDrzewoDecyzyjne
|
||||
from Tractor import Tractor
|
||||
import pygame, sys
|
||||
from traktor import Traktor
|
||||
|
||||
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'
|
||||
|
||||
|
||||
class Game(object):
|
||||
|
||||
def __init__(self):
|
||||
# Config
|
||||
self.max_tps = 2.0
|
||||
self.res = (720, 720)
|
||||
|
||||
self.gen = Gen(self)
|
||||
|
||||
# nat
|
||||
self.nawadnianie = []
|
||||
|
||||
# ola
|
||||
self.stan = []
|
||||
|
||||
self.fields = []
|
||||
self.randomize_field()
|
||||
self.nawadnianie = []
|
||||
# Initialization
|
||||
|
||||
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')
|
||||
|
||||
|
||||
pygame.init()
|
||||
self.res = (720, 720)
|
||||
self.screen = pygame.display.set_mode(self.res)
|
||||
pygame.display.set_caption('Traktorek')
|
||||
self.clock = pygame.time.Clock()
|
||||
self.dt = 0.0
|
||||
my_tree = drzewoDecyzyjne.build_tree(drzewoDecyzyjne.training_data)
|
||||
self.player = Tractor(self)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
while True:
|
||||
# Handle events
|
||||
|
||||
pole = int(self.player.y // 144 * 5 + self.player.x // 144)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit(0)
|
||||
elif event.type == pygame.K_ESCAPE:
|
||||
pygame.quit()
|
||||
sys.exit(0)
|
||||
|
||||
# Ticking
|
||||
self.dt += self.clock.tick() / 1000.0
|
||||
while self.dt > 1 / self.max_tps:
|
||||
self.tick()
|
||||
self.dt -= 1 / self.max_tps
|
||||
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()
|
||||
|
||||
# Rendering
|
||||
self.screen.fill((0, 0, 0))
|
||||
self.draw()
|
||||
pygame.display.flip()
|
||||
|
||||
def tick(self):
|
||||
self.player.tick()
|
||||
|
||||
# plansza
|
||||
def draw(self):
|
||||
self.screen.fill((0, 0, 0))
|
||||
self.screen.fill((108, 142, 191))
|
||||
self.draw_field()
|
||||
self.draw_net()
|
||||
self.player.draw()
|
||||
pygame.display.update()
|
||||
|
||||
# krata
|
||||
def draw_net(self):
|
||||
color = (255, 255, 255)
|
||||
for i in range(1, 5):
|
||||
@ -62,89 +161,116 @@ class Game(object):
|
||||
# linia pionowa
|
||||
pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1)
|
||||
|
||||
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ę
|
||||
def randomize_field(self):
|
||||
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]
|
||||
for x in range(25):
|
||||
temp = []
|
||||
# nasiona
|
||||
temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka", "puste"]))
|
||||
# gleba
|
||||
temp.append(random.choice([True, False]))
|
||||
# woda
|
||||
temp.append(random.choice(['s', 'n']))
|
||||
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)
|
||||
|
||||
# ile dni temu pole bylo podlewane
|
||||
temp.append(random.randrange(7))
|
||||
# czy ma padac
|
||||
pada = random.choice(['t', 'n'])
|
||||
temp.append(pada)
|
||||
# kiedy padalo
|
||||
kiedyPada = random.randrange(9)
|
||||
temp.append(kiedyPada)
|
||||
ind = 2
|
||||
|
||||
# nawoz
|
||||
temp.append(random.choice([True, False]))
|
||||
# # growth rate
|
||||
# temp.append(random)
|
||||
# # cost + co ile dni nalezy podlewac dana uprawe
|
||||
if temp[0] == "żyto":
|
||||
temp.append(10)
|
||||
temp.append(ind)
|
||||
#co ile dni nalezy podlewac dana uprawe
|
||||
if self.fields[x] == "rzodkiewa":
|
||||
temp.append(4)
|
||||
elif temp[0] == "jęczmień":
|
||||
temp.append(12)
|
||||
elif self.fields[x] == "papryka":
|
||||
temp.append(3)
|
||||
elif temp[0] == "owies":
|
||||
temp.append(8)
|
||||
elif self.fields[x] == "pomidor":
|
||||
temp.append(2)
|
||||
elif temp[0] == "marchew":
|
||||
temp.append(14)
|
||||
elif self.fields[x] == "marchew":
|
||||
temp.append(5)
|
||||
elif temp[0] == "rzodkiew":
|
||||
temp.append(7)
|
||||
elif self.fields[x] == "sałata":
|
||||
temp.append(5)
|
||||
elif temp[0] == "pietruszka":
|
||||
temp.append(6)
|
||||
elif self.fields[x] == "pietruszka":
|
||||
temp.append(1)
|
||||
elif temp[0] == "puste":
|
||||
temp.append(2)
|
||||
elif self.fields[x] == "puste":
|
||||
temp.append(6)
|
||||
else:
|
||||
temp.append(0)
|
||||
|
||||
self.fields.append(temp)
|
||||
# czy ma padac
|
||||
pada = random.choice(['t', 'n'])
|
||||
temp.append(pada)
|
||||
|
||||
self.nawadnianie.append([temp[3], temp[8], temp[4], temp[5], temp[2]])
|
||||
i=0
|
||||
print('\nPODLEWANIE: \n')
|
||||
# kiedy padalo
|
||||
kiedyPada = random.randrange(9)
|
||||
temp.append(kiedyPada)
|
||||
|
||||
while i<25:
|
||||
wynik = drzewoDecyzyjne.finalAnswer(self.nawadnianie[i], drzewoDecyzyjne.my_tree)
|
||||
#print(self.nawadnianie[i])
|
||||
#print(wynik)
|
||||
if (wynik == 's'):
|
||||
print('należy podlać pole ' + str(i))
|
||||
# woda
|
||||
if woda <= 5:
|
||||
temp.append('s')
|
||||
else:
|
||||
print('nie podlewać pola ' + str(i))
|
||||
i=i+1
|
||||
|
||||
def draw_field(self):
|
||||
for x in range(25):
|
||||
self.screen.fill(self.color(x), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1)))
|
||||
|
||||
def color(self, z):
|
||||
if self.fields[z][0] == 'owies':
|
||||
return (255, 200, 55)
|
||||
elif self.fields[z][0] == 'jęczmień':
|
||||
return (170, 150, 40)
|
||||
elif self.fields[z][0] == 'żyto':
|
||||
return (100, 215, 80)
|
||||
elif self.fields[z][0] == 'marchew':
|
||||
return (224, 60, 14)
|
||||
elif self.fields[z][0] == 'rzodkiew':
|
||||
return (142, 24, 104)
|
||||
elif self.fields[z][0] == 'pietruszka':
|
||||
return (254, 247, 246)
|
||||
else:
|
||||
return (0,0,0)
|
||||
temp.append('n')
|
||||
|
||||
self.nawadnianie.append(temp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user