can i do this in ruby? a simulation process

Discussion in 'Ruby' started by Bruce Wayner, Aug 17, 2010.

  1. Bruce Wayner

    Bruce Wayner Guest

    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?
     
    Bruce Wayner, Aug 17, 2010
    #1
    1. Advertisements

  2. Requirements:
    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
     
    Jean-Julien Fleck, Aug 17, 2010
    #2
    1. Advertisements

  3. Bruce Wayner

    Bruce Wayner Guest

    Cheers Thanks, anyway i already did writing and other stuff but the only
    problem is that i don't know to start my code. :(
     
    Bruce Wayner, Aug 17, 2010
    #3
  4. Cheers Thanks, anyway i already did writing and other stuff but the only
    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
     
    Jean-Julien Fleck, Aug 17, 2010
    #4
  5. 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.
     
    Brian Candler, Aug 17, 2010
    #5
  6. 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 <
     
    Andrew Wagner, Aug 17, 2010
    #6
  7. 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.
     
    Brian Candler, Aug 17, 2010
    #7
  8. It was just to get him the idea.
    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
     
    Jean-Julien Fleck, Aug 17, 2010
    #8
  9. [Note: parts of this message were removed to make it a legal post.]
    Interesting. Sounds like I've got a bug to hunt down. Agreed, as far as
    posting code.

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

    Can you expand on this? I'm curious what you mean, from a mathematical
    perspective.
     
    Andrew Wagner, Aug 17, 2010
    #9
  10. [Note: parts of this message were removed to make it a legal post.]
    Ah yes, I see now what Brian meant by "bursty". Thanks for the results!
     
    Andrew Wagner, Aug 17, 2010
    #10
  11. Bruce Wayner

    Bruce Wayner Guest

    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..
     
    Bruce Wayner, Aug 17, 2010
    #11
  12. Bruce Wayner

    Bruce Wayner Guest

    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
     
    Bruce Wayner, Aug 17, 2010
    #12
  13. Bruce Wayner

    Bruce Wayner Guest

    sorry for the double post :)
     
    Bruce Wayner, Aug 17, 2010
    #13
  14. [Note: parts of this message were removed to make it a legal post.]
    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.
     
    Andrew Wagner, Aug 17, 2010
    #14
  15. 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.
     
    Brian Candler, Aug 17, 2010
    #15
  16. Hello Bruce,
    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
     
    Jean-Julien Fleck, Aug 17, 2010
    #16
  17. You bring up an interesting point about going
    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."
    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
     
    Jean-Julien Fleck, Aug 17, 2010
    #17
  18. [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).
     
    Andrew Wagner, Aug 17, 2010
    #18
  19. 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.
     
    Brian Candler, Aug 17, 2010
    #19
  20. 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 <
     
    Andrew Wagner, Aug 17, 2010
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.