can i do this in ruby? a simulation process

A

Andrew Wagner

Well, it may be interesting, but not in terms of the question originally
asked. Actually, I'm not sure what you could ask to make it interesting if
buses are going in both directions, as I would expect the average result to
be no change in the number of people currently in either city.

On Tue, Aug 17, 2010 at 9:33 AM, 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 a= re
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 wil= l
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,
 
B

Bruce Wayner

How about like this one:
is it possible?
Example:

Vehicle A took 30 seconds to transfer 1 passenger to the second city
Vehicle B took 25 seconds to transfer 2 passenger to the second city
Vehicle C took 45 seconds to transfer 3 passenger to the second city
Vehicle D took 24 seconds to transfer 4 passenger to the second city
Vehicle E took 43 seconds to transfer 40 passenger to the second city

Vehicle D took 20 seconds to travel back to the first city
Vehicle B took 35 seconds to travel back to the first city
Vehicle A took 50 seconds to travel back to the first city
Vehicle E took 34 seconds to travel back to the first city
Vehicle C took 45 seconds to travel back to the first city

Vehicle D took 30 seconds to transfer 4 passenger to the second city
Vehicle B took 25 seconds to transfer 2 passenger to the second city
Vehicle A took 45 seconds to transfer 1 passenger to the second city
Vehicle E took 24 seconds to transfer 40 passenger to the second city
Vehicle C took 43 seconds to transfer 3 passenger to the second city
...
..
..
people transfer: 500
Total Time: 800.9
 
B

Bruce Wayner

OR like this one

Vehicle A took 30 seconds to transfer 1 passenger to the second city
Vehicle B took 25 seconds to transfer 2 passenger to the second city
Vehicle C took 45 seconds to transfer 3 passenger to the second city
Vehicle D took 24 seconds to transfer 4 passenger to the second city
Vehicle E took 43 seconds to transfer 40 passenger to the second city

Vehicle A took 20 seconds to travel back to the first city
Vehicle B took 35 seconds to travel back to the first city
Vehicle C took 50 seconds to travel back to the first city
Vehicle D took 34 seconds to travel back to the first city
Vehicle E took 45 seconds to travel back to the first city

Vehicle A took 30 seconds to transfer 4 passenger to the second city
Vehicle B took 25 seconds to transfer 2 passenger to the second city
Vehicle C took 45 seconds to transfer 1 passenger to the second city
Vehicle D took 24 seconds to transfer 40 passenger to the second city
Vehicle E took 43 seconds to transfer 3 passenger to the second city
...
..
..until reaches 500 people
people transfer: 500
Total Time: 800.9
 
J

Jean-Julien Fleck

2010/8/17 Andrew Wagner said:
Well, it may be interesting, but not in terms of the question originally
asked. Actually, I'm not sure what you could ask to make it interesting i= f
buses are going in both directions, as I would expect the average result = to
be no change in the number of people currently in either city.

Don't be fooled by the apparent simplicity of the question :eek:)
Random walks have many many interesting behaviors, one of which is
that they can go as far as you like from the origin in a time you can
estimate (from a probabilistic viewpoint).

The most simple realisation is the 1D random walk:
- choose an origin
- flip a coin to decide if you should go right or left
- go there
- flip a coin...
- etc.

For sure, as the problem is symetrical on right and left, the mean
value of the displacement is 0. But if you rather ask yourself the
question: "how far can I go from the origin" (either on the right or
on the left), then you can estimate how much time you need to achieve
this goal within a given probability.

In the OP problem, it would read: how much time do you need to
transfer 500 people either from A to B or from B to A with the given
set of rules.

Cheers,

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

Bruce Wayner

is it possible for someone to post their code? is not that i would copy
and paste it. my professor rules is that if you cannot explain the code
then you are not allowed to submit the program. and i have only two
days to finish it. and i also admit that this my first time in ruby.
Thank u for your explanation it gives was very helpful guys.

Cheers,
 
A

Andrew Wagner

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

Bruce,
In trying to code this, I would NOT consider people going from the second
city back to the first. I don't think the requirements give any indication
that you should be looking at that.
 
A

Andrew Wagner

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

I think it would be better if you posted (a link to) your code, and we can
make some suggestions.
 
B

Bruce Wayner

Andrew said:
Bruce,
In trying to code this, I would NOT consider people going from the
second
city back to the first. yes, i agree
I don't think the requirements give any
indication
that you should be looking at that.

Hi Andrew, as i was reviewing the problem again i think the vehicles are
only allowed to go the second city not going back..perhaps i write too
much..
 
A

Andrew Wagner

