Added genetic algorithm

This commit is contained in:
kkhrystsenka 2024-06-25 06:57:55 +02:00
parent 3768d3a307
commit 1fd7a90fd1
5 changed files with 146 additions and 33 deletions

View File

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.11 (ai-wozek) (2)" /> <option name="sdkName" value="Python 3.11 (ai-wozek) (2)" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (ai-wozek) (2)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (ai-wozek)" project-jdk-type="Python SDK" />
</project> </project>

6
ai-wozek/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -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.11 (ai-wozek) (2)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.12 (ai-wozek)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

Binary file not shown.

View File

@ -1,8 +1,5 @@
import pygame import pygame
import sys import sys
import random
import os
import time
from collections import deque from collections import deque
import heapq import heapq
import torch import torch
@ -11,7 +8,6 @@ from classes import *
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import torchvision.transforms as transforms import torchvision.transforms as transforms
import math
from PIL import Image from PIL import Image
class Node(): class Node():
@ -385,7 +381,7 @@ tree_data_base = pd.read_csv('paczki.csv')
def entropy(data): def entropy(data):
labels = data.iloc[:, -1] # Ostatnia kolumna zawiera etykiety klas i pomija 1 wiersz bo jest tytulowy labels = data.iloc[:, -1] # Ostatnia kolumna zawiera etykiety klas i pomija 1 wiersz bo jest tytulowy
counts = labels.value_counts() #tu zlicza wszystkie opcje counts = labels.value_counts() #tu zlicza wszystkie opcje
probabilities = counts / len(labels) probabilities = counts / len(labels)
entropy = -sum(probabilities * np.log2(probabilities)) entropy = -sum(probabilities * np.log2(probabilities))
return entropy return entropy
@ -400,11 +396,143 @@ def information_gain(data, attribute):
return (total_entropy - weighted_entropy) return (total_entropy - weighted_entropy)
# Define constants
POPULATION_SIZE = 100
GENERATIONS = 50
MUTATION_RATE = 0.1
# Define movements
MOVES = ['UP', 'DOWN', 'LEFT', 'RIGHT']
# Fitness function
def fitness(path, start, goal, obstacles, center):
position = start[:]
for move in path:
if move == 'UP':
position[1] -= 1
elif move == 'DOWN':
position[1] += 1
elif move == 'LEFT':
position[0] -= 1
elif move == 'RIGHT':
position[0] += 1
# If we reach an obstacle
if position in obstacles:
return 0
# If we reach the goal
if position == goal:
break
# Calculate fitness based on distance from center
distance_from_center = abs(center[0] - goal[0]) + abs(center[1] - goal[1])
# We want to maximize distance from center
return distance_from_center
# Generate initial population
def generate_initial_population():
return [[random.choice(MOVES) for _ in range(50)] for _ in range(POPULATION_SIZE)]
# Selection
def selection(population, fitnesses):
selected = random.choices(population, weights=fitnesses, k=POPULATION_SIZE // 2)
return selected
# Crossover
def crossover(parent1, parent2):
crossover_point = random.randint(0, len(parent1))
return parent1[:crossover_point] + parent2[crossover_point:]
# Mutation
def mutate(path):
if random.random() < MUTATION_RATE:
index = random.randint(0, len(path) - 1)
path[index] = random.choice(MOVES)
return path
# Genetic Algorithm
def genetic_algorithm(start, goal, obstacles, center):
population = generate_initial_population()
for generation in range(GENERATIONS):
fitnesses = [fitness(path, start, goal, obstacles, center) for path in population]
population = selection(population, fitnesses)
new_population = []
for i in range(0, len(population), 2):
if i + 1 < len(population):
child1 = crossover(population[i], population[i + 1])
child2 = crossover(population[i + 1], population[i])
new_population.append(mutate(child1))
new_population.append(mutate(child2))
population = new_population
best_path = max(population, key=lambda p: fitness(p, start, goal, obstacles, center))
return best_path
def find_best_destination(freight, current_position, obstacles, center):
if freight.content != 'car_parts':
img = freight.image
image = Image.open(img).convert("RGB")
tensor = transform(image).unsqueeze(0)
with torch.no_grad():
tensor = tensor.to(device)
outputs = model(tensor)
probabilities = torch.nn.functional.softmax(outputs, dim=1)
score = probabilities.cpu().numpy().flatten()
best_goal = None
if score[0] > score[1] and score[0] > score[2]:
print("flammable")
best_goal, best_path = find_best_position_with_ga(current_position, sorted(free_lime), obstacles, center)
free_lime.remove(best_goal)
elif score[1] > score[0] and score[1] > score[2]:
print("fragile")
best_goal, best_path = find_best_position_with_ga(current_position, sorted(free_pink), obstacles, center)
free_pink.remove(best_goal)
elif score[2] > score[0] and score[2] > score[1]:
print("toxic")
best_goal, best_path = find_best_position_with_ga(current_position, sorted(free_red), obstacles, center)
free_red.remove(best_goal)
else:
best_goal, best_path = find_best_position_with_ga(current_position, sorted(free_cyan), obstacles, center)
free_cyan.remove(best_goal)
return best_goal
def find_best_position_with_ga(current_position, goal_positions, obstacles, center):
best_path = None
best_score = float('-inf') # Мы ищем максимальное значение
best_goal = None
for goal in goal_positions:
path = genetic_algorithm(current_position, goal, obstacles, center)
score = fitness(path, current_position, goal, obstacles, center)
if score > best_score:
best_score = score
best_path = path
best_goal = goal
return best_goal, best_path
# Main game loop # Main game loop
def game_loop(): def game_loop():
init_game() init_game()
obstacles = []
center = [GRID_WIDTH // 2, GRID_HEIGHT // 2]
current=Node(forklift_pos,rotation,'start',None,0) current=Node(forklift_pos,rotation,'start',None,0)
dest=current_freight.pop() dest=current_freight.pop()
final=Node([dest[0],dest[1]],'N','final',None,0) final=Node([dest[0],dest[1]],'N','final',None,0)
@ -442,32 +570,11 @@ def game_loop():
reset_truck_bed_freight() reset_truck_bed_freight()
if(forklift_pos==final.position): if(forklift_pos==final.position):
handle_freight() handle_freight()
destination=[] obstacles.append(final.position)
if carrying_freight: if carrying_freight:
if(carried_freight.content!='car_parts'): destination = find_best_destination(carried_freight, forklift_pos, obstacles, center)
img=carried_freight.image
image=Image.open(img).convert("RGB")
tensor=transform(image).unsqueeze(0)
with torch.no_grad():
tensor = tensor.to(device)
outputs = model(tensor)
probabilities = torch.nn.functional.softmax(outputs, dim=1)
score=probabilities.cpu().numpy().flatten()
if(score[0]>score[1] and score[0]>score[2]):
print("flammable")
destination=free_lime.pop()
elif(score[1]>score[0] and score[1]>score[2]):
print("fragile")
destination=free_pink.pop()
elif(score[2]>score[0] and score[2]>score[1]):
print("toxic")
destination=free_red.pop()
else:
destination=free_cyan.pop()
#return probabilities.cpu().numpy().flatten()
print(destination) print(destination)
final=Node([destination[0],destination[1]],'N','final',None,0) final = Node([destination[0],destination[1]],'N','final',None,0)
else: else:
destination=current_freight.pop() destination=current_freight.pop()
print(destination) print(destination)
@ -489,7 +596,7 @@ def game_loop():
rotate_forklift('R') rotate_forklift('R')
load_images() load_images()
i=i+1 i=i+1
pygame.time.wait(500) pygame.time.wait(100)
pygame.quit() pygame.quit()
sys.exit() sys.exit()