genetic_algorithm #7
Binary file not shown.
Binary file not shown.
@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
|
|||||||
from NN.model import *
|
from NN.model import *
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import pygame
|
import pygame
|
||||||
|
from area.constants import GREY
|
||||||
|
|
||||||
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
||||||
|
|
||||||
@ -84,16 +85,22 @@ def load_image(image_path):
|
|||||||
testImage = testImage.unsqueeze(0)
|
testImage = testImage.unsqueeze(0)
|
||||||
return testImage
|
return testImage
|
||||||
|
|
||||||
|
#display the image for prediction next to the field
|
||||||
def display_image(screen, image_path, position):
|
def display_image(screen, image_path, position):
|
||||||
image = pygame.image.load(image_path)
|
image = pygame.image.load(image_path)
|
||||||
image = pygame.transform.scale(image, (250, 250))
|
image = pygame.transform.scale(image, (250, 250))
|
||||||
screen.blit(image, position)
|
screen.blit(image, position)
|
||||||
|
|
||||||
|
#display result of the guessed image (text under the image)
|
||||||
def display_result(screen, position, predicted_class):
|
def display_result(screen, position, predicted_class):
|
||||||
font = pygame.font.Font(None, 30)
|
font = pygame.font.Font(None, 30)
|
||||||
displayed_text = font.render("The predicted image is: "+str(predicted_class), 1, (255,255,255))
|
displayed_text = font.render("The predicted image is: "+str(predicted_class), 1, (255,255,255))
|
||||||
screen.blit(displayed_text, position)
|
screen.blit(displayed_text, position)
|
||||||
|
|
||||||
|
def clear_text_area(win, x, y, width, height, color=GREY):
|
||||||
|
pygame.draw.rect(win, color, (x, y, width, height))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
def guess_image(model, image_tensor):
|
def guess_image(model, image_tensor):
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
testOutput = model(image_tensor)
|
testOutput = model(image_tensor)
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
source/__pycache__/genetic.cpython-311.pyc
Normal file
BIN
source/__pycache__/genetic.cpython-311.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.
Binary file not shown.
@ -156,6 +156,6 @@ def do_actions(tractor, WIN, move_list):
|
|||||||
tractor.rotate_to_left()
|
tractor.rotate_to_left()
|
||||||
tractor.draw_tractor(WIN)
|
tractor.draw_tractor(WIN)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
time.sleep(1)
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from ground import Dirt
|
|||||||
from plant import Plant
|
from plant import Plant
|
||||||
from bfs import graphsearch, Istate, succ
|
from bfs import graphsearch, Istate, succ
|
||||||
from astar import a_star
|
from astar import a_star
|
||||||
from NN.neural_network import load_model, load_image, guess_image, display_image, display_result
|
from NN.neural_network import load_model, load_image, guess_image, display_image, display_result, clear_text_area
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from genetic import genetic_algorithm
|
from genetic import genetic_algorithm
|
||||||
|
|
||||||
@ -30,39 +30,15 @@ def main():
|
|||||||
window = drawWindow(WIN)
|
window = drawWindow(WIN)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
#getting coordinates of our "goal tile":
|
|
||||||
tile_index = get_tile_index()
|
|
||||||
tile_x, tile_y = get_tile_coordinates(tile_index)
|
|
||||||
if tile_x is not None and tile_y is not None:
|
|
||||||
print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})")
|
|
||||||
else: print("Such tile does not exist")
|
|
||||||
|
|
||||||
#mark the goal tile:
|
|
||||||
tiles[tile_index].image = "resources/images/sampling_goal.png"
|
|
||||||
image = pygame.image.load(tiles[tile_index].image).convert()
|
|
||||||
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
|
|
||||||
WIN.blit(image, (tiles[tile_index].x, tiles[tile_index].y))
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
|
|
||||||
#graphsearch activation:
|
|
||||||
istate = Istate(170, 100, 2) #initial state
|
|
||||||
|
|
||||||
goaltest = []
|
|
||||||
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
|
|
||||||
goaltest.append(tile_y)
|
|
||||||
|
|
||||||
|
#Tractor initialization:
|
||||||
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
||||||
tractor.rect.x += fieldX
|
tractor.rect.x += fieldX
|
||||||
tractor.rect.y += fieldY
|
tractor.rect.y += fieldY
|
||||||
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
|
tractor.tractor_start = ((170, 100))
|
||||||
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
|
istate = Istate(170, 100, 2) #initial state
|
||||||
tractor.tractor_end = ((goaltest[0], goaltest[1]))
|
|
||||||
|
|
||||||
#moves = (graphsearch(istate, succ, goaltest, tractor))
|
|
||||||
moves = (a_star(istate, succ, goaltest, tractor))
|
|
||||||
print(moves)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#main loop:
|
#main loop:
|
||||||
@ -72,7 +48,35 @@ def main():
|
|||||||
run = False
|
run = False
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
# movement based on route-planning (test):
|
#getting coordinates of our "goal tile":
|
||||||
|
tile_index = get_tile_index()
|
||||||
|
tile_x, tile_y = get_tile_coordinates(tile_index)
|
||||||
|
if tile_x is not None and tile_y is not None:
|
||||||
|
print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})")
|
||||||
|
else: print("Such tile does not exist")
|
||||||
|
|
||||||
|
#mark the goal tile:
|
||||||
|
tiles[tile_index].image = "resources/images/sampling_goal.png"
|
||||||
|
image = pygame.image.load(tiles[tile_index].image).convert()
|
||||||
|
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
|
||||||
|
WIN.blit(image, (tiles[tile_index].x, tiles[tile_index].y))
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
tractor.tractor_end = ((tile_x, tile_y))
|
||||||
|
goaltest = [] #final state (consists of x and y because direction doesnt matter)
|
||||||
|
goaltest.append(tile_x)
|
||||||
|
goaltest.append(tile_y)
|
||||||
|
goaltest[0] = tile_x
|
||||||
|
goaltest[1]=tile_y
|
||||||
|
|
||||||
|
#moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
|
||||||
|
moves = (a_star(istate, succ, goaltest, tractor))
|
||||||
|
print(moves)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# movement based on route-planning:
|
||||||
|
|
||||||
tractor.draw_tractor(WIN)
|
tractor.draw_tractor(WIN)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@ -89,6 +93,7 @@ def main():
|
|||||||
image_tensor = load_image(image_path)
|
image_tensor = load_image(image_path)
|
||||||
prediction = guess_image(load_model(), image_tensor)
|
prediction = guess_image(load_model(), image_tensor)
|
||||||
|
|
||||||
|
clear_text_area(WIN, WIDTH - 50, 600, 400, 50)
|
||||||
display_result(WIN, (WIDTH - 50 , 600), prediction) #display text under the photo
|
display_result(WIN, (WIDTH - 50 , 600), prediction) #display text under the photo
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
print(f"The predicted image is: {prediction}")
|
print(f"The predicted image is: {prediction}")
|
||||||
@ -155,7 +160,10 @@ def main():
|
|||||||
#work on field:
|
#work on field:
|
||||||
if predykcje == 'work':
|
if predykcje == 'work':
|
||||||
tractor.work_on_field(goalTile, d1, p1)
|
tractor.work_on_field(goalTile, d1, p1)
|
||||||
time.sleep(50)
|
|
||||||
|
#update the initial state for the next target:
|
||||||
|
istate = Istate(tile_x, tile_y, tractor.direction)
|
||||||
|
time.sleep(5)
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user