コード例 #1
0
ファイル: passenger.py プロジェクト: aterrasa/taxi_simulator
 def set_target_position(self, coords=None):
     if coords:
         self.dest = coords
     else:
         self.dest = random_position()
     logger.debug("Passenger {} target position is {}".format(
         self.agent_id, self.dest))
コード例 #2
0
ファイル: crowdsim.py プロジェクト: afcarl/crowdsim
    def add_agents(self, agent_dict):
        for agent in agent_dict:
            dx, dy = float(agent['dx']), float(agent['dy'])
            x, y = float(agent['x']), float(agent['y'])
            num = int(agent['n'])
            a_type = int(agent['type'])

            # spawn an agent in a random direction and position(within dx, dy)
            direction = (randint(-1, 1), randint(-1, 1))
            position = random_position(x, y, dx, dy)
            waypoints = [awp['id'] for awp in agent['addwaypoint']]

            rd = 0.3

            for _ in xrange(num):
                self.agents.add(
                    Agent(agent_id=self._agent_count,
                          atype=a_type,
                          screen=self.screen,
                          game=self,
                          agent_image=self.agent_image,
                          field=self.field_rect,
                          init_position=position,
                          init_direction=direction,
                          max_speed=normalvariate(1.34, 0.26),
                          radius=rd,
                          waypoints=[self.waypoints[wp] for wp in waypoints]))
                self._agent_count += 1
コード例 #3
0
ファイル: passenger.py プロジェクト: aterrasa/taxi_simulator
 def send_request(self, content=None):
     """
     Sends an :class:`ACLMessage` to the coordinator to request a taxi.
     It uses the REQUEST_PROTOCOL and the REQUEST_PERFORMATIVE.
     If no content is set a default content with the passenger_id,
     origin and target coordinates is used.
     :param content: Optional content dictionary
     :type content: :class:`dict`
     """
     if content is None or len(content) == 0:
         content = {
             "passenger_id": self.myAgent.agent_id,
             "origin": self.myAgent.current_pos,
             "dest": self.myAgent.dest
         }
     if not self.myAgent.dest:
         self.myAgent.dest = random_position()
     msg = ACLMessage()
     msg.addReceiver(coordinator_aid)
     msg.setProtocol(REQUEST_PROTOCOL)
     msg.setPerformative(REQUEST_PERFORMATIVE)
     msg.setContent(json.dumps(content))
     self.myAgent.send(msg)
     self.logger.debug("Passenger {} asked for a taxi to {}.".format(
         self.myAgent.agent_id, self.myAgent.dest))
コード例 #4
0
ファイル: passenger.py プロジェクト: aterrasa/taxi_simulator
 def set_position(self, coords=None):
     if coords:
         self.current_pos = coords
     else:
         self.current_pos = random_position()
     logger.debug("Passenger {} position is {}".format(
         self.agent_id, self.current_pos))
コード例 #5
0
ファイル: crowdsim.py プロジェクト: hohiroki/crowdsim
    def add_agents(self, agent_dict):
        for agent in agent_dict:
            dx, dy = float(agent['dx']), float(agent['dy'])
            x, y = float(agent['x']), float(agent['y'])
            num = int(agent['n'])
            a_type = int(agent['type'])

            # spawn an agent in a random direction and position(within dx, dy)
            direction = (randint(-1, 1), randint(-1, 1))
            position = random_position(x, y, dx, dy)
            waypoints = [awp['id'] for awp in agent['addwaypoint']]

            rd = 0.3

            for _ in xrange(num):
                self.agents.add(Agent(
                        agent_id = self._agent_count,
                        atype = a_type,
                        screen = self.screen,
                        game = self,
                        agent_image = self.agent_image,
                        field = self.field_rect,
                        init_position = position,
                        init_direction = direction,
                        max_speed = normalvariate(1.34, 0.26),
                        radius = rd,
                        waypoints = [self.waypoints[wp] for wp in waypoints]
                    ))
                self._agent_count += 1
コード例 #6
0
    def _process(self):
        msg = self._receive(block=True)
        content = json.loads(msg.content)
        type_ = content["type"]
        number = content["number"]
        if type_ == "taxi":
            cls = TaxiAgent
            store = self.myAgent.taxi_agents
            strategy = self.myAgent.taxi_strategy
        else:  # type_ == "passenger":
            cls = PassengerAgent
            store = self.myAgent.passenger_agents
            strategy = self.myAgent.passenger_strategy

        for _ in range(number):
            with self.myAgent.lock:
                if self.myAgent.kill_simulator.isSet():
                    break
                position = random_position()
                name = self.myAgent.faker.user_name()
                password = self.myAgent.faker.password()
                jid = name + "@127.0.0.1"
                agent = cls(jid, password, debug=self.myAgent.debug_level)
                agent.set_id(name)
                agent.set_position(position)
                store[name] = agent
                agent.start()
                if self.myAgent.simulation_running:
                    agent.add_strategy(strategy)
                logger.debug("Created {} {} at position {}".format(
                    type_, name, position))
コード例 #7
0
ファイル: taxi.py プロジェクト: aterrasa/taxi_simulator
    def set_position(self, coords=None):
        if coords:
            self.current_pos = coords
        else:
            self.current_pos = random_position()

        logger.debug("Taxi {} position is {}".format(self.agent_id,
                                                     self.current_pos))
        if self.status == TAXI_MOVING_TO_DESTINY:
            self.inform_passenger(PASSENGER_LOCATION,
                                  {"location": self.current_pos})
