Genetics Algorithms
This commit is contained in:
parent
f96f8229a9
commit
e7358998ef
BIN
src/__pycache__/agent.cpython-310.pyc
Normal file
BIN
src/__pycache__/agent.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/decisiontree.cpython-310.pyc
Normal file
BIN
src/__pycache__/decisiontree.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/genetics.cpython-310.pyc
Normal file
BIN
src/__pycache__/genetics.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/simulation.cpython-310.pyc
Normal file
BIN
src/__pycache__/simulation.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/trashtype.cpython-310.pyc
Normal file
BIN
src/__pycache__/trashtype.cpython-310.pyc
Normal file
Binary file not shown.
@ -3,6 +3,7 @@ import pygame as pg
|
||||
import heapq
|
||||
from decisiontree import decision
|
||||
from trashtype import trash_type_def
|
||||
import genetics
|
||||
|
||||
HOUSE_NUMBER = {'House_number': 0}
|
||||
|
||||
@ -40,6 +41,7 @@ class HousePOI:
|
||||
self.trash = 1
|
||||
self.dump_fullness = 0
|
||||
|
||||
|
||||
def discover_bins(self, bin_fullness, trash_type, payment):
|
||||
from simulation import Interface
|
||||
|
||||
@ -146,6 +148,7 @@ class Agent:
|
||||
self.fullness = None
|
||||
self.weights = {}
|
||||
self.orientation = 90
|
||||
self.genetic_house_list = None
|
||||
|
||||
# utworzenie grafu dróg
|
||||
roads_pos = [vector_to_tuple(pos) for pos in
|
||||
@ -299,7 +302,8 @@ class Agent:
|
||||
def a_star(self):
|
||||
fringe = []
|
||||
explored = set()
|
||||
initial_state = (self.current_pos, tuple(self.houses))
|
||||
self.genetic_house_list = genetics.genetic_algorithm(sorted(self.houses))
|
||||
initial_state = (self.current_pos, tuple(self.genetic_house_list))
|
||||
goal_state = (self.current_pos, ())
|
||||
start_node = Node(initial_state, None, 0)
|
||||
|
||||
|
95
src/genetics.py
Normal file
95
src/genetics.py
Normal file
@ -0,0 +1,95 @@
|
||||
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
|
||||
return abs(x1 - x2) + abs(y1 - y2)
|
||||
|
||||
def calculate_total_distance(houses):
|
||||
total_distance = 0
|
||||
for i in range(len(houses) - 1):
|
||||
total_distance += calculate_distance(houses[i], houses[i+1])
|
||||
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):
|
||||
tournament = random.sample(population, 5)
|
||||
winner = min(tournament, key=calculate_total_distance)
|
||||
parents.append(winner)
|
||||
return parents
|
||||
|
||||
# Krzyżowanie rodziców
|
||||
def crossover(parent1, parent2):
|
||||
child1 = [None] * len(parent1)
|
||||
child2 = [None] * len(parent1)
|
||||
start_index = random.randint(0, len(parent1) - 1)
|
||||
end_index = random.randint(start_index, len(parent1) - 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]
|
||||
|
||||
# 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)):
|
||||
if child1[j] is None:
|
||||
child1[j] = parent2[i]
|
||||
break
|
||||
|
||||
for i in range(len(parent1)):
|
||||
if parent1[i] not in child2:
|
||||
for j in range(len(parent1)):
|
||||
if child2[j] is None:
|
||||
child2[j] = parent1[i]
|
||||
break
|
||||
|
||||
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])
|
||||
mutate(child1)
|
||||
mutate(child2)
|
||||
new_population.extend([child1, child2])
|
||||
|
||||
population = new_population
|
||||
|
||||
# Znalezienie najlepszego osobnika
|
||||
best_individual = min(population, key=calculate_total_distance)
|
||||
|
||||
return best_individual
|
||||
|
201
src/tree_data.csv
Normal file
201
src/tree_data.csv
Normal file
@ -0,0 +1,201 @@
|
||||
dump_fullness,truck_fullness,trash,payment,bin_fullness,trash_types,season,day,decision
|
||||
0,0,0,1,0,glass,winter,wednesday,0
|
||||
1,1,1,0,1,mixed,spring,monday,0
|
||||
0,1,1,1,0,plastic,winter,monday,0
|
||||
1,0,1,1,0,glass,spring,monday,0
|
||||
1,1,1,0,0,paper,spring,wednesday,0
|
||||
0,1,0,1,1,plastic,spring,monday,0
|
||||
1,1,1,1,1,mixed,autumn,friday,0
|
||||
0,0,1,0,1,glass,autumn,friday,0
|
||||
1,0,0,1,1,glass,spring,monday,0
|
||||
0,0,0,0,0,paper,spring,friday,0
|
||||
1,1,0,1,0,paper,summer,friday,0
|
||||
1,1,1,0,0,mixed,spring,wednesday,0
|
||||
1,0,1,1,0,mixed,autumn,monday,0
|
||||
0,0,1,0,1,mixed,summer,wednesday,0
|
||||
1,0,1,0,0,glass,spring,monday,0
|
||||
1,1,1,0,0,paper,autumn,friday,0
|
||||
0,0,1,0,1,paper,summer,friday,0
|
||||
1,0,0,1,1,glass,autumn,wednesday,0
|
||||
1,1,1,1,0,glass,winter,wednesday,0
|
||||
0,0,0,0,0,plastic,summer,monday,0
|
||||
0,0,1,1,1,mixed,autumn,friday,0
|
||||
1,0,1,0,0,mixed,summer,friday,0
|
||||
1,0,0,1,0,paper,winter,friday,0
|
||||
1,1,1,0,1,paper,winter,wednesday,0
|
||||
0,0,1,1,1,glass,summer,wednesday,0
|
||||
1,0,0,0,0,glass,summer,friday,0
|
||||
0,0,0,1,0,glass,summer,wednesday,0
|
||||
1,0,1,1,0,paper,spring,friday,0
|
||||
1,1,1,1,1,paper,autumn,monday,0
|
||||
1,0,0,1,0,glass,winter,wednesday,0
|
||||
1,0,1,0,1,paper,spring,wednesday,0
|
||||
0,1,0,1,0,mixed,winter,wednesday,0
|
||||
1,1,1,1,1,paper,winter,friday,0
|
||||
1,0,1,0,0,glass,autumn,wednesday,0
|
||||
0,1,0,0,1,plastic,autumn,monday,0
|
||||
0,0,1,1,1,glass,winter,friday,1
|
||||
0,1,0,1,1,paper,spring,monday,0
|
||||
1,0,0,0,1,plastic,spring,monday,0
|
||||
0,0,1,1,1,mixed,autumn,monday,1
|
||||
0,1,1,0,1,glass,spring,monday,0
|
||||
0,0,1,1,0,paper,autumn,wednesday,0
|
||||
0,0,0,1,1,plastic,spring,friday,0
|
||||
1,0,1,1,0,plastic,summer,monday,0
|
||||
1,1,1,1,1,plastic,winter,friday,0
|
||||
0,0,0,1,1,mixed,autumn,friday,0
|
||||
1,0,1,0,0,paper,spring,wednesday,0
|
||||
0,0,0,1,1,glass,spring,monday,0
|
||||
0,1,1,0,0,plastic,summer,wednesday,0
|
||||
1,0,1,0,0,glass,summer,friday,0
|
||||
0,1,0,0,0,glass,winter,wednesday,0
|
||||
0,0,0,1,0,paper,summer,monday,0
|
||||
1,1,0,0,1,plastic,summer,friday,0
|
||||
1,0,0,0,0,plastic,spring,monday,0
|
||||
1,0,1,1,0,plastic,autumn,friday,0
|
||||
1,1,1,1,0,mixed,autumn,wednesday,0
|
||||
0,0,0,0,1,paper,autumn,monday,0
|
||||
0,0,1,1,0,glass,winter,friday,0
|
||||
0,1,0,1,0,glass,summer,wednesday,0
|
||||
1,1,0,0,0,plastic,summer,monday,0
|
||||
0,1,0,1,0,paper,spring,monday,0
|
||||
0,0,1,1,1,mixed,winter,wednesday,1
|
||||
0,0,1,0,0,plastic,spring,monday,0
|
||||
1,1,0,0,1,mixed,winter,monday,0
|
||||
0,0,1,1,1,mixed,summer,wednesday,1
|
||||
0,1,0,0,1,paper,winter,friday,0
|
||||
0,0,1,1,1,plastic,winter,monday,0
|
||||
1,0,1,0,0,glass,winter,monday,0
|
||||
0,0,1,0,0,mixed,summer,monday,0
|
||||
1,1,1,0,1,mixed,spring,friday,0
|
||||
1,0,1,1,1,mixed,spring,monday,0
|
||||
1,1,0,1,1,glass,autumn,friday,0
|
||||
0,1,0,0,1,plastic,spring,monday,0
|
||||
0,1,1,0,1,plastic,spring,monday,0
|
||||
0,0,1,1,1,plastic,autumn,monday,1
|
||||
1,0,0,1,0,glass,spring,wednesday,0
|
||||
1,1,1,0,0,plastic,autumn,monday,0
|
||||
0,1,0,1,1,plastic,spring,friday,0
|
||||
0,1,0,1,1,glass,autumn,wednesday,0
|
||||
1,0,0,0,1,mixed,winter,friday,0
|
||||
1,0,1,1,0,plastic,spring,monday,0
|
||||
0,0,1,0,1,plastic,summer,friday,0
|
||||
1,1,0,0,0,glass,spring,wednesday,0
|
||||
0,1,1,1,1,mixed,winter,wednesday,0
|
||||
1,1,0,1,0,mixed,winter,friday,0
|
||||
0,0,1,1,1,paper,spring,friday,1
|
||||
0,1,1,0,1,mixed,spring,friday,0
|
||||
1,1,1,0,0,glass,winter,monday,0
|
||||
1,1,1,0,1,glass,spring,monday,0
|
||||
1,0,0,1,1,plastic,spring,monday,0
|
||||
0,0,1,1,1,paper,autumn,friday,1
|
||||
0,0,0,0,1,paper,autumn,wednesday,0
|
||||
1,1,1,0,1,plastic,summer,monday,0
|
||||
0,1,1,1,1,paper,spring,monday,0
|
||||
0,1,1,1,1,plastic,spring,monday,0
|
||||
0,1,1,0,1,glass,autumn,wednesday,0
|
||||
0,0,1,1,1,paper,summer,friday,1
|
||||
1,1,1,1,1,mixed,winter,wednesday,0
|
||||
0,1,1,0,0,mixed,spring,friday,0
|
||||
1,1,0,0,0,paper,autumn,friday,0
|
||||
1,1,0,1,1,paper,winter,friday,0
|
||||
1,1,1,1,0,mixed,winter,monday,0
|
||||
1,1,0,0,0,plastic,summer,wednesday,0
|
||||
0,0,0,0,0,mixed,winter,wednesday,0
|
||||
1,1,1,1,0,paper,spring,monday,0
|
||||
1,1,1,0,0,plastic,winter,wednesday,0
|
||||
0,0,0,1,1,paper,summer,friday,0
|
||||
0,1,0,0,0,glass,autumn,friday,0
|
||||
0,1,0,0,0,plastic,spring,monday,0
|
||||
1,1,0,1,0,paper,spring,monday,0
|
||||
0,1,0,0,1,plastic,spring,wednesday,0
|
||||
0,1,0,1,0,mixed,spring,monday,0
|
||||
1,0,1,1,1,paper,summer,friday,0
|
||||
0,1,1,1,0,mixed,winter,friday,0
|
||||
0,0,1,0,0,plastic,autumn,friday,0
|
||||
1,1,0,1,0,paper,spring,friday,0
|
||||
1,0,1,0,1,mixed,winter,wednesday,0
|
||||
0,0,0,0,0,glass,autumn,monday,0
|
||||
1,0,1,1,1,mixed,autumn,friday,0
|
||||
1,1,0,1,1,paper,spring,monday,0
|
||||
1,1,1,0,0,glass,winter,wednesday,0
|
||||
0,0,1,1,1,glass,autumn,wednesday,1
|
||||
0,0,0,1,1,glass,spring,friday,0
|
||||
1,0,1,0,1,paper,winter,monday,0
|
||||
0,0,1,1,1,paper,winter,monday,1
|
||||
0,1,1,0,0,glass,summer,wednesday,0
|
||||
0,0,1,1,0,glass,spring,wednesday,0
|
||||
0,0,1,1,0,paper,spring,wednesday,0
|
||||
1,0,0,0,0,paper,summer,wednesday,0
|
||||
1,1,0,0,1,glass,autumn,monday,0
|
||||
1,1,0,0,1,paper,spring,monday,0
|
||||
0,0,0,1,1,plastic,spring,monday,0
|
||||
0,1,1,0,1,plastic,autumn,monday,0
|
||||
0,0,0,1,0,plastic,spring,wednesday,0
|
||||
0,1,1,1,0,glass,autumn,monday,0
|
||||
1,0,0,0,1,glass,summer,monday,0
|
||||
1,0,0,0,0,paper,winter,wednesday,0
|
||||
0,0,1,1,1,glass,summer,monday,1
|
||||
1,1,0,1,0,glass,summer,monday,0
|
||||
0,1,1,1,1,glass,winter,monday,0
|
||||
0,0,0,1,1,glass,winter,wednesday,0
|
||||
0,0,1,0,1,mixed,winter,friday,0
|
||||
0,1,0,1,1,paper,winter,monday,0
|
||||
0,0,1,1,1,mixed,spring,friday,1
|
||||
0,0,1,1,0,glass,winter,monday,0
|
||||
1,0,0,0,1,paper,summer,monday,0
|
||||
1,0,0,0,1,plastic,summer,wednesday,0
|
||||
0,1,1,1,1,plastic,winter,friday,0
|
||||
0,0,1,0,0,paper,autumn,friday,0
|
||||
0,0,1,1,1,plastic,summer,wednesday,1
|
||||
1,0,1,0,1,paper,winter,wednesday,0
|
||||
0,0,0,0,0,plastic,spring,wednesday,0
|
||||
1,0,1,0,0,glass,winter,wednesday,0
|
||||
1,0,1,1,1,plastic,summer,wednesday,0
|
||||
0,1,1,0,1,paper,summer,wednesday,0
|
||||
0,1,0,1,0,paper,winter,wednesday,0
|
||||
0,0,1,1,1,plastic,spring,monday,1
|
||||
0,1,1,1,0,paper,winter,wednesday,0
|
||||
0,1,1,1,0,glass,autumn,friday,0
|
||||
1,1,1,0,0,plastic,spring,monday,0
|
||||
0,0,1,0,1,mixed,spring,wednesday,0
|
||||
0,1,1,0,0,glass,summer,friday,0
|
||||
1,1,0,1,0,plastic,winter,monday,0
|
||||
0,0,0,1,0,paper,winter,wednesday,0
|
||||
1,0,1,0,1,plastic,summer,friday,0
|
||||
1,0,0,0,1,mixed,autumn,friday,0
|
||||
0,0,0,1,1,plastic,winter,friday,0
|
||||
1,0,0,0,0,plastic,winter,monday,0
|
||||
1,0,0,1,0,plastic,winter,friday,0
|
||||
1,0,0,0,1,glass,spring,monday,0
|
||||
0,0,0,1,1,plastic,autumn,friday,0
|
||||
0,1,0,1,1,glass,spring,friday,0
|
||||
0,0,0,1,1,mixed,spring,wednesday,0
|
||||
1,1,1,1,0,paper,autumn,wednesday,0
|
||||
0,0,1,1,1,mixed,winter,friday,0
|
||||
0,1,0,0,1,paper,summer,monday,0
|
||||
0,0,1,1,1,paper,summer,wednesday,0
|
||||
1,0,1,0,1,paper,autumn,wednesday,0
|
||||
0,1,1,1,1,mixed,spring,friday,0
|
||||
0,0,0,1,1,glass,winter,friday,0
|
||||
0,1,0,0,0,glass,winter,monday,0
|
||||
0,0,1,0,1,glass,autumn,wednesday,0
|
||||
0,0,0,1,0,plastic,summer,friday,0
|
||||
1,1,1,1,0,mixed,spring,wednesday,0
|
||||
1,0,1,0,1,plastic,autumn,friday,0
|
||||
0,1,1,0,1,mixed,autumn,monday,0
|
||||
0,1,0,1,1,plastic,winter,wednesday,0
|
||||
0,1,1,0,1,mixed,winter,friday,0
|
||||
1,0,0,0,1,plastic,summer,friday,0
|
||||
1,0,1,0,1,mixed,spring,friday,0
|
||||
1,1,1,1,0,paper,summer,monday,0
|
||||
1,0,1,0,1,paper,winter,friday,0
|
||||
0,1,1,0,1,paper,summer,friday,0
|
||||
0,0,1,1,1,plastic,winter,wednesday,1
|
||||
0,0,1,1,1,mixed,spring,monday,0
|
||||
0,1,1,0,1,paper,winter,wednesday,0
|
||||
1,1,1,1,0,plastic,spring,friday,0
|
||||
1,0,1,1,0,paper,summer,friday,0
|
||||
1,1,0,0,1,glass,autumn,friday,0
|
||||
0,0,1,1,1,glass,spring,wednesday,1
|
||||
1,1,0,1,1,paper,summer,friday,0
|
|
52
src/tree_in_txt.txt
Normal file
52
src/tree_in_txt.txt
Normal file
@ -0,0 +1,52 @@
|
||||
|--- dump_fullness <= 0.50
|
||||
| |--- truck_fullness <= 0.50
|
||||
| | |--- trash <= 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
|
||||
| | | | | |--- trash_types_mixed <= 0.50
|
||||
| | | | | | |--- season_summer <= 0.50
|
||||
| | | | | | | |--- trash_types_plastic <= 0.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | | |--- trash_types_plastic > 0.50
|
||||
| | | | | | | | |--- season_winter <= 0.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- season_winter > 0.50
|
||||
| | | | | | | | | |--- day_wednesday <= 0.50
|
||||
| | | | | | | | | | |--- class: 0
|
||||
| | | | | | | | | |--- day_wednesday > 0.50
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | |--- season_summer > 0.50
|
||||
| | | | | | | |--- day_wednesday <= 0.50
|
||||
| | | | | | | | |--- class: 1
|
||||
| | | | | | | |--- day_wednesday > 0.50
|
||||
| | | | | | | | |--- trash_types_plastic <= 0.50
|
||||
| | | | | | | | | |--- class: 0
|
||||
| | | | | | | | |--- trash_types_plastic > 0.50
|
||||
| | | | | | | | | |--- class: 1
|
||||
| | | | | |--- trash_types_mixed > 0.50
|
||||
| | | | | | |--- day_wednesday <= 0.50
|
||||
| | | | | | | |--- season_winter <= 0.50
|
||||
| | | | | | | | |--- day_monday <= 0.50
|
||||
| | | | | | | | | |--- season_spring <= 0.50
|
||||
| | | | | | | | | | |--- class: 0
|
||||
| | | | | | | | | |--- season_spring > 0.50
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | | | |--- day_monday > 0.50
|
||||
| | | | | | | | | |--- season_spring <= 0.50
|
||||
| | | | | | | | | | |--- class: 1
|
||||
| | | | | | | | | |--- season_spring > 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
|
Loading…
Reference in New Issue
Block a user