コード例 #1
0
def on_click(event):

    global source_point, destination_point, visited_boxes, path

    if source_point and destination_point:

        source_point = None
        destination_point = None
        visited_boxes = []
        path = []

    elif not source_point:

        source_point = event.y * SUBSAMPLE, event.x * SUBSAMPLE

    else:

        destination_point = event.y * SUBSAMPLE, event.x * SUBSAMPLE

        try:
            path, visited_boxes = pathfinder.find_path(source_point,
                                                       destination_point, mesh)
            #path, visited_boxes = pathfinder_a_star.find_path(source_point, destination_point, mesh)
            #path, visited_boxes = pathfinder_dijkstra.find_path(source_point, destination_point, mesh)
            #path, visited_boxes = pathfinder_dijkstra_bi.find_path(source_point, destination_point, mesh)

        except:
            destination_point = None
            traceback.print_exc()

    redraw()
コード例 #2
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_shortest_path_around_large_obstacle(self):
     position = Point(425, 485)
     destination = Point(284, 221)
     obstacles = (Obstacle(265, 335, 265, 8000), )
     path = find_path(position, destination, obstacles)
     print(path)
     self.assertEquals(path, [Point(265, 335), Point(265, 265), Point(284, 221)])
コード例 #3
0
def do_pathfinding(event):
	global all_polygons, all_waypoints
	path = find_path(all_polygons, all_waypoints)
	lenpath = len(path)
	for i in range(0, lenpath-1): # Draw the path
		p1 = path[i]
		p2 = path[i+1] # Next points in path
		draw_line(event.widget, p1, p2)
コード例 #4
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_two_obstacles(self):
     position = Point(0, 0)
     destination = Point(5, 0)
     obstacles = (
         Obstacle(-3, 0.5, 1, 2),
         Obstacle(-0.5, 3, 3, 4)
     )
     path = find_path(position, destination, obstacles)
     self.assertEquals(path, [Point(1, 0.5), Point(2, 0.5), Point(3, -0.5), Point(4, -0.5), Point(5, 0)])
コード例 #5
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_shortest_path_around_two_obstacles(self):
     position = Point(750, 290)
     destination = Point(607, 324)
     obstacles = (
         Obstacle(65, 325, 655, 725),
         Obstacle(265, 335, 265, 575)
     )
     path = find_path(position, destination, obstacles)
     self.assertEquals(path, [Point(725, 325), Point(655, 325), Point(607, 324)])
コード例 #6
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_no_path_available(self):
     position = Point(0, 0)
     destination = Point(3, 0)
     obstacles = (
         Obstacle(-2, 2, 1, 2),
         Obstacle(-2, 2, 4, 5),
         Obstacle(1, 2, 1, 5),
         Obstacle(-2, -1, 1, 5)
     )
     path = find_path(position, destination, obstacles)
     self.assertEquals(path, None)
コード例 #7
0
def test_graph_from_example():
    roadmap = RoadMap()

    MP1 = MapPoint(name='1', gas_station=GasStation(price=Decimal('3.17')))
    MP2 = MapPoint(name='2', gas_station=GasStation(price=Decimal('2.6')))
    MP3 = MapPoint(name='3', gas_station=None)
    MP4 = MapPoint(name='4', gas_station=None)
    MP5 = MapPoint(name='5', gas_station=None)

    roadmap.add_edge(MP1, MP2, Road(Decimal(10), point_from=MP1, point_to=MP2))
    roadmap.add_edge(MP1, MP5,
                     Road(Decimal(100000), point_from=MP1, point_to=MP5))
    roadmap.add_edge(MP1, MP4, Road(Decimal(30), point_from=MP1, point_to=MP4))
    roadmap.add_edge(MP2, MP3, Road(Decimal(50), point_from=MP2, point_to=MP3))
    roadmap.add_edge(MP4, MP3, Road(Decimal(20), point_from=MP4, point_to=MP3))
    roadmap.add_edge(MP4, MP5,
                     Road(Decimal(100000), point_from=MP4, point_to=MP5))
    roadmap.add_edge(MP3, MP5,
                     Road(Decimal(100000), point_from=MP3, point_to=MP5))

    truck = Truck(capacity=Decimal(500),
                  min_volume=Decimal(40),
                  mpg=Decimal(24))

    vol0 = Decimal(40)

    e = None
    try:
        find_path(
            roadmap=roadmap,
            from_point=MP1,
            to_point=MP5,
            across_points=(),
            truckstate=TruckState(truck=truck, volume=vol0),
        )
    except NoSolution as ex:
        e = ex
    assert e
