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 = [] 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()) 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 _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()