2024-04-12 09:12:38 +02:00
import time
2024-03-11 13:24:12 +01:00
import pygame
import random
2024-04-24 22:16:34 +02:00
import Pole
2024-03-11 21:24:16 +01:00
import displayControler as dCon
import Slot
2024-03-23 21:00:08 +01:00
import Osprzet
2024-04-12 22:11:30 +02:00
import Node
2024-05-11 14:18:40 +02:00
import Condition
import Drzewo
2024-05-26 01:08:47 +02:00
import neuralnetwork as nn
2024-05-11 14:18:40 +02:00
condition = Condition . Condition ( )
drzewo = Drzewo . Drzewo ( )
2024-05-12 14:59:23 +02:00
format_string = " {:<25} {:<25} {:<25} {:<10} {:<10} {:<10} {:<25} {:<15} {:<20} {:<10} {:<15} "
2024-05-27 09:01:51 +02:00
format_string_nn = " {:<10} {:<20} {:<20} {:<15} {:<20} "
2024-05-12 14:59:23 +02:00
2024-04-12 22:11:30 +02:00
2024-04-15 10:49:25 +02:00
tab = [ - 1 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ,
0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]
2024-04-12 22:11:30 +02:00
2024-03-11 13:24:12 +01:00
class Tractor :
2024-04-11 22:25:38 +02:00
DIRECTION_NORTH = ' N '
DIRECTION_SOUTH = ' S '
DIRECTION_WEST = ' W '
DIRECTION_EAST = ' E '
2024-04-15 10:49:25 +02:00
def __init__ ( self , slot , screen , osprzet , clock , bfs2_flag ) :
2024-04-11 22:25:38 +02:00
self . tractor_images = {
Tractor . DIRECTION_NORTH : pygame . transform . scale ( pygame . image . load ( ' images/traktorN.png ' ) ,
( dCon . CUBE_SIZE , dCon . CUBE_SIZE ) ) ,
Tractor . DIRECTION_SOUTH : pygame . transform . scale ( pygame . image . load ( ' images/traktorS.png ' ) ,
( dCon . CUBE_SIZE , dCon . CUBE_SIZE ) ) ,
Tractor . DIRECTION_WEST : pygame . transform . scale ( pygame . image . load ( ' images/traktorW.png ' ) ,
( dCon . CUBE_SIZE , dCon . CUBE_SIZE ) ) ,
Tractor . DIRECTION_EAST : pygame . transform . scale ( pygame . image . load ( ' images/traktor.png ' ) ,
( dCon . CUBE_SIZE , dCon . CUBE_SIZE ) )
}
self . direction = Tractor . DIRECTION_EAST # początkowy kierunek wschód
self . current_tractor_image = self . tractor_images [ self . direction ]
2024-03-11 13:24:12 +01:00
self . screen = screen
2024-03-23 21:00:08 +01:00
self . slot = slot
self . osprzet = osprzet
2024-04-13 01:39:39 +02:00
self . clock = clock
self . slot_hydrate_dict = { }
2024-04-15 10:49:25 +02:00
self . bfs2_flag = bfs2_flag
2024-05-11 13:24:09 +02:00
self . waterLevel = random . randint ( 0 , 100 )
2024-04-11 22:25:38 +02:00
2024-03-11 13:24:12 +01:00
def draw_tractor ( self ) :
2024-04-11 22:25:38 +02:00
self . screen . blit ( self . current_tractor_image , ( self . slot . x_axis * dCon . CUBE_SIZE , self . slot . y_axis * dCon . CUBE_SIZE ) )
2024-03-11 13:24:12 +01:00
pygame . display . update ( )
2024-04-11 22:25:38 +02:00
def turn_left ( self ) :
# zmiana kierunku w lewo
direction_map = {
Tractor . DIRECTION_EAST : Tractor . DIRECTION_NORTH ,
Tractor . DIRECTION_NORTH : Tractor . DIRECTION_WEST ,
Tractor . DIRECTION_WEST : Tractor . DIRECTION_SOUTH ,
Tractor . DIRECTION_SOUTH : Tractor . DIRECTION_EAST
}
self . direction = direction_map [ self . direction ]
self . current_tractor_image = self . tractor_images [ self . direction ]
2024-04-12 09:12:38 +02:00
self . draw_tractor ( )
2024-04-11 22:25:38 +02:00
2024-05-11 22:05:02 +02:00
def tree_move ( self , pole ) :
2024-05-11 14:18:40 +02:00
drzewo . treeLearn ( )
drzewo . plotTree ( )
2024-05-12 14:59:23 +02:00
self . snake_move_irrigation ( pole , drzewo )
def get_attributes ( self ) :
2024-05-11 14:18:40 +02:00
slot_attributes = self . slot . return_stan_for_tree ( )
climate_attributes = condition . return_condition ( )
attributes = [ ]
attributes = attributes + slot_attributes + [ self . waterLevel ] + climate_attributes
2024-05-12 14:59:23 +02:00
return attributes
def get_attributes_for_print ( self ) :
slot_attributes = self . slot . return_plant ( ) . return_status_tree ( )
climate_attributes = condition . getCondition ( )
slot_attributes = slot_attributes + [ self . waterLevel ]
return slot_attributes + climate_attributes
2024-04-11 22:25:38 +02:00
def turn_right ( self ) :
# zmiana kierunku w prawo
direction_map = {
Tractor . DIRECTION_EAST : Tractor . DIRECTION_SOUTH ,
Tractor . DIRECTION_SOUTH : Tractor . DIRECTION_WEST ,
Tractor . DIRECTION_WEST : Tractor . DIRECTION_NORTH ,
Tractor . DIRECTION_NORTH : Tractor . DIRECTION_EAST
}
self . direction = direction_map [ self . direction ]
self . current_tractor_image = self . tractor_images [ self . direction ]
2024-04-12 09:12:38 +02:00
self . draw_tractor ( )
2024-04-11 22:25:38 +02:00
2024-05-11 22:05:02 +02:00
def move_forward ( self , pole , destroy = True ) :
2024-04-11 22:25:38 +02:00
next_slot_coordinates = None
if self . direction == Tractor . DIRECTION_EAST :
next_slot_coordinates = ( self . slot . x_axis + 1 , self . slot . y_axis )
self . current_tractor_image = self . tractor_images [ self . direction ]
elif self . direction == Tractor . DIRECTION_WEST :
next_slot_coordinates = ( self . slot . x_axis - 1 , self . slot . y_axis )
self . current_tractor_image = self . tractor_images [ self . direction ]
elif self . direction == Tractor . DIRECTION_SOUTH :
next_slot_coordinates = ( self . slot . x_axis , self . slot . y_axis + 1 )
self . current_tractor_image = self . tractor_images [ self . direction ]
elif self . direction == Tractor . DIRECTION_NORTH :
next_slot_coordinates = ( self . slot . x_axis , self . slot . y_axis - 1 )
self . current_tractor_image = self . tractor_images [ self . direction ]
# sprawdzenie czy następny slot jest dobry
2024-05-11 22:05:02 +02:00
self . do_move_if_valid ( pole , next_slot_coordinates , destroy )
self . clock . tick ( 10 )
2024-04-13 00:13:51 +02:00
2024-05-11 22:05:02 +02:00
def do_move_if_valid ( self , pole , next_slot_coordinates , destroy = True ) :
2024-04-11 22:25:38 +02:00
if next_slot_coordinates and pole . is_valid_move ( next_slot_coordinates ) :
next_slot = pole . get_slot_from_cord ( next_slot_coordinates )
2024-05-11 22:05:02 +02:00
self . slot . redraw_image ( destroy )
2024-03-23 21:00:08 +01:00
self . slot = next_slot
self . draw_tractor ( )
2024-04-13 01:39:39 +02:00
return True
else :
return False
2024-03-23 21:00:08 +01:00
def random_move ( self , pole ) :
2024-04-13 01:39:39 +02:00
self . clock . tick ( 2 )
2024-04-11 22:25:38 +02:00
# losowanie skrętu
turn_direction = random . choice ( [ self . turn_left , self . turn_right ] )
turn_direction ( )
2024-04-13 01:39:39 +02:00
self . clock . tick ( 5 )
2024-04-11 22:25:38 +02:00
# wykonanie ruchu do przodu z uwzględnieniem aktualnej orientacji
self . move_forward ( pole )
2024-03-23 21:00:08 +01:00
2024-04-13 01:39:39 +02:00
def reset_pos ( self , pole ) :
self . do_move_if_valid ( pole , ( 0 , 0 ) )
def initial_move ( self , pole ) :
2024-04-15 10:49:25 +02:00
if ( self . bfs2_flag == True ) :
index = 0
for y in range ( 0 , dCon . NUM_Y ) :
if ( y % 2 == 0 ) :
for x in range ( 0 , dCon . NUM_X ) :
if ( pole . is_valid_move ( ( x , y ) ) ) :
pole . get_slot_from_cord ( ( x , y ) ) . setHydrate ( tab [ index ] )
self . snake_move ( pole , x , y )
index = index + 1
else :
for x in range ( dCon . NUM_X , - 1 , - 1 ) :
if ( pole . is_valid_move ( ( x , y ) ) ) :
pole . get_slot_from_cord ( ( x , y ) ) . setHydrate ( tab [ index ] )
self . snake_move ( pole , x , y )
index = index + 1
else :
for y in range ( 0 , dCon . NUM_Y ) :
if ( y % 2 == 0 ) :
for x in range ( 0 , dCon . NUM_X ) :
self . snake_move ( pole , x , y )
else :
for x in range ( dCon . NUM_X , - 1 , - 1 ) :
self . snake_move ( pole , x , y )
2024-04-13 01:39:39 +02:00
2024-05-11 22:05:02 +02:00
def snake_move_irrigation ( self , pole , drzewo ) :
2024-05-12 14:59:23 +02:00
headers = [ ' Wspolrzedne ' , ' Czy podlac ' , ' Poziom nawodnienia ' , ' Wzrost ' , ' Choroba ' , ' Zyznosc ' , ' Poziom wody w traktorze ' , ' Temperatura ' , ' Opady ' , ' Pora Roku ' , ' Aktualny czas ' ]
print ( format_string . format ( * headers ) )
2024-05-11 22:05:02 +02:00
initPos = ( self . slot . x_axis , self . slot . y_axis )
counter = 0
for i in range ( initPos [ 1 ] , dCon . NUM_Y ) :
for j in range ( initPos [ 0 ] , dCon . NUM_X ) :
2024-05-12 14:59:23 +02:00
attributes = self . get_attributes ( )
2024-05-11 22:05:02 +02:00
decision = drzewo . makeDecision ( attributes )
2024-05-12 14:59:23 +02:00
self . pretty_print_tree ( [ str ( " ( {:02d} , {:02d} ) " ) . format ( self . slot . x_axis , self . slot . y_axis ) , decision , * self . get_attributes_for_print ( ) ] )
2024-05-12 17:34:27 +02:00
if decision == " Tak " :
2024-05-11 22:05:02 +02:00
self . slot . irrigatePlant ( )
counter + = 1
condition . cycle ( )
2024-05-12 17:34:27 +02:00
pygame . time . delay ( 50 )
self . waterLevel = random . randint ( 0 , 100 )
2024-05-11 22:05:02 +02:00
#condition.getCondition()
self . move_forward ( pole , False )
if i % 2 == 0 and i != dCon . NUM_Y - 1 :
self . turn_right ( )
self . move_forward ( pole , False )
self . turn_right ( )
elif i != dCon . NUM_Y - 1 :
self . turn_left ( )
self . move_forward ( pole , False )
self . turn_left ( )
print ( " podlanych slotów: " , str ( counter ) )
2024-05-26 01:08:47 +02:00
def snake_move_predict_plant ( self , pole , model ) :
2024-05-27 09:01:51 +02:00
headers = [ ' Coords ' , ' Real plant ' , ' Predicted plant ' , ' Result ' , ' Fertilizer ' ]
print ( format_string_nn . format ( * headers ) )
2024-05-26 01:08:47 +02:00
initPos = ( self . slot . x_axis , self . slot . y_axis )
2024-05-26 01:55:40 +02:00
count = 0
2024-05-26 01:08:47 +02:00
for i in range ( initPos [ 1 ] , dCon . NUM_Y ) :
for j in range ( initPos [ 0 ] , dCon . NUM_X ) :
2024-05-26 13:58:48 +02:00
for event in pygame . event . get ( ) :
if event . type == pygame . QUIT :
quit ( )
2024-05-26 01:08:47 +02:00
if self . slot . imagePath != None :
predictedLabel = nn . predictLabel ( self . slot . imagePath , model )
2024-05-26 18:41:55 +02:00
2024-05-27 09:01:51 +02:00
#print(str("Coords: ({:02d}, {:02d})").format(self.slot.x_axis, self.slot.y_axis), "real:", self.slot.label, "predicted:", predictedLabel, "correct" if (self.slot.label == predictedLabel) else "incorrect", 'nawożę za pomocą:', nn.fertilizer[predictedLabel])
print ( format_string_nn . format ( f " { self . slot . x_axis , self . slot . y_axis } " , self . slot . label , predictedLabel , " correct " if ( self . slot . label == predictedLabel ) else " incorrect " , nn . fertilizer [ predictedLabel ] ) )
2024-05-26 01:55:40 +02:00
if self . slot . label != predictedLabel :
self . slot . mark_visited ( )
count + = 1
2024-05-26 01:08:47 +02:00
self . move_forward ( pole , False )
if i % 2 == 0 and i != dCon . NUM_Y - 1 :
self . turn_right ( )
self . move_forward ( pole , False )
self . turn_right ( )
elif i != dCon . NUM_Y - 1 :
self . turn_left ( )
self . move_forward ( pole , False )
self . turn_left ( )
2024-05-27 09:01:51 +02:00
print ( f " Dobrze nawiezionych roślin: { 20 * 12 - count } , źle nawiezionych roślin: { count } " )
2024-05-26 01:08:47 +02:00
2024-04-13 01:39:39 +02:00
def snake_move ( self , pole , x , y ) :
next_slot_coordinates = ( x , y )
if ( self . do_move_if_valid ( pole , next_slot_coordinates ) ) :
2024-04-24 22:16:34 +02:00
if ( x , y ) not in Pole . stoneList :
if x == 0 and y == 0 :
hydrateIndex = - 1
elif pole . get_slot_from_cord ( ( x , y ) ) . get_hydrate_stats ( ) < 60 :
hydrateIndex = 0
else :
hydrateIndex = 1
self . slot_hydrate_dict [ ( x , y ) ] = hydrateIndex #Budowanie slownika slotow z poziomem nawodnienia dla traktorka
2024-04-13 01:39:39 +02:00
self . clock . tick ( 10 )
for event in pygame . event . get ( ) :
if event . type == pygame . QUIT :
quit ( )
2024-04-14 21:39:04 +02:00
2024-04-14 00:13:27 +02:00
def move_by_root ( self , root , pole , actions = None ) :
2024-04-13 23:55:58 +02:00
for move in root :
2024-04-14 00:13:27 +02:00
self . slot . redraw_image ( )
2024-04-14 21:39:04 +02:00
if move [ 1 ] == ' forward ' :
2024-04-13 23:55:58 +02:00
self . move_forward ( pole )
2024-04-14 21:39:04 +02:00
if move [ 1 ] == ' right ' :
2024-04-13 23:55:58 +02:00
self . turn_right ( )
2024-04-14 21:39:04 +02:00
if move [ 1 ] == ' left ' :
2024-04-13 23:55:58 +02:00
self . turn_left ( )
2024-04-14 00:13:27 +02:00
for a in actions :
a ( )
2024-04-13 23:55:58 +02:00
self . clock . tick ( 3 )
2024-03-23 21:00:08 +01:00
#to tak zrobiłam już na później, może się przyda
def change_osprzet ( self , new_osprzet ) :
self . osprzet = new_osprzet
def print_osprzet_info ( self ) :
2024-03-24 13:10:02 +01:00
print ( " ID: " , self . osprzet . id )
print ( " Marka: " , self . osprzet . marka )
print ( " Model: " , self . osprzet . model )
if self . osprzet . akcje :
print ( " Akcje: " )
for akcja in self . osprzet . akcje :
print ( " - Typ: " , akcja . typ )
2024-03-23 21:00:08 +01:00
else :
2024-04-12 22:11:30 +02:00
print ( " Brak akcji przypisanych do tego sprzętu. " )
2024-05-12 14:59:23 +02:00
def pretty_print_tree ( self , attributes ) :
print ( format_string . format ( * attributes ) )
2024-04-13 23:55:58 +02:00
def irrigateSlot ( self ) :
2024-04-15 10:02:09 +02:00
try :
self . slot . irrigatePlant ( )
except :
pass