def test_graph_from_example():
    roadmap = RoadMap()

    MP1 = MapPoint(name='1', gas_station=GasStation(price=Decimal('3.17')))
    MP2 = MapPoint(name='2', gas_station=GasStation(price=Decimal('2.6')))
    MP3 = MapPoint(name='3', gas_station=None)
    MP4 = MapPoint(name='4', gas_station=None)
    MP5 = MapPoint(name='5', gas_station=None)

    roadmap.add_edge(MP1, MP2, Road(Decimal(10), point_from=MP1, point_to=MP2))
    roadmap.add_edge(MP1, MP5, Road(Decimal(100), point_from=MP1,
                                    point_to=MP5))
    roadmap.add_edge(MP1, MP4, Road(Decimal(30), point_from=MP1, point_to=MP4))
    roadmap.add_edge(MP2, MP3, Road(Decimal(50), point_from=MP2, point_to=MP3))
    roadmap.add_edge(MP4, MP3, Road(Decimal(20), point_from=MP4, point_to=MP3))
    roadmap.add_edge(MP4, MP5, Road(Decimal(60), point_from=MP4, point_to=MP5))
    roadmap.add_edge(MP3, MP5, Road(Decimal(10), point_from=MP3, point_to=MP5))

    truck = Truck(capacity=Decimal(500),
                  min_volume=Decimal(40),
                  mpg=Decimal(24))

    vol0 = Decimal(40)

    expected_cost = (Decimal(10) / Decimal(24) * Decimal('3.17') +
                     Decimal(50) / Decimal(24) * Decimal('2.6') +
                     Decimal(10) / Decimal(24) * Decimal('2.6'))

    route = find_path(
        roadmap=roadmap,
        from_point=MP1,
        to_point=MP5,
        across_points=(),
        truckstate=TruckState(truck=truck, volume=vol0),
    )
    assert route.route_points == (
        RoutePoint(MP1, 1),
        RoutePoint(MP2, 2),
        RoutePoint(MP3, 3),
        RoutePoint(MP5, 4),
    )
    assert route.cost == expected_cost
    assert route.route_fuel_pool.refuel_list == [
        RouteRefuel(RoutePoint(MP1, 1),
                    Decimal(10) / Decimal(24)),
        RouteRefuel(RoutePoint(MP2, 2),
                    Decimal(50) / Decimal(24) + Decimal(10) / Decimal(24)),
    ]
コード例 #9
0
ファイル: commands.py プロジェクト: atiaxi/chromabot
    def expand_path(cls, regions, context):
        if "*" not in regions:
            return [Region.get_region(where, context) for where in regions]
        if regions[-1] == '*':
            context.reply("You can't end a movement command with a "
                          "pathfinding instruction; I have no idea where you "
                          "want to end up!")
            return
        result = []
        curr = context.player.region
        for index, region_name in enumerate(regions):
            if region_name == '*':
                if not curr:
                    continue  # Don't bother if we don't know prev location
                if regions[index + 1] == '*':
                    continue  # Ignore consecutive pathfinding instructions
                dest = Region.get_region(regions[index + 1], context)
                if not dest:
                    return None

                # See if we allow neutral traversal
                conf = context.config
                traverse_neutrals = False
                if conf:
                    traverse_neutrals = conf["game"].get("traversable_neutrals",
                                                         False)
                path = find_path(curr, dest, context.player.team,
                                 traverse_neutrals=traverse_neutrals)
                if path:
                    path = path[1:-1]  # We already have the first and the last
                    if not path:
                        continue  # They were already adjacent!
                    result.extend(path)
                else:
                    context.reply("I couldn't find a friendly-territory path "
                                  "between %s and %s "
                                  % (curr.name, dest.name))
                    return None
                curr = dest
            else:
                dest = Region.get_region(region_name, context)
                if not dest:
                    return None
                result.append(dest)
                curr = dest
        return result
