2023-03-26 18:19:10 +02:00
|
|
|
import os
|
2023-03-10 22:52:29 +01:00
|
|
|
import pygame
|
|
|
|
import random
|
2023-03-22 21:46:26 +01:00
|
|
|
import land
|
2023-03-21 23:55:52 +01:00
|
|
|
import tractor
|
2023-03-22 17:13:24 +01:00
|
|
|
import blocks
|
2023-05-08 08:23:04 +02:00
|
|
|
import astar_search
|
2023-06-05 05:25:13 +02:00
|
|
|
import neural_network.inference
|
2023-03-13 10:36:49 +01:00
|
|
|
from pygame.locals import *
|
2023-03-10 22:52:29 +01:00
|
|
|
|
2023-05-22 09:32:17 +02:00
|
|
|
examples = [
|
|
|
|
['piasek', 'sucha', 'jalowa', 'żółty'],
|
|
|
|
['czarnoziem', 'wilgotna', 'bogata', 'brazowa'],
|
|
|
|
['rzedzina', 'wilgotna', 'bogata', 'zielona'],
|
|
|
|
['gleby murszowe', 'wilgotna', 'bogata', 'szara'],
|
|
|
|
['pustynne gleby', 'sucha', 'jalowa', 'pomarańczowa'],
|
|
|
|
['torfowiska', 'sucha', 'jalowa', 'czerwona']
|
|
|
|
]
|
|
|
|
|
|
|
|
attributes = ['typ_gleby', 'wilgotność', 'zawartość_składników', 'kolor']
|
|
|
|
|
|
|
|
# Tworzenie obiektu TreeLearn i nauka drzewa decyzyjnego
|
2023-05-22 15:46:58 +02:00
|
|
|
# tree_learner = TreeLearn()
|
|
|
|
# default_class = 'nieznane'
|
2023-05-22 09:32:17 +02:00
|
|
|
|
2023-05-22 15:46:58 +02:00
|
|
|
# tree_learner.train(examples, attributes, default_class)
|
2023-03-21 23:55:52 +01:00
|
|
|
|
2023-05-21 18:46:38 +02:00
|
|
|
class TreeLearn:
|
|
|
|
def __init__(self):
|
|
|
|
self.tree = None
|
|
|
|
|
|
|
|
def train(self, examples, attributes, default_class):
|
|
|
|
self.tree = self.build_tree(examples, attributes, default_class)
|
|
|
|
|
|
|
|
def build_tree(self, examples, attributes, default_class):
|
|
|
|
if not examples:
|
|
|
|
return Node(default_class)
|
|
|
|
|
|
|
|
if self.all_same_class(examples):
|
|
|
|
return Node(examples[0][-1])
|
|
|
|
|
|
|
|
if not attributes:
|
|
|
|
class_counts = self.get_class_counts(examples)
|
|
|
|
default_class = max(class_counts, key=class_counts.get)
|
|
|
|
return Node(default_class)
|
|
|
|
|
|
|
|
best_attribute = self.choose_attribute(examples, attributes)
|
|
|
|
root = Node(best_attribute)
|
|
|
|
|
|
|
|
attribute_values = self.get_attribute_values(examples, best_attribute)
|
|
|
|
|
|
|
|
for value in attribute_values:
|
|
|
|
new_examples = self.filter_examples(examples, best_attribute, value)
|
|
|
|
new_attributes = attributes[:]
|
|
|
|
new_attributes.remove(best_attribute)
|
|
|
|
new_default_class = max(self.get_class_counts(new_examples), key=lambda k: class_counts.get(k, 0))
|
|
|
|
|
|
|
|
subtree = self.build_tree(new_examples, new_attributes, new_default_class)
|
|
|
|
root.add_child(value, subtree)
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
def all_same_class(self, examples):
|
|
|
|
return len(set([example[-1] for example in examples])) == 1
|
|
|
|
|
|
|
|
def get_class_counts(self, examples):
|
|
|
|
class_counts = {}
|
|
|
|
for example in examples:
|
|
|
|
class_label = example[-1]
|
|
|
|
class_counts[class_label] = class_counts.get(class_label, 0) + 1
|
|
|
|
return class_counts
|
|
|
|
|
|
|
|
def choose_attribute(self, examples, attributes):
|
|
|
|
# Placeholder for attribute selection logic
|
|
|
|
return attributes[0]
|
|
|
|
|
|
|
|
def get_attribute_values(self, examples, attribute):
|
|
|
|
return list(set([example[attribute] for example in examples]))
|
|
|
|
|
|
|
|
def filter_examples(self, examples, attribute, value):
|
|
|
|
return [example for example in examples if example[attribute] == value]
|
|
|
|
|
|
|
|
|
|
|
|
class Node:
|
|
|
|
def __init__(self, label):
|
|
|
|
self.label = label
|
|
|
|
self.children = {}
|
|
|
|
|
|
|
|
def add_child(self, value, child):
|
|
|
|
self.children[value] = child
|
|
|
|
|
|
|
|
|
2023-03-21 23:55:52 +01:00
|
|
|
class Game:
|
2023-03-13 10:36:49 +01:00
|
|
|
cell_size = 50
|
2023-03-27 11:43:13 +02:00
|
|
|
cell_number = 15 # horizontally
|
2023-05-08 21:15:37 +02:00
|
|
|
blocks_number = 20
|
2023-03-22 21:09:00 +01:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
def __init__(self):
|
2023-03-27 15:29:06 +02:00
|
|
|
|
2023-03-22 21:09:00 +01:00
|
|
|
self.dead_leaf_body = []
|
|
|
|
self.green_leaf_body = []
|
|
|
|
self.stone_body = []
|
|
|
|
self.flower_body = []
|
2023-03-22 21:46:26 +01:00
|
|
|
self.dead_grass_body = []
|
|
|
|
self.grass_body = []
|
2023-05-08 21:15:37 +02:00
|
|
|
self.red_block = [] #aim block
|
2023-03-22 21:46:26 +01:00
|
|
|
|
2023-03-27 15:29:06 +02:00
|
|
|
self.fawn_seed_body = []
|
|
|
|
self.fawn_wheat_body = []
|
|
|
|
|
2023-03-27 11:43:13 +02:00
|
|
|
self.black_earth_body = []
|
2023-03-27 15:29:06 +02:00
|
|
|
self.green_earth_body = []
|
2023-03-27 11:43:13 +02:00
|
|
|
self.fawn_soil_body = []
|
|
|
|
self.fen_soil_body = []
|
|
|
|
self.allBodyPos = []
|
2023-03-26 18:19:10 +02:00
|
|
|
|
2023-03-22 21:09:00 +01:00
|
|
|
self.entire_block = {}
|
|
|
|
|
2023-03-27 11:43:13 +02:00
|
|
|
# initialize a window
|
2023-03-10 22:52:29 +01:00
|
|
|
pygame.init()
|
2023-03-27 11:43:13 +02:00
|
|
|
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number))
|
|
|
|
|
|
|
|
# finds places for every type soil and grass
|
|
|
|
self.black_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
|
|
|
|
self.black_earth.locate_soil(self.black_earth_body)
|
2023-03-27 15:29:06 +02:00
|
|
|
self.green_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
|
|
|
|
self.green_earth.locate_soil(self.green_earth_body)
|
2023-03-27 11:43:13 +02:00
|
|
|
self.fawn_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
|
|
|
|
self.fawn_soil.locate_soil(self.fawn_soil_body)
|
|
|
|
self.fen_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
|
|
|
|
self.fen_soil.locate_soil(self.fen_soil_body)
|
|
|
|
self.grass = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
|
|
|
|
|
|
|
|
self.blocks = blocks.Blocks(self.surface, self.cell_size)
|
2023-03-22 21:09:00 +01:00
|
|
|
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.dead_leaf_body)
|
|
|
|
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.stone_body)
|
|
|
|
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body)
|
2023-03-12 20:22:10 +01:00
|
|
|
|
2023-05-08 21:15:37 +02:00
|
|
|
#self.blocks.locate_blocks(1, self.cell_number, self.red_block)
|
|
|
|
|
2023-03-27 15:29:06 +02:00
|
|
|
# self.potato = blocks.Blocks(self.surface, self.cell_size)
|
|
|
|
# self.potato.locate_soil('black earth', 6, 1, [])
|
2023-03-26 18:19:10 +02:00
|
|
|
|
2023-03-21 23:55:52 +01:00
|
|
|
self.tractor = tractor.Tractor(self.surface, self.cell_size)
|
2023-03-13 10:36:49 +01:00
|
|
|
self.tractor.draw()
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
def run(self):
|
2023-03-27 11:43:13 +02:00
|
|
|
# print(self.potato.get_soil_info().get_name())
|
|
|
|
# print(self.potato.get_soil_info().get_acidity())
|
|
|
|
# print(self.potato.get_soil_info().get_irrigation())
|
2023-03-10 22:52:29 +01:00
|
|
|
running = True
|
2023-03-13 10:36:49 +01:00
|
|
|
clock = pygame.time.Clock()
|
2023-04-17 10:20:05 +02:00
|
|
|
|
|
|
|
move_tractor_event = pygame.USEREVENT + 1
|
2023-06-05 05:25:13 +02:00
|
|
|
pygame.time.set_timer(move_tractor_event, 500) # tractor moves every 1000 ms
|
2023-04-17 10:20:05 +02:00
|
|
|
tractor_next_moves = []
|
2023-05-08 08:23:04 +02:00
|
|
|
astar_search_object = astar_search.Search(self.cell_size, self.cell_number)
|
2023-03-10 22:52:29 +01:00
|
|
|
|
2023-06-05 05:25:13 +02:00
|
|
|
veggies = dict()
|
|
|
|
veggies_debug = dict()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
while running:
|
2023-03-13 10:36:49 +01:00
|
|
|
clock.tick(60) # manual fps control not to overwork the computer
|
2023-03-10 22:52:29 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == KEYDOWN:
|
|
|
|
if pygame.key.get_pressed()[K_ESCAPE]:
|
|
|
|
running = False
|
|
|
|
# in case we want to use keyboard
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_UP]:
|
2023-04-16 13:07:46 +02:00
|
|
|
# self.tractor.move('up', self.cell_size, self.cell_number)
|
|
|
|
self.tractor.move('move', self.cell_size, self.cell_number)
|
|
|
|
# if pygame.key.get_pressed()[K_DOWN]:
|
|
|
|
# self.tractor.move('down', self.cell_size, self.cell_number)
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_LEFT]:
|
2023-03-27 11:43:13 +02:00
|
|
|
self.tractor.move('left', self.cell_size, self.cell_number)
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_RIGHT]:
|
2023-03-27 11:43:13 +02:00
|
|
|
self.tractor.move('right', self.cell_size, self.cell_number)
|
2023-03-22 21:09:00 +01:00
|
|
|
if pygame.key.get_pressed()[K_SPACE]:
|
2023-03-27 11:43:13 +02:00
|
|
|
self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
|
|
|
|
# self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size)
|
2023-03-27 15:29:06 +02:00
|
|
|
if pygame.key.get_pressed()[K_q]:
|
|
|
|
self.tractor.harvest(self.fawn_seed_body, self.fawn_wheat_body, self.cell_size)
|
|
|
|
self.tractor.put_seed(self.fawn_soil_body, self.fawn_seed_body, self.cell_size)
|
2023-04-17 10:20:05 +02:00
|
|
|
if event.type == move_tractor_event:
|
|
|
|
if len(tractor_next_moves) == 0:
|
|
|
|
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
|
|
|
|
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
|
2023-04-17 15:46:17 +02:00
|
|
|
print("Generated target: ",random_x, random_y)
|
2023-05-08 21:15:37 +02:00
|
|
|
if self.red_block:
|
|
|
|
self.red_block.pop()
|
|
|
|
self.red_block.append([random_x/50, random_y/50])
|
2023-04-17 16:53:54 +02:00
|
|
|
# below line should be later moved into tractor.py
|
|
|
|
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
|
2023-05-08 08:23:04 +02:00
|
|
|
#bandaid to know about stones
|
|
|
|
tractor_next_moves = astar_search_object.astarsearch(
|
2023-05-15 16:58:23 +02:00
|
|
|
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.stone_body, self.flower_body)
|
2023-06-05 15:49:35 +02:00
|
|
|
current_veggie = next(os.walk('./neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./neural_network/images/test'))[1])-1)]
|
2023-06-05 05:25:13 +02:00
|
|
|
if(current_veggie in veggies_debug):
|
|
|
|
veggies_debug[current_veggie]+=1
|
|
|
|
else:
|
|
|
|
veggies_debug[current_veggie] = 1
|
|
|
|
|
2023-06-05 15:49:35 +02:00
|
|
|
current_veggie_example = next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2])-1)]
|
2023-06-05 05:25:13 +02:00
|
|
|
predicted_veggie = neural_network.inference.main(f"./neural_network/images/test/{current_veggie}/{current_veggie_example}")
|
|
|
|
if predicted_veggie in veggies:
|
|
|
|
veggies[predicted_veggie]+=1
|
|
|
|
else:
|
|
|
|
veggies[predicted_veggie] = 1
|
|
|
|
print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies)
|
|
|
|
|
2023-04-17 10:20:05 +02:00
|
|
|
else:
|
|
|
|
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
|
2023-03-10 22:52:29 +01:00
|
|
|
elif event.type == QUIT:
|
|
|
|
running = False
|
|
|
|
|
2023-03-27 15:29:06 +02:00
|
|
|
self.surface.fill((123, 56, 51)) # background color
|
2023-03-27 11:43:13 +02:00
|
|
|
self.grass.set_and_place_block_of_grass('good')
|
|
|
|
self.black_earth.place_soil(self.black_earth_body, 'black_earth')
|
2023-03-27 15:29:06 +02:00
|
|
|
self.green_earth.place_soil(self.green_earth_body, 'green_earth')
|
2023-03-27 11:43:13 +02:00
|
|
|
self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil')
|
|
|
|
self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil')
|
2023-03-22 17:13:24 +01:00
|
|
|
|
2023-04-17 10:20:05 +02:00
|
|
|
# plants examples
|
2023-03-22 21:09:00 +01:00
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf')
|
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.green_leaf_body, 'alive')
|
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.stone_body, 'stone')
|
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower')
|
2023-03-22 17:13:24 +01:00
|
|
|
|
2023-05-08 21:15:37 +02:00
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.red_block, 'red')
|
|
|
|
|
2023-04-17 10:20:05 +02:00
|
|
|
# seeds
|
2023-03-27 15:29:06 +02:00
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_seed_body, 'fawn_seed')
|
|
|
|
|
2023-04-17 10:20:05 +02:00
|
|
|
# wheat
|
2023-03-27 15:29:06 +02:00
|
|
|
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_wheat_body, 'fawn_wheat')
|
|
|
|
|
2023-03-13 10:36:49 +01:00
|
|
|
self.tractor.draw()
|
2023-03-13 14:38:22 +01:00
|
|
|
pygame.display.update()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
game = Game()
|
|
|
|
game.run()
|