How to divide turtles in groups by percent? - netlogo

In my world, turtles are firms. 30 % of all firms have a low output (Y = 1), 60 % a medium (Y = 2) and 10 % a high production output (Y = 3).
How can I assign an input for Y to 30 % of my turtles/firms? Best would be to have a slider in order to change the values if needed.
I have given the turtles a firm-own variable called Y which is their output. I have also created number-of-firms slider where I can decide how many firms will be in the world for each setup.
breed [ firm firms ]
firm-own [
Y ;; output
]
to setup
clear-all
setup-industry
reset-ticks
end
to setup-industry
create-firm number-of-firms [ ;; number of firms to be defined through slider
ask n-of ( count firms * 0.3 ) firms [
set Y 1 ]
ask n-of ( count firms * 0.6 ) firms [
set Y 2 ]
ask n-of ( count firms * 0.1 ) firms [
set Y 3 ]
]
end
The error message says that when I have "ask n-of ( count firms ... )" firms are expected to have 1 input, meaning a number.

The first input in breed defines the agentset whereas the second input goes for a single member. Consider following modifications:
breed [ firms firm ]
firms-own [
Y ;; output
]
...
create-firms number-of-firms [ ;; number of firms to be defined through slider
Now you won't have any error messages.

Related

calculate steps of turtles among neighbors

In the world, the two types of agents, individuals and messages, are randomly positioned in the two-dimensional attitudinal space.
If an individual believes the messages, he or she creates links with the messages.
Then, an individual adjusts his or her attitudinal position based on the calculation of distances from oneself to the messages.
Here, I have a problem.
I would like to make an individual move twice more when exposed to a message with a high value than when exposed to one with a low value.
But what I can do right now is just averaging the distance among the messages.
Here's what I've done so far
breed [individuals individual]
breed [messages message]
messages-own [value]
undirected-link-breed [messagelinks messagelink]
to setup
ca
create-individuals 100 [initiate-individuals]
reset-ticks
end
to go
new-messages
end
to initiate-messages
ifelse random-float 1 < value-p [ set value "H" ] [set value "L"]
end
to initiate-individuals
setxy random-xcor random-ycor
end
to new-messages
create-messages 30 [
initiate-messages
ask individuals [
integrate-messages myself
]
]
end
to integrate-messages [newmessages]
if random-float 1 < 0.3
[create-messagelink-with newmessages
setxy mean [xcor] of messagelink-neighbors mean [ycor] of messagelink-neighbors]
end
I am making the assumption that integrate-messages is intended to handle a set of messages, even though when a receiver executes it in your code it is only the newly created message itself, not all newly created messages. There are likely several ways to handle the problem of giving "H" messages more influence than "L" messages, but perhaps the most straightforward to simply to use a weighted average of the messages' xcors and ycors. I've written a short model that does that in integrate-messages.
breed [messages message]
breed [receivers receiver]
undirected-link-breed [messagelinks messagelink]
messages-own [value]
to setup
clear-all
create-receivers 1 [
setxy random-xcor random-ycor
set color green
]
create-messages 5 [
setxy random-xcor random-ycor
set value ifelse-value (random-float 1 < 0.5) ["H"] ["L"]
set color blue
set label value
]
reset-ticks
end
to go
ask one-of receivers [
integrate-messages n-of 3 messages
]
end
to integrate-messages [newmessages]
;to indicate which messages are being received.
ask newmessages [set color red ]
create-messagelinks-with newmessages [set color red]
;now calculate the weighted position relative to ALL linked messages.
let mssgs [other-end] of my-messagelinks
let wghts map [x -> ifelse-value ([value] of x = "H") [2] [1]] mssgs
let xc sum (map [[m w] -> w * [xcor] of m ] mssgs wghts)
let yc sum (map [[m w] -> w * [ycor] of m ] mssgs wghts)
set xcor xc / sum wghts
set ycor yc / sum wghts
;to show where the receiver ends up.
set color red
end

K on and K off in reverse reaction kinetics

I am trying to code for a reverse reaction in biology - I have already done that by taking inspiration from a sample code pasted below. What I don't understand is how is the association rate constant (Kb in the code) and dissociation constant (Ku in the code) are affecting the forward and backward reactions in this code. Are they affecting the speed of movement of the products after they are formed? Are they affecting the speed with which they are formed? I want the Ku to affect affinity for substrates to form a product, and Ku to affect time to dissociate for products. The product is a complex formation of two substrates.
Also, why do they multiple Kb by 2? To slow the product complex down or speed it up? It should slow down due to increase in mass.
breed [reactants reactant] ;; reactants are green, products are red breed [products product]
to setup clear-all set-default-shape reactants "molecule1" set-default-shape products "molecule2" create-reactants number
[ set color green
setxy random-xcor random-ycor ] reset-ticks end
to go ask turtles
[ rt random-float 10 - random-float 10 ;; wander around randomly
fd 1 ] ask turtles
[ ifelse (breed = reactants)
[ react-forward ] ; reactants
[ react-backward ] ; products
] tick end
to react-forward if (any? other reactants-here) and
;; multiply Kb by 2 because 2 molecules are involved
random-float 1000 < (Kb * 2)
[ ask one-of other reactants-here
[ die ]
set breed products
set color red ] end
to react-backward if (random-float 1000) < Ku
[ set breed reactants ;; change back to reactant
set color green
;; then split into two reactants
hatch 1 [ set heading random 360 ] ] end

How can I make the model run?

I am essentially trying to combine elements of the 'Segregation' model and 'Rebellion' model to form a model that is representative of alliance forming.
Here is what I have so far- when I attempt to run it I receive the error: THREATS breed does not own variable ACTIVE?
error while threat 0 running ACTIVE?
called by procedure GO
called by Button 'go'
breed [ agents an-agent]
breed [ threats threat ]
globals [
k ; factor for determining attack probability
threshold ; by how much must D - BS > A to make a state burden share
percent-similar ; on the average, what percent of a turtle's neighbors
; are the same color as that turtle? Likely to ally
percent-unhappy ; what percent of the turtles are unhappy? Or percieve threats?
visualization
]
agents-own [
allied-states ; R, fixed for the agent's lifetime, ranging from 0-1 (inclusive)- for each turtle, indicates whether at least %-similar-wanted percent of
; that turtle's neighbors are the same color as the turtle
perceived-threat ; T, also ranging from 0-1 (inclusive)- how many have a turtle of another color?
active? ; if true, then the agent is actively allied
; if false, then the agent is free-riding
conflict ; how many turns in conflict remain? (if 0, the agent is not in conflict)- sum of previous two variables
total-nearby ; sum of previous two variables
]
patches-own [
neighborhood ; surrounding patches within the vision radius
]
to setup
clear-all
; set globals
set k 2.3
set threshold 0.1
ask patches [
; make background a slightly dark gray
set pcolor gray - 1
; cache patch neighborhoods
set neighborhood patches in-radius vision
]
if initial-threats-density + initial-agent-density > 206 [
user-message (word
"The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY "
"should not be greater than 206.")
stop
]
; create threats
create-threats round (initial-threats-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
display-threats
]
; create agents
create-agents round (initial-agent-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
set heading 0
set allied-states random-float 1.0
set perceived-threat random-float 1.0
set active? false
set conflict 0
display-agent
]
; start clock and plot initial state of system
reset-ticks
end
to go
if all? turtles [ active? ] [ stop ]
move-unhappy-turtles
update-turtles
update-globals
tick
end
; unhappy turtles try a new spot
to move-unhappy-turtles
ask turtles with [ not active? ]
[ find-new-spot ]
end
; move until we find an unoccupied spot
to find-new-spot
rt random-float 360
fd random-float 10
if any? other turtles-here [ find-new-spot ] ; keep going until we find an unoccupied patch
move-to patch-here ; move to center of patch
end
to update-turtles
ask turtles [
; in next two lines, we use "neighbors" to test the eight patches
; surrounding the current patch
set allied-states count (turtles-on neighbors) with [ color = [ color ] of myself ]
set perceived-threat count (turtles-on neighbors) with [ color != [ color ] of myself ]
set total-nearby allied-states + perceived-threat
set active? allied-states >= (percent-similar * total-nearby / 100)
; add visualization here
if visualization = "old" [ set shape "default" set size 1.3 ]
if visualization = "square-x" [
ifelse active? [ set shape "square" ] [ set shape "X" ]
]
]
end
to update-globals
let similar-neighbors sum [ allied-states ] of turtles
let total-neighbors sum [ total-nearby ] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [ not active? ]) / (count turtles) * 100
; Agents engaged in conflict have the duration reduced at the end of each clock tick
ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ]
; update agent display
ask agents [ display-agent ]
ask threats [ display-threats ]
; advance clock and update plots
tick
end
; AGENT AND THREAT BEHAVIOR
; move to an empty patch
to move ; turtle procedure
if movement? or breed = threats [
; move to a patch in vision; candidate patches are
; empty or contain only jailed agents
let targets neighborhood with [
not any? threats-here and all? agents-here [ conflict > 0 ]
]
if any? targets [ move-to one-of targets ]
]
end
; AGENT BEHAVIOR
to determine-behavior
set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold)
end
to-report burden-sharing
report perceived-threat * (1 - alliance-protection)
end
to-report estimated-conflict-probability
let t count (threats-on neighborhood)
let a 1 + count (agents-on neighborhood) with [ active? ]
; See Info tab for a discussion of the following formula
report 1 - exp (- k * floor (t / a))
end
to alliance
if any? threats [attack]
set active? true
end
; THREAT BEHAVIOR
to attack
if any? (agents-on neighborhood) with [ active? ] [
; arrest suspect
let suspect one-of (agents-on neighborhood) with [ active? ]
move-to suspect ; move to patch of the jailed agent
ask suspect [
set active? false
set conflict random conflict-term
set color pink
]
]
end
; VISUALIZATION OF AGENTS AND COPS
to display-agent ; agent procedure
set color cyan
set shape "triangle"
end
to display-active?
set color pink
set shape "triangle"
end
to display-threats
set color red
set shape "circle 2"
end
The problem is that you have two breeds of turtles, agents and threats, and only agents "own" the variable active?. turtles is a superset of all breeds, so when the all? primitive tries to query the active? variable of literally all turtles, it tries to query active? for threats too, and can't find it. The line should be
if all? agents [ active? ] [ stop ]

