System Dynamics and Agent-based Modeling in Python
The Business Prototyping Toolkit for Python (BPTK-Py) is a computational modeling framework that enables you to build simulation models using System Dynamics (SD) and/or agent-based modeling (ABM) natively in Python and manage simulation scenarios with ease.
Next to providing the necessary SD and ABM language constructs to build simulation models directly in Python, the framework also includes a compiler for transpiling System Dynamics models conforming to the XMILE standard into Python code.
This means you can build models in a XMILE-compatible visual modeling environment (such as Stella or iThink) and then use them independently in an Python environment.
The best way to get started with BPTK-Py is to read the Quickstart that is part ot the extensive online documentation. The Quickstart provides a single page overview of all the computational modeling techniques supported by BPTK.
- The objective of the framework is to let the modeller concentrate on building simulation models by providing a seamless interface for managing model settings and scenarios and for plotting simulation results.
- The BPTK-Py framework supports System Dynamics models in XMILE Format, native SD models using a domain-specific language for System Dynamics (SD DSL) and native Agent-based models. You can also build hybrid SD-ABM-Models natively in Python.
- All plotting is done using Matplotlib.
- Simulation results are returned as Pandas dataframes and thus can easily be used for analytics.
- Model settings and scenarios are kept in JSON files. These settings are automatically loaded by the framework upon initialization, as are the model classes themselves. This makes interactive modeling, coding and testing very painless, especially if using the Jupyter notebook environment.
BPTK-Py is developed and maintained by transentis labs.
The first place to go to for help and installation instructions is the online documentation.
The Quickstart provides a single page overview of all the modeling techniques supported by BPTK.
The online documentation is generated from an extensive set of Jupyter notebooks, the BPTK Tutorial. The tutorial is available as a git repository on GitHub.
Our Business Prototyping Toolkit Meetup Group gathers online regularly. This is a good place to see BPTK in action, ask questions and suggest new features. We record every session and you can view past recordings on the meetup homepage.
We used BPTK to build our implementation of the infamous Beer Distribution Game.
Our beergame repository contains Jupyter notebooks that analyse the Beergame in-depth and also provides XMILE, SD DSL and Agent-based versions of the Beergame.
For any questions our suggestions you have regarding BPTK, please contact us at: support@transentis.com.
- BPTKServer:
run
endpoint now also works for agent-based models - Model: Add
configure_agent
,configure_properties
anddelete_agent(s)
methods - Bump versions of key dependencies
- BPTKServer: Add new endpoint
start-instances
that starts multiple instances in one goo
- BPTK: Improve handling of floating point numbers when using small DTs
- ScenarioManagerSD: Fixed an issue that caused models with biflows to be cloned incorrectly
- BPTK: Fix that caused a crash when using multiple scenario files for hybrid models
- BPTK: Fix bug in reset_scenarios for Hybrid Scenario Managers
- BPTK: Update dependencies of Pandas/Matplotlib/Sympy/Parsimonious/Pyyaml/Xlsxwriter/Jinja2/Requests/Jsonpickle/Flask
- Successfully tested with Python 3.11
- BPTK: Fix imports of SimpleDashboard class
- BPTK: Update dependency of Scipy, Numpy and Pyyaml
- BPTK: reset_cache now also resets the data collector in agent based models
- BPTK: reset_cache calls the reset_cache method on all agents
- BPTK: agents now have a reset_cache method that can be used to reset agent state
- BPTK: Updated dependency on ipywidgets to 8.0.4
- BPTK Server: Remote authorization for root, full-metrics and metrics endpoints
- BPTK Server: Add /healthy endpoint
- BPTK Server: stop-instance and load-state are now POST resources
- Bug Fix: Remove debug print message
- BPTK: Defer import of matplotlib and ipywidgets until they are needed
- BPTK: Hybrid scenarios can now be spread accross multiple scenario files
- BPTK: Remove filename attribute from hybrid scenario manager as it is obsolete
- BPTK: Scenario definitions for SD DSL scenarios now accept a runspecs setting to override starttime, stoptime and dt
- BPTK: begin_session now accepts a settings parameter to override scenario settings
- BPTK Server: begin_session now accepts a settings parameter to override scenario settings
- BPTK Server: Simplified bearer token authentication
- BPTK Server: Added optional bearer token authentication
- BPTK Server: Fixed unsafe external state adapter code
- BPTK Server: start_instance now returns correct content type
- SimpleDashboard: Deleted superfluous import statement
- bptk: Small improvements to documentation
- BPTK Server: The run_step methods now also support agent-based models
- BPTK Server: A new endpoint streams_steps runs all the steps of a model and streams the results, especially useful for models that run for a long-time.
- SimpleDashboard: A new utility class that allows easy creation of dashboards based on Jupyter Widgets.
- BPTK Server: Since v 1.5.0, BPTK Server requires a Python version >= 3.9. The package now informs the user about this upon import. Other parts of the BPTK framework should work fine with older Python3 versions.
- BPTK Server: Added file adapter as a concrete example of an external adapter
- BPTK: Improve cleanup of resources when bptk instance is destroyed
- BPTK Server: Improvements to the new state externalisation feature
- BPTK Server: Improve cleanup of resources when an instance times out
- SD DSL: Added a new module class that makes it easy to structure large models
- BPTK Server: Add new endpoints to allow externalising the instance state
- BPTK Server: Changed the default timeout for equations for instances to 12h
- SD DSL: fix to delay function
- Small bugfixes in prometheus metrics.
- BPTK: Fix to plot_lookup
- BPTK Server: 'flat' version of session results
- SD DSL: small changes to SD functions pulse and delay to make them more robust.
- BPTK Server: add metrics endpoint
- SD DSL: fixed missing imports that caused some sddsl functions to throw an exception
- BPTK Server: fixed an issue that could lead to race conditions leading to server crashes
- BPTK Server: add a keep-alive method to keep instances from timing out
- BPTK Server: start-instances now accepts a timeout structure so that instance timeouts can be set flexibly.
- BPTK Server: fixed an issue that could lead to race conditions leading to server crashes
- XMILE: fixed an issue regarding lookups (graphical functions) that do not contain values on the x-axis
- BPTK: fixed an issue in train_scenarios which caused an exception in recent versions of bptk-py
- XMILE: Array arithmetic (such as array multiplication,...) within array functions such as SUM is now handled correctly.
- XMILE: Arrayed variables that have non-arrayed inputs with "apply to all" set are now handled correctly
- XMILE: Ensure the startime and stoptime in transpiled XMILE models are floating point values (e.g. 1.0, not 1)
- XMILE: Top level variables in a model that contains modules are now referenced correctly from submodules
- XMILE: Nested modules are now handled correctly
- XMILE: Array dimensions of length <3 are now handled correctly
- Add new method session_results to bptk and corresponding endpoint session-results to Bptk server that allows the session results so far to be retrieved.
- Fixed an issue that caused bptk-server to crash if start-instance was called after a previous instance had timed out
- Small improvements to the XMILE/SMILE parser regarding element names that include special characters and regarding operators surrounded by newlines.
- Return message from end-session resource now correctly reads "session terminated"
- Rename package due to issues with test pypi
- Add missing package in setup.py
- Fix bug in run_scenarios that arose with multiple scenario managers and when return_format was json or dict
- Add begin_session, end_session and run_step methods to bptk
- Add agent endpoint to BptkServer
- BptksServer now takes a bptk_factory function instead of a bptk object in its constructor
- Add start-instance endpoint to BptkServer
- Add begin-session, end-session and run-step endpoint to BptkServer
- Improve documentation
- Bugfix for bptk.export_scenarios
- Major tidy up of the bptk API, including a number of breaking changes. In particular the run_simulation method has been renamed to run_scenarios and reset_simulation_model was renamed to reset_scenario_cache. A number of rarely used methods have been removed.
- Documentation improved and extended.
- Internal refactoring.
- Fix bug in XMILE compiler regrading parsing of names that start with a keyword
- Fix bug in XMILE compiler that causes parsing of if/then/else structures to fail under some circumstances
- Fix bug in XMILE pulse function that causes the function to misbehave in some circumstances
- Fix bug in XMILE previous function that causes the function to misbehave in some circumstances
- Improve error handling and fault tolerance on BptkServer
- Update to BptkServer internals
- Add a new experimental feature that allows REST APIs for simulation models to be set up easily.
- SD DSL: Add python power operator (**) to all SD DSL operators
- XMILE: Ensure SAFEDIV works in complex expressions
- Improve handling of SAFEDIV in SD compiler
- Fix for the SD compiler regarding dimension names usage
- Little fix regarding the SD-Compiler
- Improvement of SD compiler: Support for empty initial value of
DELAY
function. Support for dimension names as arguments for functions (e.g.SIZE(<dimension>)
)
- Improvement of Extended Data Collector: Renamed to Agent Data Collector and code optimizations
- Bugfix for the plotting component that solves compatibility issues with newer versions of matplotlib
- New dataCollector for retrieving agent-wise data (Refer to BPTK-Py Tutorial for more info)
- Fixed a bug in the interactive scenario component that caused scenarios being plotted multiple times
- Fixed a bug that caused the SD operator
delay
to not accept floating point values and fixed a bug that caused the same to be parsed incorrectly in some cases.
- Fixed a bug in the XMILE Converter that prevented the SAFEDIV operator to be parsed correctly
- SD-DSL: Fixed a bug that caused converter equations (subtraction and division) to be computed incorrectly.
- SD-DSL: Added support for right-hand side addition and subtraction to support equations such as
converter.equation = 1 - b
- Visualisation for SD-DSL elements now follows conventions to work properly with Matplotlib 3.3+
- Visualisation using matplotlib now follows conventions to work properly with Matplotlib 3.3+
- SD-DSL: System Dynamics element such as converters did not implement comparison operators (">", "<", ">=", "==", "!="). They have been added
- Another bugfix for series renaming. Simplified the code for renaming by using Pandas' standard method
rename
- Bugfix for
plot_scenarios
: Theseries_names
replacer did not work properly for when only one scenario manager / scenario is given.
- Bugfix for XMILE compiler: A little error in the parser prevented certain models being parsed correctly
- Bugfix for
plot_scenarios
: The new error messages showed up for Agent based models although the scenarios were present
- The XMILE compiler is a great tool that handles model conversion from XMILE SD Models to Python. For compatibility and readability, we change the equation names to camelCasing upon conversion. This might be confusing for some users. That's why we decided to give you a new function call that lists all equations for System Dynamics Models. Simply run
bptk.list_equations()
(optionally add scenario manager(s) and scenario(s)) and get an overview over available model elements. More details in our documentation. - Improved error messages. In previous versions, a long error trace was printed when an equation was not found. Now you get a neat error message output wiht hints as to why the plotting failed.
- If an equation / scenario / scenario manager is not found,
BPTK_Py
gives hints on which similar equations / scenarios / scenario managers might be available for use. - Register XMILE models without having to follow the directory structure:
BPTK_PY
scans thescenarios
folder upon startup to find new scenario managers and XMILE / ABM models. We developed a simpler way to add simulation models during runtime without having to add scenarios beforehand:bptk.register_model("<path_to_itmx_stmx>","<modelname>")
. You can then easily simulate the model just as you're used to.
XMILE equations make use of double-quote enclosed identifiers in case it actually looks like a function call. For example, 100*"Identifier(enclosed)"
is a valid equation where one element (stock/flow) is called Identifier(enclosed)
. However, we were not able to parse this, until now.
Update BPTK-Py using the new update mechanism: documentation
We figured that the update mechanisms via pip
might be confusing sometimes, especially for non-programmers. This is
why we decided to implement an update mechanism. Details are available in the documentation
- Bugfix to (XMILE) SD Compiler: Added support for array expressions within function calls. We had trouble with equations that contain another expression within a function call. E.g.
DELAY(arrayedElement[1,2]*5, 1, 1)
was not supported. - Improvement to (XMILE) SD Compiler: Removed replacement of currency symbols (
€
,$
etc.) and percentage signs with abbreviations. We had implemented this in earlier releases but figured it leads to confusion with modellers.
- The SD DSL now differentiates flows and biflows. Simply add a biflow using
biflow = model.biflow(<name>)
. - The SD DSL now supports: RANDOM, IF, NOT, AND, OR, NAN, SQRT, ROUND and all trigonometric and statistical builtins you know from XMILE. Furthermore the operators support Comparison Operators (>, <, >=, <=, ==, !=) and the modulo operator (x % y).
- RANDOM:
converter.equation = sd.Random(<min>, <max>)
draws a uniformly distributed float random number between and - ROUND:
converter.equation = sd.Round(sd.random(0,1),2))
rounds a random number between 0 and 1 to a 2 digit float - IF:
converter.equation = sd.If( <condition>, <then> , <else> )
corresponds toIF <condition> THEN <then> ELSE <else>
. Each term inside the IF clause can be a SD term again. Example for a valid If clause:equation = sd.If(sd.time()>10,sd.random(1,2), 100)
- AND/OR:
sd.Or(<left hand side>, <right hand side>)
andsd.And(<left hand side>, <right hand side>)
for multiple conditions - NOT: Use
sd.Not(<condition>)
for "not" conditions, e.g:sd.If(sd.Not(sd.time()>10), 1, 0 )
- NAN / INF / PI:
sd.nan()
returns a NAN value,sd.Inf()
gives you the infinity value,sd.pi()
returns the number pi. - SQRT:
sd.sqrt(<value of function>)
computes the square root - SIN / TAN / COS:
sd.sin(x) / sd.cos(x) / sd.tan(x)
for sinus, cosinus or tangent of x (radians) and of course we also support ARCCOS, ARCSIN, ARCTAN with the same syntax - SINWAVE / COSWAVE:
sd.sinwave(amplitude,period)
/sd.coswave(amplitude,period)
to generate sine / cosine waves with given amplitude and period - More documentation and how to use the statistical (random numbers from various distributions) and trigonometric operators can be found in our online documentation
- RANDOM:
- We fixed a bug that caused BPTK to crash when an XMILE model was updated while BPTK was monitoring it
- We fixed SINWAVE in the XMILE transpiler and added support for COSWAVE
- We are supporting all XMILE operators now. Note that random numbers with seed are never the same as when using Stella Architect's seed! This is due to different random number generators in Python and Stella. We neither support the min / max arguments for the random number operators. Refer to the documentation)
- RUNCOUNT and SENSIRUNCOUNT are not supported and support is not planned.
- Bugfix release: Better support for multidimensional arrays
- Bugfix release: Fixed an issue with plot_lookup
- SD Compiler: Added new operators
- Arrays and Array Operators (MIN, MAX, SUM, MEAN, SELF, SIZE, PROD)
- Statistical operators (COMBINATIONS, BETA, BINOMIAL, FACTORIAL, GAMMA, GAMMALN, EXPRND, GEOMETRIC)
- Trigonometric operators (ARCSIN, ARCCOS, ARCTAN)
- The
plot_scenarios
API now supports array calls such asstock[*]
orstock[dim1,dim2]
- SD DSL: ABS, DT, PULSE, STARTTIME, STOPTIME