コード例 #1
0
ファイル: spawner.py プロジェクト: chriserik/ipasum
	def spawn(self, time, dt):
	
		''' get appropriate spawn rate '''
		current_hour = self.spawner.get_hour()
		spawn_rate = self.spawn_rate[current_hour]
		elapsed_hour_part = dt / 3600

		''' spawn or not '''
		if elapsed_hour_part*spawn_rate >= random.random():
			''' weighted random pick type '''
			type_dict_at_current_hour = [(key,value["spawn_percentage"][current_hour]) for key, value in self.actor_attributes.iteritems()]
			actor_type = wr.choice(type_dict_at_current_hour)

			''' get size '''
			size_choices = [(ix*0.5, el) for ix, el in enumerate(self.actor_attributes[actor_type]["size_distribution"])]
			size = wr.choice(size_choices)

			''' get target building (and nearest nodes to it):'''
			building_choices = [(building.geom, building.propabilities[actor_type]) for building in self.spawner.simulation.building_manager.buildings if building.propabilities[actor_type]]
			building = self.weighted_pick(building_choices) #needs float picking functionality
			nearest_nodes_to_building = [n["nid"] for n in self.spawner.simulation.spatial_db.nodes.find({"pos": {"$near": [building.x, building.y]}}).limit(2) if n["nid"] != self.node_id]
			target_node_id = random.choice(nearest_nodes_to_building)
			target_location = building

			''' get full path '''
			full_path = self.spawner.simulation.router.full_path(self.node_id, target_node_id)
			# if full path is empty (if the target node is the source node) do not spawn)
			if full_path == []:
				print "path is empty!"
				return False

			''' get parking time of actor '''
			park_time_choices = [(ix, el) for ix, el in enumerate(self.actor_attributes[actor_type]["parking_time"])]
			parking_time = wr.choice(park_time_choices)*3600 + random.randint(0,1800)

			''' specify actor params '''
			params = {
				"size": size,
				"color": self.actor_attributes[actor_type]["color"],
				"name": self.actor_attributes[actor_type]["name"],
				"forbidden_lot_types": self.actor_attributes[actor_type]["forbidden_lot_types"],
				"pos": self.node_pos,
				"rot": 0,
				"speed": 0,
				"target_speed": 10,
				"target_location": target_location,
				"source_node": self.node_id,
				"target_node": target_node_id,
				"parking_start_time": None,
				"parking_time": parking_time,
				"parking_size": size + 2 * random.random(),
				"max_search_time": random.gauss(600, 200),
				"search_tolerance": random.gauss(100, 50),
				"init_time": self.spawner.simulation.current_time, 
				"technology": wr.choice([(None, 80), ("reserve", 20)])
			}

			''' add actor to the actor manager object. maybe this could be done more elegantly '''
			actr = Actor(self.spawner.simulation.actor_manager, self.spawner.simulation.router, params)
			self.spawner.simulation.actor_manager.add(actr)
			self.spawned +=1

			''' non public parking options '''
			if actr.params["name"] == "residential":
				if random.random() < 0.3:
					actr.stats["result"] = "private_parking"
					actr.has_private_parking()

			elif actr.params["name"] == "shopping":
				if random.random() < 0.3:
					actr.stats["result"] = "parking_garage"
					actr.has_private_parking()

			elif actr.params["name"] == "visiting":
				if random.random() < 0.1:
					actr.stats["result"] = "private_parking"
					actr.has_private_parking()

			elif actr.params["name"] == "working":
				if random.random() < 0.8:
					actr.stats["result"] = "company_parking"
					actr.has_private_parking()