NetLogo - plot sum of all turtles with value in certain range

I would like to plot the sum of all turtles holding values in a range from 2 to 4. How can I achieve that?
I only get the sum of turtles holding a value of 4 with plot count turtles with [value = 4], however, I would need something like plot count turtles with [2 <= value => 4].
How can I achieve this?
My MWE is:
breed [ turtles ]
turtles-own [ value ]
to setup
clear-all
create-turtles 100
[
set value random 4
]
reset-ticks
end
to go
ask turtles [
rt random 360
fd 1
]
tick
end
NetLogo doesn't support the 2 <= value => 4 syntax. You need to write both conditions separately:
plot count turtles with [ value >= 2 and value <= 4 ]
By the way, if you wanted to plot the sum of the values instead of the count of turtles with values in that interval, you could write:
plot sum [ value ] of turtles with [ value >= 2 and value <= 4 ]

Calculating turtle mortality based on distance from origin in netlogo

I am writing a procedure (Pass-Away-Space) to calculate mortality of turtles moving from the origin (start-patch) through out the world. Each turtle calculates its own mortality based on its distance from the origin (start-patch). The code I am attempting to implement for this procedure is as follows:
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
The error I am getting is expected input to be a number but got the list. I am not sure what the issue is and I was wondering if anyone could point out and rectify the problem with the code.
The problem here is your of turtles. Since an ask procedure affects one turtle at a time, each turtle in your procedure above is evaluating the [distance start-patch] of all turtles instead of just its own distance to the start patch. To clarify, check out the following setup:
globals [ start-patch ]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
]
set start-patch patch 0 0
end
to incorrect-example
ask turtles [
print ([ distance start-patch ] of turtles)
]
end
to correct-example
ask turtles [
print distance start-patch
]
end
Compare the print output of the incorrect-example and the correct-example procedures, and you'll see that when you use [distance start-patch] of turtles you get the list of distances of all turtles. When you ask turtles to evaluate a turtles-own variable (including color, size, etc) each turtle will automatically access its own version of that variable- there's no need to specify which turtle. So, your pass-away-space might look something more like below (untested):
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * (distance start-patch) )
if chances >= 1 [
die
set dead-count dead-count + 1
]
]
end