Example #1
0
    def move(self):
        """ Move the Person

        """
        if self.pdshow:
            fig =plt.gcf()
            fig,ax=self.L.showG('w',labels=False,alphacy=0.,edges=False,fig=fig)
            plt.draw()
            plt.ion()
        while True:
            if self.moving:
                if self.sim.verbose:
                    print 'meca: updt ag ' + self.ID + ' @ ',self.sim.now()
                
                # if np.allclose(conv_vecarr(self.destination)[:2],self.L.Gw.pos[47]):
                #     import ipdb
                #     ipdb.set_trace()


                while self.cancelled:
                    yield passivate, self
                    print "Person.move: activated after being cancelled" 
                checked = []
                for zone in self.world.zones(self):
                    if zone not in checked:
                        checked.append(zone)
                        zone(self)

                # updating acceleration
                acceleration = self.steering_mind(self)
                acceleration = acceleration.truncate(self.max_acceleration)
                self.acceleration = acceleration

                # updating velocity
                velocity = self.velocity + acceleration * self.interval
                self.velocity = velocity.truncate(self.max_speed)


                if velocity.length() > 0.2:
                    # record direction only when we've really had some
                    self.localy = velocity.normalize()
                    self.localx = vec3(self.localy.y, -self.localy.x)

                # updating position
                self.position = self.position + self.velocity * self.interval
#                self.update()
                self.position.z=0
                self.world.update_boid(self)

                self.net.update_pos(self.ID,conv_vecarr(self.position),self.sim.now())
                p=conv_vecarr(self.position).reshape(3,1)
                v=conv_vecarr(self.velocity).reshape(3,1)
                a=conv_vecarr(self.acceleration).reshape(3,1)

                # fill panda dataframe 2D trajectory
                self.df = self.df.append(pd.DataFrame({'t':pd.Timestamp(self.sim.now(),unit='s'),
                'x':p[0],
                'y':p[1],
                'vx':v[0],
                'vy':v[1],
                'ax':a[0],
                'ay':a[1]},
                columns=['t','x','y','vx','vy','ax','ay']))

                if self.pdshow:
                    ptmp =np.array([p[:2,0],p[:2,0]+v[:2,0]])

                    if hasattr(self, 'pl'):
                        self.pl[0].set_data(self.df['x'].tail(1),self.df['y'].tail(1))
                        self.pla[0].set_data(ptmp[:,0],ptmp[:,1])
                    else :
                        self.pl = ax.plot(self.df['x'].tail(1),self.df['y'].tail(1),'o',color=self.color,ms=self.radius*10)
                        self.pla = ax.plot(ptmp[:,0],ptmp[:,1],'r')

                    # try:
                    #     fig,ax=plu.displot(p[:2],p[:2]+v[:2],'r')
                    # except:
                    #     pass
                    # import ipdb
                    # ipdb.set_trace()
                    plt.draw()
                    plt.pause(0.0001) 
                if 'mysql' in self.save:
                    self.db.writemeca(self.ID,self.sim.now(),p,v,a)

                if 'txt' in self.save:
                    pyu.writemeca(self.ID,self.sim.now(),p,v,a)

                # new target when arrived in poi

                if self.arrived and\
                    (self.L.pt2ro(self.position) ==\
                        self.L.Gw.node[self.rooms[1]]['room']):

                    self.arrived = False
                    if self.endpoint:
                        self.endpoint=False
                        self.roomId = self.nextroomId
                        # remove the remaining waypoint which correspond 
                        # to current room position
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                    #
                    # If door lets continue 
                    #
                    #
                    # ig destination --> next room
                    #
                    #adjroom  = self.L.Gr.neighbors(self.roomId)
                    #Nadjroom = len(adjroom)
                        if self.cdest == 'random':
                            # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                            self.nextroomId   = random.sample(self.L.Gr.nodes(),1)[0]
                            # test 1 ) next != actualroom
                            #      2 ) nextroom != fordiden room
                            #      3 ) room not share without another agent
                            while self.nextroomId == self.roomId or (self.nextroomId in self.forbidroomId):# or (self.nextroomId in self.sim.roomlist):
                                # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                                self.nextroomId   = random.sample(self.L.Gr.nodes(),1)[0]
                        elif self.cdest == 'file':
                           self.room_counter=self.room_counter+1
                           if self.room_counter >= self.nb_room:
                                self.room_counter=0
                           self.nextroomId=self.room_seq[self.room_counter]
                           self.wait=self.room_wait[self.room_counter]
                        #self.sim.roomlist.append(self.nextroomId) # list of all destiantion of all nodes in object sim
                        self.rooms, wp =  self.L.waypointGw(self.roomId,self.nextroomId)
                        # self.dlist =  [i in self.L.Gw.ldo for i in self.rooms]
                        for tup in wp[1:]:
                            self.waypoints.append(vec3(tup)) 
                    #nextroom = adjroom[k]
                    #    print "room : ",self.roomId
                    #    print "nextroom : ",self.nextroomId
                    #p_nextroom = self.L.Gr.pos[self.nextroomId]
                    #setdoors1  = self.L.Gr.node[self.roomId]['doors']
                    #setdoors2  = self.L.Gr.node[nextroom]['doors']
                    #doorId     = np.intersect1d(setdoors1,setdoors2)[0]
                    #
                    # coord door
                    #
                    #unode = self.L.Gs.neighbors(doorId)    
                    #p1    = self.L.Gs.pos[unode[0]]
                    #p2    = self.L.Gs.pos[unode[1]]
                    #print p1
                    #print p2
                    #pdoor = (np.array(p1)+np.array(p2))/2
                        self.destination = self.waypoints[0]
                    
                        if self.sim.verbose:
                            print 'meca: ag ' + self.ID + ' wait ' + str(self.wait)#*self.interval) 
                        yield hold, self, self.wait

                    else:
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                    #print "wp : ", self.waypoints
                        if len(self.waypoints)==1:
                            self.endpoint=True
                        self.destination = self.waypoints[0]
                    #print "dest : ", self.destination
                else:
                    yield hold, self, self.interval
            else:
