如何为Poisson到达生成器的客户添加随机位置属性?
Got it, let's integrate the random position generation into your existing code. Here's a straightforward approach that includes defining a Customer class to hold arrival details (including coordinates) and modifying the arrival generator to create these customer instances with randomized positions.
Step 1: Define a Customer Class
First, let's create a simple Customer class to encapsulate all relevant data for each arriving customer—this includes their ID, arrival time, and (x,y) coordinates. If you already have a Customer class, you can just add the x and y attributes to it.
class Customer: def __init__(self, customer_id, arrival_time, x, y): self.id = customer_id self.arrival_time = arrival_time # Random position coordinates self.x = x self.y = y
Step 2: Modify the Arrival Class
Next, update your Arrival class to generate the random (x,y) values for each customer. We'll use scipy's uniform distribution to get values in your specified ranges (x: 0-50, y: 0-60) each time a customer arrives. We'll also pass the completed customer object to your referrer (assuming it has a method to handle incoming customers, like process_customer).
Here's the updated Arrival class:
import scipy.stats as spstats class Arrival(object): def __init__(self, stream_defin, referrer): self.service = stream_defin["service"] self.arrival_rate = stream_defin["rate"] self.referrer = referrer # Define position ranges (you can make these configurable via stream_defin if needed) self.x_min = 0 self.x_max = 50 self.y_min = 0 self.y_max = 60 def sim_init(self, env, sim_stats): self.sim_stats = sim_stats self.env = env self.env.process(self.sim_arrival_generator()) def sim_arrival_generator(self): i = 0 while True: # Generate inter-arrival time (Poisson arrival process) interarrival = spstats.expon(scale=1/self.arrival_rate).rvs() yield self.env.timeout(interarrival) # Generate random (x,y) coordinates x = spstats.uniform(loc=self.x_min, scale=self.x_max - self.x_min).rvs() y = spstats.uniform(loc=self.y_min, scale=self.y_max - self.y_min).rvs() # Create customer instance with arrival details customer = Customer( customer_id=i, arrival_time=self.env.now, x=x, y=y ) # Pass the customer to the referrer (adjust method name if your referrer uses something else) self.referrer.process_customer(customer) i += 1
Key Explanations:
- Position Generation: We use
spstats.uniformto generate values in your desired ranges. Thelocparameter sets the minimum value, andscalesets the range (max - min). For x, that's0to50, soscale=50; for y,0to60, soscale=60. - Customer Encapsulation: The
Customerclass keeps all customer-related data organized, making it easy to pass to other parts of your simulation (like thereferrer). - Configurable Ranges: If you want to make the x/y ranges configurable per arrival stream, you can add them to your
stream_defindictionary and initialize them in__init__(e.g.,self.x_max = stream_defin["x_range"]).
Just make sure your referrer class has a process_customer method (or whatever method name you use) to receive and handle the customer object with its coordinates.
内容的提问来源于stack exchange,提问作者shirin elahi

