Adding data to time plots during simulation run Anylogic - simulation

I've got a model with multiple agents, each with a couple of flows and stocks, and an analysis-agent. I want to let the user add a time plot for any flow/stock/variable during the simulation run. The idea is to have multiple radio buttons or combo boxes which allow the user to select any element of any agent and automatically create a plot of the element. I've found some success in triggering a cyclic event with a radio button, then the event updates a helper variable in my analysis-agent with a predefined element from another agent and plots the variable. The problem however is that I have to predefine the element I want to plot in my user-triggered event. I could make an event for every single element of my model each, but i hoped there was a way to more directly translate user-input into a useable path of the element.
As an example: I have two agents, a and b. In agent a theres the flow "flow", in agent b a plot "plot" and a button "button" (Or any other Controls element if it's more useful in this case). When the model runs the user is supposed to be able to press the button which adds "flow" to "plot". Is there an easy solution to this problem that is (at least somewhat) scaleable to a high number of agents and plottable elements?
Any help is greatly appreciated!

You will need to make use of some more advanced Java features to get this done, namely functional interfaces.
Look at the following example
I have two different flow and stock diagrams and a button for each to plot the flow.
The variable is a functional interface, called Supplier. (you can read more here https://www.baeldung.com/java-8-functional-interfaces)
The data set uses this functional interface to update the y values and time for the x.
The only thing I need to do now is just changing the dataUpdater and override the get() to show the flow that I selected to plot
In the other button I override the get() function to return flow2
The plotFlow function just resets the chart and the dataset to start fresh
plot.removeAll();
dataset.reset();
plot.addDataSet(dataset);
Now you can put the chart, the dataUpdater variable and dataset inside a separate agent and perhaps have an update function that accepts Supplier and that way you can plot literally anything, as long as it gives you a supplier that returns a double value

Related

System dynamics SEIR infectious curve for 3 waves of Covid

Using system dynamics on anylogic how can you model a simulation that will give an infectious curve of this nature(Below picture) using SEIR.
enter image description here
I have tried to simulate, however my graph goes up and down. It does not oscillate as per the attached picture.
I need to simulate something similar to the graph for my assingment.
There should be three types of events in your model.
First, lets call it "initial spread", is triggered on the start of your simulation.
Second, lets call it "winter season", is triggered annualy in November\December.
Third, lets call it "mass vaccination" - you can decide when to trigger it and for what selection of your agents.
So first two are kind of global events, and the third event is specific to some sub-population (this can make the third wave kind of "toothy" if you trigger it in slightly different moments for different populations).
That is pretty it.
Curios to see how your model will predict the fourth wave - second winter season of your simulation. So keep us updated :)
There are a number of ways to model this. One of the simplest ways is to simply use a time aspect for one of your infection rate parameters so that the infection rate increases or decreases with time.
See the example below.
I took the SIR model from the Cloud https://cloud.anylogic.com/model/d465d1f5-f1fc-464f-857a-d5517edc2355?mode=SETTINGS
And simply added an event to change the Infectivity rate using an event.
Changing the chart to only show infected people the result now looked something like this.
(See the 3 waves that were created)
You will obviously use a parameters optimization experiment to get the parameter settings as close to reality as possible

Which input block is used in Matlab simulink that provide random values with time?

I am a new user of Matlab Simulink and currently i am working with Fuzzy Logic Controller with Simulation. In the Simulink, i want to input the values(i highlighted in the picture below), that changes with time.
Here, i am using Uniform Random Number Block which sends four values(which i feed) at same time but i want one value must input first, then another and so on. My target is to use Scope block(the one which is highlighted in the right side) and check the output that varies with time. But with current configuration, i just input ONLY one value. Please help?

multiple step function for a single flow system

For Matlab simulink, how does one go about generating multiple step function at different conditions for a system
I am trying to simulate a simple flow through a tank and controlling the temperature within. At various interval say time at 10ses and 20secs i intend to draw out different flowrate/flow amount of water.
With the system designed, how do I show on a single scope how Flow in changes with the different amount of flow out drawn.
Appreciate any kind advises
There are many different ways of doing this, for example defining the data in MATLAB and using a From Workspace block or using multiple Step blocks summed together appropriately. But to start I'd suggest you look at the Signal Builder block.
To view multiple signals at once in the same Scope, either set the scope up to have multiple inports, or Mux the signals together and feed them in in the usual way.

Tuning and optimizing a MATLAB/Simulink Model

I would like to optimize output signals, by tuning some of the input parameters with ease (preferably in real-time) by looping the simulation of the model again and again at a speed where the speed of simulation can be controlled.
You have various options as far as I can see.
The first one is to have the parameters of interest defined as workspace variables, and then write a MATLAB script looping over those variables, with the use of the sim command to run the Simulink model programmatically. You can control the "speed" of the simulations with things such as step size used by the solver. If you want to combine this with optimizing parameters, you can put the call to sim in a cost function, and then pass this to the optimization routine.
The second option is to use an add-on product called Simulink Design Optimization, which provides a nice interface and uses optimization routines under the hood to loop over the parameters. This requires the Optimization Toolbox.
If you want to invest nothing in the creation of your user interface, you might want to use cell evaluation. Check out this link.
Increment Values in Code Sections
You can increment numbers within a section, rerunning that section
after every change. This helps you fine-tune and experiment with your
code.
To increment or decrement a number in a section:
Highlight or place your cursor next to the number. Right-click to open
the context menu. Select Increment Value and Run Section. A small
dialog box appears.
Input appropriate values in the / text box or / text box. Click
the , , , or button to add to, subtract from, multiply, or divide
the selected number in your section. MATLAB runs the section after
every click.

is there a way in Simulink to use the same set of blocks on multiple signals (without copying those blocks)?

I am implementing some head tracking and I get 2 matrices of horizontal velocities. (A vector field decomposed into vertical and horizontal velocities). For each of these matrices I do some math to calculate the actual head tracking.
My question is, is there a way to do that math (which is a set of blocks) on both matrices without copying the math blocks onto each signal?
It's hard to explain so here's a screen shot of my model:
You can see that the "complex to real-imag" block has 2 outputs (this is the little one in the middle). The mean block and the integrator circuit then calculate the head velocity and position for the real matrix (horizontal position). I want to do exactly the same routine on the imaginary matrix (vertical direction). Obviously I can just copy the blocks, but surely there must be a better way of doing it? In a way I'm looking for an analogue of a loop in "normal programming" like C or something, where a block of code is executed several times on different inputs.
You can create a Library in Simulink that contains code you can reference multiple times.
Go to File -> New -> Library. In the model window that opens, you can create any number of subsystems with whatever code you want. Then, just drag a subsystem from the library into your model. The subsystem will now appear in your model with a little arrow icon in the lower left. This indicates that the subsystem in the model is a link. You can drag as many instances of the library subsystem into your model as you wish, just as you can call a function as many times as you wish in any other programming language.
If you right-click on the subsystem in your model, you can select "Link Options -> Go To Library Block" to get back to the library. You can make changes in your model and propogate them back to the library as well.
One way to easily reuse a set of blocks is to create a subsystem out of them. In your case, you can create a subsystem by grouping existing blocks, then simply copy and paste your subsystem to use it for your imaginary output.
Although potentially more complicated, you could also look into using mux signals to avoid having to copy parts of your model.