Zmieniono A*, kosmetyka

This commit is contained in:
s473633 2023-06-19 11:56:13 +02:00
parent e7358998ef
commit 466e06eefc
5 changed files with 33 additions and 52 deletions

View File

@ -1,5 +1,5 @@
MP--H--------------------- MP--H---------------------
RRRRRRRRRRRRRRRRRRR---G--- RORRRRRRRRRRRRRRRRR---G---
SZ--G------G------R---GGG- SZ--G------G------R---GGG-
----G------G------R---G--- ----G------G------R---G---
-RRRG------GGGGGGGRRRRR--- -RRRG------GGGGGGGRRRRR---
@ -10,5 +10,5 @@ SZ--G------G------R---GGG-
-R--G-------H-----R------- -R--G-------H-----R-------
-R--G-----G----G--R------- -R--G-----G----G--R-------
-R--G-----G----G--R------- -R--G-----G----G--R-------
-RRRRRRRRRRRRRRRRRRRORRR-- -RRRRRRRRRRRRRRRRRRRRRRR--
-------------------------- --------------------------

View File

@ -272,9 +272,10 @@ class Agent:
house_list = state[1] house_list = state[1]
successors = [] successors = []
for pos in successors_pos: for pos in successors_pos:
if pos in house_list: if len(house_list) > 0:
if house_list[0] == pos:
house_list = list(house_list) house_list = list(house_list)
house_list.remove(pos) house_list.pop(0)
house_list = tuple(house_list) house_list = tuple(house_list)
successors.append((pos, tuple(house_list))) successors.append((pos, tuple(house_list)))

View File

@ -3,14 +3,6 @@ from sklearn import tree
from sklearn.tree import DecisionTreeClassifier 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 = pd.read_csv('tree_data.csv')
data_encoded = pd.get_dummies(data, columns=['trash_types', 'season', 'day']) data_encoded = pd.get_dummies(data, columns=['trash_types', 'season', 'day'])

View File

