Can i do this in ruby? a simulation process

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

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

OR like this one

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

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

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.

Don’t be fooled by the apparent simplicity of the question :o)
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,

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,

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

Andrew W. wrote:

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…

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.

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?

On Tue, Aug 17, 2010 at 10:41 AM, Jean-Julien F. <

Bruce W. wrote:

OR like this one

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

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.

Andrew W. wrote:

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?

On Tue, Aug 17, 2010 at 10:41 AM, Jean-Julien F. <

Yes indeed.

Brian C. wrote:

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.

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 :slight_smile:

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

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.
If I recall right, the mean distance from the origin of a 1D random
walk behaves like a diffusive process: it’s growing like the square
root of time (with a constant factor before it). Here, there are some
complications, but I would expect it to behave quite the same: if you
know the average time to get a 50 people difference between the two
cities, it will take 10**2 = 100 times more time to get a 500 people
difference in mean.

Cheers,

On Tue, Aug 17, 2010 at 7:34 AM, Andrew W. [email protected]
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. 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

On Tue, Aug 17, 2010 at 1:19 PM, [email protected] wrote:

Elapsed time (seconds)
min 533, max 2550, avg 1273.1, stdev 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

On Tue, Aug 17, 2010 at 4:39 AM, Jean-Julien F. <
[email protected]> wrote:

steps each Vehicle has to go throught in order to reach the second
PCSI1 Lycée Kléber

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 to
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
passengers
to a sum. Then just increment seconds and iterate until >= 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.

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

Josh C. wrote:

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.

Bruce W. wrote:

can someone post some code here: I’m only a newbie in ruby :frowning:

You seem to have made a good start. Why don’t you post what you’ve done
so far?

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