def check_start_paths(n, m, c): best_p, best_cost = path.find_path(0, m, c) for i in range(1,len(m)): p, cost = path.find_path(i, m, c) if cost < best_cost: best_cost = cost best_p = p # p = k_search(2, n, m, c, best_p, 100000) if DEBUG: print best_p, best_cost return best_p
def approximate_tsp(n, m, c): max_edge = 0 for i in m: row_max = max(i) if row_max > max_edge: max_edge = row_max metric_m = [] for row in m: new_row = [max_edge + i for i in row] metric_m.append(new_row) p, cost = path.find_path(0, m, c) p = check_start_paths(n, m, c) # k = int(math.ceil(len(m)/8)) # if k < 2: # k = 2 p = k_search(3, n, m, c, p, 100) # p, cost = path.find_path(0, metric_m, c) # p = check_start_paths(n, metric_m, c) # p = k_search(6, n, metric_m, c, p, 100) #p = k_random(10, 10, m, p, c) cost, x = check_cost(p, m, c) if DEBUG: print p, cost print p return p, cost
def main(ant_num, iteration): count = 0 path_list = [] # each ant completes the search process, count++ # until all ants complete this job while count != ant_num: path = find_path(0, 6) path_list.append(path) count += 1 # store the path_list of the m-th iteration in final_path_list # final_path_list[i] indicates the path_list of the i-th iteration store_iteration_result('path.xlsx', path_list, iteration) # calculate the threshold of the iteration based on path_list # append the threshold to the result list # L_list[i] indicates the threshold of the i-th iteration path_load_list, path_cost_list = calculate_load_cost(path_list) print('****', path_load_list) print('####', path_cost_list) # get the best path for one iteration best_path = [path_list[i] for i in select_best_path(path_cost_list)] best_path_load = [[path_load_list[i]] for i in select_best_path(path_cost_list)] store_iteration_result('bestPath.xlsx', best_path, iteration) store_iteration_result('bestPathLoad.xlsx', best_path_load, iteration) # reset local state as the original level after completing one iteration # then update global_node_state reset_local_node_state(local_node_state) global_node_state = global_update_state(path_list, best_path) store_global_node_state('globalNodeState.xlsx', global_node_state, iteration)
def __init__(self, initial_ent, initial_dir, speed): self.initial_ent = initial_ent self.initial_dir = initial_dir self.final_exit = None self.final_dir = 0 self.speed = speed self.acc = 0 self.wait_time = 0 self.dist_to_front = 0 self.next = None self.prev = None self.position = 0 self.turn = macros.STRAIGHT self.change_lane = 0 # TODO: could make this random process faster while True: self.final_exit = (random.randint(1, len(macros.VER_DIM) - 1), random.randint(1, len(macros.HOR_DIM) - 1)) self.final_dir = random.randint(1, 4) if map_init.is_exit(self.final_exit, self.final_dir) and \ (self.final_exit != self.initial_ent or self.final_dir != self.initial_dir): break self.my_path = path.find_path(self.initial_ent, self.final_exit)
def __init__(self, initial_ent, initial_dir, speed): self.initial_ent = initial_ent self.initial_dir = initial_dir self.final_exit = None self.final_dir = 0 self.speed = speed self.acc = 0 self.wait_time = 0 self.dist_to_front = 0 self.next = None self.prev = None self.position = 0 self.turn = macros.STRAIGHT self.change_lane = 0 # TODO: could make this random process faster while True: self.final_exit = (random.randint(1, len(macros.VER_DIM)-1), random.randint(1, len(macros.HOR_DIM)-1)) self.final_dir = random.randint(1, 4) if map_init.is_exit(self.final_exit, self.final_dir) and \ (self.final_exit != self.initial_ent or self.final_dir != self.initial_dir): break self.my_path = path.find_path(self.initial_ent, self.final_exit)
def calc_path(self, rover): return Path(path.find_path(rover.pos, (0.0, 0.0), rover.world))
for x in range(width): graph.append([]) for y in range(height): n = Node(x, y, maze[x][y]) graph[x].append(n) if n.type == 'B': continue if x != 0 and maze[x - 1][y] == 'W': n.add_neighbor(graph[x - 1][y]) graph[x - 1][y].add_neighbor(n) if y != 0 and maze[x][y - 1] == 'W': n.add_neighbor(graph[x][y - 1]) graph[x][y - 1].add_neighbor(n) nodes = [] for i in graph: for j in i: if j.type == 'W': nodes.append(j) target = find_path(nodes, graph[1][0], graph[width - 2][height - 1]) curr = target while curr.xy != (1, 0): im.putpixel(curr.xy, ImageColor.getrgb('green')) curr = curr.prev im.save("solved.png")