Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Trouble with simulation output

runtime simulation

  • Please log in to reply
3 replies to this topic

#1 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 06 April 2011 - 07:29 PM

Hello, I am having a display issue with this program. Just need some extra eyes.
I am trying to
1. determine the max line (only displays 1):cursing::cursing:
2. randomize arrival time and service time:cursing::cursing:
3. dequeue when service complete
note: sorry my queue class is cleaner than the simulator class(where the issue is)
public class LineSimulator {

    private static final int SIMULATION_TIME = 1440;
    private static int timePassed = 0;
    private static int custNum;
    private static int totalCustomers;
    private static int serviceTime;
    private static int maxLineLength;
    private static int longestWait;
    private static double avgWaitTime;
    private static String avgLineLength;
    private static int nextArrival;

    public static void main(String[] args) {

        int i = 1;
        int firstCustArrival = (int) Math.random() * SIMULATION_TIME + 1;
        serviceTime = (int) Math.random() * SIMULATION_TIME + 1;
        ShoppingLineQueue line = new ShoppingLineQueue();
        System.out.println("Little's Law Test\nA Shopping Line\n");

        while ((SIMULATION_TIME - timePassed) != 0) {

            if (timePassed == firstCustArrival) {
                custNum = i;
                line.addCustomer(custNum, serviceTime);
                System.out.println("Customer: " + custNum + " Arrived after "
                        + "opening: " + firstCustArrival + "minutes Service "
                        + "time: " + serviceTime);
                maxLineLength = line.size();
                nextArrival = (int) Math.random()
                        * 100 + 1;
            }
            nextArrival = (int) Math.random()
                    * 100 + 1;
            if (timePassed  != firstCustArrival) {
                i++;
                custNum = i;
                line.addCustomer(custNum, serviceTime);
                System.out.println("Customer: " + custNum + " Arrived after "
                        + "opening: " + nextArrival + "minutes Service "
                        + "time: " + serviceTime);
                nextArrival = (int) Math.random()
                        * (SIMULATION_TIME - timePassed) + 1;
                if (maxLineLength < line.size()) {
                    maxLineLength = line.size();
                }
            }

            if (timePassed == firstCustArrival) {
                if (line.seeFirstInLine() == timePassed) {
                    if (line.removeCustomer() == "UnderFlow") {
                        System.out.println("No Arrival Yet");
                    } else {
                        line.removeCustomer();
                    }
                }
            }

            serviceTime += ((int) Math.random()
                        * 100);
            timePassed++;
        }
        avgWaitTime = SIMULATION_TIME / custNum;
        System.out.println("Max number of customers in line at "
                + "any one time: " + maxLineLength + "\n"
                + "Longest wait time for one customer: " + longestWait
                + "\nAverage wait time for all customers: " + avgWaitTime
                + "\nAverage line length: " + avgLineLength + "\n");
    }
}

Here is the queue:
public class ShoppingLineQueue<Customer> implements Iterable<Customer> {

    private int lineSize;
    private ShopperNode firstPerson;
    private ShopperNode lastPerson;

    /**
     * Constructor
     * Big O(1)
     */
    public ShoppingLineQueue(){
        firstPerson = null;
        lastPerson = null;
    }// end of constructor

    /**
     * Check if queue empty
     * Big O(1)
     * @return boolean
     */
    public boolean isEmpty(){
        return firstPerson == null;
    }// end of isEmpty

    /**
     * Get the size of queue
     * Big O(1)
     * @return size of line
     */
    public int size(){
        return lineSize;
    }// end of size

    /**
     * Look at first person in line
     * Big O(1)
     * @return first person
     */
    public int seeFirstInLine(){
        if(isEmpty()){
            throw new RuntimeException("Underflow");
        }
        return firstPerson.timeInLine;
    }// end of seeFirstInLine

    /**
     * Add customer to end of line
     * (Enqueue)
     * Big O(1)
     * @param customer
     */
    public void addCustomer(Customer customer, int time){
        ShopperNode shopper = new ShopperNode();
        shopper.cust = customer;
        shopper.timeInLine = time;
        if(isEmpty()){
            firstPerson = shopper;
            lastPerson = shopper;
        } else {
            lastPerson.nextInLine = shopper;
            lastPerson = shopper;
        }// end of if else
        lineSize++;
    }// end of addCustomer

