diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Machine_learning_2023.iml b/.idea/Machine_learning_2023.iml new file mode 100644 index 0000000..f6175b4 --- /dev/null +++ b/.idea/Machine_learning_2023.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c69c90c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5e0b360 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Project discription.txt b/Project discription.txt index 51ef6f1..465a02b 100644 --- a/Project discription.txt +++ b/Project discription.txt @@ -56,3 +56,61 @@ Summary: The "Automatic cleaning robot" project is a simple yet educational programming project. Users are tasked with specifying the positions that the robot should clean, as well as the coordinates of obstacles. The robot, built using artificial intelligence, is responsible for avoiding obstacles, making decisions in case of random events, and cleaning the designated points. The project was written in Python with the use of artificial intelligence. The analysis of images is based on neural networks. ****** +****** + +Dokumentacja projektu "Automatyczny robot sprzątający" + +Wprowadzenie: +Projekt "Automatyczny robot sprzątający" jest projektem bazującym się na symulacji pracy robota sprzątającego w pomieszczeniu za pomocą sztucznej inteligencji. Robot ma za zadanie wyznaczać miejsca do sprzątania oraz uniknąć przeszkód oraz reagować na zdarzenia losowe. Projekt jest napisany w języku Python. + +Instrukcja obsługi: + + Uruchomienie projektu: + Aby uruchomić projekt należy uruchomić plik "main.py" za pomocą interpretera Python. Projektu wyświetli się w konsoli.Po uruchomieniu projektu na ekranie wyświetli się plansza o wymiarach NxN (default: 10x10). Robot "Cleaner" (oznaczony jako "R" na planszy) startuje z pozycji (0,0). użytkownik ma za zadanie wprowadzić pozycje do sprzątania, które są oznaczone na planszy jako litery "D". Możliwe pozycje to liczby od 0 do N-1. + + Użytkownik wprowadza pozycje za pomocą terminala. Wprowadzenie koordynat odbywa się w następujący sposób: + Najpierw wprowadzamy numer wiersza, a następnie numer kolumny, oddzielając je spacją. + Przykładowo, jeśli chcemy wskazać pozycję (4,5) wpisujemy: "4 5". + Po wskazaniu pozycji do sprzątania, użytkownik musi uniknąć przeszkód, które są oznaczone na planszy jako znak "X". Robot nie może przejść przez przeszkody. Jeśli użytkownik wskazuje pozycję przeszkody, projektu zwróci błąd i będzie wymagała podania nowych współrzędnych. + + Przebieg projektu: + Robot, zgodnie z zbudowaną mapą, musi obliczyć najkrótszą ścieżkę do sprzątania wszystkich pozycji oraz uniknąć przeszkód. Podczas sprzątania mogą wystąpić przypadkowe zdarzenia, na które robot będzie reagował. W tym celu, z pomocą sieci neuronowych, robot analizuje zdjęcie zdarzenia, aby wybrać najlepsze rozwiązania. + + Zakończenie projektu: + Program kończy swoje działanie w momencie, gdy robot posprząta wszystkie przez użytkownika wybrane pola do sprzątania. Na zakończenie programu zostanie wyświetlona liczba wykonanych ruchów przez robota oraz podjęte decyzje w przypadku zaistnienia zdarzeń. + +Możliwe modyfikacje: + Projekt zostanie napisany z myślą o możliwości łatwej modyfikacji. Można zmienić wymiary planszy, dodać lub usunąć przeszkody oraz ilość przypadkowych zdarzeń i pozycji do sprzątania. Wszystkie te zmiany można wprowadzić w pliku "config.py". + +Podsumowanie: + Projekt "Automatyczny robot sprzątający" to prosty, ale edukacyjny projekt programistyczny. Użytkownik ma za zadanie wskazanie pozycji, które robot powinien posprzątać, a także koordynat przeszkody. Natomiast zadaniem robota, który został zbudowany przy użyciu sztucznej inteligencji, jest unikanie przeszkód, podejmowanie decyzji w przypadku wystąpienia przypadkowych zdarzeń oraz sprzątanie wyznaczonych punktów. Projekt został napisany w języku Python z wykorzystaniem sztucznej inteligencji. Analiza zdjęć jest oparta na sieciach neuronowych. + +****** + +Documentation of the "Automatic Cleaning Robot" project + +Introduction: + The "Automatic Cleaning Robot" project is based on simulating the work of a cleaning robot in a room using artificial intelligence. The robot is tasked with determining the areas to be cleaned, avoiding obstacles, and reacting to random events. The project is written in Python. + +User Guide: + Starting the project: + To start the project, you need to run the "main.py" file using a Python interpreter. The project will be displayed on the console. Once the project is launched, a 10x10 board will be displayed on the screen. The "Cleaner" robot (marked as "R" on the board) starts from the position (0,0). The user needs to enter the positions to be cleaned, which are marked as the letter "D" on the board. The possible positions are numbers from 0 to 9. + + The user enters the positions through the terminal. The entry of coordinates is done as follows: + First, we enter the row number, and then the column number, separating them with a space. + For example, if we want to indicate the position (4,5), we enter "4 5". + After indicating the positions to be cleaned, the user must avoid obstacles, which are marked on the board as the "X" symbol. The robot cannot pass through obstacles. If the user points to an obstacle position, the project will return an error and require new coordinates. + + Project process: + Based on the built map, the robot must calculate the shortest path to clean all positions and avoid obstacles. Random events may occur during cleaning, to which the robot will react. To do this, with the help of neural networks, the robot analyzes the image of the event to choose the best solutions. + + Project conclusion: + The program is ending when the robot cleans all the fields selected by the user. At the end of the program, the number of robot moves performed and the decisions made in case of events will be displayed. + +Possible modifications: + The "Automatic cleaning robot" project has been designed with the possibility of easy modifications in mind. Users can change the dimensions of the board, add or remove obstacles, and adjust the number of random events and cleaning positions. All these changes can be made in the "config.py" file. + +Summary: + The "Automatic cleaning robot" project is a simple yet educational programming project. Users are tasked with specifying the positions that the robot should clean, as well as the coordinates of obstacles. The robot, built using artificial intelligence, is responsible for avoiding obstacles, making decisions in case of random events, and cleaning the designated points. The project was written in Python with the use of artificial intelligence. The analysis of images is based on neural networks. + +****** diff --git a/decisionTree/data.csv b/decisionTree/data.csv new file mode 100644 index 0000000..0563613 --- /dev/null +++ b/decisionTree/data.csv @@ -0,0 +1,22 @@ +1-2-3-4-5;1-green 2-yellow 3-orange 4-black 5-while 6-blue;in dB 0-100;0-24;0/1;in cm;in C;0/1 +Size;Color;Sound;Time;Smell;Height;Temperature;ToRemove +1;2;0;16;1;10;25;1 +2;1;0;12;0;50;24;0 +2;3;30;13;1;38;38;0 +1;4;0;7;1;5;27;1 +1;2;0;16;1;10;25;1 +2;1;0;12;0;50;24;0 +2;3;30;13;1;38;38;0 +1;4;0;7;1;5;27;1 +1;2;0;16;1;10;25;1 +2;1;0;12;0;50;24;0 +2;3;30;13;1;38;38;0 +1;4;0;7;1;5;27;1 +1;2;0;16;1;10;25;1 +2;1;0;12;0;50;24;0 +2;3;30;13;1;38;38;0 +1;4;0;7;1;5;27;1 +1;2;0;16;1;10;25;1 +2;1;0;12;0;50;24;0 +2;3;30;13;1;38;38;0 +1;4;0;7;1;5;27;1 \ No newline at end of file diff --git a/decisionTree/decision_tree_model.pkl b/decisionTree/decision_tree_model.pkl new file mode 100644 index 0000000..58f7d19 Binary files /dev/null and b/decisionTree/decision_tree_model.pkl differ diff --git a/decisionTree/evaluate.py b/decisionTree/evaluate.py new file mode 100644 index 0000000..5c27f94 --- /dev/null +++ b/decisionTree/evaluate.py @@ -0,0 +1,9 @@ +import joblib + +def evaluate(data): + # Load the model + clf = joblib.load('decisionTree/decision_tree_model.pkl') + + # Make a prediction + prediction = clf.predict(data) + return prediction \ No newline at end of file diff --git a/decisionTree/prepare.py b/decisionTree/prepare.py new file mode 100644 index 0000000..93968c9 --- /dev/null +++ b/decisionTree/prepare.py @@ -0,0 +1,21 @@ +import pandas as pd +from sklearn.tree import DecisionTreeClassifier +from sklearn.model_selection import train_test_split +from sklearn import metrics +import joblib + +pima = pd.read_csv("data.csv", header=1, delimiter=';') + +feature_cols = ['Size', 'Color', 'Sound', 'Time','Smell', 'Height','Temperature'] +X = pima[feature_cols] +y = pima.ToRemove + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) +clf = DecisionTreeClassifier() +clf = clf.fit(X_train,y_train) + +joblib.dump(clf, 'decision_tree_model.pkl') + +y_pred = clf.predict(X_test) + +print("Accuracy:",metrics.accuracy_score(y_test, y_pred)) \ No newline at end of file diff --git a/domain/entities/cat.py b/domain/entities/cat.py index a0acd5b..14e44b3 100644 --- a/domain/entities/cat.py +++ b/domain/entities/cat.py @@ -13,3 +13,5 @@ class Cat(Entity): self.busy = False self.sleeping = False self.direction = 0 + + self.props = [1,2,0,16,1,10,25] \ No newline at end of file diff --git a/domain/entities/earring.py b/domain/entities/earring.py new file mode 100644 index 0000000..f9498ec --- /dev/null +++ b/domain/entities/earring.py @@ -0,0 +1,8 @@ +from domain.entities.entity import Entity +from domain.world import World + + +class Earring(Entity): + def __init__(self, x: int, y: int): + super().__init__(x, y, "EARRING") + self.props = [2,1,0,12,0,50,24] \ No newline at end of file diff --git a/domain/entities/garbage.py b/domain/entities/garbage.py index c93501d..cda5b91 100644 --- a/domain/entities/garbage.py +++ b/domain/entities/garbage.py @@ -1,11 +1,11 @@ from domain.entities.entity import Entity -from domain.world import World class Garbage(Entity): def __init__(self, x: int, y: int): - super().__init__(x, y, "GARBAGE") + super().__init__(x, y, "PEEL") self.wet = False self.size = 0 + self.props = [1,2,0,16,1,10,25] # TODO GARBAGE: add more properties diff --git a/domain/world.py b/domain/world.py index 7f51a9d..61b02c0 100644 --- a/domain/world.py +++ b/domain/world.py @@ -35,6 +35,59 @@ class World: def is_docking_station_at(self, x: int, y: int) -> bool: return bool(self.doc_station.x == x and self.doc_station.y == y) + def accepted_move(self, checking_x, checking_y): + if ( + checking_x > self.width - 1 + or checking_y > self.height - 1 + or checking_x < 0 + or checking_y < 0 + ): + return False + + if self.is_obstacle_at(checking_x, checking_y): + return False +from decisionTree.evaluate import evaluate +from domain.entities.entity import Entity + + +class World: + def __init__(self, width: int, height: int) -> object: + self.width = width + self.height = height + self.dust = [[[] for j in range(height)] for i in range(width)] + self.obstacles = [[[] for j in range(height)] for i in range(width)] + + self.vacuum = None + self.cat = None + self.doc_station = None + + def add_entity(self, entity: Entity): + if entity.type == "PEEL": + self.dust[entity.x][entity.y].append(entity) + elif entity.type == "EARRING": + self.dust[entity.x][entity.y].append(entity) + elif entity.type == "VACUUM": + self.vacuum = entity + elif entity.type == "DOC_STATION": + self.doc_station = entity + elif entity.type == "CAT": + self.cat = entity + self.obstacles[entity.x][entity.y].append(entity) + else: + self.obstacles[entity.x][entity.y].append(entity) + + def is_obstacle_at(self, x: int, y: int) -> bool: + return bool(self.obstacles[x][y]) + + def is_garbage_at(self, x: int, y: int) -> bool: + if len(self.dust[x][y]) == 0: + return False + tmp = evaluate([self.dust[x][y][0].props]) + return bool(tmp[0]) + + def is_docking_station_at(self, x: int, y: int) -> bool: + return bool(self.doc_station.x == x and self.doc_station.y == y) + def accepted_move(self, checking_x, checking_y): if ( checking_x > self.width - 1 @@ -48,5 +101,7 @@ class World: return False return True + + def get_cost(self, x, y): return self.costs[x][y] \ No newline at end of file diff --git a/main.py b/main.py index ced1e66..cce9cc0 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,8 @@ from domain.commands.vacuum_move_command import VacuumMoveCommand from domain.entities.cat import Cat from domain.entities.entity import Entity from domain.entities.vacuum import Vacuum +from domain.entities.garbage import Garbage +from domain.entities.earring import Earring from domain.entities.docking_station import Doc_Station from domain.world import World from view.renderer import Renderer @@ -134,11 +136,11 @@ class Main: def generate_world(tiles_x: int, tiles_y: int) -> World: world = World(tiles_x, tiles_y) - for _ in range(35): + for _ in range(10): temp_x = randint(0, tiles_x - 1) temp_y = randint(0, tiles_y - 1) - world.add_entity(Entity(temp_x, temp_y, "PEEL")) - world.vacuum = Vacuum(0, 0) + world.add_entity(Garbage(temp_x, temp_y)) + world.vacuum = Vacuum(1, 1) world.doc_station = Doc_Station(9, 8) if config.getboolean("APP", "cat"): world.cat = Cat(7, 8) @@ -148,7 +150,8 @@ def generate_world(tiles_x: int, tiles_y: int) -> World: world.add_entity(Entity(3, 4, "PLANT2")) world.add_entity(Entity(8, 8, "PLANT2")) world.add_entity(Entity(9, 3, "PLANT3")) - world.add_entity(Entity(4, 0, "PLANT2")) + world.add_entity(Earring(5, 5)) + for x in range(world.width): for y in range(world.height): diff --git a/media/sprites/earrings.webp b/media/sprites/earrings.webp new file mode 100644 index 0000000..9c84a7e Binary files /dev/null and b/media/sprites/earrings.webp differ diff --git a/requirements.txt b/requirements.txt index edcb635..98bd1d8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ pygame configparser -formaFormatting: Provider - black \ No newline at end of file +pandas +scikit-learn +joblib +# formaFormatting: Provider - black \ No newline at end of file diff --git a/view/renderer.py b/view/renderer.py index e8294cb..e21d270 100644 --- a/view/renderer.py +++ b/view/renderer.py @@ -94,6 +94,13 @@ class Renderer: self.tile_height + self.tile_height / 4, ), ), + "EARRING": pygame.transform.scale( + pygame.image.load("media/sprites/earrings.webp"), + ( + self.tile_width + self.tile_width / 4, + self.tile_height + self.tile_height / 4, + ), + ), } self.cat_direction_sprite = {