Ah, I see your point. Very interesting indeed. Is it reasonable to guess
that it would take a very, very long time in this case?

Well, it may be interesting, but not in terms of the question originall= y
asked. Actually, I'm not sure what you could ask to make it interesting if
buses are going in both directions, as I would expect the average resul=
t
to
be no change in the number of people currently in either city.

Don't be fooled by the apparent simplicity of the question :eek:)
Random walks have many many interesting behaviors, one of which is
that they can go as far as you like from the origin in a time you can
estimate (from a probabilistic viewpoint).

The most simple realisation is the 1D random walk:
- choose an origin
- flip a coin to decide if you should go right or left
- go there
- flip a coin...
- etc.

For sure, as the problem is symetrical on right and left, the mean
value of the displacement is 0. But if you rather ask yourself the
question: "how far can I go from the origin" (either on the right or
on the left), then you can estimate how much time you need to achieve
this goal within a given probability.

In the OP problem, it would read: how much time do you need to
transfer 500 people either from A to B or from B to A with the given
set of rules.

Cheers,
[/QUOTE]
 
B

Brian Candler

Bruce said:
OR like this one

Vehicle A took 30 seconds to transfer 1 passenger to the second city
Vehicle B took 25 seconds to transfer 2 passenger to the second city
Vehicle C took 45 seconds to transfer 3 passenger to the second city
Vehicle D took 24 seconds to transfer 4 passenger to the second city
Vehicle E took 43 seconds to transfer 40 passenger to the second city

That's a good start, although the question says a vehicle takes between
50 and 70 seconds to get from A to B, and those values are less than 50.

You need to work out a departure time for each vehicle - which will be
related to the departure time of the previous vehicle - and the arrival
time, which is the start time plus the transit time.

You also need to pick the number of passengers for each vehicle at
random, since that's what the question asks for (the way I read it
anyway). For example, you can pick a random number between 0 and 9. If
it's 0 or 1 then the vehicle has 1 passenger; if it's 2 or 3 or 4 then
it has 2 passengers; and so on. That gives you them in the right
proportions.
Vehicle A took 20 seconds to travel back to the first city
Vehicle B took 35 seconds to travel back to the first city
Vehicle C took 50 seconds to travel back to the first city
Vehicle D took 34 seconds to travel back to the first city
Vehicle E took 45 seconds to travel back to the first city

The way I read the question, I saw nothing about travelling back to the
first city. It asks for "A simulation process of the movement of
vehicles from one point to another"

Of course you're free to try variants or different interpretations of
the question, or you may have additional information which clarifies the
problem.
 
B

Bruce Wayner

Andrew said:
Ah, I see your point. Very interesting indeed. Is it reasonable to guess
that it would take a very, very long time in this case?
Yes indeed.
 
B

Bruce Wayner

Brian said:
The way I read the question, I saw nothing about travelling back to the
first city. It asks for "A simulation process of the movement of
vehicles from one point to another"

Of course you're free to try variants or different interpretations of
the question, or you may have additional information which clarifies the
problem.

yes indeed i talk to much i didn't realize the requirements which you
said a while ago "A simulation process of the movement of vehicles from
one point to another" sorry Brian for making things more complicated...
I'll stick with that one (first city to the second city)... I have only
a limited knowledge in ruby but willing to learn more.. i know that you
cannot post any code here i understand that anyway
Thanks a lot guys
Cheers :)
 
J

Jean-Julien Fleck

2010/8/17 Andrew Wagner said:
Ah, I see your point. Very interesting indeed. Is it reasonable to guess
that it would take a very, very long time in this case?

Sure. I have no time left to play with it today, but I will try it tomorrow=
 
B

brabuhr

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.

58,387 trials

Number of vehicles used
min 16
max 77
avg 38.3
stdev 7.9

Elapsed time (seconds)
min 533
max 2550
avg 1273.1
stdev 258.1
 
J

Josh Cheek

On Tue, Aug 17, 2010 at 4:39 AM, Jean-Julien Fleck <
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,
I'm not sure how much your simulation needs to include, on one hand, you
have the pure mathematical analysis, on the other, probably count each
second. You could probably argue that it is purer in so far as it adheres t=
o
the concept of "simulating". Plus it is easy: For each second, check to see
if a vehicle should be departing, if so, add it to an array of vehicles in
transit. And check the array of vehicles in transit to see if a vehicle is
arriving, if so, remove it from the in transit array, and add its passenger=
s
to a sum. Then just increment seconds and iterate until >=3D 500 passengers
have arrived.