コード例 #10
0
ファイル: graph_sokoban.py プロジェクト: jaromiru/sr-drl
    def find_path(self, start, end):  # in x,y coordinates
        xy_dim = self.env.dim_room[::-1]

        start = np.array(start)
        end = np.array(end)

        if np.any(start < 0) or np.any(end < 0) or np.any(
                start >= xy_dim) or np.any(end >= xy_dim):
            return None  # the coordinates are out of the map

        arr_block = 1 - ((self.env.room_fixed == 0) +
                         (self.env.room_state == 4) +
                         (self.env.room_state == 3)).view(np.int8)

        path = pathfinder.find_path(arr_block, start, end)
        tpath = pathfinder.translate_path(path)

        return tpath
コード例 #11
0
ファイル: db.py プロジェクト: atiaxi/chromabot
    def resolve(self, conf=None):
        num_sectors = 1
        if conf:
            num_sectors = conf["game"].get("num_sectors", 1)

        score = [[0, 0] for _ in xrange(num_sectors + 1)]

        for skirmish in self.toplevel_skirmishes():
            skirmish.resolve()
            if skirmish.victor is not None:
                score[skirmish.sector][skirmish.victor] += skirmish.vp

        # Apply buffs
        for buff in self.region.buffs:
            # For now:  Buffs apply to whoever owns the region
            if self.region.owner is not None:
                team = self.region.owner
                for score_per_sector in score:
                    score_per_sector[team] += int(
                        score_per_sector[team] * buff.value)

        # Apply homeland defense
        self.homeland_buffs = []
        if conf and conf["game"].get("homeland_defense"):
            percents = [int(amount) / 100.0 for amount in
                        conf["game"]["homeland_defense"].split("/")]
            for score_per_sector in score:
                # Ephemeral, for reporting
                for team in range(0, 2):
                    self.homeland_buffs.append(0)
                    cap = Region.capital_for(team, self.session())
                    path = find_path(cap, self.region)
                    if path:
                        dist = len(path) - 1
                        if dist < len(percents):
                            self.homeland_buffs[team] = percents[dist] * 100
                            score_per_sector[team] += int(
                                score_per_sector[team] * percents[dist])

        final_score = [0, 0]
        sector_wins = [0, 0]
        for score0, score1 in score:
            final_score[0] += score0
            final_score[1] += score1
            if score0 > score1:
                sector_wins[0] += 1
            elif score1 > score0:
                sector_wins[1] += 1

        if conf and conf["game"].get("sector_victory", False):
            self.score0, self.score1 = sector_wins
        else:
            self.score0, self.score1 = final_score

        if self.score0 > self.score1:
            self.victor = 0
        elif self.score1 > self.score0:
            self.victor = 1
        else:
            self.victor = None

        # Ephemeral, for buff reporting only
        self.old_owner = self.region.owner
        self.old_buffs = self.region.buffs[:]

        # The new owner of wherever this battle happened is the victor
        if self.victor is not None:
            buff_expiration = None  # Will default to a week
            if conf:
                buff_expiration = conf.game.get('defense_buff_time')

            self.region.owner = self.victor
            if self.old_owner != self.victor:  # Invaders get the 'otd' buff
                self.region.buff_with(Buff.otd(buff_expiration))
            else:
                # Defenders get the 'fortified' buff
                self.region.buff_with(Buff.fortified(buff_expiration))

        # Un-commit all the loyalists for this fight, kick out the losers
        losercap = None
        # Make a copy so deletion won't screw things up
        people = list(self.region.people)
        for person in people:
            reward = self.troop_reward_for(person, self.victor, conf=conf)
            person.loyalists += reward
            if conf:
                cap = conf["game"].get("troopcap", 0)
                if cap:
                    person.loyalists = min(person.loyalists, cap)

            person.committed_loyalists = 0
            if person.team != self.victor:
                if not losercap:
                    losercap = Region.capital_for(person.team,
                                                  self.session())
                person.region = losercap
            self.session().commit()

        self.session().commit()
