This is the first post in my blog in which I plan to talk about software development topics or announce details of some of the projects (usually open-source) that I work on from time to time.
One of my recent projects has been the development of an event simulation library for .NET. The library is open-source (MIT style license) and available on github:
http://phillp.github.io/NSimulate/
My aim for the project was to produce a flexible library that is extensible and supports simulation of processes, activities, resources and event notifications.
Some inspiration for this comes from the
Simpy (python) simulation package.
There are several examples included with the library in which I have tried to demonstrate the various usage patterns. These examples are the fairly typical set for this kind of thing, but (I hope) are implemented in a concise way.
Examples include:
- Workshop with unreliable machines
- Call-center with call escalation
- Order delivery with warehouse reorder
- Alarm Clock
Each simulation runs until an end-condition (specified as a delegate) is met.
The simulation is single-thread, and processes are implemented as iterators using the c# yield statement (so that process logic can be written cleanly and without the need for callbacks). When a process yields a result, the process logic is paused until some condition (indicated by the yield result) is met; this may be the availability of a resource, the occurrence of a specified event, or simply the passing of a certain amount of simulation time.
One example implementation of a process is shown below.
// while the simulation is running
while(true){
// check if the queue for this machine is empty
if (JobQueue.Count == 0){
// if it is, wait until there is something in the queue
yield return new WaitConditionInstruction(()=>JobQueue.Count > 0);
}
else{
// take a job from the queue
var jobToProcess = JobQueue.Dequeue();
// simulate processing the job
// which takes time
yield return new WaitInstruction(jobToProcess.ProcessingTimeRequiredByJobQueue[JobQueue]);
// use the reliability indicator to determine if the machine is broken down
if (CheckForRandomBreakdown()){
// the machine has broken down
// add the job it was processing back to the queue
JobQueue.Enqueue(jobToProcess);
// obtain a repair person
var allocateInstruction = new AllocateInstruction<RepairPerson>(1);
yield return allocateInstruction;
// and wait for the machine to be fixed
yield return new WaitInstruction(RepairTimeRequired);
// then release the repair person resource
yield return new ReleaseInstruction<RepairPerson>(allocateInstruction);
}
else{
// record the fact that the job has been processed by this machine type
jobToProcess.ProcessingTimeRequiredByJobQueue.Remove(JobQueue);
// if the job still requires other processing
if (jobToProcess.RequiresMoreWork){
// add it to the next queue
jobToProcess.ProcessingTimeRequiredByJobQueue.Keys.First().Enqueue(jobToProcess);
}
else{
// otherwise remove it from the all unprocessed jobs list
_unprocessedJobsList.Remove(jobToProcess);
}
}
}
}
All feedback is welcome and thanks for reading.
http://phillp.github.io/NSimulate/