Java 生成到达和平均等待时间
在为公交线路创建模拟时,我有点困惑 问题是公共汽车在一段时间内从一个车站开到另一个车站。一旦从1号站到2号站,它将在2号站装载一些乘客 我的问题是,因为我想在2号站空闲时产生一些乘客(最后一辆公交车离开,直到新公交车到达),所以我考虑使用泊松过程,对到达时间和时间段使用lambda 在另一篇StackOverflow文章中,我找到了一个函数来计算给定平均值的泊松比,但是如何在其中引入时间约束呢Java 生成到达和平均等待时间,java,Java,在为公交线路创建模拟时,我有点困惑 问题是公共汽车在一段时间内从一个车站开到另一个车站。一旦从1号站到2号站,它将在2号站装载一些乘客 我的问题是,因为我想在2号站空闲时产生一些乘客(最后一辆公交车离开,直到新公交车到达),所以我考虑使用泊松过程,对到达时间和时间段使用lambda 在另一篇StackOverflow文章中,我找到了一个函数来计算给定平均值的泊松比,但是如何在其中引入时间约束呢 public static int getPoissonRandom(double mean) {
public static int getPoissonRandom(double mean) {
Random r = new Random();
double L = Math.exp(-mean);
int k = 0;
double p = 1.0;
do {
p = p * r.nextDouble();
k++;
} while (p > L);
return k - 1;
}
我这样问是因为这是离散事件模拟,我不可能每次都让我的模型回顾过去,所以对于每个车站,我都会存储上次出发时间。我还认为,一个好的质量指标是平均等待时间(乘客等待下一辆公交车的时间总和)为了模拟有多少人将在
车站2登上当前公交车,您需要定义两件事:
- p=每小时到达
车站2
的平均人数李>
- d=两辆公交车之间经过的时间(小时),即从最后一辆公交车离开到当前公交车离开的时间
现在的意思很简单:
mean = p*d
现在,您将计算getPoissonRandom(mean)
,这将为您提供上车人数
注意,函数不是确定性的;其思想是,它根据泊松分布给出的概率分布给出一个数字
示例
例如,假设p=5
(平均每小时5人),公交车之间的时间是d=0.5
(半小时)。如果我正确理解您的问题,您可以使用getPoissonRandom(2.5)
运行模拟:
import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.apache.commons.math3.distribution.PoissonDistribution;
public class PassengerArrivals {
private double T, lambda; // Time interval
private double waiting;
public PassengerArrivals(double lambda, double timeInterval) {
// TODO Auto-generated constructor stub
this.lambda = lambda;
this.T = timeInterval;
this.avg_waiting = 0;
// Generate the number of events that will occur
PoissonDistribution pd = new PoissonDistribution(this.lambda);
int n = pd.sample();
// Initialize an array to store the arrival times t_i, i = {1,2,..., n}.
double arrivals[] = new double[n];
// Generate the arrival times through a U[0,T]
UniformRealDistribution ud = new UniformRealDistribution(0, this.T);
for (int i = 0; i < n; ++i) {
arrivals[i] = ud.sample();
}
//The Waiting times between events will be independent and identically distributed exponentialy.
double sum_waiting_time = 0;
for (int i = 0; i < n; ++i) {
// For me, the waiting time is the time the arrival of passenger occurred and he will wait until the time interval will be done.
sum_waiting_time += timeInterval - arrivals[i];
}
// Sometimes I got a NaN, possibly because my lambda was small so no events were generated. So it doesn't hurt to check :P
if (Double.isNaN(waiting_time)) {
sum_waiting_time = 0;
}
this.avg_waiting = sum_waiting_time / n;
}
/**
*
* @return
*/
public double getAvgWaitingTime() {
return this.waiting;
}
import org.apache.commons.math3.distribution.uniformrealdribution;
导入org.apache.commons.math3.distribution.PoissonDistribution;
公务舱乘客{
私有双T,lambda;//时间间隔
私人双重等待;
公共乘客(双λ,双时间间隔){
//TODO自动生成的构造函数存根
this.lambda=lambda;
T=时间间隔;
这是平均等待时间=0;
//生成将发生的事件数
泊松分布pd=新的泊松分布(this.lambda);
int n=pd.sample();
//初始化数组以存储到达时间t_i,i={1,2,…,n}。
双倍到达[]=新双倍[n];
//通过U[0,T]生成到达时间
UniformRealDistribution ud=新的UniformRealDistribution(0,this.T);
对于(int i=0;i
但是,我如何计算这种方法的平均等待时间?