Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Elevator simulation in event time, not clock.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Elevator simulation in event time, not clock.

    http://www.javaprogrammingforums.com...ulation-3.html

    I have a very similar situation to what Norm discussed with this student last year.

    I am making a elevator simulator for my class. It needs to be "event time". I am having trouble understanding how I can make an "event time" simulator. Initially I was using a while loop to run a clock time simulation of x seconds for each event, until I ran into a concrete wall and had to call another method which took X seconds, while still needing to execute code.

    An example of what I did is:

    Spawn person -> person.goToElevator (Takes x seconds) - > person.waitForElevator (Until elevator arrives, x seconds). While this is happening, I needed to create another person and do the same thing, this will add another to the queue if the first person is still waiting. Etc etc, you get the point.

    We have not learnt about multi threading, that is the next assignment.

    Here is a quote from my lecturer:
    "That's right, it requires multi-threading which we're not doing in this paper. Here's a solution that can work:

    The person waiting for the lift and then doing lots of other things should do all that in a fraction of a second. So, you don't let the person really wait. Instead, you will set a timestamp in the future in the person object that "he's busy till that time". In all other steps in your simulation you will first check if a person is "busy" (i.e. has a timestamp in the future). If so, you can't use this object, otherwise, you can.

    Can you use that?"

    I half understand this.. Is anyone in anyway able to provide me with a tiny snippet of code to show me an example of how to go about creating this? I can't wrap my head around it simulating all this in a fraction of a second... How does it create a bigger queue? Etc etc..

    Outputs I would have in my final simulation would be, averageQueueLength, numOfPeopleInBuilding etc.

    Any help would be great.
    Last edited by Leeness; April 10th, 2014 at 07:23 AM. Reason: Added link


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Elevator simulation in event time, not clock.

    An event queue could be a queue of events ordered by time. The next event would be at the top of the queue.
    When an event is taken off the queue, the "current time" is set to the event's time. When a new event is added to the queue it is inserted into the queue according to it's time to be done.

    The simulation driver removes the next event from the queue, sets the current time, does the event and then loops back to get the next event from the queue and do it again.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Elevator simulation in event time, not clock.

    Is it possible to show me a quick example of what you mean? I can't seem to get off the ground with this. I'm sure once I have a bit more visual of what my solution needs to look like I can play around with it and see what I can come up with... Right now I am sitting with empty "goToElevator() & waitForElevator()" methods. I think I am over-thinking this too much and it has left me blank.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Elevator simulation in event time, not clock.

    Perhaps you're stuck on the notion that event time occurs like real time, one tick at a time. Instead, events are created, added to the queue. and event time is increased to the time stamp of each event as each event is removed and executed from the queue.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Elevator simulation in event time, not clock.

    Any chance of you being able to show me what this would look like in code? Still struggling to implement this.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Elevator simulation in event time, not clock.

    It could be something like this:
    define variables, etc
    add initial events to queue in time order
    begin loop
    get next event from queue
    set current time to event's time
    process the event >> could include adding new events to queue
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Elevator simulation in event time, not clock.

    The "add initital events to queue in time order" part is what I am confused with, the rest I hopefully can handle. An event being... goToElevator ? How do I add that to a queue? And by queue, do you mean a list that holds a list of something that I can access by its index?

    I am quite a beginner in Java, so a lot of terms and few basics I still don't understand as of yet.
    Last edited by Leeness; April 11th, 2014 at 05:54 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Elevator simulation in event time, not clock.

    The PriorityQueue class will take care of the ordering of events in the queue.
    Define a class for your events and extent it for the different types of events you need.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Elevator simulation in event time, not clock.

    PirorityQueue class..? Is this a class I make? I feel like this is a different solution to the one quoted from my lecturer.

    Is the way you are describing, the same way as mentioned here?

    "The person waiting for the lift and then doing lots of other things should do all that in a fraction of a second. So, you don't let the person really wait. Instead, you will set a timestamp in the future in the person object that "he's busy till that time". In all other steps in your simulation you will first check if a person is "busy" (i.e. has a timestamp in the future). If so, you can't use this object, otherwise, you can."

    Unfortunately I can't get in contact with my lecturer again until next week, which would be getting a bit behind schedule. :S

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Elevator simulation in event time, not clock.

    PriorityQueue is a java SE class.
    The "timestamp" sounds like the time value that would be used to order the queue.

    check if a person is "busy"
    I'm not sure how that logic fits in. If there is an event for that person, it will be on the queue. There are no events outside of those on the queue. There would not be any checking for a person to be busy by the event loop. There might be checking done by a statistics measuring routine that would gather stats for all the actors in the simulation.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Elevator simulation in event time, not clock.

    Hmmm, ok. Is this the only possible way of creating such a simulation (should be a very simple simulation) without multithreading? I would have assumed we would have covered these classes (PriorityQueue) in lectures. I don't really understand how these classes work at the moment.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Elevator simulation in event time, not clock.

    Is this the only possible way
    I'm sure there are many different ways.
    I'd think the simulation would NOT require mulithreading.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Ever so lost! (Elevator Program)
    By Hannah Smith <3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2014, 03:04 PM
  2. Online Employee Time Clock
    By DkParks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2013, 12:25 PM
  3. Alarm Clock (How to figure out how much time left till alarm will sound)
    By MrMario in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2013, 12:58 AM
  4. Replies: 1
    Last Post: September 15th, 2011, 02:38 PM