Can i do this in ruby? a simulation process

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?

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?

You sure can do it in ruby but first you have to decide what ruby have
to do for you: take a sheet of paper and write down the differents
steps each Vehicle has to go throught in order to reach the second
city. I’m sure you will see more clearly what you have to do next to
implement each step.

Hint: it have to do with probability distributions and you will most
certainly need the metho ‘rand’

Cheers,

Jean-Julien F. wrote:

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?

You sure can do it in ruby but first you have to decide what ruby have
to do for you: take a sheet of paper and write down the differents
steps each Vehicle has to go throught in order to reach the second
city. I’m sure you will see more clearly what you have to do next to
implement each step.

Hint: it have to do with probability distributions and you will most
certainly need the metho ‘rand’

Cheers,

Cheers Thanks, anyway i already did writing and other stuff but the only
problem is that i don’t know to start my code. :frowning:

Cheers Thanks, anyway i already did writing and other stuff but the only
problem is that i don’t know to start my code. :frowning:

Try to create a Vehicle class where you compute the arrival time and
the number of passengers based on a departure time. Then make a loop
of say 500 vehicles (to be sure everybody arrive at the end) and
create a list of vehicles that are launched every 5-35s. Do not forget
to sort the list based on arrival time rather than departure time.

In the end, it should give you something like

[…]
442 passengers where there after 654 seconds
444 passengers where there after 691 seconds
448 passengers where there after 717 seconds
449 passengers where there after 746 seconds
489 passengers where there after 761 seconds
529 passengers where there after 790 seconds

Cheers,

Bruce W. wrote:

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?

To start with, I’d ask the following: how would you perform the
simulation manually, for example using a pencil, some pieces of paper,
and some dice? (You can use any imaginary dice you need, e.g. a 30-sided
die or a 20-sided die).

I strongly suggest you do it for real, for a number of vehicles.

When you see the individual steps that you’re doing, you’ll have a
clearer idea of the individual steps your program needs. Then you can
come back and ask more specific questions.

Hmm, I may or may not disagree with you on what the output should be. I
can’t speak for his assignment, but in real life, I would think it would
be
more useful to perform the simulation some number of times, printing out
the
number of seconds it took to get to 500 passengers each time, and the
average of all the trials. It’s not clear to me from the description of
the
assignment whether the requirement is for the results of a single trial
(rather silly) or an average.

Anyway, I did this, for fun, and consistently got an average of around
833
seconds on average for 100000 trials. Anyone else give this a shot? I’m
curious to see whether different implementations give different results.
The
standard deviation seems to be pretty high.

On Tue, Aug 17, 2010 at 6:00 AM, Jean-Julien F. <

Andrew W. wrote:

Anyway, I did this, for fun, and consistently got an average of around
833
seconds on average for 100000 trials. Anyone else give this a shot? I’m
curious to see whether different implementations give different results.

Ruby quiz :slight_smile:

After 10,000 trials I got an average of 803 seconds, and after a second
10,000 trials I got 807 seconds. But I don’t think we should post any
code until the OP has had a go.

I would certainly expect wide variation between runs, since it would be
affected very strongly by when the buses set off. However I think the
point of the exercise is really the simulation, because it becomes very
simple if you treat it statistically.

(1) Average number of occupants per vehicle =
0.21+0.32+0.13+0.14+0.3*40 = 13.5
(2) You can ignore the different travel times. The average rate at which
vehicles depart must be the same as the average rate at which they
arrive (law of conservation of vehicles)
(3) The vehicles depart on average every 20 seconds

So you should get an average of 0.675 passengers per second, which
implies 500 passengers will pass any particular point after 740.7
seconds.

It then takes each passenger an average of 60 seconds to get from A to
B, so that makes 800.7 seconds for the original question.

I would expect the real simulation answer to be slightly different
because of the discontinuous (bursty) nature of arrivals.

After 10,000 trials I got an average of 803 seconds, and after a second
10,000 trials I got 807 seconds. But I don’t think we should post any
code until the OP has had a go.

Interesting. Sounds like I’ve got a bug to hunt down. Agreed, as far as
posting code.

(3) The vehicles depart on average every 20 seconds

So you should get an average of 0.675 passengers per second, which
implies 500 passengers will pass any particular point after 740.7
seconds.

It then takes each passenger an average of 60 seconds to get from A to
B, so that makes 800.7 seconds for the original question.

Nice analysis. I did a very similar set of calculations, and came up
with
roughly the same result.

I would expect the real simulation answer to be slightly different
because of the discontinuous (bursty) nature of arrivals.

Can you expand on this? I’m curious what you mean, from a mathematical
perspective.

2010/8/17 Andrew W. [email protected]:

Hmm, I may or may not disagree with you on what the output should be. I
can’t speak for his assignment,

It was just to get him the idea.

Anyway, I did this, for fun, and consistently got an average of around 833
seconds on average for 100000 trials. Anyone else give this a shot? I’m
curious to see whether different implementations give different results. The
standard deviation seems to be pretty high.