#                self.update()
                self.world.update_boid(self)
                self.net.update_pos(self.ID,conv_vecarr(self.position),self.sim.now())

                yield hold, self, self.interval
Example #2
0
    def move(self):
        """
            Move the Agent
        """

        while True:
            if self.moving:
                if self.ID == 0:
                    print 'meca update @', self.sim.now()

                while self.cancelled:
                    yield passivate, self
                    print "Person.move: activated after being cancelled"
                checked = []
                for zone in self.world.zones(self):
                    if zone not in checked:
                        checked.append(zone)
                        zone(self)
                acceleration = self.steering_mind(self)
                acceleration = acceleration.truncate(self.max_acceleration)
                self.acceleration = acceleration
                velocity = self.velocity + acceleration * self.interval
                self.velocity = velocity.truncate(self.max_speed)
                if velocity.length() > 0.2:
                    # record direction only when we've really had some
                    self.localy = velocity.normalize()
                    self.localx = vec3(self.localy.y, -self.localy.x)

                self.position = self.position + self.velocity * self.interval
                #                self.update()
                self.world.update_boid(self)

                self.net.update_pos(self.ID, conv_vecarr(self.position))
                if len(self.save) != 0:
                    p = conv_vecarr(self.position)
                    v = conv_vecarr(self.velocity)
                    a = conv_vecarr(self.acceleration)
                if 'mysql' in self.save:
                    self.db.writemeca(self.ID, self.sim.now(), p, v, a)
                if 'txt' in self.save:
                    pyu.writemeca(self.ID, self.sim.now(), p, v, a)
                if self.arrived:

                    self.arrived = False
                    if self.endpoint:
                        self.endpoint = False
                        #pr = self.sim.roomlist.index(self.nextroomId)
                        #self.sim.roomlist.pop(pr)
                        self.roomId = self.nextroomId

                        #
                        # Si porte on continue
                        #
                        #
                        # ig destination --> next room
                        #
                        #adjroom  = self.L.Gr.neighbors(self.roomId)
                        #Nadjroom = len(adjroom)
                        if self.cdest == 'random':
                            self.nextroomId = int(
                                np.floor(uniform(0, self.L.Gr.size())))
                            # test 1 ) next != actualroom
                            #      2 ) nextroom != fordiden room
                            #      3 ) room not share without another agent
                            while self.nextroomId == self.roomId or (
                                    self.nextroomId in self.forbidroomId
                            ):  # or (self.nextroomId in self.sim.roomlist):
                                self.nextroomId = int(
                                    np.floor(uniform(0, self.L.Gr.size())))
                        elif self.cdest == 'file':
                            self.room_counter = self.room_counter + 1
                            if self.room_counter >= self.nb_room:
                                self.room_counter = 0
                            self.nextroomId = self.room_seq[self.room_counter]
                            self.wait = self.room_wait[self.room_counter]
                        #self.sim.roomlist.append(self.nextroomId) # list of all destiantion of all nodes in object sim
                        wp = self.L.waypointGw(self.roomId, self.nextroomId)
                        for tup in wp[1:]:
                            self.waypoints.append(vec3(tup))
                    #nextroom = adjroom[k]
                    #    print "room : ",self.roomId
                    #    print "nextroom : ",self.nextroomId
                    #p_nextroom = self.L.Gr.pos[self.nextroomId]
                    #setdoors1  = self.L.Gr.node[self.roomId]['doors']
                    #setdoors2  = self.L.Gr.node[nextroom]['doors']
                    #doorId     = np.intersect1d(setdoors1,setdoors2)[0]
                    #
                    # coord door
                    #
                    #unode = self.L.Gs.neighbors(doorId)
                    #p1    = self.L.Gs.pos[unode[0]]
                    #p2    = self.L.Gs.pos[unode[1]]
                    #print p1
                    #print p2
                    #pdoor = (np.array(p1)+np.array(p2))/2
                        self.destination = self.waypoints[0]
                        #waittime = uniform(0,10)

                        #if self.manager:
                        #    if self.manager(self, *self.manager_args):
                        #    yield hold , self , waittime
                        #else:
                        #    yield hold, self , waittime

                        #                        self.wait=abs(gauss(50,50))
                        #                        self.wait=abs(gauss(1,1))
                        print 'wait', self.wait * self.interval
                        yield hold, self, self.wait

                    else:
                        del self.waypoints[0]
                        #print "wp : ", self.waypoints
                        if len(self.waypoints) == 1:
                            self.endpoint = True
                        self.destination = self.waypoints[0]
                    #print "dest : ", self.destination
                else:
                    yield hold, self, self.interval
            else:
                #                self.update()
                self.world.update_boid(self)
                self.net.update_pos(self.ID, conv_vecarr(self.position))

                yield hold, self, self.interval