コード例 #12
0
ファイル: db.py プロジェクト: atiaxi/chromabot
    def resolve(self, conf=None):
        num_sectors = 1
        if conf:
            num_sectors = conf["game"].get("num_sectors", 1)

        score = [[0, 0] for _ in xrange(num_sectors + 1)]

        for skirmish in self.toplevel_skirmishes():
            skirmish.resolve()
            if skirmish.victor is not None:
                score[skirmish.sector][skirmish.victor] += skirmish.vp

        # Apply buffs
        for buff in self.region.buffs:
            # For now:  Buffs apply to whoever owns the region
            if self.region.owner is not None:
                team = self.region.owner
                for score_per_sector in score:
                    score_per_sector[team] += int(score_per_sector[team] *
                                                  buff.value)

        # Apply homeland defense
        self.homeland_buffs = []
        if conf and conf["game"].get("homeland_defense"):
            percents = [
                int(amount) / 100.0
                for amount in conf["game"]["homeland_defense"].split("/")
            ]
            for score_per_sector in score:
                # Ephemeral, for reporting
                for team in range(0, 2):
                    self.homeland_buffs.append(0)
                    cap = Region.capital_for(team, self.session())
                    path = find_path(cap, self.region)
                    if path:
                        dist = len(path) - 1
                        if dist < len(percents):
                            self.homeland_buffs[team] = percents[dist] * 100
                            score_per_sector[team] += int(
                                score_per_sector[team] * percents[dist])

        final_score = [0, 0]
        sector_wins = [0, 0]
        for score0, score1 in score:
            final_score[0] += score0
            final_score[1] += score1
            if score0 > score1:
                sector_wins[0] += 1
            elif score1 > score0:
                sector_wins[1] += 1

        if conf and conf["game"].get("sector_victory", False):
            self.score0, self.score1 = sector_wins
        else:
            self.score0, self.score1 = final_score

        if self.score0 > self.score1:
            self.victor = 0
        elif self.score1 > self.score0:
            self.victor = 1
        else:
            self.victor = None

        # Ephemeral, for buff reporting only
        self.old_owner = self.region.owner
        self.old_buffs = self.region.buffs[:]

        # The new owner of wherever this battle happened is the victor
        if self.victor is not None:
            buff_expiration = None  # Will default to a week
            if conf:
                buff_expiration = conf.game.get('defense_buff_time')

            self.region.owner = self.victor
            if self.old_owner != self.victor:  # Invaders get the 'otd' buff
                self.region.buff_with(Buff.otd(buff_expiration))
            else:
                # Defenders get the 'fortified' buff
                self.region.buff_with(Buff.fortified(buff_expiration))

        # Un-commit all the loyalists for this fight, kick out the losers
        losercap = None
        # Make a copy so deletion won't screw things up
        people = list(self.region.people)
        for person in people:
            reward = self.troop_reward_for(person, self.victor, conf=conf)
            person.loyalists += reward
            if conf:
                cap = conf["game"].get("troopcap", 0)
                if cap:
                    person.loyalists = min(person.loyalists, cap)

            person.committed_loyalists = 0
            if person.team != self.victor:
                if not losercap:
                    losercap = Region.capital_for(person.team, self.session())
                person.region = losercap
            self.session().commit()

        self.session().commit()
コード例 #13
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_no_obstacles(self):
     position = Point(0, 0)
     destination = Point(3, 0)
     obstacles = ()
     path = find_path(position, destination, obstacles)
     self.assertEqual(path, [(3, 0)])
コード例 #14
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_destination_inside_obstacle(self):
     position = Point(4, 4)
     destination = Point(0, 0)
     obstacles = (Obstacle(-2, 2, -2, 2), )
     path = find_path(position, destination, obstacles)
     self.assertEquals(path, None)
コード例 #15
0
ファイル: tests.py プロジェクト: pfertyk/simple_pathfinder
 def test_one_obstacle(self):
     position = Point(0, 0)
     destination = Point(5, 0)
     obstacles = (Obstacle(-3, 1, 2, 3), )
     path = find_path(position, destination, obstacles)
     self.assertEquals(path, [Point(2, 1), Point(3, 1), Point(5, 0)])