I also did, twice on 100000 trials:
First: In mean, it took 808.3 seconds to go there.
Second: In mean, it took 809.3 seconds to go there.

It’s quite consistent with the little calculation Brian explained.
I would expect the mean result to always be above the 800.7 seconds
Brian found as there is very little chance that the sum of the
passengers arriving in town be exactly 500 (it ranges from 500 to 539
due to the buses)

Cheers,

It’s quite consistent with the little calculation Brian explained.
I would expect the mean result to always be above the 800.7 seconds
Brian found as there is very little chance that the sum of the
passengers arriving in town be exactly 500 (it ranges from 500 to 539
due to the buses)

Ah yes, I see now what Brian meant by “bursty”. Thanks for the results!

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.

20% of vehicles (1passenger) -> must transfer 100(20% of 500) people
from A -> B

30% of vehicles (2passenger) -> must transfer 150 people from A -> B

10% of vehicles (3passenger) -> must transfer 50 people from A -> B

10% of vehicles (4passenger) -> must transfer 50 people from A -> B

30% of vehicles(bus) (3passenger) -> must transfer 150 people from A ->
B

5-35sec (rand?) -> departure time for vehicles from A to B +
50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -> traveling time from A
-> B

and then

50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -> traveling time from B
-> A
5-35sec (rand?) -> departure time for vehicles from B -> A

i would like to if you used rand() in departure time/traveling time…

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.

20% of vehicles (1passenger) -> must transfer 100(20% of 500) people
from A -> B

30% of vehicles (2passenger) -> must transfer 150 people from A -> B

10% of vehicles (3passenger) -> must transfer 50 people from A -> B

10% of vehicles (4passenger) -> must transfer 50 people from A -> B

30% of vehicles(bus) (3passenger) -> must transfer 150 people from A ->
B

5-35sec (rand?) -> departure time for vehicles from A to B +
50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -> traveling time from A
-> B

and then

50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -> traveling time from B
-> A
5-35sec (rand?) -> departure time for vehicles from B -> A

i would like to know if you used rand() in departure time/traveling
time…

Cheers

i would like to know if you used rand() in departure time/traveling
time…

I used rand() for both departure time and traveling time, assuming all
the
times were equally likely. You bring up an interesting point about going
from B -> A. In my simulation, I only considered vehicles going in one
direction. Seems like the specification is unclear about whether they
are
leaving every 5-35 seconds from both cities or just one. Though if
they’re
going from both directions, there’s certainly no guarantee that you will
ever move a total of 500 from one to the other.

sorry for the double post :slight_smile:

Bruce W. wrote:

i would like to know if you used rand() in departure time/traveling
time…

Yes. rand() by itself gives a random number between 0.0 and 1.0 (well
actually 0.99999999…). Hence, rand*30.0 gives you a value between 0
and 30.

Hello Bruce,

50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -> traveling time from B
-> A
5-35sec (rand?) -> departure time for vehicles from B -> A

i would like to know if you used rand() in departure time/traveling
time…

Yes, we did. But also when taking into account the number of
passengers: in real life, on a certain amount of time, you don’t know
if there will be a tad more buses and a tad less monodrivers than in
mean. The only thing you do know is that if you count for a very long
time, you end up with the given percentages.

Cheers,

Ah. Yes, I didn’t mean to say that I used rand without any parameters. I
used the fact that, for example, to get a random number from 0 to 5,
inclusive, you can call rand(6).

Bruce W. wrote:

20% of vehicles (1passenger) -> must transfer 100(20% of 500) people
from A -> B

No, and in any case that’s the wrong approach for a simulation.

In any particular run, it’s possible (although rather unlikely) that all
the vehicles will depart will all be buses. Or that they will all be
cars with one driver. In practice, the mix will vary between runs, and
you don’t know in advance what it will be.

Treat each vehicle individually. Each one has its own departure time,
its own arrival time, and its own number of occupants. If you do the
simulation by hand, you’ll see what I mean.

To start with, you could use ruby as the random number generator, while
you do the rest of the work by hand. Suppose the first vehicle leaves on
the dot of 12:00:00 (or 0 seconds past midday). How many passengers does
it contain? When does it arrive? When does the second vehicle leave? And
so on.

Yes, but you take that into account by doing a large number of trials,
right? You still consider all values equally likely in any individual
case?

On Tue, Aug 17, 2010 at 9:29 AM, Jean-Julien F. <

You bring up an interesting point about going
from B -> A. In my simulation, I only considered vehicles going in one
direction. Seems like the specification is unclear about whether they are
leaving every 5-35 seconds from both cities or just one.

Specifications are clear, at least to me:

“A vehicle leaves the first city every 5-35sec. […] How long does it
take for 500 people to arrive in the second city.”

Though if they’re
going from both directions, there’s certainly no guarantee that you will
ever move a total of 500 from one to the other.

Then the problem is turning into a (sort of) random walk: it’s getting
far more complicated (but far more interesting ?)

Cheers,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs