2015-10-27 39 views
0

我模拟一个跑道的机场。等待着陆并等待起飞的飞机有两排队列。一次只能在跑道上有一架飞机,在任何飞机起飞前,空中的所有飞机都必须降落。这里是我到目前为止(有,我会得到后摆脱别人没用变量时,我敢肯定,我不需要他们):一个简单的机场模拟

public class Runway<E> { 
private LinkedBlockingQueue<Plane> takeoff; 
private LinkedBlockingQueue<Plane> landing; 
private LinkedBlockingQueue<Plane> runway; 
private int planesLanded; 
private int planesTookoff; 
private double averageTakeOffWait; 
private double averageLandWait; 
private int totalTakeoffWait; 
private int totalLandingWait; 
private int planesWaitingToTakeOff; 
private int planesWaitingToLand; 
private int maxLandingQueueLength; 
private int maxTakeOffQueueLength; 

public int timeToLand = 5; 
public int timeToTakeoff = 4; 
public double landingProbability = .1; 
public double takeOffProbability = .1; 
public int simulationLength = 1440; 

public Runway() { 
    takeoff = new LinkedBlockingQueue<>(); 
    landing = new LinkedBlockingQueue<>(); 
    runway = new LinkedBlockingQueue<>(); 

    planesLanded = 0; 
    planesTookoff = 0; 
    averageTakeOffWait = 0; 
    averageLandWait = 0; 
    totalTakeoffWait = 0; 
    totalLandingWait = 0; 
    planesWaitingToTakeOff = 0; 
    planesWaitingToLand = 0; 
    maxLandingQueueLength = 0; 
    maxTakeOffQueueLength = 0; 
    //reset(); 
} 

public void simulate(int duration, double takeoffRate, double landingRate, int landingTime, int takeoffTime) { 
    for(int count = 0; count < duration; count++) { 
     if(Math.random() < takeoffRate) { 
      Plane p = new Plane(landingTime, takeoffTime, count); 
      takeoff.offer(p); 
     } 
     if(Math.random() < landingRate) { 
      Plane p = new Plane(landingTime, takeoffTime, count); 
      landing.offer(p); 
     } 

     if(runway.size() == 0) { 
       if(landing.peek() != null) { 
        Plane landingPlane = landing.poll(); 
        runway.offer(landingPlane); 
        planesLanded++; 
        int landTimeWaited = count - landingPlane.getArrivalTime(); 
        totalLandingWait += landTimeWaited; 
        runway.poll(); 
       } else if(takeoff.peek() != null) { 
        Plane takeoffPlane = takeoff.poll(); 
        runway.offer(takeoffPlane); 
        planesTookoff++; 
        int takeoffTimeWaited = count - takeoffPlane.getArrivalTime(); 
        totalLandingWait += takeoffTimeWaited; 
        runway.poll(); 
       } 
      } 
    } 
} 

public void report() { 
    System.out.println(planesLanded + " planes have landed."); 
    System.out.println(planesTookoff + " planes have taken off."); 
    System.out.println(landing.size() + " planes still waiting to land."); 
    System.out.println(takeoff.size() + " planes still waiting to takeoff."); 

    System.out.println(1.0*totalTakeoffWait/planesTookoff + " average take off wait time."); 
    System.out.println(1.0*totalLandingWait/planesLanded + " average landing wait time."); 

    } 

} 

我的问题是,飞机勉强曾经有等待任何事情。队列中没有剩下任何飞机。我知道我必须以某种方式让它在5分钟内通过,然后下一架飞机才能做任何事情,但我该怎么做?这里是一个测试运行:

Runway myAirport = new Runway(); 
    myAirport.simulate(1440, .1, .1, 5, 4); 
    myAirport.report(); 

146 planes have landed. 
155 planes have taken off. 
0 planes still waiting to land. 
0 planes still waiting to takeoff. 
0.0 average take off wait time. 
0.14383561643835616 average landing wait time. 
+1

您可以使用Thread.sleep,它会使操作花费一些时间。添加完成后,您总是会执行runway.poll,因此也许在投票前,您可以使用睡眠。另外,因为您使用.poll,跑道尺寸是否始终为零?你也可以随意制作。 – matt

回答

0

在你的循环中你总是清除跑道队列,这意味着你将永远清除你添加的飞机。你应该做的是使用某种时间来检查更新。

if(runway.size()==0){ 
    //decide to get from takeoff or landing, your current choice is always take off 
    runway.offer(chosenPlane); 
    inUseTime = 100; 
}else{ 
    Plane p = runway.peek(); 
    if(p.getRunwayTime()>=inUseTime){ 
     runway.poll(); 
    } else{ 
     p.updateRunwayTime(); 
    } 
} 

在这个例子中,如果飞机已经完成时间,它只会从跑道上移开。这意味着更多的计划可以在等待当前结束时进入其他队列。