def vertical_path(x, y, start_h, end_h): """生成垂直路径""" if start_h < end_h: return [Coordinate(x, y, i) for i in range(start_h, end_h + 1)] else: return [Coordinate(x, y, i) for i in reversed(range(end_h, start_h + 1))]
def lab(): coordinates = [ Coordinate(33.643653, -117.841143), Coordinate(33.649985, -117.831387), Coordinate(33.687698, -117.771220) ] coordinates_json = [coordinate.getJSON() for coordinate in coordinates] json = {"status": 200, "coordinates": coordinates_json} return jsonify(json)
def loc_path_to_coordinate_path(path: [libjpsp.XYLoc], height) -> [Coordinate]: res = [] for loc in path: res.append(Coordinate(loc.x, loc.y, height)) return res
def read_csv(): with open('example.csv', 'rb') as csvfile: spamreader = csv.reader(csvfile) count = 0 for row in spamreader: if count == 0: count += 1 pass else: print row #array #should loop through the row and push elements of the array into the DB location = row[0] date = row[1] notes = row[2] lattitude = row[3] lattitude_direction = row[4] longitude = row[5] print longitude longitude_direction = row[6] coordinate = Coordinate( location=location, date=date, notes=notes, lattitude=lattitude, lattitude_direction=lattitude_direction, longitude=longitude, longitude_direction=longitude_direction) db.session.add(coordinate) db.session.commit() count += 1
def findRoutes(): start_time = time() body = None body = request.get_json(force=True) temp = request.args.get('use_heuristic') flag = False if temp != "false": flag = True print(body) lista = list(body) if len(lista) < 2: return [] destinations = None destinations = RouteController() for i in lista: destinations.add_cord(Coordinate(i['lat'], i['lng'], 'A')) #TODO sa = None sa = SimulatedAnnealing(destinations, initial_temperature=1000, cooling_rate=0.0015, use_heuristic=flag) sa.run() response = [] for i in sa.best: response.append({'lat': i.lat, 'lng': i.long}) elapsed_time = time() - start_time print("Elapsed time: %0.10f seconds." % elapsed_time) return jsonify(response)
def gen_random_points(num_points, map_info: MapInfo): """ 生成当前地图中的合法的随机点,把空闲无人机分散到各个角落 :param num_points: 所需生成的坐标点数量 :param map_info: 当前地图信息 :return: """ low, high = 0, map_info.map_range.x xy = np.random.randint(low=low, high=high, size=(2, num_points)) # 生成随机坐标 points = [ Coordinate(x=x, y=y, z=map_info.h_low) for x, y in zip(xy[0, :], xy[1, :]) ] # 对于不合法的点(和障碍物重叠),重新生成 for i in range(len(points)): while points[i].is_overlap(map_info.buildings): x, y = np.random.randint(low=0, high=map_info.map_range.x, size=2) points[i] = Coordinate(x, y, map_info.h_low) return points
def _search(self, start, end, search_height, obstacles=None): """调用搜索算法进行路径搜索。""" if self.map_info.map_range.x > 0 and env.jpsp_finders \ and search_height in env.jpsp_finders: # 大地图并且存在合法的JPSPlus对象,使用JPS算法搜索 # print("UAV %d, JPS Plus search, height %d, " % (self.uav.no, search_height), end='') # jpsp_start_time = time.time() # jpsp_path = env.jpsp_finders[search_height].get_path( # xy_to_loc(start.x, start.y), xy_to_loc(end.x, end.y)) # print("time %s" % (time.time() - jpsp_start_time)) print("UAV %d, JPSPlus search, height %d, " % (self.uav.no, search_height), end='') jpsp_start_time = time.time() jpsp_path = env.jpsp_finders[search_height].get_path( bridge.XYLoc(start.x, start.y), bridge.XYLoc(end.x, end.y)) print("time %s" % (time.time() - jpsp_start_time)) trans_start = time.time() search_result = bridge.to_coord_path(path=jpsp_path, height=search_height) print("Transform time %s" % (time.time() - trans_start)) else: print("UAV %d, A-Star search, height %d, " % (self.uav.no, search_height), end='') self.problem.set_config(start=Coordinate(start.x, start.y, search_height), end=Coordinate(end.x, end.y, search_height), h_low=self.map_info.h_low, h_high=self.map_info.h_high, obstacles=obstacles, map_range=self.map_info.map_range) a_star_start_time = time.time() search_result = astar(self.problem, graph_search=True) print("time %s" % (time.time() - a_star_start_time)) search_result = [c for _, c in search_result.path()] return search_result if search_result else []
def main(): destinations = RouteController() ciudad1 = Coordinate(-12.095266834590417, -77.0542065585971, 'Lima') destinations.add_cord(ciudad1) ciudad1 = Coordinate(-12.08264894268049, -77.04563370491826, 'Argentina') destinations.add_cord(ciudad1) ciudad2 = Coordinate(-12.092396932744766, -77.03341079647996, 'Brasil') destinations.add_cord(ciudad2) ciudad3 = Coordinate(-12.071923319293719, -77.0513069176675, 'Bolivia') destinations.add_cord(ciudad3) ciudad4 = Coordinate(-12.064787751282571, -77.03733650569475, 'Paraguay') destinations.add_cord(ciudad4) sa = SimulatedAnnealing(destinations, initial_temperature=1000, cooling_rate=0.0000003, use_heuristic=False) print(sa.route.show()) print(sa.route.get_distance(sa.dictionary)) sa.run() print(sa.route.show()) print(sa.route.get_distance(sa.dictionary)) print("Termino el algoritmo con %i vueltas" % sa.iterations)
def findRoutes(): start_time = time() body = None body = request.get_json(force=True) lista = list(body) if len(lista) < 2: return [] destinations = [] for i in lista: destinations.append(Coordinate(i['lat'], i['lng'], 'A')) #TODO ga = None ga = GeneticAlgorithm.GeneticAlgorithm(destinations, 0.5, 10) ga.generations(2000) best = ga.getBestIndividuo() response = [] for i in best: response.append({'lat': i.lat, 'lng': i.long}) elapsed_time = time() - start_time print("Elapsed time: %0.10f seconds." % elapsed_time) return jsonify(response)
self.plan(self.uav.loc, self.goods.end, task_type=TaskType.TO_GOODS_END) self.next_step = self.uav.loc # res = self.path[self.index] # self.index += 1 elif self.uav.loc == self.goods.end: # 货物送到目的地 self.reset() # 此时无人机算空闲, 直接重置状态 # 飞机垂直上升到(x, y, h_low) self.path = self.vertical_path( self.uav.loc.x, self.uav.loc.y, self.uav.loc.z, self.map_info.h_low) self.next_step = self.path[self.index] self.index += 1 else: # 有可能出错了,暂时原地不动 self.next_step = self.uav.loc if __name__ == '__main__': _a = Coordinate(1, 0, 0) _b = Coordinate(0, 0, 0) for di in HORIZONTAL_DIRECTIONS: _next_a = _a + di for dj in HORIZONTAL_DIRECTIONS: _next_b = _b + dj res = is_encounter(_a, _next_a, _b, _next_b) if res: print(_next_a, _next_b, res)
def xy_loc_to_coord(loc: XYLoc, height: int): return Coordinate(loc.x, loc.y, height)
def to_coordinate_path(path: [Point], height) -> [Coordinate]: coordinate_path = [] for point in path: x, y = point_to_xy(point) coordinate_path.append(Coordinate(x, y, height)) return coordinate_path
def _earlier(our_loc, enemy_loc, end): x, y = end.x, end.y h_low = map_info.h_low return manhattan_dis_3d(our_loc, Coordinate(x, y, h_low), h_low) < \ manhattan_dis_3d(enemy_loc, Coordinate(x, y, 0), h_low)
def attack_enemy(map_info: MapInfo, step_info: StepInfo): """ 主动攻击敌方无人机,主动攻击的己方无人机需要满足两个如下条件: 1.无任务在身。2.价值最小或者比攻击的敌方无人机价值小 :param map_info: 地图信息:障碍物,地图大小,停机坪位置。。。 :param step_info: 当前赛场信息,包括己方无人机信息,敌方无人机信息,货物信息等等。 :return: 不需要返回,直接对Agent进行操作 """ print('attack enemy') _threshold = manhattan_distance(Coordinate(0, 0, 0), map_info.map_range) / 2 print('threshold', _threshold) def _near(coord1, coord2, threshold): """ simplify, decide if attacker is near end """ return manhattan_distance(coord1, coord2) <= threshold def _done(enemy_id): # 需要考虑雾区 uav = step_info.uav_enemy[enemy_id] return uav.status == UAVStatus.CRASHED or uav.goods_no == -1 def _valuable(enemy_id): # enemy should carry good, and good lifetime should be valid uav = step_info.uav_enemy[enemy_id] if uav.goods_no == -1: return False # return True good = step_info.goods[uav.goods_no] return good.left_time > manhattan_dis_3d(uav.loc, good.end, map_info.h_low) def _earlier(our_loc, enemy_loc, end): x, y = end.x, end.y h_low = map_info.h_low return manhattan_dis_3d(our_loc, Coordinate(x, y, h_low), h_low) < \ manhattan_dis_3d(enemy_loc, Coordinate(x, y, 0), h_low) # attack ends -> idle for agent in env.agents.values(): if agent.task_type == TaskType.ATTACK_ENEMY: enemy_id = env.attackerToEnemy.get(agent.uav.no) if enemy_id and _done(enemy_id): env.attackerToEnemy.pop(agent.uav.no) agent.task_type = TaskType.NO_TASK print('ATTACK -> NO_TASK') # idle -> attack do assign & plan for agent in env.agents.values(): if agent.task_type == TaskType.NO_TASK or agent.task_type == TaskType.TO_RANDOM_POINT: for enemy_uav in step_info.uav_enemy.values(): _enemy_id = enemy_uav.no if _enemy_id not in env.attackerToEnemy.values() \ and enemy_uav.status == UAVStatus.NORMAL \ and _valuable(_enemy_id): # enemy 没有被assign && 可见 && 有价值 print('In here') goods_no = step_info.uav_enemy[_enemy_id].goods_no x, y = step_info.goods[goods_no].end.x, step_info.goods[ goods_no].end.y _end = Coordinate(x, y, map_info.h_low) # if _near(agent.uav.loc, _end, threshold): if _near(agent.uav.loc, _end, _threshold) and _earlier( agent.uav.loc, enemy_uav.loc, _end): print('NO_TASK->ATTACK') env.attackerToEnemy[agent.uav.no] = _enemy_id agent.plan(agent.uav.loc, _end, task_type=TaskType.ATTACK_ENEMY)