def shift_car(individual: Individual) -> None: ''' Not tested! :param individual: :return: ''' individual.street_order = shift_one_marker(individual.street_order, marker=-1)
def delete_random_comeback(individual: Individual) -> None: zer_counter = np.count_nonzero(individual.street_order == 0) if zer_counter > 1: zero_indexes = np.where(individual.street_order == 0)[0] index = np.random.choice(zero_indexes, 1) if individual.street_order[index] == 0: individual.street_order = np.delete(individual.street_order, index)
def cross_indvidual_random(ind1: Individual, ind2: Individual) -> Individual: ''' :param ind1: :param ind2: :return: new individual cross of ind1 and ind2 ''' random.seed(datetime.now()) pos = random.randint(0, ind1.street_order.shape[0] - 1) il_el = random.randint(1, min(ind1.street_order.shape[0] - pos, 10)) part1 = ind1.street_order[pos:pos + il_el].copy() new = ind2.street_order.copy() for i in range(0, len(part1)): j = 0 while j < len(new): if part1[i] == new[j]: new = np.delete(new, j) break j += 1 new = np.array(new) part1 = np.array(part1) child = Individual() pos2 = random.randint(0, len(new)) tmp = np.concatenate((new[0:pos2], part1, new[pos2:len(new)]), axis=None) child.street_order = tmp child.genesis = combine_stats(stat1=ind1.genesis, stat2=ind2.genesis) child.genesis.cross_indvidual_random_counter += 1 child.max_number_of_cars = ind1.max_number_of_cars child.number_of_cars = np.count_nonzero(child.street_order == -1) return child
def swap_element_order(individual: Individual, n_elements: int = 2) -> None: ''' :param individual: :param n_elements: :return: ''' individual.street_order = swap_random_elements(individual.street_order, n_elements)
def delete_random_car(individual: Individual) -> None: print('dupa') car_counter = np.count_nonzero(individual.street_order == -1) if car_counter > 1: car_indexes = np.where(individual.street_order == 0)[0] index = np.random.choice(car_indexes, 1) if individual.street_order[index] == -1: individual.street_order = np.delete(individual.street_order, index)
def change_random_element(individual: Individual): ''' 1: Wygeneruj losowa wartosc z zakresu 2: Wez te liczbe i usun ja z kolejki 3: wstaw liczbe w dowolne miejsce :param individual: :return: Zmutowany osobnik o zmienionym jednym elemencie wariancji ''' individual.street_order = change_one_element_permutation(np.copy(individual.street_order))
def add_random_car(individual: Individual) -> None: ''' :param individual: :param n_elements: insert -1 to vector n_eleements times :return: ''' if individual.max_number_of_cars > individual.number_of_cars + 1: individual.street_order = add_marker_in_random(individual.street_order, -1, n_elements=1) individual.number_of_cars = np.count_nonzero(individual.street_order == -1)
def cross_indvidual_longest_common(ind1: Individual, ind2: Individual) -> Individual: ''' :param ind1: :param ind2: :return: new individual cross of ind1 and ind2 ''' child = Individual() child.street_order = create_cross_table(np.copy(ind1.street_order), np.copy(ind2.street_order)) child.genesis = combine_stats(stat1=ind1.genesis, stat2=ind2.genesis) child.genesis.cross_indvidual_longest_common_counter += 1 child.max_number_of_cars = ind1.max_number_of_cars child.number_of_cars = np.count_nonzero(child.street_order == -1) return child
def wrapped(): return func(*args, **kwargs) return wrapped x = [] random = [] common = [] reapets_num = 250 for i in range(5, 250): print(i) first = Individual() second = Individual() first.street_order = np.random.randint(low=-1, high=297, size=i) first.street_order = np.insert(first.street_order, i - 3, -1) first.street_order = np.insert(first.street_order, i - 2, -1) first.street_order = np.insert(first.street_order, i - 1, -1) second.street_order = np.random.randint(low=-1, high=297, size=i) second.street_order = np.insert(second.street_order, i - 3, -1) second.street_order = np.insert(second.street_order, i - 2, -1) second.street_order = np.insert(second.street_order, i - 1, -1) wrapped = wrapper(cross_indvidual_random, first, second) wrappedd = wrapper(cross_indvidual_longest_common, first, second) x.append(i) random.append(timeit.timeit(wrapped, number=reapets_num)) common.append(timeit.timeit(wrappedd, number=reapets_num)) plt.plot(x, random, label='krzyżowanie losowe')
from data_structers.inputdata import InputData from data_structers.evolutional_algorithm import evolution_simulator import random from datetime import datetime import copy import numpy as np from import_data.distance_matrix_calculator import matrix_array_calculate import os random.seed(datetime.now()) data_set = 'trivial' gmina = "../data/" + data_set csv_output = "../Reports/" + 'tsp_' + data_set + ".csv" order_outpur = "../Reports/" + 'tsp_' + data_set + ".txt" best_solution = 1000000 matrix_array_calculate(gmina + "_streets.csv", gmina + "_distance_matrix") inp_data = InputData(gmina) x = Individual(inp_data) for i in range(100000): x.street_order = np.arange(1, 7) np.random.shuffle(x.street_order) x.calculate_cost_value(inp_data) if x.cost_function_value < best_solution: best_solution = x.cost_function_value print(best_solution)
return wrapped xx = [] y = [] yy = [] yyy = [] yyyy = [] yyyyy = [] yyyyyy = [] reapets_num = 3000 for i in range(5, 1000): x = Individual() x.street_order = np.random.randint(low=-1, high=297, size=i) x.street_order = np.insert(x.street_order, i - 3, -1) x.street_order = np.insert(x.street_order, i - 2, -1) x.street_order = np.insert(x.street_order, i - 1, -1) wrapped = wrapper(change_random_element, x) wrappedd = wrapper(swap_element_order, x) wrappeddd = wrapper(add_random_car, x) wrappedddd = wrapper(shift_car, x) wrappeddddd = wrapper(add_random_comeback, x) wrappedddddd = wrapper(delete_random_comeback, x) random_element_stat = [] xx.append(i) y.append(timeit.timeit(wrapped, number=reapets_num)) yy.append(timeit.timeit(wrappedd, number=reapets_num)) yyy.append(timeit.timeit(wrappeddd, number=reapets_num))
def add_random_comeback(individual: Individual) -> None: individual.street_order = add_marker_in_random(individual.street_order, 0, 1)
from data_structers.individual import Population, Individual from collections import namedtuple import sys import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.axes3d import Axes3D from pympler import asizeof pops = [] nods = [] size = [] for pop in range(200, 350): for nod in range(1, 200): for xx in range(1, pop): xx = Individual() xx.street_order = np.zeros(nod, np.int32) size.append(round(pop * asizeof.asizeof(xx) / 1000)) pops.append(pop) nods.append(nod) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot(pops, nods, size) ax.set_xlabel('rozmiar populacji') ax.set_ylabel('liczba węzłów') ax.set_zlabel('pamięć [KB]') plt.title('F:(rozmiar populacji, liczba ulic) -> zużywana pamięć') plt.savefig("../Reports/memory_complex.svg", format="svg", dpi=1200) plt.show()