def test_get_population(): ga = DiffusionGA('just_for_test') arr = numpy.empty(4) for i in range(4): arr[i] = i ga._fitness_arr = numpy.empty(2) ga._fitness_arr[0] = arr[0] ga._fitness_arr[1] = arr[1] ga._chrom_arr = numpy.empty(2) ga._chrom_arr[0] = arr[2] ga._chrom_arr[1] = arr[3] assert (ga.population[0] == arr[2:]).all() assert (ga.population[1] == arr[:2]).all()
def test_get_neighbour(fit_list, row, column): ga = RealGA(fitness_test_sin_func) dga = DiffusionGA(ga) dga._chrom_arr = numpy.array(list(range(9))).reshape((3, 3)) dga._fitness_arr = numpy.array(fit_list).reshape(3, 3) shape = dga._chrom_arr.shape selected_chromosome = dga._get_neighbour(row, column) indices = numpy.where(dga._chrom_arr == selected_chromosome) coords = (indices[0][0], indices[1][0]) valid_indices = [((row - 1) % shape[0], column), ((row + 1) % shape[0], column), (row, (column - 1) % shape[1]), (row, (column + 1) % shape[1])] assert coords in valid_indices