Exemplo n.º 1
0
    def update(dt):

        if not state.started:
            return

        if bird.alive:

            state.t_to_next_pipe -= dt

            if state.t_to_next_pipe < 0:
                pipe = Pipe(space=75 * settings.scale, window=window)
                pipes.append(pipe)
                state.t_to_next_pipe += 2

            for pipe in copy(pipes):
                if not pipe.visible:
                    pipes.remove(pipe)

            # Move everything
            background.update(dt)
            for pipe in pipes:
                pipe.update(dt)
            floor.update(dt)

            # Check for collisions
            collision = check_collision(bird, floor) or any([check_collision(bird, pipe) for pipe in pipes])
            if collision or bird.y > window.height:
                bird.die()

        if not bird.dead:
            bird.update(dt)

        if bird.dying and bird.y < -100:
            bird.stop()
Exemplo n.º 2
0
    def _check_for_collisions(self):
        """Check if any atoms have collided."""
        for atom_A in self.atoms:
            for atom_B in self.atoms:
                if atom_A != atom_B:
                    if check_collision(atom_A, atom_B):

                        if atom_A.type == "ZERO" or atom_B == "ZERO":
                            self.atom_zero_collisions += 1

                            if atom_A.type == "ZERO" and atom_B.type == "HEALTHY":
                                atom_B.type = "INFECTED"
                                atom_B.color = color["CARRIER"]
                                self.atom_zero_distances.append(
                                    self.ticks_between_collisions
                                    * atom_A.velocity_vector
                                )

                            elif atom_A.type == "HEALTHY" and atom_B.type == "ZERO":
                                atom_A.type = "INFECTED"
                                atom_A.color = color["CARRIER"]
                                self.atom_zero_distances.append(
                                    self.ticks_between_collisions
                                    * atom_B.velocity_vector
                                )

                            # If atom zero collided, reset tick between collisions counter
                            self.ticks_between_collisions = 0

                        self._collide(atom_A, atom_B)
Exemplo n.º 3
0
 def walled(self, data):
     """
     >>> from rectangular_world import RectangularWorld
     >>> m = Cacher(RectangularWorld(None))
     >>> d = [{'speed':1,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])}]
     >>> m.walled(d)
     True
     >>> d = [{'speed':5,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])}]
     >>> m.walled(d)
     False
     >>> d = [{'speed':1,'angle':10,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])}]
     >>> m.walled(d)
     False
     >>> d = [{'speed':1,'angle':0,'position':np.array([20,20])},{'speed':1,'angle':0,'position':np.array([1,1])},{'speed':1,'angle':0,'position':np.array([1,1])}]
     >>> m.walled(d)
     False
     """
     
     for i in range(len(data)):
         if not utils.check_collision(data[i]['position'], self.pos_limits, self.shape, update = False, extended = True):
             return False
         if data[i]['speed'] > 1e-12:
             return False
     
     return True
Exemplo n.º 4
0
 def get_explore_goal(self):
     """
     Etermine next location to explore. 
     If using edge goals, move to next corner when you hit the wall.
     Otherwise use random legal position.
     """
     collision = utils.check_collision(self.goal, self.world.pos_limits, self.world.shape,
                                       update = False, extended = True)
     return (get_legal_position(self.goal, self.world.pos_limits, self.world) if collision 
             else self.goal)
Exemplo n.º 5
0
 def likelihood(self, loc, obs, x):
     
     collision = utils.check_collision(loc, self.pos_limits, self.shape, update = False)
     if not collision:
         expected = self.score( self.dists(loc, x) )
     else:
         assert obs < 1e-12
         expected = np.array([0.0]*len(x))
     
     return -(obs - expected)**2/float(2*self.noise**2)
