def test_corridor(): """ Environment: One direction corridor Pedestrians: walking two ways, but variables randomly set the initial setted pedestrians will still be walking around """ print "Simple Corridor Test Case" num_ped = random.randint(0, 10) print "number of pedestrians: ", num_ped screen_size = (800, 600) sim_env = sim.Sim('Pedestrian Simulation', screen_size) """ adding obstacles """ param = {'start': np.array([0, 100]), 'end': np.array([800, 100])} sim_env.add_object('obs', param) param = {'start': np.array([0, 500]), 'end': np.array([800, 500])} sim_env.add_object('obs', param) """ adding pedestrians """ def dist_obstacle(s): """ calculate the closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array( [0., random.uniform(500, 1000) * random.choice([1, -1])]) for pid in range(num_ped): right_direction = random.randint(0, 1) v0 = random.uniform(60., 130.) vmax = 1.3 * v0 theta_v = random.uniform(-np.pi / 6, np.pi / 6) tau = random.uniform(0.4, 0.6) V_obs = random.uniform(20000., 30000.) R_obs = random.uniform(100., 500.) sight_angle = random.uniform(np.pi / 4, np.pi / 2) sight_const = random.uniform(0.4, 1.0) friend = np.array([]) V_others = np.array([]) R_others = np.array([]) for i in range(pid): friend = np.append(friend, random.choice([True, False])) V_others = np.append(V_others, random.uniform(5000., 100000.)) R_others = np.append(R_others, random.uniform(20., 50.)) if right_direction: init_s = np.array( [random.uniform(-300., -100.), random.uniform(130., 470.)]) exp_s = np.array([900., init_s[1]]) init_v = np.array([v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) else: init_s = np.array( [random.uniform(900., 1100.), random.uniform(130., 470.)]) exp_s = np.array([-100., init_s[1]]) init_v = np.array([-v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) param = { 'init_s': init_s, 'exp_s': exp_s, 'v0': v0, 'vmax': vmax, 'init_v': init_v, 'tau': tau, 'V_obs': V_obs, 'R_obs': R_obs, 'sight_angle': sight_angle, 'sight_const': sight_const, 'friend': friend, 'V_others': V_others, 'R_others': R_others, 'dist_obs_func': dist_obstacle, 'pid': pid } sim_env.add_object('ped', param) running = True while running: sim_env.clock_tick() sim_env.reset_screen() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for ped in sim_env.pedestrians: if is_arrival(ped, sim_env): pid = ped.pid sim_env.remove_pedestrians([pid]) if len(sim_env.pedestrians) == 0: running = False sim_env.move() for ped in sim_env.pedestrians: obstacle_constraint(ped) sim_env.display() pygame.display.flip()
def test_continuous_corridor(): """ Environment: One direction corridor Pedestrians: walking two ways, but variables randomly set there will continuous be pedestrians walking in and out """ print "Continous Corridor Test Case" num_ped = random.randint(1, 7) maximum_num_ped = 20 # there will be a pedestrian walking in every this amount of time time_walkin = [1., 2.5] print "Initial number of pedestrians: ", num_ped print "maximum capacity of pedestrians: ", maximum_num_ped screen_size = (800, 600) sim_env = sim.Sim('Pedestrian Simulation', screen_size) """ adding obstacles """ param = {'start': np.array([0, 100]), 'end': np.array([800, 100])} sim_env.add_object('obs', param) param = {'start': np.array([0, 500]), 'end': np.array([800, 500])} sim_env.add_object('obs', param) """ adding pedestrians """ def dist_obstacle(s): """ calculate the closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array( [0., random.uniform(500, 1000) * random.choice([1, -1])]) for pid in range(num_ped): right_direction = random.randint(0, 1) v0 = random.uniform(60., 130.) vmax = 1.3 * v0 theta_v = random.uniform(-np.pi / 6, np.pi / 6) tau = random.uniform(0.4, 0.6) V_obs = random.uniform(20000., 30000.) R_obs = random.uniform(100., 500.) sight_angle = random.uniform(np.pi / 4, np.pi / 2) sight_const = random.uniform(0.4, 1.0) friend = np.array([]) V_others = np.array([]) R_others = np.array([]) for i in range(pid): friend = np.append(friend, random.choice([True, False])) V_others = np.append(V_others, random.uniform(5000., 100000.)) R_others = np.append(R_others, random.uniform(20., 50.)) if right_direction: init_s = np.array( [random.uniform(-300., -100.), random.uniform(130., 470.)]) exp_s = np.array([900., init_s[1]]) init_v = np.array([v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) else: init_s = np.array( [random.uniform(900., 1100.), random.uniform(130., 470.)]) exp_s = np.array([-100., init_s[1]]) init_v = np.array([-v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) param = { 'init_s': init_s, 'exp_s': exp_s, 'v0': v0, 'vmax': vmax, 'init_v': init_v, 'tau': tau, 'V_obs': V_obs, 'R_obs': R_obs, 'sight_angle': sight_angle, 'sight_const': sight_const, 'friend': friend, 'V_others': V_others, 'R_others': R_others, 'dist_obs_func': dist_obstacle, 'pid': pid } sim_env.add_object('ped', param) # the next pedestrian id next_pid = num_ped last_walkin_time = time.time() running = True while running: sim_env.clock_tick() sim_env.reset_screen() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for ped in sim_env.pedestrians: if is_arrival(ped, sim_env): pid = ped.pid sim_env.remove_pedestrians([pid]) if len(sim_env.pedestrians) == 0: running = False sim_env.move() for ped in sim_env.pedestrians: obstacle_constraint(ped) # add a new pedestrian while some time passes if time.time() - last_walkin_time > random.uniform( time_walkin[0], time_walkin[1]): num_ped = len(sim_env.pedestrians) if num_ped < maximum_num_ped: # add a new pedestrian right_direction = random.randint(0, 1) v0 = random.uniform(60., 130.) vmax = 1.3 * v0 theta_v = random.uniform(-np.pi / 6, np.pi / 6) tau = random.uniform(0.4, 0.6) V_obs = random.uniform(20000., 30000.) R_obs = random.uniform(100., 500.) sight_angle = random.uniform(np.pi / 4, np.pi / 2) sight_const = random.uniform(0.4, 1.0) friend = np.array([]) V_others = np.array([]) R_others = np.array([]) for i in range(num_ped): friend = np.append(friend, random.choice([True, False])) V_others = np.append(V_others, random.uniform(5000., 100000.)) R_others = np.append(R_others, random.uniform(20., 50.)) if right_direction: init_s = np.array([ random.uniform(-300., -100.), random.uniform(130., 470.) ]) exp_s = np.array([900., init_s[1]]) init_v = np.array( [v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) else: init_s = np.array([ random.uniform(900., 1100.), random.uniform(130., 470.) ]) exp_s = np.array([-100., init_s[1]]) init_v = np.array( [-v0 * np.cos(theta_v), v0 * np.sin(theta_v)]) param = { 'init_s': init_s, 'exp_s': exp_s, 'v0': v0, 'vmax': vmax, 'init_v': init_v, 'tau': tau, 'V_obs': V_obs, 'R_obs': R_obs, 'sight_angle': sight_angle, 'sight_const': sight_const, 'friend': friend, 'V_others': V_others, 'R_others': R_others, 'dist_obs_func': dist_obstacle, 'pid': next_pid } next_pid += 1 last_walkin_time = time.time() sim_env.add_object('ped', param) print "new pedestrian join!" print "currently number of pedestrians: ", len( sim_env.pedestrians) sim_env.display() pygame.display.flip()
def test1(): """ test distance matrix calculation, party formulation variable, id record method, add_object method and remove_object method """ print "################TEST1##################" sim_env = sim.Sim('Pedestrian Simulation', (800, 600)) sim_env._get_dist_matrix() print "test1-1: no object dist_matrix calculation" print "should print out []:" print sim_env.PARTY_DIS print "" print "test1-2: no object party v calculation" print "should print out []" print sim_env.PARTY_V print "" print "test1-3: no object party r calculation" print "should print out []" print sim_env.PARTY_R print "" print "test1-4: no object friend id" print "should print out []" print sim_env.FRIEND print "" print "test1-5: pedestian id dictionary confirmation" print "should print out {}" print sim_env.PID print "" def dist_obstacle(s): """ calculate closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array([100000, 100000]) param = { 'init_s': np.array([100., 150.]), 'exp_s': np.array([900., 150.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([]), 'V_others': np.array([]), 'R_others': np.array([]), 'dist_obs_func': dist_obstacle, 'pid': 5 } sim_env.add_object('ped', param) sim_env._get_dist_matrix() print "test1-6: single object dist_matrix calculation" print "should print out [[[0., 0.]]]:" print sim_env.PARTY_DIS print "" print "test1-7: no object party v calculation" print "should print out [[0.]]" print sim_env.PARTY_V print "" print "test1-8: no object party r calculation" print "should print out [[0.]]" print sim_env.PARTY_R print "" print "test1-9: no object friend id" print "should print out [[True]]" print sim_env.FRIEND print "" print "test1-10: pedestian id dictionary confirmation" print "should print out {5:0}" print sim_env.PID print "" param = { 'init_s': np.array([130., 200.]), 'exp_s': np.array([900., 200.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([True]), 'V_others': np.array([100.]), 'R_others': np.array([50.]), 'dist_obs_func': dist_obstacle, 'pid': 3 } sim_env.add_object('ped', param) sim_env._get_dist_matrix() print "test1-11: double object dist_matrix calculation" print 'should print out [[[0., 0.], [30., 50.]], \n \ [[-30., -50.], [0., 0.]]]:' print sim_env.PARTY_DIS print "" print "test1-12: no object party v calculation" print "should print out [[0., 100.], [100., 0.]]" print sim_env.PARTY_V print "" print "test1-13: no object party r calculation" print "should print out [[0., 50.], [50., 0.]]" print sim_env.PARTY_R print "" print "test1-14: no object friend id" print "should print out [[True, True], [True, True]]" print sim_env.FRIEND print "" print "test1-15: pedestian id dictionary confirmation" print "should print out {5:0, 3:1}" print sim_env.PID print "" param = { 'init_s': np.array([200., 300.]), 'exp_s': np.array([900., 250.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([False, True]), 'V_others': np.array([200., 300.]), 'R_others': np.array([60., 70.]), 'dist_obs_func': dist_obstacle, 'pid': 1 } sim_env.add_object('ped', param) sim_env._get_dist_matrix() print "test1-16: triple object dist_matrix calculation" print 'should print out [[0.,0.],[30.,50.],[100., 150.]]\n \ [[-30.,-50.],[0.,0.],[70., 100.]]\n\ [[-100.,-150.],[-70.,-100.],[0.,0.]]' print sim_env.PARTY_DIS print "" print "test1-17: no object party v calculation" print "should print out [[0., 100., 200.], \n\ [100., 0., 300.],\n[200.,300.,0.]]" print sim_env.PARTY_V print "" print "test1-18: no object party r calculation" print "should print out [[0., 50., 60.], \n\ [50., 0., 70.], \n[60., 70., 0.]]" print sim_env.PARTY_R print "" print "test1-19: no object friend id" print "should print out [[True, True, False], \n\ [True, True, True], \n[False, True, True]]" print sim_env.FRIEND print "" print "test1-20: pedestian id dictionary confirmation" print "should print out {5:0, 3:1, 1:2}" print sim_env.PID print "" #test of remove function sim_env.remove_pedestrians([3]) sim_env._get_dist_matrix() print "test1-21: triple object dist_matrix calculation" print 'should print out [[0.,0.],[100., 150.]], [[-100.,-150.],[0.,0.]]' print sim_env.PARTY_DIS print "" print "test1-22: remove pedestrian function on party r result" print "should print out [[0., 60.],[60., 0.]]" print sim_env.PARTY_R print "" print "test1-23: remove pedestrian function on pedestian id" print "should print out {5:0, 1:1}" print sim_env.PID print "" print "test1-24: remove pedestrian function on pedestrian list" print "should print out 5 , 1" for ped in sim_env.pedestrians: print ped.pid sim_env.remove_pedestrians([1]) sim_env._get_dist_matrix() print "test1-25: triple object dist_matrix calculation" print 'should print out [[0.,0.]]' print sim_env.PARTY_DIS print "" print "test1-26: remove pedestrian function on party r result" print "should print out [[0.]]" print sim_env.PARTY_R print "" print "test1-27: remove pedestrian function on pedestian id" print "should print out {5:0}" print sim_env.PID print "" print "test1-28: remove pedestrian function on pedestrian list" print "should print out 5" for ped in sim_env.pedestrians: print ped.pid
def test4(): """ test auto terminates the file """ print "################TEST4##################" print "test4 test of custom auto terminate the file \n\ test of variables testing\n\ test of clock tick function and reset screen function" screen_size = (800, 600) sim_env = sim.Sim('Pedestrian Simulation', screen_size) """ adding obstacles """ param = {'start': np.array([100, 100]), 'end': np.array([700, 100])} sim_env.add_object('obs', param) param = {'start': np.array([100, 500]), 'end': np.array([700, 500])} sim_env.add_object('obs', param) """ adding pedestrians """ def dist_obstacle(s): """ calculate the closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array([100000, 100000]) param = { 'init_s': np.array([100., 150.]), 'exp_s': np.array([700., 150.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 20000., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([]), 'V_others': np.array([]), 'R_others': np.array([]), 'dist_obs_func': dist_obstacle, 'pid': 0 } sim_env.add_object('ped', param) param = { 'init_s': np.array([100., 200.]), 'exp_s': np.array([700., 200.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 20000., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([True]), 'V_others': np.array([10000.]), 'R_others': np.array([30.]), 'dist_obs_func': dist_obstacle, 'pid': 6 } sim_env.add_object('ped', param) def is_arrival(ped, sim_env): """ check if the pedestrian has accomplish his/her tasks Input: ped: Ped; the pedestrian we want to check sim_env: Sim; the simulation environment Return: is_arrival: boolean; is the pedestrian arrives its destination """ time_step = sim_env.TDIFF vmax = ped.vmax exp_s = ped.exp_s s = ped.s if np.linalg.norm(s - exp_s) < time_step * vmax * 5: return True else: return False running = True while running: sim_env.clock_tick() sim_env.reset_screen() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for ped in sim_env.pedestrians: if is_arrival(ped, sim_env): pid = ped.pid sim_env.remove_pedestrians([pid]) if len(sim_env.pedestrians) == 0: running = False sim_env.move() sim_env.display() pygame.display.flip()
def test3(): """ test simple run function """ print "################TEST3##################" sim_env = sim.Sim('Pedestrian Simulation', (800, 600)) """ adding obstacles """ param = {'start': np.array([100, 100]), 'end': np.array([700, 100])} sim_env.add_object('obs', param) param = {'start': np.array([100, 500]), 'end': np.array([700, 500])} sim_env.add_object('obs', param) """ adding pedestrians """ def dist_obstacle(s): """ calculate the closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array([100000, 100000]) param = { 'init_s': np.array([100., 150.]), 'exp_s': np.array([900., 150.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([]), 'V_others': np.array([]), 'R_others': np.array([]), 'dist_obs_func': dist_obstacle, 'pid': 0 } sim_env.add_object('ped', param) param = { 'init_s': np.array([100., 200.]), 'exp_s': np.array([900., 200.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([True]), 'V_others': np.array([100.]), 'R_others': np.array([50.]), 'dist_obs_func': dist_obstacle, 'pid': 6 } sim_env.add_object('ped', param) param = { 'init_s': np.array([100., 250.]), 'exp_s': np.array([900., 250.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([False, True]), 'V_others': np.array([100., 100.]), 'R_others': np.array([50., 50.]), 'dist_obs_func': dist_obstacle, 'pid': 4 } sim_env.add_object('ped', param) sim_env.run()
def test2(): """ test target force, obstace force, repulsive force, sight angle max velocity correctness """ print "################TEST2##################" sim_env = sim.Sim('Pedestrian Simulation', (800, 600)) """ adding obstacles """ param = {'start': np.array([100, 100]), 'end': np.array([700, 100])} sim_env.add_object('obs', param) """ adding pedestrians """ def dist_obstacle(s): """ calculate the closest vector distant from the ped to obstacle Input: s: np.1darray; the location of the pedestrian """ if s[1] - 100 < 500 - s[1]: return np.array([0., s[1] - 100.]) if s[1] - 100 > 500 - s[1]: return np.array([0., s[1] - 500.]) return np.array([100000, 100000]) param = { 'init_s': np.array([100., 150.]), 'exp_s': np.array([900., 150.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([]), 'V_others': np.array([]), 'R_others': np.array([]), 'dist_obs_func': dist_obstacle, 'pid': 0 } sim_env.add_object('ped', param) param = { 'init_s': np.array([100., 200.]), 'exp_s': np.array([900., 200.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([True]), 'V_others': np.array([100.]), 'R_others': np.array([50.]), 'dist_obs_func': dist_obstacle, 'pid': 6 } sim_env.add_object('ped', param) param = { 'init_s': np.array([100., 250.]), 'exp_s': np.array([900., 250.]), 'v0': 100, 'vmax': 130, 'init_v': np.array([100., 0.]), 'tau': 0.5, 'V_obs': 100., 'R_obs': 500., 'sight_angle': np.pi / 2, 'sight_const': 0.5, 'friend': np.array([False, True]), 'V_others': np.array([100., 100.]), 'R_others': np.array([50., 50.]), 'dist_obs_func': dist_obstacle, 'pid': 4 } sim_env.add_object('ped', param) print "test2-1: three pedestrians obstacle, repulsive force and maximum velocity check" obstacle_force_check(sim_env) repulsive_force_check(sim_env) maximum_velocity_check(sim_env) print "test2-2: two pedestrians obstacle, repulsive force and maximum velocity check" sim_env.remove_pedestrians([6]) obstacle_force_check(sim_env) repulsive_force_check(sim_env) maximum_velocity_check(sim_env) print "test2-2: one pedestrians obstacle, repulsive force and maximum velocity check" sim_env.remove_pedestrians([0]) obstacle_force_check(sim_env) maximum_velocity_check(sim_env)