    /**
     * Remove first customer after service complete
     * (Dequeue)
     * Big O(1)
     * @return customer
     */
    public Customer removeCustomer(){
        if(isEmpty()){
            throw new RuntimeException("Line is empty");
        }
        Customer customer = firstPerson.cust;
        firstPerson = firstPerson.nextInLine;
        lineSize--;
        if(isEmpty()){
            lastPerson = null;
        }
        return customer;
    }// end of removeCustomer

    /**
     * Iterates through list
     * Big O(1) but is really Big O(n) when returning
     * @return new FIFOIterator
     */
    public Iterator<Customer> iterator() {
        return new FIFOLineIterator();
    }// end of Iterator

    /**
     * Standard Iterator
     * Big O(n) worst case
     */
    private class FIFOLineIterator implements Iterator<Customer>{
        private ShopperNode pos = firstPerson;

        public boolean hasNext(){
            return pos != null;
        }

        /* not implemented */
        public void remove(){
            throw new UnsupportedOperationException();
        }

        public Customer next(){
            if(!hasNext()){
                throw new NoSuchElementException();
            }// end of if
            Customer customer = pos.cust;
            pos = pos.nextInLine;
            return customer;
        }// end of next
    }// end of FIFOLineIterator private class

    /**
     * Node for shopper
     */
    private class ShopperNode{
        private Customer cust;
        private int timeInLine;
        private ShopperNode nextInLine;
    }// end of shopper Node private class

}// end of ShoppingLineQueue class
The code displays this(basically):
Customer: 1      Arrived after opening: 1...    ....   ....
...
...
...
Customer: 1433 Arrived after opening: 1minutes Service time: 1
Customer: 1439 Arrived after opening: 1minutes Service time: 1
Customer: 1440 Arrived after opening: 1minutes Service time: 1
Max number of customers in line at any one time: 1438
Longest wait time for one customer: 0
Average wait time for all customers: 1.0
Average line length: null
Thanks
  • 0

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 April 2011 - 07:45 PM

One of your problems may be here:
int firstCustArrival = (int) Math.random() * SIMULATION_TIME + 1;
Your Math.random number is casted to an int before you multiply any values out.
So the decimal part of Math.random is lost which results in 0*1440+1.

Same idea with:
serviceTime = (int) Math.random() * SIMULATION_TIME + 1;

I'm still skimming the code...
  • 0

#3 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 April 2011 - 07:50 PM

Also here:
            nextArrival = (int) Math.random()
                    * 100 + 1;
and here
            serviceTime += ((int) Math.random()
                        * 100);

Edit**: And inside of your first 2 if's.

It might look something like:
someTime += (int) (Math.random() * someNum + someOtherNum);

The logic is confusing me. It seems your second if is the only statement ever evaluated to true. :
            if (timePassed  != firstCustArrival) {
            	//System.out.println("second if");
                i++;
                System.out.println(i);
                line.addCustomer(custNum, serviceTime);
                System.out.println("Customer: " + custNum + " Arrived after "
                        + "opening: " + nextArrival + "minutes Service "
                        + "time: " + serviceTime);
                nextArrival = (int) (Math.random()
                        * (SIMULATION_TIME - timePassed) + 1);
                if (maxLineLength < line.size()) {
                    maxLineLength = line.size();
                }
            }

Because timePassed != firstCustArrival is always true.
This causes i++ to be carried out 1440 times.
So in the end 1440 divided by 1440 equals 1.

That's all I can do for now. (Very late here :( )
Hopefully the Guru's can step in and be of more assistance.
  • 0

#4 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 07 April 2011 - 05:55 AM

@lethalwire
Thanks for the second pair of eyes.
After I posted this I looked at the code and noticed that I have to create an event that is bounded
with the current time and not over because the time in line would exceed the time of simulation.
Thanks for pointing out the casting...foolish error on my behalf. I was getting tired after writing the
queue class and testing if it worked. I also have to display the customer is leaving after removing
from the queue...in other words... I think I have to rewrite the main method from the beginning which
shouldnt be tooo bad after a good night sleep...ahhh... Ill post the edited code when finished....
..or close to being finished.
Thanks again
  • 0





Also tagged with one or more of these keywords: runtime, simulation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download