From 068ee153a0510b8c5cd11bb0a7ca56e79c0bc359 Mon Sep 17 00:00:00 2001 From: s481904 Date: Fri, 7 Jun 2024 13:03:50 +0200 Subject: [PATCH] gen alg 2 --- __pycache__/board.cpython-311.pyc | Bin 0 -> 7171 bytes __pycache__/constant.cpython-311.pyc | Bin 0 -> 400 bytes __pycache__/kolejka.cpython-311.pyc | Bin 0 -> 6782 bytes __pycache__/tractor.cpython-311.pyc | Bin 0 -> 1554 bytes gen_algorythm.py | 126 --------------------------- 5 files changed, 126 deletions(-) create mode 100644 __pycache__/board.cpython-311.pyc create mode 100644 __pycache__/constant.cpython-311.pyc create mode 100644 __pycache__/kolejka.cpython-311.pyc create mode 100644 __pycache__/tractor.cpython-311.pyc delete mode 100644 gen_algorythm.py diff --git a/__pycache__/board.cpython-311.pyc b/__pycache__/board.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ee87ce86abc078b35553b5877cebe0cea4fdddec GIT binary patch literal 7171 zcmbVRZ)h7w7N6bKN-M>-oM`3$Y2(;VqNbN7apJ^@DMyp*pQPs|b=qEJ=Tt>2yK-d9 zomCQ|%00uCVv3+@@SV-kg1HYln{d#B<31P;P9^u@=%;&c zR=ZkBmPs|hO-Q<@JYf$M957k0-}f6nozf zr_P-jdq<5^b?iozMyJN!nvS2RiCgipOE;4;}H-Iec7v5m4Q|KdirJu4k-3wZjxvg7lS3>@X^Qa(c@6i>or&mF z+z^>v7%nseLu9=gF6;}PQ0#^wGQMCKsYKFnn=Ggwgf^{&?uIOF0Lbh%7rVpQz=;y| z*8p>5GuXK>wjMmN7CfLGJY5J5>cK%YfK9n$;mviqZ%yvg4h$6JQ@VT#>b;wR_JyJK zK<`?hSL-`j2n^_f0aoAAwUAtIIk?txP&+hOXgQ;|oT=e+svw`%<I8UWEMOb8b%God#j1704SY1%dlLedUDRXLI8tGpE$kWI@bmLIO@2v;HY{`dmx z2p#9OeY4=fpS8m;_*^%vA8z|z&Xjv;o8_ECDtm?r*tmXMk~kk!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^fV#;?UipWnp=w5D4jkP?2!E2R7vW>vCvK4z2WmEv&p?2TuPhirCam znxBIUa5kyKq1||~J`H!4k%-}oM5a>8P1tKAMnfd>;mv5${6hDDbnHHoHe&9_2pE*m z*AXrvyoG?yjH2S`y9hr+7)8K;V62)8$-Lae(?3Ok%L$_X8(_|Uio8qgC=wf*+~QD? z*wD<0A8;_zGP*lO2={ClY7!yPv0)!Cz6c7lK;p$~(A{?&Q1gexLq(4T27W7=GLjpP_%+VHeIHtj_>m5JwzmC`8<0@Dt+D z;qI2)kO(Zu(yAUOA?04^a~ifdzCk^|u{M`Ir%|vVT^NAvRY@$njfU2|TftrgA+b#V z3Ew;Z-Dn$HE|O>>j5kktJu-A8(qvQ{B8z!tNL*x4t8^(0U5G3z=Eh5;5V>)s;BROR zc4*U&`|Pd~vSQ;%MiX|Giwl{~1iPMQWm>aR77t=vvs)RB>q4@)$g4Wa>(g?vX$>X? zpJw7*9)lHzR}G+ixfo-O0<}$jaIy-Nr)E0nt~!b+uKm3yzIs$dw~rLn{-N?T-B+$n H*#(0if=6lT literal 0 HcmV?d00001 diff --git a/__pycache__/kolejka.cpython-311.pyc b/__pycache__/kolejka.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..aea34857dc0f166f4d0d4b6d2a330e9b8afa226e GIT binary patch literal 6782 zcmcIoYitwQ6`rT~CRFe(iUAR+AbI)gTjL1Fx5fT~f=}5#{N&H%@=lt-%P+aL5BFVm3ztYo1BK=B|^z&Y`2FpK+|W#_7(Fx7l0$Qr$m6HIhbT{uW2JKp?AyZ6=0&TV^X<4qWU`EKPx=( z7`X-I#VsSJdOAoYG>zH=2?fkk`$i%2fKhB)7;uiPWsji#vCWg%erhAx>1;D?+YO@Z zG|J|F%QCsngzkgS9M?`z05}Ri5KE9E$m_1i&`>NP5ic@$iV3(8j4^PiTgj+9xQ~g^ zP=t_JLNPHiOZc&T2*?=os;FdKnYebRWuj&3$4f;4wJ5OcEuA>^#ksrZranx2Ycy|- z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WIiCMy_FsSTvh2SBrPe-u literal 0 HcmV?d00001 diff --git a/gen_algorythm.py b/gen_algorythm.py deleted file mode 100644 index 97123d022..000000000 --- a/gen_algorythm.py +++ /dev/null @@ -1,126 +0,0 @@ -import numpy as np -import random -import pygame -from constant import width, height, size, rows, cols -from board import Board -from tractor import Tractor - - -routes_num = 20 # Ilość ścieżek, które będziemy generować -board = Board() -dirt_positions = board.get_dirt_positions() -dirt_count = len(dirt_positions) - -def manhattan(a, b): - return abs(a[0] - b[0]) + abs(a[1] - b[1]) - -def find_routes(routes_num): - population_set = [] # zapisujemy trasy - losowe ułóżenia - for i in range(routes_num): - # losowo wygenerowane kolejności na trasie - single_route = np.random.choice(list(range(dirt_count)), dirt_count, replace=False) - population_set.append(single_route) - return np.array(population_set) - - -def sum_up_for_route(route_indices): - sum = 0 - for i in range(len(route_indices) - 1): - current_dirt = dirt_positions[route_indices[i]] - next_dirt = dirt_positions[route_indices[i + 1]] - sum += manhattan(current_dirt, next_dirt) - return sum - - -def routes_sum(population_set): # zapisujemy na liście finalne sumy odległości dla każdej z opcji tras - list_of_sums = np.zeros(routes_num) - - for i in range(routes_num): - list_of_sums[i] = sum_up_for_route(population_set[i]) # wywołujemy dla każdej trasy na liście - - return list_of_sums - - -def calculate_fitness(distances): - # Odwrotność odległości jako fitness - # Dodajemy małą wartość (np. 1) aby uniknąć dzielenia przez zero - return 1 / (distances + 1) - - -def selection(population_set, list_of_sums): - # Oblicz wartości fitness dla każdej trasy - fitness_values = calculate_fitness(list_of_sums) - # Normalizuj wartości fitness, aby sumowały się do 1 (wymagane dla np.random.choice) - probabilities = fitness_values / fitness_values.sum() - # Wybierz rodziców na podstawie prawdopodobieństw (wartości fitness) - progenitor_indices_a = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True) - progenitor_indices_b = np.random.choice(range(len(population_set)), len(population_set), p=probabilities, replace=True) - # Wybierz rzeczywiste trasy - progenitor_a = population_set[progenitor_indices_a] - progenitor_b = population_set[progenitor_indices_b] - - return np.array([progenitor_a, progenitor_b]) - -def one_point_crossover(parent_a, parent_b): #krzyzowanie jednopunktowe - crossover_point = np.random.randint(1, len(parent_a)) - child = np.concatenate((parent_a[:crossover_point], [x for x in parent_b if x not in parent_a[:crossover_point]])) - return child - -def population_mating(progenitor_list): - new_population_set = [] - for i in range(len(progenitor_list[0])): - progenitor_a, progenitor_b = progenitor_list[0][i], progenitor_list[1][i] - child = one_point_crossover(progenitor_a, progenitor_b) - new_population_set.append(child) - return new_population_set - -def mutation_of_child(child): - for i in range(dirt_count): # dla każdego elementu dajemy losową szansę zamiany int *rate - x = np.random.randint(0, dirt_count) - y = np.random.randint(0, dirt_count) - - child[x], child[y] = child[y], child[x] # zamiana miejscami - - return child - -'''def mutation_of_child(child, mutation_rate=0.1):#procent moze pomoc w niezaklucaniu trasy gdy jesy duza trasa ale idk - num_mutations = int(len(child) * mutation_rate) - for _ in range(num_mutations): - x = np.random.randint(0, len(child)) - y = np.random.randint(0, len(child)) - child[x], child[y] = child[y], child[x] - return child''' - - -def mutate_population(new_population_set): - final_mutated_population = [] - for child in new_population_set: - final_mutated_population.append(mutation_of_child(child)) # dodajemy zmutowane dziecko do finalnej listy - return final_mutated_population - - -if __name__ == '__main__': - - population_set = find_routes(routes_num) - list_of_sums = routes_sum(population_set) - progenitor_list = selection(population_set, list_of_sums) - new_population_set = population_mating(progenitor_list) - final_mutated_population = mutate_population(new_population_set) - final_route = [-1, np.inf, np.array([])] # format listy - for i in range(20): - list_of_sums = routes_sum(final_mutated_population) - # zapisujemy najlepsze rozwiązanie - if list_of_sums.min() < final_route[1]: - final_route[0] = i - final_route[1] = list_of_sums.min() - final_route[2] = np.array(final_mutated_population)[list_of_sums.min() == list_of_sums] - - - progenitor_list = selection(population_set, list_of_sums) - new_population_set = population_mating(progenitor_list) - final_mutated_population = mutate_population(new_population_set) - - print(f"Najlepsza trasa znaleziona w iteracji: {final_route[0]}") - print(f"Minimalna suma odległości: {final_route[1]}") - print(f"Kolejne pola: {final_route[2]}") -