@ -1,20 +1,17 @@
import random import random
# Parametry algorytmu genetycznego
POPULATION_SIZE = 100 POPULATION_SIZE = 100
MUTATION_RATE = 0.01 MUTATION_RATE = 0.01
NUM_GENERATIONS = 100 NUM_GENERATIONS = 100
# Generowanie początkowej populacji
def generate_individual(houses): def generate_individual(houses):
return random.sample(houses, len(houses)) return random.sample(houses, len(houses))
def generate_population(houses, size): def generate_population(houses, size):
return [generate_individual(houses) for _ in range(size)] return [generate_individual(houses) for _ in range(size)]
# Obliczanie odległości między domkami
def calculate_distance(house1, house2): def calculate_distance(house1, house2):
x1, y1 = house1 x1, y1 = house1
x2, y2 = house2 x2, y2 = house2
@ -27,7 +24,6 @@ def calculate_total_distance(houses):
total_distance += calculate_distance(houses[-1], houses[0]) # Zamknięcie cyklu total_distance += calculate_distance(houses[-1], houses[0]) # Zamknięcie cyklu
return total_distance return total_distance
# Selekcja rodziców za pomocą turniejowej metody
def select_parents(population, num_parents): def select_parents(population, num_parents):
parents = [] parents = []
for _ in range(num_parents): for _ in range(num_parents):
@ -36,7 +32,6 @@ def select_parents(population, num_parents):
parents.append(winner) parents.append(winner)
return parents return parents
# Krzyżowanie rodziców
def crossover(parent1, parent2): def crossover(parent1, parent2):
child1 = [None] * len(parent1) child1 = [None] * len(parent1)
child2 = [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] child1[start_index:end_index+1] = parent1[start_index:end_index+1]
child2[start_index:end_index+1] = parent2[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)): for i in range(len(parent1)):
if parent2[i] not in child1: if parent2[i] not in child1:
for j in range(len(parent2)): for j in range(len(parent2)):
@ -62,23 +56,18 @@ def crossover(parent1, parent2):
return child1, child2 return child1, child2
# Mutacja: zamiana dwóch losowych domków
def mutate(individual): def mutate(individual):
index1, index2 = random.sample(range(len(individual)), 2) index1, index2 = random.sample(range(len(individual)), 2)
individual[index1], individual[index2] = individual[index2], individual[index1] individual[index1], individual[index2] = individual[index2], individual[index1]
# Algorytm genetyczny
def genetic_algorithm(houses): def genetic_algorithm(houses):
population = generate_population(houses, POPULATION_SIZE) population = generate_population(houses, POPULATION_SIZE)
for _ in range(NUM_GENERATIONS): 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] fitness_scores = [calculate_total_distance(individual) for individual in population]
# Wybór rodziców do reprodukcji
parents = select_parents(population, 2) parents = select_parents(population, 2)
# Tworzenie nowej populacji za pomocą krzyżowania i mutacji
new_population = [] new_population = []
for i in range(POPULATION_SIZE // 2): for i in range(POPULATION_SIZE // 2):
child1, child2 = crossover(parents[0], parents[1]) child1, child2 = crossover(parents[0], parents[1])
@ -88,7 +77,6 @@ def genetic_algorithm(houses):
population = new_population population = new_population
# Znalezienie najlepszego osobnika
best_individual = min(population, key=calculate_total_distance) best_individual = min(population, key=calculate_total_distance)
return best_individual return best_individual

View File

@ -1,26 +1,26 @@
|--- dump_fullness <= 0.50 |--- payment <= 0.50
| |--- truck_fullness <= 0.50 | |--- class: 0
| | |--- trash <= 0.50 |--- payment > 0.50
| |--- trash <= 0.50
| | |--- class: 0
| |--- trash > 0.50
| | |--- bin_fullness <= 0.50
| | | |--- class: 0 | | | |--- class: 0
| | |--- trash > 0.50 | | |--- bin_fullness > 0.50
| | | |--- payment <= 0.50 | | | |--- truck_fullness <= 0.50
| | | | |--- class: 0 | | | | |--- dump_fullness <= 0.50
| | | |--- payment > 0.50
| | | | |--- bin_fullness <= 0.50
| | | | | |--- class: 0
| | | | |--- bin_fullness > 0.50
| | | | | |--- trash_types_mixed <= 0.50 | | | | | |--- trash_types_mixed <= 0.50
| | | | | | |--- season_summer <= 0.50 | | | | | | |--- season_summer <= 0.50
| | | | | | | |--- trash_types_plastic <= 0.50 | | | | | | | |--- day_monday <= 0.50
| | | | | | | | |--- class: 1 | | | | | | | | |--- class: 1
| | | | | | | |--- trash_types_plastic > 0.50 | | | | | | | |--- day_monday > 0.50
| | | | | | | | |--- season_winter <= 0.50 | | | | | | | | |--- season_winter <= 0.50
| | | | | | | | | |--- class: 1 | | | | | | | | | |--- class: 1
| | | | | | | | |--- season_winter > 0.50 | | | | | | | | |--- season_winter > 0.50
| | | | | | | | | |--- day_wednesday <= 0.50 | | | | | | | | | |--- trash_types_plastic <= 0.50
| | | | | | | | | | |--- class: 0
| | | | | | | | | |--- day_wednesday > 0.50
| | | | | | | | | | |--- class: 1 | | | | | | | | | | |--- class: 1
| | | | | | | | | |--- trash_types_plastic > 0.50
| | | | | | | | | | |--- class: 0
| | | | | | |--- season_summer > 0.50 | | | | | | |--- season_summer > 0.50
| | | | | | | |--- day_wednesday <= 0.50 | | | | | | | |--- day_wednesday <= 0.50
| | | | | | | | |--- class: 1 | | | | | | | | |--- class: 1
@ -32,21 +32,21 @@
| | | | | |--- trash_types_mixed > 0.50 | | | | | |--- trash_types_mixed > 0.50
| | | | | | |--- day_wednesday <= 0.50 | | | | | | |--- day_wednesday <= 0.50
| | | | | | | |--- season_winter <= 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 | | | | | | | | | | |--- class: 0
| | | | | | | | | |--- season_spring > 0.50 | | | | | | | | |--- season_spring > 0.50
| | | | | | | | | |--- day_monday <= 0.50
| | | | | | | | | | |--- class: 1 | | | | | | | | | | |--- class: 1
| | | | | | | | |--- day_monday > 0.50 | | | | | | | | | |--- day_monday > 0.50
| | | | | | | | | |--- season_spring <= 0.50
| | | | | | | | | | |--- class: 1
| | | | | | | | | |--- season_spring > 0.50
| | | | | | | | | | |--- class: 0 | | | | | | | | | | |--- class: 0
| | | | | | | |--- season_winter > 0.50 | | | | | | | |--- season_winter > 0.50
| | | | | | | | |--- class: 0 | | | | | | | | |--- class: 0
| | | | | | |--- day_wednesday > 0.50 | | | | | | |--- day_wednesday > 0.50
| | | | | | | |--- class: 1 | | | | | | | |--- class: 1
| |--- truck_fullness > 0.50 | | | | |--- dump_fullness > 0.50
| | |--- class: 0 | | | | | |--- class: 0
|--- dump_fullness > 0.50 | | | |--- truck_fullness > 0.50
| |--- class: 0 | | | | |--- class: 0