Example #3
0
    def move(self):
        """ Move the Agent

        """

        while True:
            if self.moving:
                if self.ID == 0:
                    print 'meca update @',self.sim.now()

                while self.cancelled:
                    yield passivate, self
                    print "Person.move: activated after being cancelled" 
                checked = []
                for zone in self.world.zones(self):
                    if zone not in checked:
                        checked.append(zone)
                        zone(self)

                acceleration = self.steering_mind(self) 
                acceleration = acceleration.truncate(self.max_acceleration)

                self.acceleration = acceleration 
                velocity = self.velocity + acceleration * self.interval
                self.velocity = velocity.truncate(self.max_speed) 

                if velocity.length() > 0.2:
                    # record direction only when we've really had some
                    self.localy = velocity.normalize()
                    self.localx = vec3(self.localy.y, -self.localy.x)

                self.position = self.position + self.velocity * self.interval
#                self.update()
                self.world.update_boid(self)

                self.net.update_pos(self.ID,conv_vecarr(self.position),self.sim.now())
                if len(self.save)!=0:
                    p=conv_vecarr(self.position)
                    v=conv_vecarr(self.velocity)
                    a=conv_vecarr(self.acceleration)
                if 'mysql' in self.save:
                    self.db.writemeca(self.ID,self.sim.now(),p,v,a)
                if 'txt' in self.save:
                    pyu.writemeca(self.ID,self.sim.now(),p,v,a)
                if self.arrived:
                    self.arrived = False
                    if self.endpoint:
                        self.endpoint=False
                        #pr = self.sim.roomlist.index(self.nextroomId)
                        #self.sim.roomlist.pop(pr)
                        self.roomId = self.nextroomId
                    #
                    # Si porte on continue
                    #
                    #
                    # ig destination --> next room
                    #
                    #adjroom  = self.L.Gr.neighbors(self.roomId)
                    #Nadjroom = len(adjroom)
                        if self.cdest == 'random':
                            # self.nextroomId   = int(np.floor(uniform(0,self.L.Gr.size())))
                            self.nextroomId   = sample(self.L.Gr.nodes(),1)[0]
                            # test 1 ) next != actualroom
                            #      2 ) nextroom != fordiden room
                            #      3 ) room not share without another agent
                            while self.nextroomId == self.roomId or (self.nextroomId in self.forbidroomId):# or (self.nextroomId in self.sim.roomlist):
                                # self.nextroomId   = int(np.floor(uniform(0,self.L.Gr.size())))
                                self.nextroomId   = sample(self.L.Gr.nodes(),1)[0]
                        elif self.cdest == 'file':
                           self.room_counter=self.room_counter+1
                           if self.room_counter >= self.nb_room:
                                self.room_counter=0
                           self.nextroomId=self.room_seq[self.room_counter]
                           self.wait=self.room_wait[self.room_counter]
                        #self.sim.roomlist.append(self.nextroomId) # list of all destiantion of all nodes in object sim
                        wp        =  self.L.waypointGw(self.roomId,self.nextroomId)
                        for tup in wp[1:]:
                            self.waypoints.append(vec3(tup)  ) 
                    #nextroom = adjroom[k]
                    #    print "room : ",self.roomId
                    #    print "nextroom : ",self.nextroomId
                    #p_nextroom = self.L.Gr.pos[self.nextroomId]
                    #setdoors1  = self.L.Gr.node[self.roomId]['doors']
                    #setdoors2  = self.L.Gr.node[nextroom]['doors']
                    #doorId     = np.intersect1d(setdoors1,setdoors2)[0]
                    #
                    # coord door
                    #
                    #unode = self.L.Gs.neighbors(doorId)    
                    #p1    = self.L.Gs.pos[unode[0]]
                    #p2    = self.L.Gs.pos[unode[1]]
                    #print p1
                    #print p2
                    #pdoor = (np.array(p1)+np.array(p2))/2
                        self.destination = self.waypoints[0]
                    #waittime = uniform(0,10)

                    #if self.manager:
                    #    if self.manager(self, *self.manager_args):
                    #    yield hold , self , waittime
                    #else:
                    #    yield hold, self , waittime 

