I’m trying to simulate a DVD production line in Java. The thing I have a problem with is the calculation of the time in this kind of simulation and the number of DVDs per hour.
The production line consists of 4 machines, each of which spends a certain random time working on 1 DVD. The third one can work on more than 1 DVD at the same time (max. 100). Also, the third one will always accept more DVDs to work on, as long it’s capacity is < 50%, which I plan to label as m3Capacity
. But for now, let’s just ignore the capacity characteristic.
What I have so far is that, with a random number generator (since each machine has random DVD processing time each time it starts working on a new DVD), I assigned a random processing time to each of the four machines, summed up their processing times for the total time spent in one-go production (totalProdTime = processTime1 + processTime2 + processTime3 + processTime4
), and then I keep summing up those totalProdTime values until the total time of the whole simulation reaches the desired duration of the simulation specified by the user (totalProdTimeSim = totalProdTimeSim + mTotal.totalProdTime
). Later I just verify whether the totalProdTimeSim has reached the desired time and that’s it.
Now, the problem is that, this way, the production of the DVDs waits for one instance of production to finish all the way until the very end of the machine 4, and then the work begins on the second DVD, etc. What I want to do is that after machine 1 finishes it’s work, and the second one begins its work, machine 1 begins work on another DVD with it’s newly generated time. The same goes for all other machines. After machine 2 finishes work on a DVD, it immediately begins working on another DVD.
Now, since all machines have random processing times each time they start working on another DVD, it will often happen that one machine ‘wants to work on another DVD’ while the previous one hasn’t finished its work on the current one. In this case, the idle machine just ‘waits’ for the previous machine to finish processing and then accepts the DVD from the previous machine.
So, in my example, we have that all the machines work one by one, but I want them all to work constantly, even after they finish working on the current DVD (except in the first iteration, where the further machines need to wait for the previous ones to forward them their DVD they previously worked on).
How do you think I should calculate the total simulation time and DVD output? I’ve been thinking last night about it, but haven’t gotten any ideas.
My sketch of how it should work can be found in the picture below. In the sketch all machines behave as if they constantly have something to work on, but should wait from the input from the previous machine.
Thanks in advance!
1
An odd question, and I’m not really sure it fits here… Here is a brute-force approach.
If I were to write code to solve this, I would create a ProductionLine class that contains a list of Machine-objects, along with a DiskOuput counter. The ProductionLine-class would have a method (‘timePasses’) you can call to simulate the passing of time, say 1-second increments. This would in turn call onto the Machine-objects who would also have a timePasses method. The Machine knows how many calls of timePasses are needed before it finishes wirting a disk (the random amount of seconds). When a Machine finishes writing a disk, the timePasses object would return a value to indicate a disk was finished. The Machine would then reset the random number of seconds needed to write a disk and start over. The ProductionLine class knows that when timePasses returns a value, the DiskOuput counter should be incremented because a disk was finished.
You now have an application where all machines start at the same time. You can run this with a loop until a certain amount of time passes or until DiskOuput reaches a certain value.
Now, to make your machines dependent on the first disk… Your ProductionLine would have a list of Machines (the ‘pool’ of available machines in the line) and a list of RunningMachines (that are currently producing something). You would only call timePasses on running machines. When you call timePasses on ProductionLine the first time it would take a Machine from the pool, put in onto RunningMachines and call timePasses on it. Every time a call to timePasses on a Machine indicates that a new disk is ready, the ProductionLine could check if additional machines are available and if so, put them onto the RunningMachines-list and start working with it.
What you are looking for is called Discrete Event Simulation. There are multiple libraries you can use to help you.
Your situation is almost textbook example of this kind of simulation. Each of the lines is an “agent”, that when it receives a DVD, it picks a random time it will take to process and asks the system to “notify it” (creates an event) when that time comes. When the system notifies the line, it will mark current DVD as processed, passes it to next line and picks next one. Each of those lines (or agents) is independent and behaves almost like a separate “thread”. The system is then responsible to “progress the time” by picking next event in the queue and executing it. You can then repeat this simulation multiple time with different parameters or different random times to get some stochastic metrics about the system.