def _draw(self): self._canvas("clear_screen") group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) == False and math.isnan(y) == False: r = force_model.group_pedestrian_a_property(i, "radius") #every pedestrian is equal, no more group id, thus we put 0 self._canvas("draw_pedestrian", x, y, r, 0) else: print("Position is unidentified") sys.exit() for s in self.parameters['start_areas']: self._canvas("draw_start_area", s) turning_draw = (20.0, 9.0, 50.0, 41.0) self._canvas("draw_start_area", turning_draw) for w in self.parameters['walls']: self._canvas("draw_wall", w) monitor = (-1000, self.monitor_point, 1000.0, self.monitor_point) self._canvas("draw_wall", monitor) #self._canvas("draw_target", self.parameters['targets'][0][0], self.parameters['targets'][0][1]) self._canvas("draw_text", "t = %.2f" % self.time) self.show_canvas.update()
def _draw(self): self._canvas("clear_screen") group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) == False and math.isnan(y) == False: r = force_model.group_pedestrian_a_property(i, "radius") ped_id = int( force_model.group_pedestrian_a_property(i, "ped_id")) is_tracked = self._canvas("is_tracked_pedestrian", x, y) if (is_tracked and constants.tracked_pedestrian_id == -1): self._canvas("reset_tracked_position") constants.tracked_pedestrian_id = int( force_model.group_pedestrian_a_property(i, "ped_id")) self.pedestrian_track._reset( self.time, constants.tracked_pedestrian_id) self.pedestrian_track._force_color('r.-', 'k.-') self.draw_tracked_ped() else: if constants.tracked_pedestrian_id == ped_id: #break in order to not depend on i self.draw_tracked_ped() else: self._canvas("draw_pedestrian", x, y, r, 0) else: print("Position is unidentified") sys.exit() for s in self.parameters['start_areas']: self._canvas("draw_start_area", s) #monitor = (self.monitor_point,-40,self.monitor_point,40.0) #self._canvas("draw_wall", monitor) for w in self.parameters['walls']: self._canvas("draw_wall", w) split_draw = (20.0 - self.shift, -25.0, 60.0 - self.shift, 25.0 ) #(60.0, -19.0, 80.0, 19.0) merge_draw = (100.0 - self.shift, -25.0, 140.0 - self.shift, 25.0 ) #(130.0, -19.0,150.0, 19.0) obstacle = (60.0 - self.shift, -2, 100.0 - self.shift, 2.0) #self._canvas("draw_start_area", split_draw) #self._canvas("draw_start_area", merge_draw) self._canvas("draw_obstacle", obstacle) monitor = (self.monitor_point, -40, self.monitor_point, 40.0) self._canvas("draw_wall", monitor) #self._canvas("draw_text", "t = %.2f" % self.time) self.show_canvas.update()
def _draw(self): self._canvas("clear_screen") group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) == False and math.isnan(y) == False: r = force_model.group_pedestrian_a_property(i, "radius") group_id = force_model.group_pedestrian_a_property( i, "groupid") self._canvas("draw_pedestrian", x, y, r, group_id) else: print("Position is unidentified") sys.exit() for s in self.parameters['start_areas']: self._canvas("draw_start_area", s) turning_draw = (-11.0, -54.0, 21.0, -24.0) self._canvas("draw_start_area", turning_draw) for w in self.parameters['walls']: self._canvas("draw_wall", w) monitor = (self.monitor_point, -400, self.monitor_point, 400.0) self._canvas("draw_wall", monitor) self._canvas("draw_text", "t = %.2f" % self.time) self.show_canvas.update()
def _dump_frame(self, context_index, current_frame): population_number = int(force_model.get_population_size()) pedestrians_frame = [] for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") r = force_model.group_pedestrian_a_property(i, "radius") group_id = force_model.group_pedestrian_a_property(i, "groupid") ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) (target_x, target_y) = force_model.group_pedestrian_a_property(i, "target") pedestrians_frame.append( dict( pedestrian_id=ped_id, # group_id=group_id, # radius=r, # position=(x, y), # in_group_a_strength=self. parameters['in_group_a_strength'], # in_group_a_range=self.parameters['in_group_a_range'], # in_group_r_strength=self. parameters['in_group_r_strength'], # in_group_r_range=self.parameters['in_group_r_range'], out_group_a_strength=self. parameters['out_group_a_strength'], # out_group_a_range=self.parameters['out_group_a_range'], out_group_r_strength=self. parameters['out_group_r_strength'], out_group_r_range=self.parameters['out_group_r_range'], #target point target=(target_x, target_y), # target_a_strength=self.parameters['target_a_strength'], target_a_range=self.parameters['target_a_range'])) # frame_generator = frame_context(current_frame, pedestrians_frame) disp_level = self.parameters['out_group_r_strength'] / self.parameters[ 'in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(current_frame)) log_file = open( "%s.json" % os.path.join(constants.framecontext_dir, frame_filename), "w") json_obj = json.dumps(frame_generator, cls=FrameContextLog_Encoder) log_file.write(json_obj) log_file.close()
def _draw(self): self._canvas("clear_screen") group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): (x,y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) ==False and math.isnan(y)==False: r = force_model.group_pedestrian_a_property(i, "radius") ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) is_tracked = self._canvas("is_tracked_pedestrian", x,y) if (is_tracked and constants.tracked_pedestrian_id== -1) : self._canvas("reset_tracked_position") constants.tracked_pedestrian_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) self.pedestrian_track._reset(self.time, constants.tracked_pedestrian_id) self.pedestrian_track._force_color('r.-','k.-') self.draw_tracked_ped() else: if constants.tracked_pedestrian_id == ped_id : #break in order to not depend on i self.draw_tracked_ped() else: self._canvas("draw_pedestrian", x,y,r,0) else: print("Position is unidentified") sys.exit() for s in self.parameters['start_areas']: self._canvas("draw_start_area", s) #turning_draw = (20.0,9.0,50.0, 41.0) #self._canvas("draw_start_area", turning_draw) for w in self.parameters['walls']: self._canvas("draw_wall", w) monitor = (-1000,self.monitor_point,1000.0,self.monitor_point) self._canvas("draw_wall", monitor) #self._canvas("draw_target", self.parameters['targets'][0][0], self.parameters['targets'][0][1]) #self._canvas("draw_text", "t = %.2f" % self.time) self.show_canvas.update()
def _revise_target(self): population_number = int(force_model.get_population_size()) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if x >= self.x_vertical_line: data = dict(ped_index=int(i), target=self.target_final) force_model.target_changed(data)
def _plot_track_ped(self): if constants.tracked_pedestrian_id != -1: target_force = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "target_force") if target_force != -999.0: wall_force = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "wall_force") ingroup_force = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "ingroup_force") outgroup_force = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "outgroup_force") velocity_x = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "velocity_x") speed = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "velocity") (x, y) = force_model.group_pedestrian_id_property( constants.tracked_pedestrian_id, "position") current_position = int(round(x + self.shift)) # +30 #plot for force and velocity based on time #self.pedestrian_track._addsample(self.time , target_force, ingroup_force, outgroup_force, wall_force,velocity_x,current_position) self.pedestrian_track._addsample(self.time, target_force, ingroup_force, outgroup_force, wall_force, speed, current_position) data = [] group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) target_force = force_model.group_pedestrian_a_property( i, "target_force") ingroup_force = force_model.group_pedestrian_a_property( i, "ingroup_force") outgroup_force = force_model.group_pedestrian_a_property( i, "outgroup_force") wall_force = force_model.group_pedestrian_a_property( i, "wall_force") velocity_x = force_model.group_pedestrian_a_property( i, "velocity_x") speed = force_model.group_pedestrian_id_property(i, "velocity") (x, y) = force_model.group_pedestrian_a_property(i, "position") current_position = int(round(x + self.shift)) #+30 #data.append([ped_id,target_force,ingroup_force,outgroup_force,wall_force,velocity_x,current_position]) data.append([ ped_id, target_force, ingroup_force, outgroup_force, wall_force, speed, current_position ]) self.pedestrian_track._addsample2(self.time, data)
def _draw(self): self._canvas("clear_screen") group_population_number = int(force_model.get_population_size()) for i in range(group_population_number): (x,y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) ==False and math.isnan(y)==False: r = force_model.group_pedestrian_a_property(i, "radius") group_id = force_model.group_pedestrian_a_property(i, "groupid") self._canvas("draw_pedestrian", x,y,r,group_id) else: print("Position is unidentified") sys.exit() for s in self.parameters['start_areas']: self._canvas("draw_start_area", s) for w in self.parameters['walls']: self._canvas("draw_wall", w) #self._canvas("draw_target", self.target_upper[0], self.target_upper[1]) #self._canvas("draw_target", self.target_below[0], self.target_below[1]) split_draw = (20.0, -25.0, 60.0, 25.0)#(60.0, -19.0, 80.0, 19.0) merge_draw = (100.0, -25.0, 140.0, 25.0)#(130.0, -19.0,150.0, 19.0) obstacle = (60.0,-2,100.0, 2.0) self._canvas("draw_start_area", split_draw) self._canvas("draw_start_area", merge_draw) self._canvas("draw_obstacle", obstacle) monitor = (self.monitor_point,-40,self.monitor_point,40.0) self._canvas("draw_wall", monitor) self._canvas("draw_text", "t = %.2f" % self.time) self.show_canvas.update()
def _revise_target(self): population_number = int(force_model.get_population_size()) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") test_in_area = self.turning_area.contains_point((x, y)) if test_in_area or y < self.turning_up: data = dict(ped_index=int(i), target=self.target_final) force_model.target_changed(data) else: if y > self.turning_up: data = dict(ped_index=int(i), target=self.target_original) force_model.target_changed(data)
def _spawn_pedestrians(self, context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) ids_each_group = [] for i in range(population_number): ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) ids_each_group.append(ped_id) #we get from json file rather than add manually by code disp_level = self.parameters['out_group_r_strength'] / self.parameters[ 'in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file.read() currentframecontext = json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list = currentframecontext.get_pedestrian_list() additional_id_existed_ped = currentframecontext.get_additional_id_existed_ped( ) adaptivecontext_file.close() '''we also need to dump additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list = additional_id_existed_ped[str(ped_id)] if len(current_list) > 0: current_list = [int(item) for item in current_list] data = dict(ped_id=int(ped_id), additional_count=len(current_list), friend_zone=tuple(current_list)) force_model.add_additional_group_member(data) '''update position and target shift for pedestrian list ''' if pedestrian_list is not None and len(pedestrian_list) > 0: for pedestrian in pedestrian_list: position = (pedestrian['position'][0], pedestrian['position'][1] + self.shift) # del pedestrian['position'] pedestrian['position'] = position target = (pedestrian['target'][0], pedestrian['target'][1] + self.shift) # del pedestrian['target'] pedestrian['target'] = target force_model.add_group_pedestrian(pedestrian)
def _done(self): if self.time > self.simulation_duration: pedestrian_escaped = 0 # count pedestrians over the monitor point and printout the flow rate population_number = int(force_model.get_population_size()) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if x >= self.monitor_point: pedestrian_escaped += 1.0 print("flowrate " + str(pedestrian_escaped / self.simulation_duration)) return True return False
def _plot_sample(self): population_number = int(force_model.get_population_size()) velocity_trajectories = [[] for i in range(population_number)] #here we also measure the influential matrix of pedestrian in narrowing corridor influential_matrix = dict() crowd_info = [[] for i in range(2)] #since we have 2 groups #initialize the position and id of each pedestrian for ped in range(population_number): (x, y) = force_model.group_pedestrian_a_property(ped, "position") ped_id = int(force_model.group_pedestrian_a_property( ped, "ped_id")) group_id = int( force_model.group_pedestrian_a_property(ped, "groupid")) crowd_info[group_id].append([ped_id, x, y]) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") velocity_x = force_model.group_pedestrian_a_property( i, "velocity_x") if math.isnan(x) == False and math.isnan( y) == False and math.isnan(velocity_x) == False: #get ped_id ped_id = int( force_model.group_pedestrian_a_property(i, "ped_id")) velocity_trajectories[ped_id] = [x + 30, y, velocity_x] if x > 50.0 and x < 93: #we only find the most influential pedestrian in narrowing corridor group_id = int( force_model.group_pedestrian_a_property(i, "groupid")) closest_distance = 999 closest_ped = -1 #find the nearest pedestrian in the list for member in crowd_info[group_id]: if member[0] != ped_id: temp_distance = math.sqrt((x - member[1])**2 + (y - member[2])**2) if temp_distance < closest_distance: closest_distance = temp_distance closest_ped = member[0] influential_matrix[ped_id] = closest_ped self.plots._add_new_sample(int(self.time), velocity_trajectories, influential_matrix)
def _plot_sample(self): population_number = int(force_model.get_population_size()) effectiveness = [] turning_angle = [] #here we also measure the influential matrix of pedestrian in narrowing corridor influential_matrix = dict() crowd_info = dict() #initialize the position and id of each pedestrian for ped in range(population_number): (x, y) = force_model.group_pedestrian_a_property(ped, "position") ped_id = int(force_model.group_pedestrian_a_property( ped, "ped_id")) crowd_info[ped_id] = (x, y) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) == False and math.isnan(y) == False: test_in_area = self.turning_area.contains_point((x, y)) if test_in_area: (v_x, v_y) = force_model.group_pedestrian_a_property( i, "velocity_direction") (desired_x, desired_y) = force_model.group_pedestrian_a_property( i, "desired_direction") angle = self.angle_between((v_x, v_y), (desired_x, desired_y)) turning_angle.append(angle) effect = (desired_x * v_x) + (desired_y * v_y) effectiveness.append(effect) #we also find the closest group member of this pedestrian ped_id = int( force_model.group_pedestrian_a_property(i, "ped_id")) friend_zone = force_model.group_pedestrian_a_property( i, "group_list") friend_zone = friend_zone.split('-') friend_zone = [ int(friend_zone[index]) for index in range(len(friend_zone) - 1) ] #assume closest distance is the 1st friend in the list friend_coordinate = crowd_info[friend_zone[0]] closest_ped_distance = math.sqrt( (x - friend_coordinate[0])**2 + (y - friend_coordinate[1])**2) closest_ped = friend_zone[0] for member in friend_zone: friend_coordinate = crowd_info[member] temp_distance = math.sqrt( (x - friend_coordinate[0])**2 + (y - friend_coordinate[1])**2) if temp_distance < closest_ped_distance: closest_ped_distance = temp_distance closest_ped = member influential_matrix[ped_id] = closest_ped total_effective = 0 if len(effectiveness) > 0: total_effective = np.sum(effectiveness) total_effective /= len(effectiveness) self.plots._add_new_sample(turning_angle, total_effective, influential_matrix)
def _spawn_pedestrians(self, context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) current_pedestrian_position = [] group_num = [ int(constants.spawn_rate / 2), constants.spawn_rate - int(constants.spawn_rate / 2) ] ids_each_group = [] #we also need to know the current id of each group for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) ids_each_group.append(ped_id) current_pedestrian_position.append([x, y]) if self.spawn_new_pedestrians == True: context1 = adaptive_context(self.parameters) context1._generateContext(current_pedestrian_position, group_num, self.parameters['start_areas'][0]) context2 = adaptive_context(self.parameters) context2._generateContext(current_pedestrian_position, group_num, self.parameters['start_areas'][1]) population_generator1 = PopulationGenerator( self.parameters, self.parameters['in_group_a_strength'], self.parameters['in_group_a_range'], self.parameters['in_group_r_strength'], self.parameters['in_group_r_range'], self.parameters['out_group_a_strength'], self.parameters['out_group_a_range'], self.parameters['out_group_r_strength'], self.parameters['out_group_r_range'], self.parameters['target_a_strength'], self.parameters['target_a_range'], group_num) population_generator2 = PopulationGenerator( self.parameters, self.parameters['in_group_a_strength'], self.parameters['in_group_a_range'], self.parameters['in_group_r_strength'], self.parameters['in_group_r_range'], self.parameters['out_group_a_strength'], self.parameters['out_group_a_range'], self.parameters['out_group_r_strength'], self.parameters['out_group_r_range'], self.parameters['target_a_strength'], self.parameters['target_a_range'], group_num) radii_generator1 = context1._get_radii_generators() placement_generator1 = context1._get_placement_generators() radii_generator2 = context2._get_radii_generators() placement_generator2 = context2._get_placement_generators() pedestrian_current_id1 = population_generator1._generate_population( radii_generator1, placement_generator1, population_number) current_ped_generator1 = [ temp_id for temp_id in range(population_number, pedestrian_current_id1) ] pedestrian_current_id2 = population_generator2._generate_population( radii_generator2, placement_generator2, pedestrian_current_id1) current_ped_generator2 = [ temp_id for temp_id in range(pedestrian_current_id1, pedestrian_current_id2) ] additional_pedestrians1, additional_id_existed_ped1 = population_generator1._get_generated_group_pedestrians_population( ids_each_group + current_ped_generator2) additional_pedestrians2, additional_id_existed_ped2 = population_generator2._get_generated_group_pedestrians_population( ids_each_group + current_ped_generator1) additional_pedestrians = additional_pedestrians1 + additional_pedestrians2 '''we need to add additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list1 = additional_id_existed_ped1[ped_id] if len(current_list1) > 0: current_list1 = [int(item) for item in current_list1] data = dict(ped_id=ped_id, additional_count=len(current_list1), friend_zone=tuple(current_list1)) force_model.add_additional_group_member(data) current_list2 = additional_id_existed_ped2[ped_id] if len(current_list2) > 0: current_list2 = [int(item) for item in current_list2] data = dict(ped_id=ped_id, additional_count=len(current_list1), friend_zone=tuple(current_list1)) force_model.add_additional_group_member(data) '''we add new pedestrian into ''' if additional_pedestrians is not None and len( additional_pedestrians) > 0: for group_member in additional_pedestrians: force_model.add_group_pedestrian(group_member) #we then dump these context into folder temp_additional context '''dump log file by using frame''' disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] frame_generator1 = frame_context(str(self.frames), additional_pedestrians1, additional_id_existed_ped1) frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s_1.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(frame_generator1, cls=FrameContextLog_Encoder) log_file.write(json_obj) log_file.close() frame_generator2 = frame_context(str(self.frames), additional_pedestrians2, additional_id_existed_ped2) frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s_2.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(frame_generator2, cls=FrameContextLog_Encoder) log_file.write(json_obj) log_file.close() else: #we get from json file rather than add manually by code #the difference between two types is just group list, they should have the same placement #so if self.parameters['nearby_in_group'] = 1 we have to re-assign group id for them disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file1 = open( "%s_1.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file1.read() currentframecontext1 = json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list1 = currentframecontext1.get_pedestrian_list() additional_id_existed_ped1 = currentframecontext1.get_additional_id_existed_ped( ) adaptivecontext_file1.close() adaptivecontext_file2 = open( "%s_2.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file2.read() currentframecontext2 = json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list2 = currentframecontext2.get_pedestrian_list() additional_id_existed_ped2 = currentframecontext2.get_additional_id_existed_ped( ) adaptivecontext_file2.close() additional_pedestrians = pedestrian_list1 + pedestrian_list2 '''we need to add additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list1 = additional_id_existed_ped1[ped_id] if len(current_list1) > 0: current_list1 = [int(item) for item in current_list1] data = dict(ped_id=ped_id, additional_count=len(current_list1), friend_zone=tuple(current_list1)) force_model.add_additional_group_member(data) current_list2 = additional_id_existed_ped2[ped_id] if len(current_list2) > 0: current_list2 = [int(item) for item in current_list2] data = dict(ped_id=ped_id, additional_count=len(current_list1), friend_zone=tuple(current_list1)) force_model.add_additional_group_member(data) if additional_pedestrians is not None and len( additional_pedestrians) > 0: for group_member in additional_pedestrians: force_model.add_group_pedestrian(group_member)
def _spawn_pedestrians(self, context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) current_pedestrian_position = [] ids_each_group = [] #we also need to know the current id of each group for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) ids_each_group.append(ped_id) current_pedestrian_position.append([x, y]) if self.spawn_new_pedestrians == True: #we get new pedestrians group_num = [ int(constants.spawn_rate / 2), constants.spawn_rate - int(constants.spawn_rate / 2) ] context = adaptive_context(self.parameters) context._generateContext(current_pedestrian_position, group_num) population_generator = PopulationGenerator( self.parameters, self.parameters['in_group_a_strength'], self.parameters['in_group_a_range'], self.parameters['in_group_r_strength'], self.parameters['in_group_r_range'], self.parameters['out_group_a_strength'], self.parameters['out_group_a_range'], self.parameters['out_group_r_strength'], self.parameters['out_group_r_range'], self.parameters['target_a_strength'], self.parameters['target_a_range'], group_num) disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % ( str(self.parameters['in_group_r_strength']) + "_" + str(self.parameters['in_group_a_strength']) + "_" + str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file.read() currentframecontext = json.loads(json_str, cls=AdaptiveContextLog_Decoder) radii_generator = currentframecontext._get_radii_generators() placement_generator = currentframecontext._get_placement_generators( ) #radii_generator = context._get_radii_generators() #placement_generator= context._get_placement_generators() population_generator._generate_population(radii_generator, placement_generator, population_number) additional_pedestrians, additional_id_existed_ped = population_generator._get_generated_group_pedestrians_population( ids_each_group) '''we need to add additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list = additional_id_existed_ped[ped_id] if len(current_list) > 0: current_list = [int(item) for item in current_list] data = dict(ped_id=ped_id, additional_count=len(current_list), friend_zone=tuple(current_list)) force_model.add_additional_group_member(data) #print(ped_id, tuple(current_list)) '''we add new pedestrian into ''' if additional_pedestrians is not None and len( additional_pedestrians) > 0: for group_member in additional_pedestrians: '''should modify target position based on y''' (x, y) = group_member['position'] if y <= self.y_horizontal_line: del group_member['target'] group_member['target'] = self.target_upper else: del group_member['target'] group_member['target'] = self.target_below force_model.add_group_pedestrian(group_member) #we then dump these context into folder temp_additional context '''dump log file by using frame''' frame_generator = frame_context(str(self.frames), additional_pedestrians, additional_id_existed_ped) disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] #frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % ( str(self.parameters['in_group_r_strength']) + "_" + str(self.parameters['in_group_a_strength']) + "_" + str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(frame_generator, cls=FrameContextLog_Encoder) log_file.write(json_obj) log_file.close() else: #we get from json file rather than add manually by code #the difference between two types is just group list, they should have the same placement #so if self.parameters['nearby_in_group'] = 1 we have to re-assign group id for them disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] #frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % ( str(self.parameters['in_group_r_strength']) + "_" + str(self.parameters['in_group_a_strength']) + "_" + str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file.read() currentframecontext = json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list = currentframecontext.get_pedestrian_list() additional_id_existed_ped = currentframecontext.get_additional_id_existed_ped( ) adaptivecontext_file.close() '''we also need to dump additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list = additional_id_existed_ped[str(ped_id)] if len(current_list) > 0: current_list = [int(item) for item in current_list] data = dict(ped_id=int(ped_id), additional_count=len(current_list), friend_zone=tuple(current_list)) force_model.add_additional_group_member(data) if pedestrian_list is not None and len(pedestrian_list) > 0: for group_member in pedestrian_list: '''should modify target position based on y''' (x, y) = group_member['position'] if y <= self.y_horizontal_line: del group_member['target'] group_member['target'] = self.target_upper else: del group_member['target'] group_member['target'] = self.target_below force_model.add_group_pedestrian(group_member)
def _plot_sample(self): population_number = int(force_model.get_population_size()) effectiveness = [] turning_angle = [] #here we also measure the influential matrix of pedestrian in narrowing corridor influential_matrix = dict() crowd_info = [[] for i in range(2)] #since we have 2 groups #initialize the position and id of each pedestrian for ped in range(population_number): (x, y) = force_model.group_pedestrian_a_property(ped, "position") ped_id = int(force_model.group_pedestrian_a_property( ped, "ped_id")) group_id = int( force_model.group_pedestrian_a_property(ped, "groupid")) crowd_info[group_id].append([ped_id, x, y]) for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) == False and math.isnan(y) == False: test_in_area = self.turning_area.contains_point((x, y)) if test_in_area: (v_x, v_y) = force_model.group_pedestrian_a_property( i, "velocity_direction") #select vector from desired velocity direction (desired_x, desired_y) = force_model.group_pedestrian_a_property( i, "desired_direction") angle = self.angle_between((v_x, v_y), (desired_x, desired_y)) turning_angle.append(angle) effect = (desired_x * v_x) + (desired_y * v_y) effectiveness.append(effect) ped_id = int( force_model.group_pedestrian_a_property(i, "ped_id")) group_id = int( force_model.group_pedestrian_a_property(i, "groupid")) closest_distance = 999 closest_ped = -1 #find the nearest pedestrian in the list for member in crowd_info[group_id]: if member[0] != ped_id: temp_distance = math.sqrt((x - member[1])**2 + (y - member[2])**2) if temp_distance < closest_distance: closest_distance = temp_distance closest_ped = member[0] influential_matrix[ped_id] = closest_ped total_effective = 0 if len(effectiveness) > 0: total_effective = np.sum(effectiveness) total_effective /= len(effectiveness) self.plots._add_new_sample(turning_angle, total_effective, influential_matrix)
def _spawn_pedestrians(self,context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) current_pedestrian_position = [] group_num = [int(constants.spawn_rate/2), constants.spawn_rate - int(constants.spawn_rate/2)] for i in range(population_number): (x,y) = force_model.group_pedestrian_a_property(i, "position") current_pedestrian_position.append([x,y]) if self.spawn_new_pedestrians==True: context = adaptive_context(self.parameters) context._generateContext(current_pedestrian_position, group_num) else: #we get from json file rather than add manually by code disp_level = self.parameters['out_group_r_strength']/self.parameters['in_group_r_strength'] # frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) #frame_filename = "%s" % (str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % (str(self.parameters['in_group_r_strength']) + "_" + str(self.parameters['in_group_a_strength']) + "_" + str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file.read() context= json.loads(json_str, cls =AdaptiveContextLog_Decoder) adaptivecontext_file.close() population_generator = PopulationGenerator(self.parameters, self.parameters['in_group_a_strength'],self.parameters['in_group_a_range'], self.parameters['in_group_r_strength'],self.parameters['in_group_r_range'], self.parameters['out_group_a_strength'],self.parameters['out_group_a_range'], self.parameters['out_group_r_strength'],self.parameters['out_group_r_range'], self.parameters['target_a_strength'],self.parameters['target_a_range'],group_num) radii_generator = context._get_radii_generators() placement_generator= context._get_placement_generators() population_generator._generate_population(radii_generator,placement_generator,population_number) additional_pedestrians = population_generator._get_generated_group_pedestrians_population() if additional_pedestrians is not None and len(additional_pedestrians)>0: for group_member in additional_pedestrians: '''should modify target position based on y''' (x,y) = group_member['position'] if y <= self.y_horizontal_line: del group_member['target'] group_member['target'] = self.target_upper else: del group_member['target'] group_member['target'] = self.target_below force_model.add_group_pedestrian(group_member) if self.spawn_new_pedestrians==True: #we then dump these context into folder temp_additional context disp_level = self.parameters['out_group_r_strength']/self.parameters['in_group_r_strength'] #frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % (str(self.parameters['in_group_r_strength']) + "_" + str(self.parameters['in_group_a_strength']) + "_" + str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(context, cls=AdaptiveContextLog_Encoder) log_file.write(json_obj) log_file.close()
def _plot_sample(self): population_number = int(force_model.get_population_size()) influential_matrix_split_area = dict() influential_matrix_merge_area = dict() turning_angle_split_area = [] turning_angle_merge_area = [] effectiveness_split_area = [] effectiveness_merge_area = [] crowd_info = [[] for i in range(2)] #since we have 2 groups #initialize the position and id of each pedestrian for ped in range(population_number): (x,y) = force_model.group_pedestrian_a_property(ped, "position") ped_id = int(force_model.group_pedestrian_a_property(ped, "ped_id")) group_id = int(force_model.group_pedestrian_a_property(ped, "groupid")) crowd_info[group_id].append([ped_id,x,y]) for i in range(population_number): (x,y) = force_model.group_pedestrian_a_property(i, "position") if math.isnan(x) ==False and math.isnan(y) ==False: test_in_split_area = self.split_area.contains_point((x,y)) if test_in_split_area: (v_x,v_y) = force_model.group_pedestrian_a_property(i,"velocity_direction") (desired_x,desired_y) = force_model.group_pedestrian_a_property(i,"desired_direction") angle = self.angle_between((v_x,v_y),(desired_x,desired_y)) turning_angle_split_area.append(angle) effect =(desired_x* v_x ) + (desired_y * v_y) effectiveness_split_area.append(effect) #get ped_id ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) group_id = int(force_model.group_pedestrian_a_property(i, "groupid")) closest_distance = 999 closest_ped = -1 #find the nearest pedestrian in the list for member in crowd_info[group_id]: if member[0] != ped_id: temp_distance = math.sqrt((x-member[1])**2 + (y-member[2])**2) if temp_distance < closest_distance: closest_distance = temp_distance closest_ped = member[0] influential_matrix_split_area[ped_id] = closest_ped test_in_merge_area = self.merge_area.contains_point((x,y)) if test_in_merge_area: (v_x,v_y) = force_model.group_pedestrian_a_property(i,"velocity_direction") (desired_x,desired_y) = force_model.group_pedestrian_a_property(i,"desired_direction") angle = self.angle_between((v_x,v_y),(desired_x,desired_y)) turning_angle_merge_area.append(angle) effect =(desired_x* v_x ) + (desired_y * v_y) effectiveness_merge_area.append(effect) #get ped_id ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) group_id = int(force_model.group_pedestrian_a_property(i, "groupid")) closest_distance = 999 closest_ped = -1 #find the nearest pedestrian in the list for member in crowd_info[group_id]: if member[0] != ped_id: temp_distance = math.sqrt((x-member[1])**2 + (y-member[2])**2) if temp_distance < closest_distance: closest_distance = temp_distance closest_ped = member[0] influential_matrix_merge_area[ped_id] = closest_ped total_effective_split = 0 if len(effectiveness_split_area)>0: total_effective_split = np.sum(effectiveness_split_area) total_effective_split /=len(effectiveness_split_area) total_effective_merge = 0 if len(effectiveness_merge_area)>0: total_effective_merge = np.sum(effectiveness_merge_area) total_effective_merge /=len(effectiveness_merge_area) self.plots._add_new_sample(turning_angle_split_area,turning_angle_merge_area, total_effective_split,total_effective_merge, influential_matrix_split_area,influential_matrix_merge_area)
def _spawn_pedestrians(self, context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) current_pedestrian_position = [] group_num = [ int(constants.spawn_rate / 2), constants.spawn_rate - int(constants.spawn_rate / 2) ] for i in range(population_number): (x, y) = force_model.group_pedestrian_a_property(i, "position") current_pedestrian_position.append([x, y]) if self.spawn_new_pedestrians == True: context1 = adaptive_context(self.parameters) context1._generateContext(current_pedestrian_position, group_num, self.parameters['start_areas'][0]) context2 = adaptive_context(self.parameters) context2._generateContext(current_pedestrian_position, group_num, self.parameters['start_areas'][1]) else: #we get from json file rather than add manually by code disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] #frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) frame_filename = "%s" % (str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file1 = open( "%s_1.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file1.read() context1 = json.loads(json_str, cls=AdaptiveContextLog_Decoder) adaptivecontext_file1.close() adaptivecontext_file2 = open( "%s_2.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file2.read() context2 = json.loads(json_str, cls=AdaptiveContextLog_Decoder) adaptivecontext_file2.close() population_generator = PopulationGenerator( self.parameters, self.parameters['in_group_a_strength'], self.parameters['in_group_a_range'], self.parameters['in_group_r_strength'], self.parameters['in_group_r_range'], self.parameters['out_group_a_strength'], self.parameters['out_group_a_range'], self.parameters['out_group_r_strength'], self.parameters['out_group_r_range'], self.parameters['target_a_strength'], self.parameters['target_a_range'], group_num) radii_generator1 = context1._get_radii_generators() placement_generator1 = context1._get_placement_generators() pedestrian_current_id = population_generator._generate_population( radii_generator1, placement_generator1, population_number) additional_pedestrians1 = population_generator._get_generated_group_pedestrians_population( ) if additional_pedestrians1 is not None and len( additional_pedestrians1) > 0: for group_member in additional_pedestrians1: force_model.add_group_pedestrian(group_member) radii_generator2 = context2._get_radii_generators() placement_generator2 = context2._get_placement_generators() population_generator._generate_population(radii_generator2, placement_generator2, pedestrian_current_id) additional_pedestrians2 = population_generator._get_generated_group_pedestrians_population( ) if additional_pedestrians2 is not None and len( additional_pedestrians2) > 0: for group_member in additional_pedestrians2: force_model.add_group_pedestrian(group_member) if self.spawn_new_pedestrians == True: #we then dump these context into folder temp_additional context disp_level = self.parameters[ 'out_group_r_strength'] / self.parameters['in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s_1.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(context1, cls=AdaptiveContextLog_Encoder) log_file.write(json_obj) log_file.close() frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) log_file = open( "%s_2.json" % os.path.join(self.adaptive_context_dir, frame_filename), "w") json_obj = json.dumps(context2, cls=AdaptiveContextLog_Encoder) log_file.write(json_obj) log_file.close()
def _spawn_pedestrians(self,context_index): #re-change by spawn by file #first we get current position so that they are not overlap population_number = int(force_model.get_population_size()) ids_each_group = [] for i in range(population_number): ped_id = int(force_model.group_pedestrian_a_property(i, "ped_id")) ids_each_group.append(ped_id) #we get from json file rather than add manually by code disp_level = self.parameters['out_group_r_strength']/self.parameters['in_group_r_strength'] frame_filename = "%s" % (str(disp_level) + "_" + str(context_index) + "_" + str(int(self.frames))) adaptivecontext_file1 = open( "%s_1.json" % os.path.join(self.adaptive_context_dir,frame_filename)) json_str = adaptivecontext_file1.read() currentframecontext1= json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list1 = currentframecontext1.get_pedestrian_list() additional_id_existed_ped1 = currentframecontext1.get_additional_id_existed_ped() adaptivecontext_file1.close() adaptivecontext_file2 = open( "%s_2.json" % os.path.join(self.adaptive_context_dir, frame_filename)) json_str = adaptivecontext_file2.read() currentframecontext2= json.loads(json_str, cls=FrameContextLog_Decoder) pedestrian_list2 = currentframecontext2.get_pedestrian_list() additional_id_existed_ped2 = currentframecontext2.get_additional_id_existed_ped() adaptivecontext_file2.close() additional_pedestrians = pedestrian_list1 + pedestrian_list2 '''we need to add additional for existed ped here''' #we also need to update friend list for existed pedestrians for ped_id in ids_each_group: #we need to fetch that friend list for this pedestrian and update in model current_list1 = additional_id_existed_ped1[str(ped_id)] if len(current_list1)>0: current_list1 = [int(item) for item in current_list1] data = dict(ped_id=ped_id, additional_count = len(current_list1), friend_zone = tuple(current_list1)) force_model.add_additional_group_member(data) current_list2 = additional_id_existed_ped2[str(ped_id)] if len(current_list2)>0: current_list2 = [int(item) for item in current_list2] data = dict(ped_id=ped_id, additional_count = len(current_list1), friend_zone = tuple(current_list1)) force_model.add_additional_group_member(data) if additional_pedestrians is not None and len(additional_pedestrians)>0: for group_member in additional_pedestrians: position = (group_member['position'][0]-self.shift_x,group_member['position'][1]+self.shift_y)# del group_member['position'] group_member['position'] = position target = (group_member['target'][0]-self.shift_x,group_member['target'][1]+self.shift_y)# del group_member['target'] group_member['target'] = target force_model.add_group_pedestrian(group_member)