Friday 7 June 2013

NSimulate: a discrete-event simulation library for .NET

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/

2 comments:

  1. Coin Casino: No deposit bonuses for Real Money, Bitcoin
    Coin Casino: No 코인카지노 deposit bonuses for Real 온카지노 Money, Bitcoin, Btc, C$500 Bonus Code, No Deposit Bonus · Claim 퍼스트 카지노 $50 free and get 50 free spins no deposit. · Sign up and

    ReplyDelete
  2. Casino games on king slot machine | The best casino game
    A guide to playing air jordan 18 retro varsity red my site the 벳 페어 most air jordan 18 retro men blue online site popular slots and real air jordan 18 retro toro mens sneakers jackpots online. where to find air jordan 18 retro varsity red Find out how to play on king slot machine for free at Jordan8-retro.

    ReplyDelete