diff --git a/.idea/Traktor AI.iml b/.idea/Traktor AI.iml index 078ddf5..858c4d5 100644 --- a/.idea/Traktor AI.iml +++ b/.idea/Traktor AI.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8d93904 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/source/NN/__pycache__/model.cpython-39.pyc b/source/NN/__pycache__/model.cpython-39.pyc new file mode 100644 index 0000000..60d7965 Binary files /dev/null and b/source/NN/__pycache__/model.cpython-39.pyc differ diff --git a/source/NN/__pycache__/neural_network.cpython-39.pyc b/source/NN/__pycache__/neural_network.cpython-39.pyc new file mode 100644 index 0000000..e7987c2 Binary files /dev/null and b/source/NN/__pycache__/neural_network.cpython-39.pyc differ diff --git a/source/__pycache__/astar.cpython-39.pyc b/source/__pycache__/astar.cpython-39.pyc index d9b59f2..2075401 100644 Binary files a/source/__pycache__/astar.cpython-39.pyc and b/source/__pycache__/astar.cpython-39.pyc differ diff --git a/source/__pycache__/genetic.cpython-39.pyc b/source/__pycache__/genetic.cpython-39.pyc new file mode 100644 index 0000000..31f9ad7 Binary files /dev/null and b/source/__pycache__/genetic.cpython-39.pyc differ diff --git a/source/__pycache__/ground.cpython-39.pyc b/source/__pycache__/ground.cpython-39.pyc index d3c6d44..358a7ba 100644 Binary files a/source/__pycache__/ground.cpython-39.pyc and b/source/__pycache__/ground.cpython-39.pyc differ diff --git a/source/__pycache__/plant.cpython-39.pyc b/source/__pycache__/plant.cpython-39.pyc index c7fc95f..c904c3a 100644 Binary files a/source/__pycache__/plant.cpython-39.pyc and b/source/__pycache__/plant.cpython-39.pyc differ diff --git a/source/__pycache__/tile.cpython-39.pyc b/source/__pycache__/tile.cpython-39.pyc index a03d927..fba915a 100644 Binary files a/source/__pycache__/tile.cpython-39.pyc and b/source/__pycache__/tile.cpython-39.pyc differ diff --git a/source/area/__pycache__/field.cpython-39.pyc b/source/area/__pycache__/field.cpython-39.pyc index 3badf32..347309b 100644 Binary files a/source/area/__pycache__/field.cpython-39.pyc and b/source/area/__pycache__/field.cpython-39.pyc differ diff --git a/source/area/field.py b/source/area/field.py index 25c1c7b..b08c028 100644 --- a/source/area/field.py +++ b/source/area/field.py @@ -6,6 +6,9 @@ from area.constants import WIDTH,FIELD_WIDTH,TILE_SIZE,GREY,ROWS,COLS from tile import Tile from ground import Dirt +from genetic import genetic_algorithm +import os + tiles = [] fieldX = (WIDTH-FIELD_WIDTH)/2 @@ -20,7 +23,7 @@ def positionFieldElements(): t.y += fieldY -def createTiles(): +''' def createTiles(): for y in range(0, COLS): for x in range(0, ROWS): tile = Tile(x*TILE_SIZE, y*TILE_SIZE) @@ -30,8 +33,47 @@ def createTiles(): tile.randomizeContent() tiles.append(tile) 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 + def createField(win): createTiles() for t in tiles: diff --git a/source/genetic.py b/source/genetic.py new file mode 100644 index 0000000..8b2873e --- /dev/null +++ b/source/genetic.py @@ -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 diff --git a/source/ground.py b/source/ground.py index 5f13386..a755c5c 100644 --- a/source/ground.py +++ b/source/ground.py @@ -20,7 +20,10 @@ class Dirt: elif i == 4: self.weed = True self.pest = True - elif i == 5: - self.obstacle = True + '''elif i == 5: + self.obstacle = True''' + + def set_ocstacle(self, obstacle_status): + self.obstacle = obstacle_status # add init, getters,setters diff --git a/source/main.py b/source/main.py index cf13d41..9a54d8b 100644 --- a/source/main.py +++ b/source/main.py @@ -13,7 +13,10 @@ from plant import Plant from bfs import graphsearch, Istate, succ from astar import a_star from NN.neural_network import load_model, load_image, guess_image, display_image, display_result -from PIL import Image +from PIL import Image +from genetic import genetic_algorithm + +from area.field import createTiles pygame.init() WIN_WIDTH = WIDTH + 300 @@ -23,6 +26,7 @@ pygame.display.set_caption('Intelligent tractor') def main(): run = True + window = drawWindow(WIN) pygame.display.update() diff --git a/source/model_data.csv b/source/model_data.csv index 8212e0f..e52cdaf 100644 --- a/source/model_data.csv +++ b/source/model_data.csv @@ -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 -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 diff --git a/source/resources/images/plant_photos/apples/5243790-apples-c-rex.jpg b/source/resources/images/plant_photos/apples/5243790-apples-c-rex.jpg new file mode 100644 index 0000000..366e00c Binary files /dev/null and b/source/resources/images/plant_photos/apples/5243790-apples-c-rex.jpg differ diff --git a/source/resources/images/plant_photos/apples/91FfdTLrL7L.jpg b/source/resources/images/plant_photos/apples/91FfdTLrL7L.jpg new file mode 100644 index 0000000..0341b3c Binary files /dev/null and b/source/resources/images/plant_photos/apples/91FfdTLrL7L.jpg differ diff --git a/source/resources/images/plant_photos/apples/apple.jpeg b/source/resources/images/plant_photos/apples/apple.jpeg new file mode 100644 index 0000000..3fa9f54 Binary files /dev/null and b/source/resources/images/plant_photos/apples/apple.jpeg differ diff --git a/source/resources/images/plant_photos/apples/fruits-apple-tree-parkland-358x256.jpg b/source/resources/images/plant_photos/apples/fruits-apple-tree-parkland-358x256.jpg new file mode 100644 index 0000000..00ed786 Binary files /dev/null and b/source/resources/images/plant_photos/apples/fruits-apple-tree-parkland-358x256.jpg differ diff --git a/source/resources/images/plant_photos/apples/images.jpeg b/source/resources/images/plant_photos/apples/images.jpeg new file mode 100644 index 0000000..cdb77ba Binary files /dev/null and b/source/resources/images/plant_photos/apples/images.jpeg differ diff --git a/source/resources/images/plant_photos/cauliflowers/0008247_amazing-cauliflower-plant_550.jpeg b/source/resources/images/plant_photos/cauliflowers/0008247_amazing-cauliflower-plant_550.jpeg new file mode 100644 index 0000000..703892d Binary files /dev/null and b/source/resources/images/plant_photos/cauliflowers/0008247_amazing-cauliflower-plant_550.jpeg differ diff --git a/source/resources/images/plant_photos/cauliflowers/cauliflower-growing-in-raised-bed-garden.png b/source/resources/images/plant_photos/cauliflowers/cauliflower-growing-in-raised-bed-garden.png new file mode 100644 index 0000000..d18c8b8 Binary files /dev/null and b/source/resources/images/plant_photos/cauliflowers/cauliflower-growing-in-raised-bed-garden.png differ diff --git a/source/resources/images/plant_photos/cauliflowers/ccc.jpeg b/source/resources/images/plant_photos/cauliflowers/ccc.jpeg new file mode 100644 index 0000000..bef5c33 Binary files /dev/null and b/source/resources/images/plant_photos/cauliflowers/ccc.jpeg differ diff --git a/source/resources/images/plant_photos/cauliflowers/how-to-grow-cauliflower-1403494-03-2bd8fd57ddc24aff98fff1c092c84586.jpg b/source/resources/images/plant_photos/cauliflowers/how-to-grow-cauliflower-1403494-03-2bd8fd57ddc24aff98fff1c092c84586.jpg new file mode 100644 index 0000000..9e32e40 Binary files /dev/null and b/source/resources/images/plant_photos/cauliflowers/how-to-grow-cauliflower-1403494-03-2bd8fd57ddc24aff98fff1c092c84586.jpg differ diff --git a/source/resources/images/plant_photos/radishes/imawes.jpeg b/source/resources/images/plant_photos/radishes/imawes.jpeg new file mode 100644 index 0000000..43acb54 Binary files /dev/null and b/source/resources/images/plant_photos/radishes/imawes.jpeg differ diff --git a/source/resources/images/plant_photos/radishes/imewges.jpeg b/source/resources/images/plant_photos/radishes/imewges.jpeg new file mode 100644 index 0000000..963dc6c Binary files /dev/null and b/source/resources/images/plant_photos/radishes/imewges.jpeg differ diff --git a/source/resources/images/plant_photos/radishes/radd.jpeg b/source/resources/images/plant_photos/radishes/radd.jpeg new file mode 100644 index 0000000..4e091bf Binary files /dev/null and b/source/resources/images/plant_photos/radishes/radd.jpeg differ diff --git a/source/resources/images/plant_photos/wheats/01cf52ec-8cf0-48ed-b55b-64c6fefbe9bf.png b/source/resources/images/plant_photos/wheats/01cf52ec-8cf0-48ed-b55b-64c6fefbe9bf.png new file mode 100644 index 0000000..9023274 Binary files /dev/null and b/source/resources/images/plant_photos/wheats/01cf52ec-8cf0-48ed-b55b-64c6fefbe9bf.png differ diff --git a/source/resources/images/plant_photos/wheats/0f6acdc3-fb9f-4aac-a670-28606e6c71ae.jpg b/source/resources/images/plant_photos/wheats/0f6acdc3-fb9f-4aac-a670-28606e6c71ae.jpg new file mode 100644 index 0000000..d0e00ab Binary files /dev/null and b/source/resources/images/plant_photos/wheats/0f6acdc3-fb9f-4aac-a670-28606e6c71ae.jpg differ diff --git a/source/resources/images/plant_photos/wheats/1a940558-0168-48a4-b68d-944dcdf47b3e.png b/source/resources/images/plant_photos/wheats/1a940558-0168-48a4-b68d-944dcdf47b3e.png new file mode 100644 index 0000000..2c9f6c3 Binary files /dev/null and b/source/resources/images/plant_photos/wheats/1a940558-0168-48a4-b68d-944dcdf47b3e.png differ diff --git a/source/resources/images/plant_photos/wheats/1b5fca6a-50be-45d4-8156-4e143c36b3bb.jpg b/source/resources/images/plant_photos/wheats/1b5fca6a-50be-45d4-8156-4e143c36b3bb.jpg new file mode 100644 index 0000000..5a4e2de Binary files /dev/null and b/source/resources/images/plant_photos/wheats/1b5fca6a-50be-45d4-8156-4e143c36b3bb.jpg differ diff --git a/source/resources/images/plant_photos/wheats/1c76aa4d-11f4-47d1-8bdd-2cb78deeeccf.jpg b/source/resources/images/plant_photos/wheats/1c76aa4d-11f4-47d1-8bdd-2cb78deeeccf.jpg new file mode 100644 index 0000000..09edf5c Binary files /dev/null and b/source/resources/images/plant_photos/wheats/1c76aa4d-11f4-47d1-8bdd-2cb78deeeccf.jpg differ diff --git a/source/resources/images/plant_photos/wheats/Triticum_monococcum_IMG_3029.jpg b/source/resources/images/plant_photos/wheats/Triticum_monococcum_IMG_3029.jpg new file mode 100644 index 0000000..1fe28d1 Binary files /dev/null and b/source/resources/images/plant_photos/wheats/Triticum_monococcum_IMG_3029.jpg differ diff --git a/source/resources/images/plant_photos/wheats/pexels-photo-12801993.jpeg b/source/resources/images/plant_photos/wheats/pexels-photo-12801993.jpeg new file mode 100644 index 0000000..a69c53a Binary files /dev/null and b/source/resources/images/plant_photos/wheats/pexels-photo-12801993.jpeg differ