Exemplo n.º 6
0
    def update(dt):

        global score 

        if not state.started:
            return

        if bird.alive:

            state.t_to_next_pipe -= dt

            if state.t_to_next_pipe < 0:
                pipe = Pipe(space=75 * settings.scale, window=window)
                pipes.append(pipe)
                state.t_to_next_pipe += 2
                # update score -- problem is for the first one
                score += 1
                # directly setting text on 
                scoreLabel._set_text(str(score))

            for pipe in copy(pipes):
                if not pipe.visible:
                    pipes.remove(pipe)

            # Move everything
            background.update(dt)
            for pipe in pipes:
                pipe.update(dt)
            floor.update(dt)

            # Check for collisions
            collision = check_collision(bird, floor) or any([check_collision(bird, pipe) for pipe in pipes])
            if collision or bird.y > window.height:
                bird.die()

        if not bird.dead:
            bird.update(dt)

        if bird.dying and bird.y < -100:
            bird.stop()
            # reset the score on death
            score = -1
Exemplo n.º 7
0
    def act(self, p):

        # samples = self.model.samples
        # c = Counter([tuple(np.array(s,dtype=int)) for s in samples])
        # points = c.keys()
        # counts = [c[i] for i in points]
        # maxes = np.array([np.array(points[i]) for i in utils.which_max(counts, ties = 'all')])

        #if self.goal is None:
        #    self.goal = p.pos

        # if bg_val >= 1.0#self.threshold:
        #     self.goal = pos
        # else:
        #     if len(self.social) > 0:
        #         self.goal = np.mean(self.social, 0) + 20*np.random.random(2) - 10

        #     if tuple(np.array(self.goal,dtype=int)) not in points:
        #         self.goal = utils.closest(self.goal, maxes)

        # p.go_towards(self.goal)

        if self.last_bg >= 1.0:  # self.threshold:
            g = self.last_pos
        else:
            if len(
                    self.social
            ) > 0 and self.watch_others:  # and (random.random() < 1/8.0):
                g = np.mean(self.social, 0)  # + 20*np.random.random(2) - 10
            else:
                if self.world.edge_goal:
                    collision, sides = utils.check_collision(
                        self.last_pos,
                        self.world.pos_limits,
                        self.world.shape,
                        update=False,
                        extended=True,
                        return_side=True)
                    if collision:
                        g = next_corner(sides, self.world.pos_limits,
                                        self.turn)
                    else:
                        g = closest_wall(self.last_pos, self.world.pos_limits)
                else:
                    g = self.goal

        p.go_towards(g)

        return g
Exemplo n.º 8
0
 def likelihood(self, loc, obs, x):
     
     collision = utils.check_collision(loc, self.pos_limits, self.shape, update = False, extended = True)
     if collision:
         try:
             assert abs(obs - 0.1) < 1e-12 or abs(obs - 1.0) < 1e-12
         except:
             import pdb; pdb.set_trace()
         expected = self.score( self.dists(loc, x) )
     else:
         try:
             assert obs < 1e-12
         except:
             import pdb; pdb.set_trace()
         expected = np.array([0.0]*len(x))
     
     return -(obs - expected)**2/float(2*self.noise**2)
Exemplo n.º 9
0
    def get_random_position(self):
        """
        >>> np.random.seed(1)
        >>> w = RectangularWorld('../test/')
        >>> np.round(np.max([w.get_random_position()[0] for i in range(10000)]))
        482.0
        >>> round(np.min([w.get_random_position()[0] for i in range(10000)]))
        3.0
        >>> round(np.min([w.get_random_position()[1] for i in range(10000)]))
        3.0
        >>> round(np.max([w.get_random_position()[1] for i in range(10000)]))
        277.0
        """

        pos = None

        if self.edge_goal:
            while True:
                pos = np.array([
                    np.random.uniform(self.pos_limits["x_min"],
                                      self.pos_limits["x_max"]),
                    np.random.uniform(self.pos_limits["y_min"],
                                      self.pos_limits["y_max"])
                ])
                if utils.check_collision(pos,
                                         self.pos_limits,
                                         self.shape,
                                         update=False,
                                         extended=True):
                    break
        else:
            pos = np.array([
                np.random.uniform(self.pos_limits["x_min"],
                                  self.pos_limits["x_max"]),
                np.random.uniform(self.pos_limits["y_min"],
                                  self.pos_limits["y_max"])
            ])

        return pos