can i do this in ruby? a simulation process

B

Bruce Wayner

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?
 
J

Jean-Julien Fleck

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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
B

Bruce Wayner

Jean-Julien Fleck said:
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. :(
 
J

Jean-Julien Fleck

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

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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
B

Brian Candler

Bruce said:
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.
 
A

Andrew Wagner

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 th=
e
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. Th=
e
standard deviation seems to be pretty high.

On Tue, Aug 17, 2010 at 6:00 AM, Jean-Julien Fleck <
Cheers Thanks, anyway i already did writing and other stuff but the onl= y
problem is that i don't know to start my code. :(

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,
 
B

Brian Candler

Andrew said:
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 :)

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.2*1+0.3*2+0.1*3+0.1*4+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.
 
J

Jean-Julien Fleck

2010/8/17 Andrew Wagner said:
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 83= 3
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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]
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.

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.2*1+0.3*2+0.1*3+0.1*4+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.
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.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]
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!
 
B

Bruce Wayner

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..
 
B

Bruce Wayner

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
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]
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..

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.
 
B

Brian Candler

Bruce said:
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.
 
J

Jean-Julien Fleck

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,


--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
J

Jean-Julien Fleck

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,


--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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).
 
B

Brian Candler

Bruce said:
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.
 
A

Andrew Wagner

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 Fleck <
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top