def find_neighbors(grid, open_list=[], close_list=[], generated_map=[]): grid_list = [] if is_valid_grid(grid.x, grid.y - 1, open_list, close_list, generated_map): grid_list.append(Grid(grid.x, grid.y - 1)) if is_valid_grid(grid.x, grid.y + 1, open_list, close_list, generated_map): grid_list.append(Grid(grid.x, grid.y + 1)) if is_valid_grid(grid.x - 1, grid.y, open_list, close_list, generated_map): grid_list.append(Grid(grid.x - 1, grid.y)) if is_valid_grid(grid.x + 1, grid.y, open_list, close_list, generated_map): grid_list.append(Grid(grid.x + 1, grid.y)) return grid_list
def test_setitem_slice_y(grid): new_grid = Grid(1, 3, 99) grid[0, :] = new_grid assert grid[0, :] == [99] * 3 grid[0, :] = 20 assert grid[0, :] == [20, 20, 20] grid[1, 1:3] = 30 assert grid[1, 1:3] == [30, 30] new_grid = Grid(1, 3, 99) grid[0, :] = new_grid assert grid[0, :] == [99] * 3
def test_instanciate_with_values(): w, h = 4, 3 values = list(range(w * h)) mygrid = Grid(w, h, values) assert mygrid[0] == [values, values, values, values] assert mygrid[1, :] == [values] * 3
def test_instanciate_with_value_copy(): w, h = 4, 3 class Foo(): pass mygrid = Grid(w, h, value=Foo(), copy=True) assert mygrid[0, 0] is not mygrid[0, 1]
def test_setitem_slice_x(grid): value = [10, 11, 12, 13] grid[:, 0] = value assert grid[:, 0] == [value] * 4 grid[:, 0] = 20 assert grid[:, 0] == [20, 20, 20, 20] grid[1:5:2, 2] = 30 assert grid[:, 2] == [8, 30, 10, 30] new_grid = Grid(4, 1, 99) grid[:, 0] = new_grid assert grid[:, 0] == [99] * 4 with pytest.raises(ValueError): new_grid = Grid(4, 2, 99) grid[:, 0] = new_grid
def test_eq(grid): w, h = grid.width, grid.height ngrid = Grid(w, h) assert ngrid != grid for j, line in enumerate(ngrid): for i, elt in enumerate(line): ngrid[j][i] = w * j + i assert ngrid == grid assert not ngrid is grid
def grid(): # 0 1 2 3 # 4 5 6 7 # 8 9 10 11 w, h = 4, 3 grid = Grid(w, h) for j, line in enumerate(grid): for i, elt in enumerate(line): grid[j][i] = w * j + i return grid
def single_turn(unittype, generated_map, CarSetting): walkables = all_walkable(generated_map) print("-" * 10, "run new simulation turn", "-" * 10, "\n") # 设置起点和终点 start_grid = Grid(2, 1) end_grid = Grid(2, 5) if CarSetting.behavior_type == 0: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) if CarSetting.behavior_type == 1: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) if random.randint(0, 4) > 2: end_grid = Grid(parkings[unittype][0], parkings[unittype][1]) else: end_grid = Grid(e[0], e[1]) if CarSetting.behavior_type == 2: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] if random.randint(0, 4) > 2: start_grid = Grid(inners[unittype][0], inners[unittype][1]) else: start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) if CarSetting.behavior_type == 3: s = walkables[random.randint(0, len(walkables) // 3)] e = walkables[random.randint(len(walkables) // 3, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) # 搜索街区终点 result_grid = mycar_walking(start_grid, end_grid, generated_map) # 回溯街区路径 path = [] while result_grid is not None: path.append(Grid(result_grid.x, result_grid.y)) result_grid = result_grid.parent abel_score = 0 # 输出街区和路径,路径用星号表示 for i in range(0, len(generated_map)): for j in range(0, len(generated_map[0])): if contain_grid(path, i, j): # star = colored('*', 'magenta', attrs=['reverse', 'blink']) # print(star +", ", end="") cprint("*" + ", ", "green", attrs=["reverse", "blink"], end="") abel_score += 1 else: if generated_map[i][j] == 1: cprint("1" + ", ", "grey", attrs=["reverse", "blink"], end="") else: print(str(generated_map[i][j]) + ", ", end="") print() print("abel_score=", abel_score) return abel_score
def single_turn(unittype, generated_map, CarSetting): walkables = all_walkable(generated_map) # print('walkables=', walkables, '#-# '*10) # 设置随机数种子seed np.random.seed(456789) # 生成5个均值为len(walkables)//20,标准差为5的正态分布样本,模拟电量 r = np.random.normal(loc=len(walkables) // 20, scale=5, size=5) simulate_battery_init = int(r[random.randint(0, 4)]) print("正态分布样 simulate_battery_init:", simulate_battery_init) s, e = None, None # 模拟早上和晚上 if random.randint(0, 1) == 0: # 模拟早上 s = walkables[random.randint(0, len(walkables) - 1)] while s[2] != 2: s = walkables[random.randint(0, len(walkables) - 1)] e = walkables[random.randint(0, len(walkables) - 1)] else: # 模拟晚上,正好相反 e = walkables[random.randint(0, len(walkables) - 1)] while e[2] != 2: e = walkables[random.randint(0, len(walkables) - 1)] s = walkables[random.randint(0, len(walkables) - 1)] # 更多场景 可以修改上面的代码 start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) print("-" * 10, "run new simulation turn", "-" * 10, "\n") # 设置起点和终点 # start_grid = Grid(2, 1) # end_grid = Grid(2, 5) # if CarSetting.behavior_type == 0 : # s = walkables[random.randint(0, len(walkables)//2)] # e = walkables[random.randint(len(walkables)//2, len(walkables)-1)] # start_grid = Grid(s[0], s[1]) # end_grid = Grid(e[0], e[1]) # if CarSetting.behavior_type == 1: # s = walkables[random.randint(0, len(walkables)//2)] # e = walkables[random.randint(len(walkables)//2, len(walkables)-1)] # start_grid = Grid(s[0], s[1]) # if random.randint(0, 4) > 2: # end_grid = Grid(parkings[unittype][0], parkings[unittype][1]) # else: # end_grid = Grid(e[0], e[1]) # if CarSetting.behavior_type == 2: # s = walkables[random.randint(0, len(walkables)//2)] # e = walkables[random.randint(len(walkables)//2, len(walkables)-1)] # if random.randint(0, 4) > 2: # start_grid = Grid(inners[unittype][0], inners[unittype][1]) # else: # start_grid = Grid(s[0], s[1]) # end_grid = Grid(e[0], e[1]) # if CarSetting.behavior_type == 3 : # s = walkables[random.randint(0, len(walkables)//3)] # e = walkables[random.randint(len(walkables)//3, len(walkables)-1)] # start_grid = Grid(s[0], s[1]) # end_grid = Grid(e[0], e[1]) # 搜索街区终点 result_grid = mycar_walking(start_grid, end_grid, generated_map) # 回溯街区路径 path = [] while result_grid is not None: path.append(Grid(result_grid.x, result_grid.y)) result_grid = result_grid.parent ALL_PATHS.append(path) abel_score = 0 # 输出街区和路径,路径用星号表示 for i in range(0, len(generated_map)): for j in range(0, len(generated_map[0])): if contain_grid(path, i, j): # star = colored('*', 'magenta', attrs=['reverse', 'blink']) # print(star +", ", end="") cprint("*" + ", ", "green", attrs=["reverse", "blink"], end="") abel_score += 1 else: if generated_map[i][j] == 1: cprint("1" + ", ", "grey", attrs=["reverse", "blink"], end="") else: print(str(generated_map[i][j]) + ", ", end="") print() print("abel_score=", abel_score) return abel_score
def test_instanciate_with_gen(): w, h = 4, 3 values = range(w * h) mygrid = Grid.from_iter(w, h, values) assert mygrid[0] == [0, 1, 2, 3] assert mygrid[1, :] == [1, 5, 9]
def test_instanciate_with_iter_wrong_len(): w, h = 4, 3 values = list(range(w * h - 2)) with pytest.raises(ValueError): mygrid = Grid.from_iter(w, h, values)
def test_instanciate_with_iter(): w, h = 4, 3 iterable = list(range(w * h)) mygrid = Grid.from_iter(w, h, iterable) assert mygrid[0] == [0, 1, 2, 3] assert mygrid[1, :] == [1, 5, 9]
def test_len(): grid = Grid(10, 10) assert len(grid) == 100
def test_wrong_size(): with pytest.raises(ValueError): grid = Grid(-1, 10)
def test_setitem_slice_xy(grid): new_grid = Grid(2, 2, 99) grid[1:3, 0:2] = new_grid assert grid[:, 0] == [0, 99, 99, 3] assert grid[1, :] == [99, 99, 9]
def transpose(m): new = [] for i in range(len(m[0])): # new.append([]) row = [] for j in range(len(m)): # row[i].append(m[j][i]) row.append(m[j][i]) new.append(row) return new # newmatrix = transpose(multiplication(transfor_matrix, transpose(fmatrix))) newmatrix = transpose(multiplication(transfor_matrix, transpose(amatrix))) g = Grid() for i in range(len(amatrix)): x1, y1 = amatrix[i] x2, y2 = amatrix[(i+1) % len(amatrix)] # print(x1, y1, x2, y2) g.draw_line(x1, y1, x2, y2, 'red') for i in range(len(newmatrix)): x1, y1 = newmatrix[i] x2, y2 = newmatrix[(i+1) % len(newmatrix)] # print(x1, y1, x2, y2) g.draw_line(x1, y1, x2, y2, 'red') for i in amatrix:
def single_turn(unittype, generated_map, oldman): walkables = all_walkable(generated_map) # print('$'*20, walkables) # print(len(walkables)) # welcome = colored('#'*10+' This turn generated_map:'+'#'*10, 'red', attrs=['reverse', 'blink']) # print(welcome, '\n') print('-' * 10, 'run new simulation turn', '-' * 10, '\n') # 设置起点和终点 start_grid = Grid(2, 1) end_grid = Grid(2, 5) if oldman.behavior_type == 0: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) if oldman.behavior_type == 1: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) if random.randint(0, 4) > 2: end_grid = Grid(beds[unittype][0], beds[unittype][1]) else: end_grid = Grid(e[0], e[1]) if oldman.behavior_type == 2: s = walkables[random.randint(0, len(walkables) // 2)] e = walkables[random.randint(len(walkables) // 2, len(walkables) - 1)] if random.randint(0, 4) > 2: start_grid = Grid(bathrooms[unittype][0], bathrooms[unittype][1]) else: start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) if oldman.behavior_type == 3: s = walkables[random.randint(0, len(walkables) // 3)] e = walkables[random.randint(len(walkables) // 3, len(walkables) - 1)] start_grid = Grid(s[0], s[1]) end_grid = Grid(e[0], e[1]) # 搜索房间终点 result_grid = elder_walking(start_grid, end_grid, generated_map) # 回溯房间路径 path = [] while result_grid is not None: path.append(Grid(result_grid.x, result_grid.y)) result_grid = result_grid.parent abel_score = 0 # 输出房间和路径,路径用星号表示 for i in range(0, len(generated_map)): for j in range(0, len(generated_map[0])): if contain_grid(path, i, j): # star = colored('*', 'magenta', attrs=['reverse', 'blink']) # print(star +", ", end="") cprint('*' + ", ", "green", attrs=['reverse', 'blink'], end="") abel_score += 1 else: if generated_map[i][j] == 1: cprint('1' + ", ", "grey", attrs=['reverse', 'blink'], end="") else: print(str(generated_map[i][j]) + ", ", end="") print() print('abel_score=', abel_score) return abel_score