Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Multi-Threaded Elevator Simulator

by particle (Vicar)
on Aug 07, 2002 at 12:40 UTC ( [id://188292]=note: print w/replies, xml ) Need Help??


in reply to Multi-Threaded Elevator Simulator

in the main package,

# join the people threads and collect statistics my ($total_wait, $total_ride, $max_wait, $max_ride) = (0,0,0,0); foreach (@people) { my ($wait1, $wait2, $ride1, $ride2) = $_->join; $total_wait += $wait1 + $wait2; $total_ride += $ride1 + $ride2; $max_wait = $wait1 if $wait1 > $max_wait; $max_wait = $wait2 if $wait2 > $max_wait; $max_ride = $ride1 if $ride1 > $max_ride; $max_ride = $ride2 if $ride2 > $max_ride; }
is shorter and more clearly written as:

# join the people threads and collect statistics my ($total_wait, $total_ride, $max_wait, $max_ride) = (0,0,0,0); foreach (@people) { my ($wait1, $wait2, $ride1, $ride2) = $_->join; $total_wait = $wait1 + $wait2; $total_ride = $ride1 + $ride2; $max_wait = $wait1 > $wait2 ? $wait1 : $wait2; $max_ride = $ride1 > $ride2 ? $ride1 : $ride2; }
in the Elevator class, the constant STARTING is declared and never used.

as an aside: in new york city, there are three buildings with double-decker elevators (i've worked in two of them.) lobby and mezzanine are the base levels, and when going up you must use the lobby for odd floors (L 3 5 7 ... ) and mezzanine for odd floors (M 4 6 8 ...). going down, you can reach any floor from either level (except the edge case at the bottom.) i believe the Elevator class can be easily subclassed to handle this scenario as well. cool.

~Particle *accelerates*

Replies are listed 'Best First'.
Re: Re: Multi-Threaded Elevator Simulator
by samtregar (Abbot) on Aug 07, 2002 at 14:31 UTC
    Your version is shorter but it's not right! My code computes max_ride and max_wait across all people. Yours finds the max_ride and max_wait for each person and only keeps the last one.

    As far as making the simulation more accurate, there are many ways I could go. For one thing I could limit the number of people in a single elevator. And I could have the elevators communicate so they don't all rush off to the same floor at once. But ultimately this would be counter-productive as a teaching example since it would only make the code longer and thicker.

    Thanks,
    -sam

      As far as making the simulation more accurate, there are many ways I could go.

      I suggest you go to a near-by university and observe people's habbits getting on and off elevators (as well as finding out how the elevators operate), calculating how long people usually wait for an elevator to come down before leaving ... etc.

      (Or if that doesn't strike you as fun, or you don't have several weeks ... :), you could always check out Knuth's The Art of Computer Programming. I don't have the books here as a reference, but I believe it is either the first or second one. Knuth does just what I described above--memory permitting, of course. Its pretty informative if you have the books or can get them at a library--of course, I make no guarentees that he talks about threading, either in Perl 5.8.0 or otherwise :)

        At my university (NYU) they actually had security guards posted at all the ground floor elevator doors to tell people how to get on and off the elevators. No joke.

        But ultimately, this exercise isn't about building the best possible elevator simulator. It's about showing people how to use Perl's threading support. In fact, if anyone could show me how to build a dumber, simpler simulator that still works I'd probably use that. The elevator algorithm is really just a distraction here.

        -sam

      man...I hate when people point out the obvious when its not even related to the point. The point was to test the new and improved 5.8 threading...which I think was a good idea....besides...
      $max_wait = $wait1 > $wait2 ? $wait1 : $wait2;

      does not equal
      $max_wait = $wait1 if $wait1 > $max_wait;

      because $max_wait is an aggregate value and is not in the same scope....you would lose your max value at every iteration....instead...
      $max_wait = ($wait1 > $wait2) ? ($wait1 > $max_wait) ? $wait1 : $max +_wait : ($wait2 > $max_wait) ? $wait2 : $max_wait;

      would work....
      -insomnia
        $max_wait = ($wait1 > $wait2) ? ($wait1 > $max_wait) ? $wait1 : $max +_wait : ($wait2 > $max_wait) ? $wait2 : $max_wait;
        If I was your teacher, I'd rap you on the knuckles with a ruler for writing that! Gah!

        Please, consider this much clearer alternative:

        $max_wait < $_ and $max_wait = $_ for $wait1, $wait2;
        Or, if you don't like the "and" there...
        for ($wait1, $wait2) { $max_wait = $_ if $max_wait < $_; }

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://188292]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-18 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found