2024-03-07 18:01:12 +01:00
|
|
|
import pygame
|
2024-03-25 01:03:25 +01:00
|
|
|
import time
|
|
|
|
import random
|
2024-05-12 19:20:33 +02:00
|
|
|
import pandas as pd
|
|
|
|
import joblib
|
2024-03-07 18:01:12 +01:00
|
|
|
|
2024-04-20 14:47:25 +02:00
|
|
|
from area.constants import WIDTH, HEIGHT, TILE_SIZE
|
2024-03-07 18:01:12 +01:00
|
|
|
from area.field import drawWindow
|
2024-04-14 17:20:27 +02:00
|
|
|
from area.tractor import Tractor, do_actions
|
2024-04-10 01:52:13 +02:00
|
|
|
from area.field import tiles, fieldX, fieldY
|
2024-04-14 14:29:11 +02:00
|
|
|
from area.field import get_tile_coordinates
|
2024-03-25 01:03:25 +01:00
|
|
|
from ground import Dirt
|
|
|
|
from plant import Plant
|
2024-04-14 14:29:11 +02:00
|
|
|
from bfs import graphsearch, Istate, succ
|
2024-04-27 22:49:47 +02:00
|
|
|
from astar import a_star
|
2024-03-07 18:01:12 +01:00
|
|
|
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pygame.display.set_caption('Intelligent tractor')
|
|
|
|
|
2024-04-14 17:20:27 +02:00
|
|
|
|
2024-03-07 18:01:12 +01:00
|
|
|
def main():
|
|
|
|
run = True
|
|
|
|
window = drawWindow(WIN)
|
|
|
|
pygame.display.update()
|
2024-04-09 15:02:58 +02:00
|
|
|
|
2024-04-14 23:19:06 +02:00
|
|
|
#getting coordinates of our "goal tile":
|
2024-04-14 23:38:08 +02:00
|
|
|
tile_index=127
|
2024-04-14 14:29:11 +02:00
|
|
|
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")
|
|
|
|
|
2024-04-14 23:19:06 +02:00
|
|
|
#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()
|
|
|
|
|
|
|
|
|
2024-04-14 17:20:27 +02:00
|
|
|
#graphsearch activation:
|
2024-04-20 14:47:25 +02:00
|
|
|
istate = Istate(170, 100, 2) #initial state
|
|
|
|
|
|
|
|
goaltest = []
|
|
|
|
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
|
2024-04-14 23:19:06 +02:00
|
|
|
goaltest.append(tile_y)
|
|
|
|
|
|
|
|
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
|
|
|
tractor.rect.x += fieldX
|
|
|
|
tractor.rect.y += fieldY
|
|
|
|
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
|
2024-04-27 22:49:47 +02:00
|
|
|
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
|
2024-04-14 23:19:06 +02:00
|
|
|
tractor.tractor_end = ((goaltest[0], goaltest[1]))
|
|
|
|
|
2024-04-27 22:49:47 +02:00
|
|
|
#moves = (graphsearch(istate, succ, goaltest, tractor))
|
|
|
|
moves = (a_star(istate, succ, goaltest, tractor))
|
2024-04-14 17:20:27 +02:00
|
|
|
print(moves)
|
2024-04-14 14:29:11 +02:00
|
|
|
|
2024-04-14 23:19:06 +02:00
|
|
|
|
|
|
|
|
2024-04-14 14:29:11 +02:00
|
|
|
#main loop:
|
2024-03-07 18:01:12 +01:00
|
|
|
while run:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
|
|
|
|
2024-04-14 14:29:11 +02:00
|
|
|
#small test of work_on_field method:
|
2024-04-10 01:52:13 +02:00
|
|
|
time.sleep(1)
|
2024-03-25 01:03:25 +01:00
|
|
|
tile1 = tiles[0]
|
|
|
|
p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100))
|
|
|
|
d1 = Dirt(random.randint(1, 100), random.randint(1,100))
|
|
|
|
d1.pests_and_weeds()
|
|
|
|
tile1.ground=d1
|
2024-05-12 19:20:33 +02:00
|
|
|
if d1.pest:
|
|
|
|
pe = 1
|
|
|
|
else:
|
|
|
|
pe = 0
|
|
|
|
if d1.weed:
|
|
|
|
we = 1
|
|
|
|
else:
|
|
|
|
we = 0
|
|
|
|
if p1.plant_type == 'cereal':
|
|
|
|
t1 = True
|
|
|
|
t2 = False
|
|
|
|
t3 = False
|
|
|
|
t4 = False
|
|
|
|
else:
|
|
|
|
t1 = False
|
|
|
|
if p1.plant_type == 'fruit':
|
|
|
|
t2 = True
|
|
|
|
t3 = False
|
|
|
|
t4 = False
|
|
|
|
else:
|
|
|
|
t2 = False
|
|
|
|
if p1.plant_type == 'vegetable':
|
|
|
|
t4 = True
|
|
|
|
t3 = False
|
|
|
|
else:
|
|
|
|
t3 = True
|
|
|
|
t4 = False
|
|
|
|
|
|
|
|
dane = {
|
|
|
|
'anomalies': [True],
|
|
|
|
'temp': [17],
|
|
|
|
'water': [d1.water_level],
|
|
|
|
'nutri': [d1.nutrients_level],
|
|
|
|
'pests': [pe],
|
|
|
|
'weeds': [we],
|
|
|
|
'ripeness': [p1.growth_level],
|
|
|
|
'season_autumn': [True], 'season_spring': [False], 'season_summer': [False], 'season_winter': [False],
|
|
|
|
'weather_heavyCloudy': [False], 'weather_partCloudy': [False], 'weather_precipitation': [False],
|
|
|
|
'weather_sunny': [True],
|
|
|
|
'type_cereal': [t1], 'type_fruit': [t2], 'type_none': [t3], 'type_vegetable': [t4]
|
|
|
|
}
|
|
|
|
df = pd.DataFrame(dane)
|
|
|
|
df.to_csv('model_data.csv', index=False)
|
|
|
|
|
|
|
|
model = joblib.load('model.pkl')
|
|
|
|
nowe_dane = pd.read_csv('model_data.csv')
|
|
|
|
|
|
|
|
predykcje = model.predict(nowe_dane)
|
|
|
|
|
|
|
|
# movement based on route-planning (test):
|
2024-04-14 23:19:06 +02:00
|
|
|
|
2024-04-09 15:02:58 +02:00
|
|
|
tractor.draw_tractor(WIN)
|
2024-04-10 01:52:13 +02:00
|
|
|
time.sleep(1)
|
2024-04-14 18:59:37 +02:00
|
|
|
if moves != False:
|
2024-04-14 17:20:27 +02:00
|
|
|
do_actions(tractor, WIN, moves)
|
2024-05-12 19:20:33 +02:00
|
|
|
print(predykcje)
|
|
|
|
if predykcje == 'work':
|
|
|
|
tractor.work_on_field(tile1, d1, p1)
|
2024-04-14 23:38:08 +02:00
|
|
|
time.sleep(30)
|
2024-04-14 17:20:27 +02:00
|
|
|
print("\n")
|
2024-04-10 01:52:13 +02:00
|
|
|
|
|
|
|
|
2024-03-07 18:01:12 +01:00
|
|
|
main()
|