Coder Social home page Coder Social logo

simsharp's Introduction

SimSharp Logo

Sim# (SimSharp) is a .NET port and extension of SimPy, process-based discrete event simulation framework

Build status


Disclaimer: Sim# is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Sim# is free software: Sim# can be redistributed and/or modified under the terms of the MIT License.


This is a short introduction, please refer to the documentation for more information on working with Sim# and an overview of the sample models provided.

Sim# aims to port the concepts used in SimPy [1] to the .NET world. Sim# is implemented in C# and is available via Nuget for .NET Framework 4.5 and is also .NET Standard 2.0 compliant. Sim# uses an efficient event queue (adapted from [3]) that allows to compute models very quickly. Simulating 10 years of the MachineShop sample [4], that uses preemptive resources, requires less than 1.5s on a Core i7-7 2.7Ghz. This model generates more than 5 million events.

Sim# allows modeling processes easily and with little boiler plate code. A process is described as a method that yields events. When an event is yielded, the process waits on it. Processes are themselves events and so it is convenient to spawn sub-processes that can either be waited upon or that run next to each other. There is no need to inherit from classes or understand a complex object oriented design.

To demonstrate how simple models can be expressed with little code, consider a model of an m/m/1 queuing system as expressed in the current version of Sim#:

using static SimSharp.Distributions;

ExponentialTime ARRIVAL = EXP(TimeSpan.FromSeconds(...));
ExponentialTime PROCESSING = EXP(TimeSpan.FromSeconds(...));
TimeSpan SIMULATION_TIME = TimeSpan.FromHours(...);

IEnumerable<Event> MM1Q(Simulation env, Resource server) {
  while (true) {
    yield return env.Timeout(ARRIVAL);
    env.Process(Item(env, server));
  }
}

IEnumerable<Event> Item(Simulation env, Resource server) {
  using (var s = server.Request()) {
    yield return s;
    yield return env.Timeout(PROCESSING);
    Console.WriteLine("Duration {0}", env.Now - s.Time);
  }
}

void RunSimulation() {
  var env = new Simulation(randomSeed: 42);
  var server = new Resource(env, capacity: 1) {
    QueueLength = new TimeSeriesMonitor(env, collect: true)
  };
  env.Process(MM1Q(env, server));
  env.Run(SIMULATION_TIME);
  Console.WriteLine(server.QueueLength.Summarize());
}

This model uses the monitoring capabilities introduced with Sim# 3.2. Monitoring allows describing certain variables in the model, e.g. the number of waiting items before the server. The monitor maintains a set of statistical properties such as mean, standard deviation, and can also print histograms. Monitors can be assigned to the variables exposed by resources or they can be used to track other variables.

Sim# tries to be as easy to use as SimPy, but also remains true to the .NET Framework. The most obvious difference between SimPy and Sim# is handling process interruptions. In Sim# a process that can be interrupted needs to call

if (Environment.ActiveProcess.HandleFault()) {...}

after each yield in which an interruption can occur and before continuing to yield further events. This is due to a limitation of the .NET Framework: In Python it is possible to put a try-except block around a yield statement, and an exception can be injected into the iterator. In .NET this is not possible.

Also in Sim# it was decided to base the unit for current time and delays on DateTime and TimeSpan in the simulation clock. There is however an API, called D-API (short for double-API) that allows you to use doubles as in SimPy, e.g. env.Now returns a DateTime, env.NowD returns a double, env.Timeout(delay) expects a TimeSpan as delay, env.TimeoutD(delay) expects a double, etc.. It is possible to initialize the Environment with a default timestep in case both APIs are used:

var env = new Simulation(defaultStep: TimeSpan.FromMinutes(1));

In that environment, calling env.TimeoutD(1) would be equal to calling the more elaborate standard API env.Timeout(TimeSpan.FromMinutes(1)). In case timeouts are sampled from a distribution, it is important to distinguish the TimeoutD(IDistribution<double>)and Timeout(IDistribution<TimeSpan>) methods. Again, the former assumes the unit that is given in defaultStep, e.g., minutes as in the case above. For instance, env.TimeoutD(new Exponential(2)) would indicate a mean of 2 minutes in the above environment, while env.Timeout(new Exponential(TimeSpan.FromMinutes(2)) would always mean two minutes, regardless of the defaultStep. In generally, the TimeSpan API is preferred as it already expresses time in the appropriate units.

For shortcuts of the distribution classes a static class Distributions exists. You can put using static SimSharp.Distributions; in the using declarations and then use those methods without a qualifier. The following code snippet shows this feature.

using static SimSharp.Distributions;
// ... additional code excluded
yield return env.TimeoutD(UNIF(10, 20));

References

  1. Python Simpy Package
  2. Nuget package
  3. High speed priority queue
  4. Machine Shop Example

simsharp's People

Contributors

abeham avatar crystalwindsnake avatar irubataru avatar mattkotsenas avatar nimzwei avatar tscheler avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.