From 466e06eefc0c34e16570b0c46a03076cadecb810 Mon Sep 17 00:00:00 2001 From: s473633 Date: Mon, 19 Jun 2023 11:56:13 +0200 Subject: [PATCH] Zmieniono A*, kosmetyka --- res/map.txt | 4 ++-- src/agent.py | 9 ++++---- src/decisiontree.py | 8 ------- src/genetics.py | 12 ----------- src/tree_in_txt.txt | 52 ++++++++++++++++++++++----------------------- 5 files changed, 33 insertions(+), 52 deletions(-) diff --git a/res/map.txt b/res/map.txt index 34cd844..bef2596 100644 --- a/res/map.txt +++ b/res/map.txt @@ -1,5 +1,5 @@ MP--H--------------------- -RRRRRRRRRRRRRRRRRRR---G--- +RORRRRRRRRRRRRRRRRR---G--- SZ--G------G------R---GGG- ----G------G------R---G--- -RRRG------GGGGGGGRRRRR--- @@ -10,5 +10,5 @@ SZ--G------G------R---GGG- -R--G-------H-----R------- -R--G-----G----G--R------- -R--G-----G----G--R------- --RRRRRRRRRRRRRRRRRRRORRR-- +-RRRRRRRRRRRRRRRRRRRRRRR-- -------------------------- \ No newline at end of file diff --git a/src/agent.py b/src/agent.py index 0de0e65..3b54ce1 100644 --- a/src/agent.py +++ b/src/agent.py @@ -272,10 +272,11 @@ class Agent: house_list = state[1] successors = [] for pos in successors_pos: - if pos in house_list: - house_list = list(house_list) - house_list.remove(pos) - house_list = tuple(house_list) + if len(house_list) > 0: + if house_list[0] == pos: + house_list = list(house_list) + house_list.pop(0) + house_list = tuple(house_list) successors.append((pos, tuple(house_list))) diff --git a/src/decisiontree.py b/src/decisiontree.py index 5f81c0f..d337d50 100644 --- a/src/decisiontree.py +++ b/src/decisiontree.py @@ -3,14 +3,6 @@ from sklearn import tree from sklearn.tree import DecisionTreeClassifier -def encode_category(category, categories): - encoded = pd.get_dummies(categories).astype(int) - if category in encoded.columns: - return encoded[category].values.tolist() - else: - return [0] * len(encoded.columns) - - data = pd.read_csv('tree_data.csv') data_encoded = pd.get_dummies(data, columns=['trash_types', 'season', 'day']) diff --git a/src/genetics.py b/src/genetics.py index 7881fa2..31c800c 100644 --- a/src/genetics.py +++ b/src/genetics.py @@ -1,20 +1,17 @@ import random -# Parametry algorytmu genetycznego POPULATION_SIZE = 100 MUTATION_RATE = 0.01 NUM_GENERATIONS = 100 -# Generowanie początkowej populacji def generate_individual(houses): return random.sample(houses, len(houses)) def generate_population(houses, size): return [generate_individual(houses) for _ in range(size)] -# Obliczanie odległości między domkami def calculate_distance(house1, house2): x1, y1 = house1 x2, y2 = house2 @@ -27,7 +24,6 @@ def calculate_total_distance(houses): total_distance += calculate_distance(houses[-1], houses[0]) # Zamknięcie cyklu return total_distance -# Selekcja rodziców za pomocą turniejowej metody def select_parents(population, num_parents): parents = [] for _ in range(num_parents): @@ -36,7 +32,6 @@ def select_parents(population, num_parents): parents.append(winner) return parents -# Krzyżowanie rodziców def crossover(parent1, parent2): child1 = [None] * len(parent1) child2 = [None] * len(parent1) @@ -45,7 +40,6 @@ def crossover(parent1, parent2): child1[start_index:end_index+1] = parent1[start_index:end_index+1] child2[start_index:end_index+1] = parent2[start_index:end_index+1] - # Uzupełnienie brakujących domków z drugiego rodzica for i in range(len(parent1)): if parent2[i] not in child1: for j in range(len(parent2)): @@ -62,23 +56,18 @@ def crossover(parent1, parent2): return child1, child2 -# Mutacja: zamiana dwóch losowych domków def mutate(individual): index1, index2 = random.sample(range(len(individual)), 2) individual[index1], individual[index2] = individual[index2], individual[index1] -# Algorytm genetyczny def genetic_algorithm(houses): population = generate_population(houses, POPULATION_SIZE) for _ in range(NUM_GENERATIONS): - # Obliczanie wartości przystosowania (odległości) dla każdego osobnika fitness_scores = [calculate_total_distance(individual) for individual in population] - # Wybór rodziców do reprodukcji parents = select_parents(population, 2) - # Tworzenie nowej populacji za pomocą krzyżowania i mutacji new_population = [] for i in range(POPULATION_SIZE // 2): child1, child2 = crossover(parents[0], parents[1]) @@ -88,7 +77,6 @@ def genetic_algorithm(houses): population = new_population - # Znalezienie najlepszego osobnika best_individual = min(population, key=calculate_total_distance) return best_individual diff --git a/src/tree_in_txt.txt b/src/tree_in_txt.txt index b8ac7a3..634b8dc 100644 --- a/src/tree_in_txt.txt +++ b/src/tree_in_txt.txt @@ -1,26 +1,26 @@ -|--- dump_fullness <= 0.50 -| |--- truck_fullness <= 0.50 -| | |--- trash <= 0.50 +|--- payment <= 0.50 +| |--- class: 0 +|--- payment > 0.50 +| |--- trash <= 0.50 +| | |--- class: 0 +| |--- trash > 0.50 +| | |--- bin_fullness <= 0.50 | | | |--- class: 0 -| | |--- trash > 0.50 -| | | |--- payment <= 0.50 -| | | | |--- class: 0 -| | | |--- payment > 0.50 -| | | | |--- bin_fullness <= 0.50 -| | | | | |--- class: 0 -| | | | |--- bin_fullness > 0.50 +| | |--- bin_fullness > 0.50 +| | | |--- truck_fullness <= 0.50 +| | | | |--- dump_fullness <= 0.50 | | | | | |--- trash_types_mixed <= 0.50 | | | | | | |--- season_summer <= 0.50 -| | | | | | | |--- trash_types_plastic <= 0.50 +| | | | | | | |--- day_monday <= 0.50 | | | | | | | | |--- class: 1 -| | | | | | | |--- trash_types_plastic > 0.50 +| | | | | | | |--- day_monday > 0.50 | | | | | | | | |--- season_winter <= 0.50 | | | | | | | | | |--- class: 1 | | | | | | | | |--- season_winter > 0.50 -| | | | | | | | | |--- day_wednesday <= 0.50 -| | | | | | | | | | |--- class: 0 -| | | | | | | | | |--- day_wednesday > 0.50 +| | | | | | | | | |--- trash_types_plastic <= 0.50 | | | | | | | | | | |--- class: 1 +| | | | | | | | | |--- trash_types_plastic > 0.50 +| | | | | | | | | | |--- class: 0 | | | | | | |--- season_summer > 0.50 | | | | | | | |--- day_wednesday <= 0.50 | | | | | | | | |--- class: 1 @@ -32,21 +32,21 @@ | | | | | |--- trash_types_mixed > 0.50 | | | | | | |--- day_wednesday <= 0.50 | | | | | | | |--- season_winter <= 0.50 -| | | | | | | | |--- day_monday <= 0.50 -| | | | | | | | | |--- season_spring <= 0.50 +| | | | | | | | |--- season_spring <= 0.50 +| | | | | | | | | |--- day_friday <= 0.50 +| | | | | | | | | | |--- class: 1 +| | | | | | | | | |--- day_friday > 0.50 | | | | | | | | | | |--- class: 0 -| | | | | | | | | |--- season_spring > 0.50 +| | | | | | | | |--- season_spring > 0.50 +| | | | | | | | | |--- day_monday <= 0.50 | | | | | | | | | | |--- class: 1 -| | | | | | | | |--- day_monday > 0.50 -| | | | | | | | | |--- season_spring <= 0.50 -| | | | | | | | | | |--- class: 1 -| | | | | | | | | |--- season_spring > 0.50 +| | | | | | | | | |--- day_monday > 0.50 | | | | | | | | | | |--- class: 0 | | | | | | | |--- season_winter > 0.50 | | | | | | | | |--- class: 0 | | | | | | |--- day_wednesday > 0.50 | | | | | | | |--- class: 1 -| |--- truck_fullness > 0.50 -| | |--- class: 0 -|--- dump_fullness > 0.50 -| |--- class: 0 +| | | | |--- dump_fullness > 0.50 +| | | | | |--- class: 0 +| | | |--- truck_fullness > 0.50 +| | | | |--- class: 0