#                        self.wait=abs(gauss(50,50))
#                        self.wait=abs(gauss(1,1))
                        print 'wait',self.wait*self.interval    
                        yield hold, self, self.wait 

                    else:    
                        del self.waypoints[0]
                    #print "wp : ", self.waypoints
                        if len(self.waypoints)==1:
                            self.endpoint=True
                        self.destination = self.waypoints[0]
                    #print "dest : ", self.destination    
                else:
                    yield hold, self, self.interval
            else:
#                self.update()
                self.world.update_boid(self)
                self.net.update_pos(self.ID,conv_vecarr(self.position),self.sim.now())

                yield hold, self, self.interval
Example #4
0
    def move(self):
        """ Move the Person

        """
        if self.pdshow:
            fig = plt.gcf()
            fig, ax = self.L.showG('w',
                                   labels=False,
                                   alphacy=0.,
                                   edges=False,
                                   fig=fig)
            plt.draw()
            plt.ion()
        while True:
            if self.moving:
                if self.sim.verbose:
                    print 'meca: updt ag ' + self.ID + ' @ ', self.sim.now()

                # if np.allclose(conv_vecarr(self.destination)[:2],self.L.Gw.pos[47]):
                #     import ipdb
                #     ipdb.set_trace()

                while self.cancelled:
                    yield passivate, self
                    print "Person.move: activated after being cancelled"
                checked = []
                for zone in self.world.zones(self):
                    if zone not in checked:
                        checked.append(zone)
                        zone(self)

                # updating acceleration
                acceleration = self.steering_mind(self)
                acceleration = acceleration.truncate(self.max_acceleration)
                self.acceleration = acceleration

                # updating velocity
                velocity = self.velocity + acceleration * self.interval
                self.velocity = velocity.truncate(self.max_speed)

                if velocity.length() > 0.2:
                    # record direction only when we've really had some
                    self.localy = velocity.normalize()
                    self.localx = vec3(self.localy.y, -self.localy.x)

                # updating position
                self.position = self.position + self.velocity * self.interval
                #                self.update()
                self.position.z = 0
                self.world.update_boid(self)

                self.net.update_pos(self.ID, conv_vecarr(self.position),
                                    self.sim.now())
                p = conv_vecarr(self.position).reshape(3, 1)
                v = conv_vecarr(self.velocity).reshape(3, 1)
                a = conv_vecarr(self.acceleration).reshape(3, 1)

                # fill panda dataframe 2D trajectory
                self.df = self.df.append(
                    pd.DataFrame(
                        {
                            't': pd.Timestamp(self.sim.now(), unit='s'),
                            'x': p[0],
                            'y': p[1],
                            'vx': v[0],
                            'vy': v[1],
                            'ax': a[0],
                            'ay': a[1]
                        },
                        columns=['t', 'x', 'y', 'vx', 'vy', 'ax', 'ay']))

                if self.pdshow:
                    ptmp = np.array([p[:2, 0], p[:2, 0] + v[:2, 0]])

                    if hasattr(self, 'pl'):
                        self.pl[0].set_data(self.df['x'].tail(1),
                                            self.df['y'].tail(1))
                        self.pla[0].set_data(ptmp[:, 0], ptmp[:, 1])
                        circle = plt.Circle(
                            (self.df['x'].tail(1), self.df['y'].tail(1)),
                            radius=self.radius,
                            alpha=0.3)
                        ax.add_patch(circle)
                    else:
                        self.pl = ax.plot(self.df['x'].tail(1),
                                          self.df['y'].tail(1),
                                          'o',
                                          color=self.color,
                                          ms=self.radius * 10)
                        self.pla = ax.plot(ptmp[:, 0], ptmp[:, 1], 'r')
                        circle = plt.Circle(
                            (self.df['x'].tail(1), self.df['y'].tail(1)),
                            radius=self.radius,
                            alpha=0.3)
                        ax.add_patch(circle)
                    # try:
                    #     fig,ax=plu.displot(p[:2],p[:2]+v[:2],'r')
                    # except:
                    #     pass
                    # import ipdb
                    # ipdb.set_trace()
                    plt.draw()
                    plt.pause(0.0001)
                if 'mysql' in self.save:
                    self.db.writemeca(self.ID, self.sim.now(), p, v, a)

                if 'txt' in self.save:
                    pyu.writemeca(self.ID, self.sim.now(), p, v, a)

                # new target when arrived in poi

                if self.arrived and\
                    (self.L.pt2ro(self.position) ==\
                        self.L.Gw.node[self.rooms[1]]['room']):

                    self.arrived = False
                    if self.endpoint:
                        self.endpoint = False
                        self.roomId = self.nextroomId
                        # remove the remaining waypoint which correspond
                        # to current room position
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                        #
                        # If door lets continue
                        #
                        #
                        # ig destination --> next room
                        #
                        #adjroom  = self.L.Gr.neighbors(self.roomId)
                        #Nadjroom = len(adjroom)
                        if self.cdest == 'random':
                            # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                            self.nextroomId = random.sample(
                                self.L.Gr.nodes(), 1)[0]
                            # test 1 ) next != actualroom
                            #      2 ) nextroom != fordiden room
                            #      3 ) room not share without another agent
                            while self.nextroomId == self.roomId or (
                                    self.nextroomId in self.forbidroomId
                            ):  # or (self.nextroomId in self.sim.roomlist):
                                # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                                self.nextroomId = random.sample(
                                    self.L.Gr.nodes(), 1)[0]
                        elif self.cdest == 'file':
                            self.room_counter = self.room_counter + 1
                            if self.room_counter >= self.nb_room:
                                self.room_counter = 0
                            self.nextroomId = self.room_seq[self.room_counter]
                            self.wait = self.room_wait[self.room_counter]
                        #self.sim.roomlist.append(self.nextroomId) # list of all destiantion of all nodes in object sim
                        self.rooms, wp = self.L.waypointGw(
                            self.roomId, self.nextroomId)
                        # self.dlist =  [i in self.L.Gw.ldo for i in self.rooms]
                        for tup in wp[1:]:
                            self.waypoints.append(vec3(tup))
                    #nextroom = adjroom[k]
                    #    print "room : ",self.roomId
                    #    print "nextroom : ",self.nextroomId
                    #p_nextroom = self.L.Gr.pos[self.nextroomId]
                    #setdoors1  = self.L.Gr.node[self.roomId]['doors']
                    #setdoors2  = self.L.Gr.node[nextroom]['doors']
                    #doorId     = np.intersect1d(setdoors1,setdoors2)[0]
                    #
                    # coord door
                    #
                    #unode = self.L.Gs.neighbors(doorId)
                    #p1    = self.L.Gs.pos[unode[0]]
                    #p2    = self.L.Gs.pos[unode[1]]
                    #print p1
                    #print p2
                    #pdoor = (np.array(p1)+np.array(p2))/2
                        self.destination = self.waypoints[0]

                        if self.sim.verbose:
                            print 'meca: ag ' + self.ID + ' wait ' + str(
                                self.wait)  #*self.interval)
                        yield hold, self, self.wait

                    else:
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                        #print "wp : ", self.waypoints
                        if len(self.waypoints) == 1:
                            self.endpoint = True
                        self.destination = self.waypoints[0]
                    #print "dest : ", self.destination
                else:
                    yield hold, self, self.interval
            else:
                #                self.update()
                self.world.update_boid(self)
                self.net.update_pos(self.ID, conv_vecarr(self.position),
                                    self.sim.now())

                yield hold, self, self.interval