How to switch agents from one breed to another during a simulation? - netlogo

I have a stock market simulation with two types of breeds, passive and active. They both have different formulas for investing. However, I would like the agents to switch between the two breed types. I have a condition they should follow to decide when to switch. However, I am not sure how to program the agents to switch.

You should be able to simply set the breed of an agent, just as would any other variable:
ask one-of passives [ set breed actives ]
Assuming you used passives and actives as your breed plural names.
You can read more in the NetLogo programming guide.

Related

Problem making a calculation in a monitor on the interface in Netlogo

I have a model that tracks energy levels in sheep, which I have defined as a breed. I have set up a monitor to calculate the mean energy level for the sheep, but need to consider what happens if there are no sheep at some point. I have tried numerous variations, the simplest of which is:
ifelse (any? sheep) ["NA"] [mean [energy] of sheep]
Unfortunately, I keep getting the error
Expected reporter.
I can work around this by creating a new global variable and reporter in the code, but this seems to be a bit of a waste. Am I doing something wrong or are there limitations on what kind of calculations can be done in a monitor on the Interface? If so, where are these limitations summarized?
Answer in three steps:
Difference between ifelse and ifelse-value
You are getting a syntax error there because a monitor expects a reporter, but ifelse is made to handle commands. The reporter version of ifelse is ifelse-value. If you just change ifelse to ifelse-value in your example, you see that you don't get any syntax error anymore. However, you will also see that if you do so and start your model with sheep having their energy, the monitor will show NA - see next point.
Correct use of any?
This happens because you are using any? the other way around. As you can see, any? reports true if the agentset is not empty. This means that the following reporter:
ifelse-value (any? sheep) ["NA"] [mean [energy] of sheep]
will report "NA" when there are sheep in the model, because that is the first reporter block.
Switch to:
ifelse-value (any? sheep) [mean [energy] of sheep] ["NA"]
and you have what you want. Just as in English: "If there are any sheep, do the calculation, else, this does not apply".
In any case, this does the job but it is superflous - see next point.
What monitors can do
Monitors are able to handle some of their reporters' runtime errors. You can simply put mean [energy] of sheep in your monitor and it will automatically show N/A when there are no sheep, without the need for you to handle the case.

Netlogo: is nobody still being treated as an agent?

I have a list of parcel agents. but I will kill the parcels periodically. However, the list is still recording someting like this: [nobody nobody nobody nobody nobody nobody nobody nobody], and overtime the running of the model is getting slower and eventually pop up message "your model is too large to run with available memory"
In this case, are the dead agents (i.e. nobody) still treated as an agents that consumes much of the memory? what if it is a pure list of numbers or strings? would it cause the same OOM issue? how big the list can be in Netlogo and any uppper limit?
From, the NetLogo dictionary for die: If you have a list of agents and the agent dies, then the agent is removed from any agentset and:
The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
Any variable that was storing the agent will now instead have nobody in it
The dead agents are not consuming resources, but the list is (as you have found by printing out the list). You can see this with the following model:
globals [mylist myagentset]
to setup
clear-all
create-turtles 1
set mylist sort-on [who] turtles
set myagentset turtles
reset-ticks
end
to go
create-turtles 1
[ set myagentset (turtle-set myagentset self)
]
set mylist lput one-of turtles mylist
ask one-of turtles [die]
type "turtles: " print count turtles
type "list: " print length mylist
type "agentset: " print count myagentset
tick
end
If you want the dead turtle to be removed from the list, you need to explicitly do so with remove-item. The same is true of lists of numbers, strings etc.
Alternatively, if the list doesn't need to be maintained over ticks, but can be reconstructed (eg if it is a sorted list of the turtles agentset), you could create it each tick and that list would only contain turtles that are alive.

In NetLogo, what is the most efficient way to copy the values of variables in an agentset to another agentset with the same number of agents

In NetLogo, I have the same number of turtles on different patches. Now I want to copy the agent variable values of the agents on one patch to those agents on another patch. I know I can define a list of members for each patch, and then copy the values agent by agent according to the order of the list, but I have to define a list for each patch, which may take some memory and reduce the running speed.
In other words, I want the agents on one patch with their agent variable values the same as the agents on another patch.
Are there more efficient ways to do this?
I use something like this hatch makes exact duplicates of the original turtles same variables same color etc
targ is the patch you want them at
to dup-turtles-to [targ]
ask turtles-here
[
hatch 1 [move-to targ]
]
end
I hope that is helpful and I understood your question correctly.

Can I store a variable for each patch or should I use turtles?

I am modeling tax evasion. My initial thought was to led a random number of patches be "businesses" and have wandering turtles that chose to shop at the business that can offer the best price within a given range of the turtle.
I need to somehow store the total transactions (turnover) of each business (random patches).
In addition, each business will either chose to report all income to the tax authority, or choose to evade a certain amount, which in turn will depend on each business' profile - some are willing to evade if the competition is to high, some always evade, and some never evade.
So my question is; can I assign each business-patch a different "personality", store the sum of transactions, and make them report their income? Or can these kinds of variables only be stored in a turtle? In other words, should I make a model with several breeds of turtles in stead, where some are customers and some are stationary businesses?
Patches can have patches-own variables just like turtles can have turtles-own variables. See: http://ccl.northwestern.edu/netlogo/docs/programming.html#variables
If your businesses really are stationary, it makes sense to model them as patches.
Modeling them as turtles does give you a bit more flexibility, however. You could set shape "house" for visual effect. You could have them enter in a network eventually. You could change their size according to some relevant variable. Etc.

Comparing two agent variables

I am currently making a simulation (for homework) using genetic algorithms. What I want to do is compare the fitness of agents on a specific patch and the one with the lowest fitness will die.
I have scoured the net and found this code: if any? breed1-here with [fitness > fitness-of myself] [die]]
But this doesn't seem to work and now I'm completely out of ideas.
let goner min-one-of breed1-here [fitness]
if is-turtle? goner [ ask goner [ die ] ]`
the is-turtle? check is necessary because the patch might be empty.
Yes, that code is from an old version of the NetLogo language. That line of code should be re-written as:
if any? breed1-here with [fitness > [fitness] of myself] [die]]
Of course, that code will kill all turtles in a patch except for the one(s) with maximum fitness, which is not exactly what you want.