{"data":{"markdownRemark":{"html":"<p>The global state is an object that can be read by all agents. If a model doesn't need global variables, the model and all of its agents must be parameterized with the <code class=\"language-text\">GlobalState</code> type to compile correctly. If a model does need global variables, create a class for those global variables which extends <code class=\"language-text\">GlobalState</code>, then parameterize the model and its agents with that type.</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token comment\">// Model and agent using GlobalState.</span>\n<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><span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">public</span> <span class=\"token keyword\">static</span> <span class=\"token keyword\">final</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyAgent</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Agent</span><span class=\"token generics function\"><span class=\"token punctuation\">&lt;</span>GlobalState<span class=\"token punctuation\">></span></span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// Custom globals class, with model and agent using this custom class.</span>\n<span class=\"token keyword\">public</span> <span class=\"token keyword\">static</span> <span class=\"token keyword\">final</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyGlobals</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">GlobalState</span> <span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">public</span> <span class=\"token keyword\">float</span> preference <span class=\"token operator\">=</span> <span class=\"token number\">0.35f</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">public</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyModel2</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>MyGlobals<span class=\"token punctuation\">></span></span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">public</span> <span class=\"token keyword\">static</span> <span class=\"token keyword\">final</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyAgent2</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Agent</span><span class=\"token generics function\"><span class=\"token punctuation\">&lt;</span>MyGlobals<span class=\"token punctuation\">></span></span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>Use the <code class=\"language-text\">getGlobals()</code> function, which is part of the <code class=\"language-text\">GlobalState</code>, to get global values and update them as necessary. </p>\n<div class=\"ui segment info message\" ><h4>Modifying Globals via Agents</h4>\n<p>Do not attempt to change the values of globals from inside agents. Since agent actions happen in parallel, updating global values from agents may result in unexpected behavior. For best practices you should either update these values via a different function called by step, or if you wish to handle agent information via a singular agent that will receiving messages from desired agents and update separately to avoid conflicts.</p></div>\n<p><a href=\":version/reference/modelling/annotations\">Annotations</a> can either set global state values from the console or report those values to the console.</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\">static</span> <span class=\"token keyword\">final</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">MyGlobals</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">GlobalState</span> <span class=\"token punctuation\">{</span>\n   <span class=\"token annotation punctuation\">@Input</span> <span class=\"token keyword\">public</span> <span class=\"token keyword\">float</span> preference <span class=\"token operator\">=</span> <span class=\"token number\">0.35f</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<div class=\"ui segment info message\" ><h4>Setting Globals During Setup</h4>\n<p>Globals allows you to define variables that can be accessed and modified by both the model and agents within the system. However, during the setup process when you are creating agents via an injector, you should not by modifying globals. This is due to Java 8 lambda's. If you wish to properly modify Globals, make sure it is part of the message passing action of an agent, or make usage of an accumulator.</p></div>","headings":[],"frontmatter":{"title":"Global State","toc":null,"experimental":null}},"site":{"siteMetadata":{"title":"Simudyne Docs","latestVersion":"2.6"}}},"pageContext":{"absolutePath":"/home/vsts/work/1/s/content/2.6/reference/modelling/globals.md","versioned":false,"version":"2.6","kind":"reference","pagePath":"/reference/modelling/globals","chronology":{"prev":{"name":"Model Configuration","path":"/reference/modelling/model-configuration"},"next":{"name":"Determinism","path":"/reference/modelling/determinism"}},"lastUpdated":"2026-04-21T13:56:54.869Z"}}