Merge pull request 'genetic_algorithm' (#7) from genetic_algorithm into master
Reviewed-on: #7
@ -4,7 +4,7 @@
|
|||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="Python 3.10 (Traktor)" jdkType="Python SDK" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
4
.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
BIN
source/NN/__pycache__/model.cpython-39.pyc
Normal file
BIN
source/NN/__pycache__/neural_network.cpython-39.pyc
Normal file
@ -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)
|
||||||
|
BIN
source/__pycache__/genetic.cpython-311.pyc
Normal file
BIN
source/__pycache__/genetic.cpython-39.pyc
Normal file
BIN
source/__pycache__/main.cpython-311.pyc
Normal file
@ -6,6 +6,9 @@ from area.constants import WIDTH,FIELD_WIDTH,TILE_SIZE,GREY,ROWS,COLS
|
|||||||
from tile import Tile
|
from tile import Tile
|
||||||
from ground import Dirt
|
from ground import Dirt
|
||||||
|
|
||||||
|
from genetic import genetic_algorithm
|
||||||
|
import os
|
||||||
|
|
||||||
tiles = []
|
tiles = []
|
||||||
|
|
||||||
fieldX = (WIDTH-FIELD_WIDTH)/2
|
fieldX = (WIDTH-FIELD_WIDTH)/2
|
||||||
@ -20,7 +23,7 @@ def positionFieldElements():
|
|||||||
t.y += fieldY
|
t.y += fieldY
|
||||||
|
|
||||||
|
|
||||||
def createTiles():
|
''' def createTiles():
|
||||||
for y in range(0, COLS):
|
for y in range(0, COLS):
|
||||||
for x in range(0, ROWS):
|
for x in range(0, ROWS):
|
||||||
tile = Tile(x*TILE_SIZE, y*TILE_SIZE)
|
tile = Tile(x*TILE_SIZE, y*TILE_SIZE)
|
||||||
@ -30,8 +33,47 @@ def createTiles():
|
|||||||
tile.randomizeContent()
|
tile.randomizeContent()
|
||||||
tiles.append(tile)
|
tiles.append(tile)
|
||||||
positionFieldElements()
|
positionFieldElements()
|
||||||
|
return tiles '''
|
||||||
|
|
||||||
|
def createTiles():
|
||||||
|
best = genetic_algorithm(50, 20, 0.7, 10)
|
||||||
|
|
||||||
|
for y in range(COLS):
|
||||||
|
for x in range (ROWS):
|
||||||
|
tile = Tile(x * TILE_SIZE, y * TILE_SIZE)
|
||||||
|
dirt = Dirt(random.randint(1, 100), random.randint(1, 100))
|
||||||
|
dirt.pests_and_weeds()
|
||||||
|
|
||||||
|
crop = best[y][x]
|
||||||
|
if crop == 'apple':
|
||||||
|
tile.image = "resources/images/sampling.png"
|
||||||
|
photo_path = random.choice(os.listdir("resources/images/plant_photos/apples"))
|
||||||
|
tile.photo = os.path.join("resources/images/plant_photos/apples", photo_path)
|
||||||
|
elif crop == 'cauliflower':
|
||||||
|
tile.image = "resources/images/sampling.png"
|
||||||
|
photo_path = random.choice(os.listdir("resources/images/plant_photos/cauliflowers"))
|
||||||
|
tile.photo = os.path.join("resources/images/plant_photos/cauliflowers", photo_path)
|
||||||
|
elif crop == 'radish':
|
||||||
|
tile.image = "resources/images/sampling.png"
|
||||||
|
photo_path = random.choice(os.listdir("resources/images/plant_photos/radishes"))
|
||||||
|
tile.photo = os.path.join("resources/images/plant_photos/radishes", photo_path)
|
||||||
|
elif crop == 'wheat':
|
||||||
|
tile.image = "resources/images/sampling.png"
|
||||||
|
photo_path = random.choice(os.listdir("resources/images/plant_photos/wheats"))
|
||||||
|
tile.photo = os.path.join("resources/images/plant_photos/wheats", photo_path)
|
||||||
|
elif crop == 'rock_dirt':
|
||||||
|
tile.image = "resources/images/rock_dirt.png"
|
||||||
|
dirt.set_ocstacle(True)
|
||||||
|
else:
|
||||||
|
tile.image = "resources/images/dirt.png"
|
||||||
|
tile.ground = "resources/images/background.jpg"
|
||||||
|
|
||||||
|
tile.ground = dirt
|
||||||
|
tiles.append(tile)
|
||||||
|
positionFieldElements()
|
||||||
return tiles
|
return tiles
|
||||||
|
|
||||||
|
|
||||||
def createField(win):
|
def createField(win):
|
||||||
createTiles()
|
createTiles()
|
||||||
for t in tiles:
|
for t in tiles:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
from NN.neural_network import clear_text_area
|
||||||
from crop_protection_product import CropProtectionProduct
|
from crop_protection_product import CropProtectionProduct
|
||||||
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH
|
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, WIDTH
|
||||||
from area.field import fieldX, fieldY, tiles
|
from area.field import fieldX, fieldY, tiles
|
||||||
import pygame
|
import pygame
|
||||||
import time
|
import time
|
||||||
@ -38,16 +39,19 @@ class Tractor:
|
|||||||
self.image = pygame.image.load('resources/images/tractor_left.png').convert_alpha()
|
self.image = pygame.image.load('resources/images/tractor_left.png').convert_alpha()
|
||||||
|
|
||||||
|
|
||||||
def work_on_field(self, tile, ground, plant1):
|
def work_on_field(self, screen, tile, ground, plant1):
|
||||||
|
results = []
|
||||||
if plant1 is None:
|
if plant1 is None:
|
||||||
tile.randomizeContent()
|
tile.randomizeContent()
|
||||||
# sprobuj zasadzic cos
|
# sprobuj zasadzic cos
|
||||||
print("Tarctor planted something")
|
print("Tarctor planted something")
|
||||||
|
results.append("Tarctor planted something")
|
||||||
elif plant1.growth_level == 100:
|
elif plant1.growth_level == 100:
|
||||||
tile.plant = None
|
tile.plant = None
|
||||||
ground.nutrients_level -= 40
|
ground.nutrients_level -= 40
|
||||||
ground.water_level -= 40
|
ground.water_level -= 40
|
||||||
print("Tractor collected something")
|
print("Tractor collected something")
|
||||||
|
results.append("Tractor collected something")
|
||||||
else:
|
else:
|
||||||
plant1.try_to_grow(50,50) #mozna dostosowac jeszcze
|
plant1.try_to_grow(50,50) #mozna dostosowac jeszcze
|
||||||
ground.nutrients_level -= 11
|
ground.nutrients_level -= 11
|
||||||
@ -61,6 +65,7 @@ class Tractor:
|
|||||||
elif plant1.plant_type == self.spinosad.plant_type:
|
elif plant1.plant_type == self.spinosad.plant_type:
|
||||||
t = "Tractor used Spinosad"
|
t = "Tractor used Spinosad"
|
||||||
print(t)
|
print(t)
|
||||||
|
results.append(t)
|
||||||
ground.pest = False
|
ground.pest = False
|
||||||
if ground.weed:
|
if ground.weed:
|
||||||
# traktor pozbywa się chwastow
|
# traktor pozbywa się chwastow
|
||||||
@ -71,13 +76,21 @@ class Tractor:
|
|||||||
elif plant1.plant_type == self.metazachlor.plant_type:
|
elif plant1.plant_type == self.metazachlor.plant_type:
|
||||||
t = "Tractor used Metazachlor"
|
t = "Tractor used Metazachlor"
|
||||||
print(t)
|
print(t)
|
||||||
|
results.append(t)
|
||||||
ground.weed = False
|
ground.weed = False
|
||||||
if ground.water_level < plant1.water_requirements:
|
if ground.water_level < plant1.water_requirements:
|
||||||
ground.water_level += 20
|
ground.water_level += 20
|
||||||
print("Tractor watered the plant")
|
print("Tractor watered the plant")
|
||||||
|
results.append("Tractor watered the plant")
|
||||||
if ground.nutrients_level < plant1.nutrients_requirements:
|
if ground.nutrients_level < plant1.nutrients_requirements:
|
||||||
ground.nutrients_level += 20
|
ground.nutrients_level += 20
|
||||||
print("Tractor added some nutrients")
|
print("Tractor added some nutrients")
|
||||||
|
results.append("Tractor added some nutrients")
|
||||||
|
|
||||||
|
clear_text_area(screen, WIDTH-90, 100, 400, 100)
|
||||||
|
for idx, result in enumerate(results):
|
||||||
|
display_work_results(screen, result, (WIDTH-90, 100 + idx * 30))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -135,27 +148,58 @@ class Tractor:
|
|||||||
|
|
||||||
|
|
||||||
def draw_tractor(self, win):
|
def draw_tractor(self, win):
|
||||||
|
|
||||||
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
||||||
win.blit(imageTractor, (self.rect.x, self.rect.y))
|
win.blit(imageTractor, (self.rect.x, self.rect.y))
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
def store_tile_image(self, tile):
|
||||||
|
return pygame.image.load(tile.image).convert_alpha()
|
||||||
|
|
||||||
|
def restore_tile_image(self, screen, tile):
|
||||||
|
image = pygame.image.load(tile.image).convert_alpha()
|
||||||
|
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
|
||||||
|
screen.blit(image, (tile.x, tile.y))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#translates move_list generated by bfs into the actual movement:
|
#translates move_list generated by bfs into the actual movement:
|
||||||
def do_actions(tractor, WIN, move_list):
|
def do_actions(tractor, WIN, move_list):
|
||||||
trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
|
# trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
|
||||||
trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
|
# trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
|
||||||
|
tile_images = {}
|
||||||
|
for tile in tiles:
|
||||||
|
tile_images[(tile.x, tile.y)] = tractor.store_tile_image(tile)
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
for move in move_list:
|
for move in move_list:
|
||||||
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
|
# WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
|
||||||
|
current_tile = None
|
||||||
|
for tile in tiles:
|
||||||
|
if tile.x == tractor.rect.x and tile.y == tractor.rect.y:
|
||||||
|
current_tile = tile
|
||||||
|
break
|
||||||
|
if current_tile:
|
||||||
|
tractor.restore_tile_image(WIN, current_tile)
|
||||||
|
|
||||||
if move == "move":
|
if move == "move":
|
||||||
tractor.move()
|
tractor.move()
|
||||||
elif move == "rotate_right":
|
elif move == "rotate_right":
|
||||||
tractor.rotate_to_right()
|
tractor.rotate_to_right()
|
||||||
elif move == "rotate_left":
|
elif move == "rotate_left":
|
||||||
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.35)
|
||||||
|
|
||||||
|
|
||||||
|
#displays results of the "work_on_field" function next to the field:
|
||||||
|
def display_work_results(screen, text, position):
|
||||||
|
font = pygame.font.Font(None, 30)
|
||||||
|
displayed_text = font.render(text, 1, (255,255,255))
|
||||||
|
screen.blit(displayed_text, position)
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ def heuristic(current_x, current_y, end_x, end_y):
|
|||||||
# actions(string): move, rotate_to_left, rotate_to_right
|
# actions(string): move, rotate_to_left, rotate_to_right
|
||||||
|
|
||||||
# main search function:
|
# main search function:
|
||||||
def a_star(istate, succ, goaltest, tractor):
|
def a_star(istate, succ_astar, goaltest):
|
||||||
fringe = []
|
fringe = []
|
||||||
explored = set()
|
explored = set()
|
||||||
node = Node(0, istate.get_x(), istate.get_y(), istate.get_direction(), None, None, 0)
|
node = Node(0, istate.get_x(), istate.get_y(), istate.get_direction(), None, None, 0)
|
||||||
@ -109,7 +109,7 @@ def a_star(istate, succ, goaltest, tractor):
|
|||||||
|
|
||||||
explored.add(elem)
|
explored.add(elem)
|
||||||
|
|
||||||
for (action, state) in succ(temp, tractor):
|
for (action, state) in succ_astar(temp):
|
||||||
fringe_tuple = []
|
fringe_tuple = []
|
||||||
explored_tuple = []
|
explored_tuple = []
|
||||||
|
|
||||||
|
@ -159,6 +159,42 @@ def succ(elem, tractor):
|
|||||||
return actions_states
|
return actions_states
|
||||||
|
|
||||||
|
|
||||||
|
#its the copy of successor function for A* only - tractor can ride through stones if there is no other way:
|
||||||
|
def succ_astar(elem):
|
||||||
|
actions_states = []
|
||||||
|
temp = copy.copy(elem.get_direction())
|
||||||
|
|
||||||
|
if temp == 1:
|
||||||
|
temp = 4
|
||||||
|
else:
|
||||||
|
temp -= 1
|
||||||
|
actions_states.append(("rotate_left", (elem.get_x(), elem.get_y(), temp)))
|
||||||
|
|
||||||
|
temp = copy.copy(elem.get_direction())
|
||||||
|
if temp == 4:
|
||||||
|
temp = 1
|
||||||
|
else:
|
||||||
|
temp += 1
|
||||||
|
actions_states.append(("rotate_right", (elem.get_x(), elem.get_y(), temp)))
|
||||||
|
|
||||||
|
temp_move_east = elem.get_x() + TILE_SIZE
|
||||||
|
temp_move_west = elem.get_x() - TILE_SIZE
|
||||||
|
temp_move_north = elem.get_y() - TILE_SIZE
|
||||||
|
temp_move_south = elem.get_y() + TILE_SIZE
|
||||||
|
|
||||||
|
if Tractor.can_it_move_node(elem) == "move east":
|
||||||
|
actions_states.append(("move", (temp_move_east, elem.get_y(), elem.get_direction())))
|
||||||
|
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move west":
|
||||||
|
actions_states.append(("move", (temp_move_west, elem.get_y(), elem.get_direction())))
|
||||||
|
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move north":
|
||||||
|
actions_states.append(("move", (elem.get_x(), temp_move_north, elem.get_direction())))
|
||||||
|
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move south":
|
||||||
|
actions_states.append(("move", (elem.get_x(), temp_move_south, elem.get_direction())))
|
||||||
|
|
||||||
|
return actions_states
|
||||||
|
|
||||||
#returns list of actions
|
#returns list of actions
|
||||||
def get_moves(elem):
|
def get_moves(elem):
|
||||||
|
98
source/genetic.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
def make_population(population_s, field_s):
|
||||||
|
population = []
|
||||||
|
crops = ['apple', 'cauliflower', 'radish', 'wheat', 'rock_dirt', 'dirt']
|
||||||
|
|
||||||
|
for _ in range(population_s):
|
||||||
|
i = []
|
||||||
|
for _ in range(field_s):
|
||||||
|
row = random.choices(crops, k=field_s)
|
||||||
|
i.append(row)
|
||||||
|
population.append(i)
|
||||||
|
return population
|
||||||
|
|
||||||
|
def calculate_fitness(individual):
|
||||||
|
cost = 0
|
||||||
|
for i in range(len(individual)):
|
||||||
|
for j in range(len(individual[i])):
|
||||||
|
crop = individual[i][j]
|
||||||
|
neighbors = [
|
||||||
|
individual[x][y]
|
||||||
|
for x in range(max(0, i-1), min(len(individual), i+2))
|
||||||
|
for y in range(max(0, j-1), min(len(individual), j+2))
|
||||||
|
if (x,y) != (i,j)
|
||||||
|
]
|
||||||
|
for n in neighbors:
|
||||||
|
if crop == 'wheat' and n == 'apple':
|
||||||
|
cost += 2
|
||||||
|
elif crop == 'cauliflower' and n == 'radish':
|
||||||
|
cost += 4
|
||||||
|
|
||||||
|
fitness = 1/(1+cost)
|
||||||
|
return fitness
|
||||||
|
|
||||||
|
def select_parents(population, fitnesses):
|
||||||
|
fitnesses_sum = sum(fitnesses)
|
||||||
|
selection_parts = [fitness / fitnesses_sum for fitness in fitnesses]
|
||||||
|
parents = random.choices(population, weights=selection_parts, k=2)
|
||||||
|
return parents
|
||||||
|
|
||||||
|
def crossover(parent_1, parent_2):
|
||||||
|
crossover_point = random.randint(1, (len(parent_1)-1))
|
||||||
|
child_1 = parent_1[:crossover_point] + parent_2[crossover_point:]
|
||||||
|
child_2 = parent_2[:crossover_point] + parent_1[crossover_point:]
|
||||||
|
return child_1, child_2
|
||||||
|
|
||||||
|
def mutation(individual, chance):
|
||||||
|
crops = ['apple', 'cauliflower', 'radish', 'wheat', 'rock_dirt', 'dirt']
|
||||||
|
if random.random() < chance:
|
||||||
|
row = random.randint(0, len(individual) - 1)
|
||||||
|
column = random.randint(0, len(individual[0]) - 1)
|
||||||
|
individual[row][column] = random.choice(crops)
|
||||||
|
return individual
|
||||||
|
|
||||||
|
def genetic_algorithm(population_s, field_s, chance, limit):
|
||||||
|
population = make_population(population_s, field_s)
|
||||||
|
|
||||||
|
best_fitness = 0
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
while best_fitness < 1:
|
||||||
|
fitnesses = [calculate_fitness(individual) for individual in population]
|
||||||
|
new_population = []
|
||||||
|
|
||||||
|
for _ in range(population_s // 2):
|
||||||
|
parent_1, parent_2 = select_parents(population, fitnesses)
|
||||||
|
|
||||||
|
p1c = calculate_fitness(parent_1)
|
||||||
|
p2c = calculate_fitness(parent_2)
|
||||||
|
print("p1c: ",p1c,"\np2c: ",p2c)
|
||||||
|
|
||||||
|
child_1, child_2 = crossover(parent_1, parent_2)
|
||||||
|
child_1 = mutation(child_1, chance)
|
||||||
|
child_2 = mutation(child_2, chance)
|
||||||
|
new_population.append(child_1)
|
||||||
|
new_population.append(child_2)
|
||||||
|
|
||||||
|
combined_population = population + new_population
|
||||||
|
combined_population = sorted(combined_population, key=calculate_fitness, reverse=True)
|
||||||
|
|
||||||
|
population = combined_population[:population_s]
|
||||||
|
|
||||||
|
current_best_fitness = calculate_fitness(population[0])
|
||||||
|
if current_best_fitness > best_fitness:
|
||||||
|
best_fitness = current_best_fitness
|
||||||
|
count = 0
|
||||||
|
else:
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
if count >= limit:
|
||||||
|
break
|
||||||
|
|
||||||
|
best_child = max(population, key=calculate_fitness)
|
||||||
|
|
||||||
|
bsf = calculate_fitness(best_child)
|
||||||
|
print("bsf: ", bsf)
|
||||||
|
|
||||||
|
return best_child
|
@ -20,7 +20,10 @@ class Dirt:
|
|||||||
elif i == 4:
|
elif i == 4:
|
||||||
self.weed = True
|
self.weed = True
|
||||||
self.pest = True
|
self.pest = True
|
||||||
elif i == 5:
|
'''elif i == 5:
|
||||||
self.obstacle = True
|
self.obstacle = True'''
|
||||||
|
|
||||||
|
def set_ocstacle(self, obstacle_status):
|
||||||
|
self.obstacle = obstacle_status
|
||||||
|
|
||||||
# add init, getters,setters
|
# add init, getters,setters
|
||||||
|
141
source/main.py
@ -5,15 +5,18 @@ import pandas as pd
|
|||||||
import joblib
|
import joblib
|
||||||
from area.constants import WIDTH, HEIGHT, TILE_SIZE
|
from area.constants import WIDTH, HEIGHT, TILE_SIZE
|
||||||
from area.field import drawWindow
|
from area.field import drawWindow
|
||||||
from area.tractor import Tractor, do_actions
|
from area.tractor import Tractor, do_actions, display_work_results
|
||||||
from area.field import tiles, fieldX, fieldY
|
from area.field import tiles, fieldX, fieldY
|
||||||
from area.field import get_tile_coordinates, get_tile_index
|
from area.field import get_tile_coordinates, get_tile_index
|
||||||
from ground import Dirt
|
from ground import Dirt
|
||||||
from plant import Plant
|
from plant import Plant
|
||||||
from bfs import graphsearch, Istate, succ
|
from bfs import graphsearch, Istate, succ_astar, 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 area.field import createTiles
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
WIN_WIDTH = WIDTH + 300
|
WIN_WIDTH = WIDTH + 300
|
||||||
@ -26,7 +29,23 @@ def main():
|
|||||||
window = drawWindow(WIN)
|
window = drawWindow(WIN)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
#getting coordinates of our "goal tile":
|
|
||||||
|
#Tractor initialization:
|
||||||
|
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
||||||
|
tractor.rect.x += fieldX
|
||||||
|
tractor.rect.y += fieldY
|
||||||
|
tractor.tractor_start = ((170, 100))
|
||||||
|
istate = Istate(170, 100, 2) #initial state
|
||||||
|
|
||||||
|
|
||||||
|
#main loop:
|
||||||
|
while run:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
run = False
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#getting coordinates of our "goal tile":
|
||||||
tile_index = get_tile_index()
|
tile_index = get_tile_index()
|
||||||
tile_x, tile_y = get_tile_coordinates(tile_index)
|
tile_x, tile_y = get_tile_coordinates(tile_index)
|
||||||
if tile_x is not None and tile_y is not None:
|
if tile_x is not None and tile_y is not None:
|
||||||
@ -41,35 +60,19 @@ def main():
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
#graphsearch activation:
|
tractor.tractor_end = ((tile_x, tile_y))
|
||||||
istate = Istate(170, 100, 2) #initial state
|
goaltest = [] #final state (consists of x and y because direction doesnt matter)
|
||||||
|
goaltest.append(tile_x)
|
||||||
goaltest = []
|
|
||||||
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
|
|
||||||
goaltest.append(tile_y)
|
goaltest.append(tile_y)
|
||||||
|
goaltest[0] = tile_x
|
||||||
|
goaltest[1]=tile_y
|
||||||
|
|
||||||
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
#moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
|
||||||
tractor.rect.x += fieldX
|
moves = (a_star(istate, succ_astar, goaltest))
|
||||||
tractor.rect.y += fieldY
|
|
||||||
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
|
|
||||||
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
|
|
||||||
tractor.tractor_end = ((goaltest[0], goaltest[1]))
|
|
||||||
|
|
||||||
#moves = (graphsearch(istate, succ, goaltest, tractor))
|
|
||||||
moves = (a_star(istate, succ, goaltest, tractor))
|
|
||||||
print(moves)
|
print(moves)
|
||||||
|
|
||||||
|
|
||||||
|
# movement based on route-planning:
|
||||||
#main loop:
|
|
||||||
while run:
|
|
||||||
for event in pygame.event.get():
|
|
||||||
if event.type == pygame.QUIT:
|
|
||||||
run = False
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# movement based on route-planning (test):
|
|
||||||
|
|
||||||
tractor.draw_tractor(WIN)
|
tractor.draw_tractor(WIN)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
if moves != False:
|
if moves != False:
|
||||||
@ -85,6 +88,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}")
|
||||||
@ -98,7 +102,8 @@ def main():
|
|||||||
#getting the name and type of the recognized plant:
|
#getting the name and type of the recognized plant:
|
||||||
p1.update_name(prediction)
|
p1.update_name(prediction)
|
||||||
|
|
||||||
#decission tree test:
|
|
||||||
|
#decission tree test:
|
||||||
if d1.pest:
|
if d1.pest:
|
||||||
pe = 1
|
pe = 1
|
||||||
else:
|
else:
|
||||||
@ -127,19 +132,71 @@ def main():
|
|||||||
t3 = True
|
t3 = True
|
||||||
t4 = False
|
t4 = False
|
||||||
|
|
||||||
|
weather_n = random.randint(1, 4)
|
||||||
|
if weather_n == 1:
|
||||||
|
h1 = True
|
||||||
|
h2 = False
|
||||||
|
h3 = False
|
||||||
|
h4 = False
|
||||||
|
else:
|
||||||
|
h1 = False
|
||||||
|
if weather_n == 2:
|
||||||
|
h2 = True
|
||||||
|
h3 = False
|
||||||
|
h4 = False
|
||||||
|
else:
|
||||||
|
h2 = False
|
||||||
|
if weather_n == 3:
|
||||||
|
h3 = True
|
||||||
|
h4 = False
|
||||||
|
else:
|
||||||
|
h3 = False
|
||||||
|
h4 = True
|
||||||
|
|
||||||
|
season_n = random.randint(1,4)
|
||||||
|
if season_n == 1:
|
||||||
|
s1 = True
|
||||||
|
s2 = False
|
||||||
|
s3 = False
|
||||||
|
s4 = False
|
||||||
|
temp_n = random.randint(0,22)
|
||||||
|
else:
|
||||||
|
s1 = False
|
||||||
|
if season_n == 2:
|
||||||
|
s2 = True
|
||||||
|
s3 = False
|
||||||
|
s4 = False
|
||||||
|
temp_n = random.randint(0,22)
|
||||||
|
else:
|
||||||
|
s2 = False
|
||||||
|
if season_n == 3:
|
||||||
|
s3 = True
|
||||||
|
s4 = False
|
||||||
|
temp_n = random.randint(20,39)
|
||||||
|
else:
|
||||||
|
s3 = False
|
||||||
|
s4 = True
|
||||||
|
temp_n = random.randint(-20, 10)
|
||||||
|
|
||||||
|
anomaly_n = random.randint(1, 10)
|
||||||
|
if anomaly_n == 1:
|
||||||
|
a1 = True
|
||||||
|
else:
|
||||||
|
a1 = False
|
||||||
dane = {
|
dane = {
|
||||||
'anomalies': [True],
|
'anomalies': [a1],
|
||||||
'temp': [17],
|
'temp': [temp_n],
|
||||||
'water': [d1.water_level],
|
'water': [d1.water_level],
|
||||||
'nutri': [d1.nutrients_level],
|
'nutri': [d1.nutrients_level],
|
||||||
'pests': [pe],
|
'pests': [pe],
|
||||||
'weeds': [we],
|
'weeds': [we],
|
||||||
'ripeness': [p1.growth_level],
|
'ripeness': [p1.growth_level],
|
||||||
'season_autumn': [True], 'season_spring': [False], 'season_summer': [False], 'season_winter': [False],
|
'season_autumn': [s1], 'season_spring': [s2], 'season_summer': [s3], 'season_winter': [s4],
|
||||||
'weather_heavyCloudy': [False], 'weather_partCloudy': [False], 'weather_precipitation': [False],
|
'weather_heavyCloudy': [h1], 'weather_partCloudy': [h2], 'weather_precipitation': [h3],
|
||||||
'weather_sunny': [True],
|
'weather_sunny': [h4],
|
||||||
'type_cereal': [t1], 'type_fruit': [t2], 'type_none': [t3], 'type_vegetable': [t4]
|
'type_cereal': [t1], 'type_fruit': [t2], 'type_none': [t3], 'type_vegetable': [t4]
|
||||||
}
|
}
|
||||||
|
|
||||||
df = pd.DataFrame(dane)
|
df = pd.DataFrame(dane)
|
||||||
df.to_csv('model_data.csv', index=False)
|
df.to_csv('model_data.csv', index=False)
|
||||||
|
|
||||||
@ -150,8 +207,20 @@ 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(WIN, goalTile, d1, p1)
|
||||||
time.sleep(50)
|
|
||||||
|
#update the initial state for the next target:
|
||||||
|
istate = Istate(tile_x, tile_y, tractor.direction)
|
||||||
|
|
||||||
|
#old goalTile is displayed with a black border - to show that it was an old target:
|
||||||
|
tiles[tile_index].image = "resources/images/sampling_old_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.draw_tractor(WIN)
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
anomalies,temp,water,nutri,pests,weeds,ripeness,season_autumn,season_spring,season_summer,season_winter,weather_heavyCloudy,weather_partCloudy,weather_precipitation,weather_sunny,type_cereal,type_fruit,type_none,type_vegetable
|
anomalies,temp,water,nutri,pests,weeds,ripeness,season_autumn,season_spring,season_summer,season_winter,weather_heavyCloudy,weather_partCloudy,weather_precipitation,weather_sunny,type_cereal,type_fruit,type_none,type_vegetable
|
||||||
True,17,32,2,0,0,54,True,False,False,False,False,False,False,True,True,False,False,False
|
True,17,72,73,0,0,60,True,False,False,False,False,False,False,True,False,True,False,False
|
||||||
|
|
After Width: | Height: | Size: 22 KiB |
BIN
source/resources/images/plant_photos/apples/91FfdTLrL7L.jpg
Normal file
After Width: | Height: | Size: 542 KiB |
BIN
source/resources/images/plant_photos/apples/apple.jpeg
Normal file
After Width: | Height: | Size: 7.0 KiB |
After Width: | Height: | Size: 37 KiB |
BIN
source/resources/images/plant_photos/apples/images.jpeg
Normal file
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 743 KiB |
BIN
source/resources/images/plant_photos/cauliflowers/ccc.jpeg
Normal file
After Width: | Height: | Size: 8.3 KiB |
After Width: | Height: | Size: 130 KiB |
BIN
source/resources/images/plant_photos/radishes/imawes.jpeg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
source/resources/images/plant_photos/radishes/imewges.jpeg
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
source/resources/images/plant_photos/radishes/radd.jpeg
Normal file
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 245 KiB |
After Width: | Height: | Size: 281 KiB |
After Width: | Height: | Size: 234 KiB |
After Width: | Height: | Size: 190 KiB |
After Width: | Height: | Size: 266 KiB |
After Width: | Height: | Size: 1.8 MiB |
After Width: | Height: | Size: 197 KiB |
BIN
source/resources/images/sampling_old_goal.png
Normal file
After Width: | Height: | Size: 77 KiB |