find flower path: fix deleted main.py
This commit is contained in:
parent
fdd3b40163
commit
0f8d9a1032
284
src/main.py
Normal file
284
src/main.py
Normal file
@ -0,0 +1,284 @@
|
|||||||
|
import pygame
|
||||||
|
import random
|
||||||
|
#import Flower
|
||||||
|
import Beehive
|
||||||
|
import Frames
|
||||||
|
from GeneticAlgorithm import GA
|
||||||
|
from Field import Field, Neighbors
|
||||||
|
from queue import Queue
|
||||||
|
import heapq as h
|
||||||
|
from checkFlower import chek_flower
|
||||||
|
from PIL import Image
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def Manhattan_dis(start, end):
|
||||||
|
start_x, start_y = start
|
||||||
|
end_x, end_y = end
|
||||||
|
return abs(start_x - end_x) + abs(start_y - end_y)
|
||||||
|
|
||||||
|
|
||||||
|
def Make_path(start, end, previos):
|
||||||
|
path = [end]
|
||||||
|
while start not in path:
|
||||||
|
path.append(previos[path[-1]])
|
||||||
|
path.reverse()
|
||||||
|
return path
|
||||||
|
|
||||||
|
def A_star(field, start, end):
|
||||||
|
open = []
|
||||||
|
previous = {}
|
||||||
|
closed = set()
|
||||||
|
checked = set()
|
||||||
|
now = (0, start)
|
||||||
|
h.heappush(open, (0, start))
|
||||||
|
while len(open) > 0:
|
||||||
|
now = h.heappop(open)
|
||||||
|
if now[1] in closed:
|
||||||
|
continue
|
||||||
|
if now[1][0:2] == end:
|
||||||
|
path = Make_path(start, now[1], previous)
|
||||||
|
return path
|
||||||
|
closed.add(now[1])
|
||||||
|
checked.add(now[1][0:2])
|
||||||
|
for x in Neighbors(field, now[1]):
|
||||||
|
if x not in closed and x not in checked:
|
||||||
|
previous[x] = now[1]
|
||||||
|
if now[1][0:2] != x[0:2]:
|
||||||
|
added_cost = field[x[0], x[1]] + Manhattan_dis(x[0:2], end)
|
||||||
|
else:
|
||||||
|
added_cost = 0
|
||||||
|
h.heappush(open, (now[0] + added_cost, x))
|
||||||
|
checked.add(x)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
white = (255, 255, 255)
|
||||||
|
|
||||||
|
#setting caption and icon
|
||||||
|
pygame.display.set_caption('Smart Bee')
|
||||||
|
setIcon = pygame.image.load('spritesNtiles/bee64.png')
|
||||||
|
pygame.display.set_icon(setIcon)
|
||||||
|
|
||||||
|
#
|
||||||
|
###
|
||||||
|
########################################
|
||||||
|
|
||||||
|
|
||||||
|
#SHORT = pygame.image.load('spritesNtiles/shortGrass64.png') #0
|
||||||
|
#TALL = pygame.image.load('spritesNtiles/tallGrass64.png') #1
|
||||||
|
#TREE = pygame.image.load('spritesNtiles/dirtTree64.png') #10
|
||||||
|
|
||||||
|
|
||||||
|
### tilemap
|
||||||
|
FLOWER = 0
|
||||||
|
SHORT = 1
|
||||||
|
TALL = 80
|
||||||
|
TREE = 100
|
||||||
|
BEE = pygame.image.load('spritesNtiles/bee64.png')
|
||||||
|
|
||||||
|
Directions = {
|
||||||
|
0 : 'north',
|
||||||
|
1 : 'east',
|
||||||
|
2 : 'south',
|
||||||
|
3 : 'west'
|
||||||
|
}
|
||||||
|
|
||||||
|
TREE_IMG_PATH = 'spritesNtiles/dirtTree64.png'
|
||||||
|
SHORT_IMG_PATH = 'spritesNtiles/shortGrass64.png'
|
||||||
|
TALL_IMG_PATH = 'spritesNtiles/tallGrass64.png'
|
||||||
|
BEE_HOME = 'spritesNtiles/beehome.png'
|
||||||
|
|
||||||
|
tiles_types = {
|
||||||
|
SHORT: pygame.image.load('spritesNtiles/shortGrass64.png'),
|
||||||
|
TALL: pygame.image.load('spritesNtiles/tallGrass64.png'),
|
||||||
|
TREE: pygame.image.load('spritesNtiles/dirtTree64.png'),
|
||||||
|
FLOWER: pygame.image.load('ANN\\rdy-dataset\\test\\rose\\377277099_544769262c_c.jpg')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
tilemapSizeX = 12
|
||||||
|
tilemapSizeY = 12
|
||||||
|
tileSize = 64
|
||||||
|
|
||||||
|
tilemap_types = []
|
||||||
|
|
||||||
|
for x in range(tilemapSizeX):
|
||||||
|
tilemap_types.append([])
|
||||||
|
for y in range(tilemapSizeY):
|
||||||
|
rr = random.choices([SHORT, TALL, FLOWER], weights=[0.5, 0.4, 0.1])[0]
|
||||||
|
tilemap_types[x].append(rr)
|
||||||
|
|
||||||
|
|
||||||
|
#example tilemap values
|
||||||
|
tilemap_values = []
|
||||||
|
|
||||||
|
SHORT_VALUE = 1
|
||||||
|
TALL_VALUE = 100
|
||||||
|
TREE_VALUE = 200
|
||||||
|
FLOWER_VALUE = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
#calculate the window size
|
||||||
|
disX = tilemapSizeX * tileSize
|
||||||
|
disY = tilemapSizeY * tileSize
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
dis = pygame.display.set_mode((disX, disY))
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
#position of the bee and pos changings
|
||||||
|
|
||||||
|
|
||||||
|
# Define parameters
|
||||||
|
flower_positions = [(flower_x, flower_y) for flower_x in range(tilemapSizeX) for flower_y in range(tilemapSizeY) if tilemap_types[flower_x][flower_y] == FLOWER]
|
||||||
|
generations = 10
|
||||||
|
# Create an instance of the GeneticAlgorithm class
|
||||||
|
ga = GA(10, generations,flower_positions, tilemap_types)
|
||||||
|
|
||||||
|
# Run the genetic algorithm
|
||||||
|
best_individual = ga.run()
|
||||||
|
print(best_individual)
|
||||||
|
|
||||||
|
# Retrieve the best tree arrangement from the best individual
|
||||||
|
tree_arrangement = best_individual
|
||||||
|
|
||||||
|
# Render the tilemap with the optimized tree positions
|
||||||
|
for x, y in tree_arrangement:
|
||||||
|
tilemap_types[x][y] = TREE
|
||||||
|
print(x,y)
|
||||||
|
|
||||||
|
for X in range(0, len(tilemap_types)):
|
||||||
|
tilemap_values.append([])
|
||||||
|
for Y in range(0, len(tilemap_types[X])):
|
||||||
|
if tilemap_types[X][Y] == SHORT: value = SHORT_VALUE
|
||||||
|
elif tilemap_types[X][Y] == TALL: value = TALL_VALUE
|
||||||
|
elif tilemap_types[X][Y] == TREE: value = TREE_VALUE
|
||||||
|
elif tilemap_types[X][Y] == FLOWER: value = FLOWER_VALUE
|
||||||
|
tilemap_values[X].append(value)
|
||||||
|
field = Field(tilemap_types, tilemap_values)
|
||||||
|
|
||||||
|
|
||||||
|
for X in range(tilemapSizeX):
|
||||||
|
for Y in range(tilemapSizeY):
|
||||||
|
if(tilemap_types[X][Y] == TREE):
|
||||||
|
field.set_image(X, Y, TREE_IMG_PATH)
|
||||||
|
elif(tilemap_types[X][Y] == TALL):
|
||||||
|
field.set_image(X, Y, TALL_IMG_PATH)
|
||||||
|
elif(tilemap_types[X][Y] == SHORT):
|
||||||
|
field.set_image(X, Y, SHORT_IMG_PATH)
|
||||||
|
elif(tilemap_types[X][Y] == FLOWER):
|
||||||
|
flower_type_num = random.randint(0, 5)
|
||||||
|
class_names = os.listdir("ANN\\dataset")
|
||||||
|
flower_type = class_names[flower_type_num]
|
||||||
|
field.set_flower_type(X, Y, flower_type)
|
||||||
|
|
||||||
|
selected_flower_list = os.listdir(f"ANN\\dataset\\{flower_type}\\")
|
||||||
|
flower_img = f"ANN\\dataset\\{flower_type}\\{random.choice(selected_flower_list)}"
|
||||||
|
field.set_image(X, Y, flower_img)
|
||||||
|
|
||||||
|
|
||||||
|
bee_dir = 'west'
|
||||||
|
while True:
|
||||||
|
bee_x = random.randint(0, tilemapSizeX-2)
|
||||||
|
bee_y = random.randint(0, tilemapSizeY-2)
|
||||||
|
if(field.get_type([bee_x, bee_y]) != TREE):
|
||||||
|
break
|
||||||
|
|
||||||
|
field.set_image(bee_x, bee_y, BEE_HOME)
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
random_x = random.randint(0, tilemapSizeX-2)
|
||||||
|
random_y = random.randint(0, tilemapSizeY-2)
|
||||||
|
if(field.get_type([random_x, random_y]) != TREE):
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
path = []
|
||||||
|
random.shuffle(flower_positions)
|
||||||
|
|
||||||
|
prev_x = bee_x
|
||||||
|
prev_y = bee_y
|
||||||
|
start_pos = True
|
||||||
|
for x, y in flower_positions:
|
||||||
|
path_A_star = A_star(field, (prev_x, prev_y, 3), (x, y))
|
||||||
|
if path_A_star is not None:
|
||||||
|
if start_pos is True:
|
||||||
|
path.extend(path_A_star)
|
||||||
|
start_pos = False
|
||||||
|
prev_x = x
|
||||||
|
prev_y = y
|
||||||
|
else:
|
||||||
|
path.extend(path_A_star[1:]) # Exclude the starting position of the current path
|
||||||
|
prev_x = x
|
||||||
|
prev_y = y
|
||||||
|
else:
|
||||||
|
continue # Skip the current flower position if no path is found
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# path_A_star = A_star(field, (bee_x, bee_y, 3), (random_x, random_y))
|
||||||
|
check_path_A = []
|
||||||
|
for x in path:
|
||||||
|
if x[0:2] not in check_path_A:
|
||||||
|
check_path_A.append(x[0:2])
|
||||||
|
print(check_path_A)
|
||||||
|
print(len(check_path_A))
|
||||||
|
#path = path_A_star
|
||||||
|
|
||||||
|
|
||||||
|
current_node = 0
|
||||||
|
step = 1
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# check for events
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
quit()
|
||||||
|
|
||||||
|
|
||||||
|
bee_x, bee_y, bee_dir = path[current_node]
|
||||||
|
|
||||||
|
bee_dir = Directions[bee_dir]
|
||||||
|
|
||||||
|
# render the bee and the tilemap
|
||||||
|
dis.fill(white)
|
||||||
|
for x in range(tilemapSizeX):
|
||||||
|
for y in range(tilemapSizeY):
|
||||||
|
tile = tilemap_types[x][y]
|
||||||
|
surface = pygame.image.load(field.get_image((x, y)))
|
||||||
|
dis.blit(surface, (x * tileSize, y * tileSize))
|
||||||
|
|
||||||
|
# rotate and render the bee
|
||||||
|
if bee_dir == 'west':
|
||||||
|
bee_image = BEE
|
||||||
|
elif bee_dir == 'east':
|
||||||
|
bee_image = pygame.transform.rotate(BEE, 180)
|
||||||
|
elif bee_dir == 'north':
|
||||||
|
bee_image = pygame.transform.rotate(BEE, -90)
|
||||||
|
elif bee_dir == 'south':
|
||||||
|
bee_image = pygame.transform.rotate(BEE, 90)
|
||||||
|
|
||||||
|
dis.blit(bee_image, (bee_x * tileSize, bee_y * tileSize))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
clock.tick(5)
|
||||||
|
|
||||||
|
if(current_node + 1 == len(path)):
|
||||||
|
step *= -1
|
||||||
|
if(step == -1 and current_node == 0):
|
||||||
|
step *= -1
|
||||||
|
current_node += step
|
Loading…
Reference in New Issue
Block a user