From: Ian Hobson Date: 2010-08-19T05:44:07+09:00 Subject: Re: can i do this in ruby? a simulation process On 17/08/2010 10:11, Bruce Wayner wrote: > still i don't know how to begin my program on this problem: > > A superhighway connects one large metropolitan area to another. > A vehicle leaves the first city every 5-35sec. 20% of the vehicles have > 1 passenger, 30% have 2 passengers, 10% have 3 passengers and and 10% > have 4 passengers. the remaining 30% of the vehicles are buses, which > carry 40 people. > It takes 50sec-1min&10sec for vehicles to travel between the two > metropolitan > areas. How long does it take for 500 people to arrive in the second > city. > > Requirements: > 1. A simulation process of the movement of vehicles from one point to > another. > 2. The time (in minutes) for 500 people to arrive in the second city. > > i can't even think on how can i start this one in ruby a newbie like me > have only limited knowledge on ruby. can someone help with my program? Hi Bruce, Some concepts for you. A simulation has exactly three types of "events". a) Processes that start immediately resources that they need become available. b) Processes that free resources a known time later. These processes can continue but they must not require more resources (the resources may not be available). c) Events that happen on the clock and report the status. To run a simulation, you need a queue of time based events to handle, and a loop that will handle them as follows. 1) Step forward to the next time point (pop the head of the queue). 2) Do all the type b events that can be done at this time, freeing the resources. 3) Starts all the type a) processes that can be started with the freed resource(s) (most urgent first), booking the resources as you go. 4) Reports the situation if required. 5) Determine if you should stop or continue. You will also need queues of "work" waiting to be done when resources become available. For your problem you can forget the c events - you need to test the status after each car arrives. For your problem you have to clarify what "A vehicle leaves the first city every 5-35sec " means. Vehicles usually arrive at a queue according to a Poisson distribution, but a Poisson distribution is defined by a single parameter (this is all of its mean, its standard deviation, and the number of vehicles expected per unit of time). That you don't have. The phrase *may* mean that the departure times are uniformly distributed between 5 and 35 seconds. You also have to clarify how the arrival occurs - do the vehicles have to exit the super-highway using an off-ramp that also is limited to one every 5 seconds? Assuming both, I would simulate the movement as something like this. There are two queues of "work" - one is the notional arrival of the vehicles. These could be generated in advance, but, with the arrival distribution given it will be better to generate the next vehicle as the previous one leaves. The second queue of work is the queue to use the off-ramp. There are two "a" events: When the on-ramp is free, you create a vehicle that can use the on-ramp. Use random variables to decide what it is, how full it is, how long into the 5-35 gap it will take before it leaves. Book the on-ramp and queue the "departure" event. If there is a vehicle waiting to use the off-ramp and the off-ramp is free, book the off-ramp, and queue up the arrival event in 5 seconds time. There are three "b" type events depart: free the on-ramp, compute how long the journey will take and and queue up the exit-highway event. exit-highway : queue the vehicle to exit the highway. arrive : free the off-ramp, and accumulate the people who have arrived. If you have over 500, stop the program. Although in this simple scenario it may be possible to book the car directly on the off-ramp if it is free, don't do this. It will complicate your code for no benefit. Regards Ian