// TODO // add controls for: // serverRate // customerArrivalRate // numServers // An applet to simulate an M/M/n queue // which means: arrival rate/service time/number of servers // todo: have arrival rate of one queue, need to add: // number of servers // service time modelling float customerRate = 20; // 1 means 1 per second float serverRate = 3; int numServers = 5; int numCustomers=0; Queue[] queues; int numQueues = 150; void setup(){ queues = new Queue[numQueues]; for(int qu=0; qu t*1000){ last = currentTime; lastt = t; t = findt(rate); return true; }else{ return false; } } // Is there anyone at the front of the queue // And is there a free server void seeIfWeCanServeAnyone() { // iterate through servers and allocate customers for(int s=0; s 0){ servers[s].allocateCustomer(customers[0]); shuffleCustomersUp(); } } } } void shuffleCustomersUp(){ // we want to get rid of the first entry in the array Customer[] nC = new Customer[500]; // put all the customers we have in an array for(int c=0; c0){ nC[c-1] = customers[c]; } } numCustomers--; customers = nC; // As we've less customers, need to tell them all they should be further along for(int c=0; c xDest){ // if customer's been served, we just want to shuffle him off fast if(served){ xPos = xPos-1; }else{ xPos = xPos - 2; } } if(served){ stroke(255,0,0); }else{ stroke(0,0,0); } point(xPos, yPos); //point(xPos+1, yPos); } } class Server { // Each server can serve at most one customer at a time Customer cm; Boolean busy; float rate; float t, last, lastt; Server(float r) { busy = false; rate = r; last = 0; t = findt(rate); } void allocateCustomer(Customer c){ cm = c; cm.setServed(); busy = true; } Boolean seeIfDoneServingCustomer(){ Boolean areWe = areWeDone(); if(areWe){ busy = false; } return areWe; } Boolean isFree(){ return (! busy); } Boolean areWeDone(){ int currentTime = millis(); if(currentTime-last > t*1000){ last = currentTime; lastt = t; t = findt(rate); return true; }else{ return false; } } void draw(){ if(busy){ cm.draw(); } } } // function to generate a waiting time float findt(float rate){ return -1/rate * (float)Math.log(1 - Math.random()); }