You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Poisson到达生成器的客户添加随机位置属性?

Adding Random (x,y) Coordinates to Your Poisson Arrival Generator

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.uniform to generate values in your desired ranges. The loc parameter sets the minimum value, and scale sets the range (max - min). For x, that's 0 to 50, so scale=50; for y, 0 to 60, so scale=60.
  • Customer Encapsulation: The Customer class keeps all customer-related data organized, making it easy to pass to other parts of your simulation (like the referrer).
  • Configurable Ranges: If you want to make the x/y ranges configurable per arrival stream, you can add them to your stream_defin dictionary 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:02:42