{"data":{"markdownRemark":{"html":"<p>The <code class=\"language-text\">Model</code> is the main abstraction in the Simudyne SDK. It incorporates all the elements required to build and run a simulation from setup to teardown.</p>\n<p>The <code class=\"language-text\">Model</code> Interface below outlines three stages. Let's describe them as well as the model flow in more detail.</p>\n<h2 id=\"model-lifecycle\"><a href=\"#model-lifecycle\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Model Lifecycle</h2>\n<p>The lifecycle of a model is organized through four different functions:</p>\n<ul>\n<li>the initialization of the model through <a href=\":version/reference/modelling/model-interface#modelinit-initializing-your-model\">Model#init</a></li>\n<li>the setup of the simulation through <a href=\":version/reference/modelling/model-interface#modelstep-defining-computations-and-operations-at-each-step\">Model#setup</a></li>\n<li>the core logic of your simulation called through <a href=\":version/reference/modelling/model-interface#modelstep-defining-computations-and-operations-at-each-step\">Model#step</a></li>\n<li>the tear-down of the simulation through <a href=\":version/reference/modelling/model-interface#modeldone-specifying-a-tear-down\">Model#done</a></li>\n</ul>\n<p>Except for Model#step, each of these function is only called once during the entire simulation. Model#step will be called as many time as you would have requested.\nEach step will run after the previous one has been completed, creating a discrete series of events.</p>\n<p>Models can easily be controlled through the console, the GUI provided by the SimudyneSDK. However other method are also available:</p>\n<ul>\n<li>using the <strong>REST API</strong> (documentation is available <a href=\":version/rest_api/rest_api\">here</a>)</li>\n<li>using the <strong>CLI</strong> (documentation is available <a href=\":version/reference/run_deploy/cli_run\">here</a>)</li>\n<li>using the <strong>ModelRunner API</strong> (documentation is available <a href=\":version/reference/run_deploy/multirun_setup\">here</a>)</li>\n</ul>\n<h2 id=\"init---initializing-your-model\"><a href=\"#init---initializing-your-model\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>init() - initializing your model</h2>\n<p>The <code class=\"language-text\">Model</code> interface has an init phase, which gets executed when the model is initialised, this is where you should register your <code class=\"language-text\">Links</code>, <code class=\"language-text\">Agents</code> and create your <code class=\"language-text\">Accumulators</code>, if your <code class=\"language-text\">Model</code> is an <code class=\"language-text\">AgentBasedModel</code>. To execute code during model initialisation, the <code class=\"language-text\">Model#init()</code> method should be overridden with specific initialisation operations.</p>\n<p>The <code class=\"language-text\">Model#init()</code> method can also be used to setup model variables.</p>\n<p>Nothing happens in this init phase by default but you can trigger a specific init by overriding the <code class=\"language-text\">Model#init()</code> method to define initialisation operations here.</p>\n<p>When creating Agent Based Models, the init phase is where accumulators are created, and agents and links are registered. For more information on this see <a href=\":version/reference/modelling/model-initialisation\">Model Initialisation</a>.</p>\n<h2 id=\"setup---defining-pre-simulation-calculations-and-operations\"><a href=\"#setup---defining-pre-simulation-calculations-and-operations\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>setup() - defining pre-simulation calculations and operations</h2>\n<p>The <code class=\"language-text\">Model</code> interface has optional setup phase. In the Model Interface diagram this occurs when the console calls <code class=\"language-text\">setup()</code> on the model.</p>\n<p>Nothing happens in this setup phase by default but you can trigger a specific setup by overriding the <code class=\"language-text\">Model#setup()</code> method and defining calculations and operations here.</p>\n<p class=\"code-header\">Specifying a setup (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyModel</span> <span class=\"token keyword\">implements</span> <span class=\"token class-name\">Model</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation punctuation\">@Variable</span> <span class=\"token keyword\">double</span> myVariable<span class=\"token punctuation\">;</span>\n\n  <span class=\"token annotation punctuation\">@Override</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">void</span> <span class=\"token function\">setup</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    myVariable <span class=\"token operator\">=</span> ModelContext<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">getPRNG</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">uniform</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">sample</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2 id=\"step---defining-computations-and-operations-at-each-step\"><a href=\"#step---defining-computations-and-operations-at-each-step\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>step() - defining computations and operations at each step</h2>\n<p>One iteration in your simulation involves a set of <code class=\"language-text\">step</code>s. At this stage, you need to specify calculations in your model to form a <code class=\"language-text\">step</code>. These are then run in order to complete one iteration of your simulation.</p>\n <p class=\"code-header\">Defining a step (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyModel</span> <span class=\"token keyword\">implements</span> <span class=\"token class-name\">Model</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation punctuation\">@Variable</span> <span class=\"token keyword\">double</span> myVariable <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token annotation punctuation\">@Override</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">void</span> <span class=\"token function\">step</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    myVariable <span class=\"token operator\">*=</span> <span class=\"token number\">1.1</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2 id=\"done---specifying-a-tear-down\"><a href=\"#done---specifying-a-tear-down\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>done() - specifying a tear-down</h2>\n<p>On completion of your simulation, you may want to save results as a file or to perform specific tear-down operations. This can be done by defining <code class=\"language-text\">done()</code> in your model.</p>\n<p>If you wish to in another section tell your model to complete (say if a variable condition is reached) you can call the <code class=\"language-text\">finish()</code> function. This will start the process of the teardown and call the <code class=\"language-text\">done</code> function. </p>\n<p>This will also call any enabled output channel to post their final results and in conjunction with a <code class=\"language-text\">ModelRunner</code> a user can make usage of the <code class=\"language-text\">awaitOutput</code> function before either calling additional Java code or <code class=\"language-text\">System.exit(0);</code></p>\n<h2 id=\"introducing-the-agent-based-flavour\"><a href=\"#introducing-the-agent-based-flavour\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introducing the Agent Based flavour</h2>\n<p>To create Agent Based Models, extend <code class=\"language-text\">AgentBasedModel</code>.</p>\n<h3 id=\"accessing-the-context-and-the-configuration\"><a href=\"#accessing-the-context-and-the-configuration\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Accessing the Context and the Configuration</h3>\n<p>From the <code class=\"language-text\">AgentBasedModel</code>, you can get access to the <code class=\"language-text\">ModelContext</code> using the <code class=\"language-text\">getContext()</code> method. This <code class=\"language-text\">ModelContext</code> contains the PRNG of your model, as well as the Tick value, the Configuration, and more.\nThe Configuration contains the set of values for the parameters of the model. These specific settings within your simulation are important especially when it comes to distributing your model.</p>\n<p><a href=\":version/reference/modelling/model-configuration\">More on Model Config</a></p>\n<p class=\"code-header\">AgentBasedModels (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">public</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyModel</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">AgentBasedModel</span><span class=\"token generics function\"><span class=\"token punctuation\">&lt;</span>GlobalState<span class=\"token punctuation\">></span></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation punctuation\">@Variable</span> <span class=\"token keyword\">int</span> myVariable<span class=\"token punctuation\">;</span>\n\n  <span class=\"token annotation punctuation\">@Override</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">void</span> <span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">registerAgentTypes</span><span class=\"token punctuation\">(</span>AgentA<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">,</span> AgentB<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token function\">registerLinkTypes</span><span class=\"token punctuation\">(</span>LinkA<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">,</span> LinkB<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token function\">createDoubleAccumulator</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"accA\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token function\">createDoubleAccumulator</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"accB\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n\n  <span class=\"token annotation punctuation\">@Override</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">void</span> <span class=\"token function\">setup</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    SeededRandom ran <span class=\"token operator\">=</span> <span class=\"token function\">getContext</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">getPRNG</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// accessing the prng of the model through the Context</span>\n    myVariable <span class=\"token operator\">=</span> ran<span class=\"token punctuation\">.</span><span class=\"token function\">uniform</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">sample</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token annotation punctuation\">@Override</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">void</span> <span class=\"token function\">step</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    myVariable <span class=\"token operator\">+=</span> <span class=\"token function\">getContext</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">getTick</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// accessing the current Tick of the Model</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Several setup operations are required for <code class=\"language-text\">AgentBasedModel</code> at the first stage of building the simulation model. These have to be defined at <code class=\"language-text\">setup()</code>.</p>\n<p><a href=\":version/reference/agents\">More on Agent Based Models</a></p>","headings":[{"value":"Model Lifecycle","depth":2},{"value":"init() - initializing your model","depth":2},{"value":"setup() - defining pre-simulation calculations and operations","depth":2},{"value":"step() - defining computations and operations at each step","depth":2},{"value":"done() - specifying a tear-down","depth":2},{"value":"Introducing the Agent Based flavour","depth":2},{"value":"Accessing the Context and the Configuration","depth":3}],"frontmatter":{"title":"Model Interface","toc":null,"experimental":null}},"site":{"siteMetadata":{"title":"Simudyne Docs","latestVersion":"2.6"}}},"pageContext":{"absolutePath":"/home/vsts/work/1/s/content/2.5/reference/modelling/model-interface.md","versioned":true,"version":"2.5","kind":"reference","pagePath":"/reference/modelling/model-interface","chronology":{"prev":{"name":"Modelling","path":"/reference/modelling"},"next":{"name":"Model Initialisation","path":"/reference/modelling/model-initialisation"}},"lastUpdated":"2026-04-21T13:56:54.862Z"}}