コード例 #8
0
ファイル: cli.py プロジェクト: aterrasa/taxi_simulator
def create_agents_batch(type_, number, coordinator):
    if type_ == "taxi":
        cls = TaxiAgent
        store = coordinator.taxi_agents
        strategy = coordinator.taxi_strategy
    else:  # type_ == "passenger":
        cls = PassengerAgent
        store = coordinator.passenger_agents
        strategy = coordinator.passenger_strategy
    for _ in range(number):
        with coordinator.lock:
            if coordinator.kill_simulator.isSet():
                break
            position = random_position()
            name = coordinator.faker.user_name()
            password = coordinator.faker.password()
            agent = create_agent(cls, name, password, position, None,
                                 coordinator.debug_level)
            store[name] = agent
            agent.start()
            if coordinator.simulation_running:
                agent.add_strategy(strategy)
            logger.debug("Created {} {} at position {}".format(
                type_, name, position))
コード例 #9
0
ファイル: powerup.py プロジェクト: KiRtAp2/shooter2
    def __init__(self):

        # random init
        self.x, self.y = utils.random_position()
        self.type = random.choice(powerup_types)
        self.sx = self.sy = 50
コード例 #10
0
ファイル: main.py プロジェクト: Hamms/cognomancy
from objects import Container, Avatar, Minion
from hex import Hex
from actions import Action

pygame.init()

surface = pygame.display.set_mode((
    int((constants.MAP_WIDTH - 2/3) * Hex.pixel_width() * 3/4),
    int((constants.MAP_HEIGHT - 0.5) * Hex.pixel_height())
))

objects = []
minions = []

rocks = Container(float("inf"), True, False)
rocks._set_pos(*random_position())
objects.append(rocks)

bucket = Container(0, False, True)
bucket._set_pos(*random_position())
objects.append(bucket)

avatar = Avatar(minions, objects)
avatar._set_pos(*random_position())
objects.append(avatar)

for _ in range(3):
    minion = Minion(objects)
    minion._set_pos(*random_position())
    minions.append(minion)
    objects.append(minion)
コード例 #11
0
ファイル: asteroids.py プロジェクト: laconic75/Asteroids
 def put_rock(self):
     rock_position = utils.random_position(WIDTH, HEIGHT)
     rock = MovingSprite (self.rock_img, rock_position[0], rock_position[1],
                          sound=self.explosion_snd, diff=self.difficulty,
                          radius=40, batch=self.rocks)
     self.rock_group.add(rock)
コード例 #12
0
ファイル: rrt_star.py プロジェクト: rhidra/autopilot
def rrt_star(ros_node, start, goal, world_dim, display=True):
    rospy.loginfo('Computing RRT* algorithm...')
    nodes = []
    nodes.append(Node(start, None))

    i = 0
    goal_node = None
    while i < MAX_ITERATIONS and (goal_node is None or i < MIN_ITERATIONS):
        i = i + 1

        if i % 20 == 0 and goal_node is not None:
            x_rand = goal
        else:
            x_rand = random_position(world_dim)

        # Search nearest node to the rand point
        try:
            nearest = min(nodes,
                          key=lambda node: dist(node, x_rand)
                          if node is not goal_node else 1e1000)
        except ValueError:
            continue

        # Obtain the new node by casting the nearest node. The new node can be against a wall
        _, new = ros_node.cast_ray(nearest.pos,
                                   x_rand,
                                   radius=UAV_THICKNESS,
                                   max_dist=INCREMENT_DISTANCE)

        # Search the neighbors of the nearest node
        radius = min(
            NEIGHBOR_RADIUS * (math.log(len(nodes)) / len(nodes))**(1 / 3.),
            INCREMENT_DISTANCE)
        neighbors = filter(
            lambda node: dist(node, nearest) <= NEIGHBOR_RADIUS and
            not ros_node.cast_ray(node.pos, new, radius=UAV_THICKNESS)[
                0] and node is not goal_node, nodes)

        # Select best possible neighbor
        try:
            nearest = min(neighbors,
                          key=lambda node: node.cost + dist(node, new))
        except ValueError:
            pass

        new = Node(new, nearest)
        nodes.append(new)

        # Rewiring of the tree
        for node in neighbors:
            if new.cost + dist(new, node) < node.cost:
                node.parent = new
                node.cost = new.cost + dist(new, node)

        if goal_node is None and not ros_node.cast_ray(
                new.pos, goal, radius=UAV_THICKNESS)[0]:
            # Goal discovered
            goal_node = Node(goal, new)
            nodes.append(goal_node)
        elif goal_node is not None and not ros_node.cast_ray(
                new.pos, goal, radius=UAV_THICKNESS)[0] and new.cost + dist(
                    new.pos, goal) < goal_node.cost:
            # Goal node rewiring
            goal_node.parent = new
            goal_node.cost = new.cost + dist(new, node)

        if display and i % 50 == 0:
            ros_node.visualize_path(
                nodes=nodes,
                start=start,
                goal=goal,
                path=build_path(goal_node)[0] if goal_node is not None else [])
            ros_node.rate.sleep()

    if goal_node is None:
        raise ValueError('No Path Found')

    path = build_path(goal_node)
    return path, nodes, i
コード例 #13
0
ファイル: main.py プロジェクト: davidarias/py-shooting-game
 def add_alien(self):
     self.aliens.add(
         Alien(position=random_position(0, self.size[0])))