我正在尝试构建一个包含三个主要事件的流量模拟器:
程序应根据用户输入输出事件组合......但由于某种原因,它无法正常工作。输出会反复喷出一个事件(例如,到达事件),或者只有两个事件,而不是根据用户输入的几个事件的组合,如此;
arriveeast
arriveeast
arrivenorth
lighteast
等等......
我想知道是否有人会如此善意地指出我忽略的是什么?我对此比较陌生,而且我很难找到人们反省的想法。我认为循环失败,但我不知道如何/在哪里,我没有得到错误消息......
using System;
using System.Collections;
namespace sim
{
public static class Simulation
{
public static void Main()
{
double average; // average of all trials
double interArrivalEast, interArrivalNorth; // Arrival times between cars coming from East and North
double onTimeEast, onTimeNorth, onTimeBoth; // Green light on times for East and North lights as well as both lights
double lastLeaveEast, lastLeaveNorth; // Last time a car left from the East and North
double totalTime; // Total time for all events to occur
double sum = 0;
double u; // Uniformly distributed random variable
int numEvents; // Total number of events possible
Event temp = new Event("", 0);
Random rand1; // Random seed
PriorityQueue<Event> events; // Priority queue of events
Queue east, north; // Queue of cars approaching from the East and North
bool eastLight, northLight;
east = new Queue();
north = new Queue();
rand1 = new Random();
Console.Write("Please enter the inter-arrival time for cars approaching from the East: ");
interArrivalEast = -(Convert.ToDouble(Console.ReadLine()) * Math.Log(u = rand1.NextDouble()));
Console.Write("Please enter the inter-arrival time for cars approaching from the North: ");
interArrivalNorth = -(Convert.ToDouble(Console.ReadLine()) * Math.Log(u));
Console.Write("Please enter the green light on time for cars approaching from the East: ");
onTimeEast = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter the green light on time for cars approaching from the North: ");
onTimeNorth = Convert.ToDouble(Console.ReadLine());
onTimeBoth = onTimeEast + onTimeNorth;
numEvents = Convert.ToInt32((interArrivalEast * 50 / onTimeEast) + (interArrivalNorth * 50 / onTimeNorth) + (onTimeEast * 50 / 3) + (onTimeNorth * 50 / 3) + 100);
events = new PriorityQueue<Event>(numEvents);
totalTime = ((50 * onTimeEast) + (50 * onTimeNorth));
for (double i = 0; i < totalTime; i += interArrivalEast)
{
events.Add(new Event("arriveeast", i));
}
for (double j = 0; j < totalTime; j += interArrivalNorth)
{
events.Add(new Event("arrivenorth", j));
}
for (double k = 0; k < totalTime; k += onTimeBoth)
{
events.Add(new Event("lighteast", k));
for (double l = k; l < k + onTimeBoth; l += 3)
events.Add(new Event("leaveeast", l));
}
for (double m = onTimeEast; m < totalTime; m += onTimeBoth)
{
events.Add(new Event("lightnorth", m));
for (double n = m; n < m + onTimeBoth; n += 3)
events.Add(new Event("leavenorth", n));
}
for (int o = 0; o < 1000; o++)
{
while (!events.Empty())
{
temp = events.Front();
switch (temp.EType)
{
case "arriveeast":
east.Enqueue(temp.Time);
break;
case "arrivenorth":
north.Enqueue(temp.Time);
break;
case "leaveeast":
east.Dequeue();
break;
case "leavenorth":
north.Dequeue();
break;
case "lighteast":
eastLight = true;
northLight = false;
break;
case "lightnorth":
northLight = true;
eastLight = false;
break;
}
Console.WriteLine("{0} {1}", Convert.ToString(o), temp.EType);
events.Remove();
}
sum += temp.Time;
}
average = sum / 1000;
Console.WriteLine(Convert.ToString(average));
Console.ReadKey();
答案 0 :(得分:0)
我很确定你的问题可以缩小到:
首先添加所有arriveeast
的
for (double i = 0; i < totalTime; i += interArrivalEast)
{
events.Add(new Event("arriveeast", i));
}
然后添加norths
...
然后在你的循环中按顺序写出来:
temp = events.Front();
//switch
Console.WriteLine("{0} {1}", Convert.ToString(o), temp.EType);
您的Console.WriteLine
似乎并不关心switch
声明中发生的事情,它只是在它们进来时写出来。