I was getting about 783 seconds when the first vehicle leaves at zero
seconds, and 804 seconds when the first vehicle leaves 5-35 seconds after
time starts.
 
B

brabuhr

58,387 trials

Number of vehicles used
=A0min 16, =A0max 77, =A0avg 38.3, =A0stdev 7.9

Elapsed time (seconds)
=A0min 533, =A0max 2550, =A0avg 1273.1, =A0stdev 258.1

Two alternate implementations:

5,689 trials

Vehicles
min, max, avg, stdev: 16, 77, 38.4, 8.0

Time
min, max, avg, stdev: 348, 1637, 808.9, 169.2

and:

19,121 trials

Vehicles
min, max, avg, stdev: 14, 80, 38.4, 8.0

Time
min, max, avg, stdev: 318, 1714, 807.9, 168.3
 
B

Bruce Wayner

can someone post some code here: I'm only a newbie in ruby :( and
basically i know how to code my simulation :( if its ok :(
 
B

Brian Candler

Josh said:
For each second, check to
see
if a vehicle should be departing, if so, add it to an array of vehicles
in
transit. And check the array of vehicles in transit to see if a vehicle
is
arriving, if so, remove it from the in transit array, and add its
passengers
to a sum. Then just increment seconds and iterate until >= 500
passengers
have arrived.

You have to be very careful with the termination condition.

Let's say that a car leaves, arrives after 70 seconds, and that brings
increments your total from 499 to 500.

However it's still possible that another vehicle could leave 5 seconds
later and arrive after 50 seconds (15 seconds before that car), which
means the 500 limit is actually reached earlier than you first thought.

Regards,

Brian.
 
B

Bruce Wayner

sorry I'm totally suck in ruby here is my code:


print "Number of Vehicle: " #That will be maximum of 5 only
n = gets().to_i

print "\n Vehicle number 1: "
$delay1 = rand(4)
$delay2 = rand(10)
$depart = rand(35)
$travelTime = rand(70)
$ET = $depart
$ET1= $travelTime

if $delay1==0;
$delay1 = $delay1+(1);

elsif $delay1==1
$delay1 = $delay1+(1);

end
sleep($delay1)

$EXT = Time.now
sleep($delay2)
$CEXT = Time.now
$WT = ($delay1+$delay2)
$passenger2= 1;
$delay101 = rand(10)
puts ("\nDeparture Time: #$ET seconds");
puts ("Traveling Time: #$ET1 seconds");
puts ("# of passenger Arrive in Second City: #{$passenger2}");
puts ("Total Traveling Time: #{$delay1} seconds");
$i = 1+1;
$num = n+1;

while $i < $num do

puts ("\n Vehicle number: #{$i}");
$i +=1;

$delay1 = rand(10)
$delay2 = rand(10)
$delay3 = rand(10)

$AddTimein = rand(59)
$AddExitTime = rand(30)
$AddCET = rand(15)
$AddCEXT = rand(5)

if $ET.eql?$ET
$ET = Time.now - $AddTimein
sleep($delay1)
if $delay1==0
$delay1+(2)
elsif $delay1==1
$delay1+(2)
end

elsif $ET > $ET
$ET = Time.now - $AddTimein

sleep($delay1)
if $delay1==0
$delay1+(2)
elsif $delay1==1
$delay1+(2)
end
elsif $ET < $ET
$ET = Time.now - $AddTimein

sleep($delay1)
if $delay1==0
$delay1+(2)
elsif $delay1==1
$delay1+(2)
end

end

if $EXT.eql?$ET
$EXT = Time.now
sleep($delay2)
if $delay2==0
$delay2+(2)
elsif $delay2==1
$delay2+(2)
end

elsif $EXT > $ET
$EXT = Time.now + $AddExitTime
sleep($delay2)
if $delay2==0
$delay2+(2)
elsif $delay2==1
$delay2+(2)
end
end

$CET = Time.now + $AddCET
sleep($delay3)
if $delay3==0
$delay3+(2)
elsif $delay3==1
$delay3+(2)
end

$CEXT = Time.now

$buytime = $EXT - $ET
$passenger1 = 2;
$totalTime = $EXT - $CET
$totalTravel = $timewait.to_i
$PackTime = $CEXT - $CET
$PackTime1 = $PackTime.to_i
$depart1 = rand(35)
$travelTime1 = rand(70)
$ET = $depart1
$ET1= $travelTime1
puts ("\n Departure Time: #$ET1 seconds");
puts ("Traveling Time: #$ET1 seconds");
puts ("# of people Arrive in second city: #{$passenger1}");
puts ("Total Traveling Time: #$totalTravel");



end
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top