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
Related
I need help in Netlogo: when a turtle interacts witch a patch where it is currently placed on and the patch-variable (which is a string) decides if the turtles variable (which is an integer) grows. For example:
turtles-own [weight]
patches-own [food]
to setup
clear-all
create-turtles 1 [
setxy random-xcor random-ycor
]
ask patches[
if pxcor >= 0 [set pcolor green
set food "Fries" ]
if pxcor < 0 [set pcolor blue
set food "Burger" ]
]
reset-ticks
end
to go
increase-weight
walk-around
tick
end
to walk-around
ask turtles [
right random 120 - 60
fd 0.1
]
end
to increase-weight
ask turtles [
if food != "fries" [
set weight (weight + 1)]
if food != "burger" and [
set weight (weight + 10)]
]
end
The problem is the turles weight goes up by 11 not with the value of 1 OR 10. I guess its something with patch-here !? But i cant get it running.
Thanks a lot
You were close. The Major issue was the differences in strings. "Fries" and "fries" are not exactly equal. Change one to match the other.
While the line if food = "Fries" will work I would recommend using the patch-here in an if statement with the name of the patch variable in brackets like this:
to increase-weight
ask turtles
[
if [food] of patch-here = "Fries"
[set weight (weight + 1)]
if [food] of patch-here = "Burger"
[set weight (weight + 10)]
set label weight
]
end
That will make it super clear where the food variable is coming from.
I was confused by your use of the != and what type of food you thought the turtle was on. The above code increments weight by 1 for fries and by 10 for a burger. Your original code with your != though inverted that, giving +10 for fries and +1 for burger. The use of the != did account for your increase by 11 (10 and 1) because it was always not on either of the patches.
References:
How to check the value of a variable that belongs to a patch
NetLogo User Manual
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
I am new in using NetLogo, so I hope you can help me with this code.
I would like to build a network with two subnetwork, A and B, where A has N nodes and B beta*N nodes, where beta=0.5. Each network should be initialised with five fully connected nodes. I need to add a new node at a time, assuming that each has fixed out-degree k. Once a new node i comes into the network, it links to a randomly selected target node j. The other remaining k-1 nodes should be selected as following: with probability p, i should be linked to a random node of j's; with probability 1-p, i should be connected to another randomly selected node in A.
On the other hand, the nodes in B should be linked (directed link) to each node in A with probability P. P can vary from 0 to 1.
What I already tried is built the two networks with N and alpha*N nodes respectively. But, as I said, I am new in using NetLogo and I am finding many difficulties to build this network that should be really easy in a different programming language, I would be more familiar with.
; Global variables
breed [agents agent]
breed [bagents bagent]
to setup
clear-all
setup-agent
setup-bagent
end
; Defining agents
to setup-agent
set-default-shape turtles "person" ; agent shape
create-agents n-of-agents ; # of agents
[set size 2 ; agent size
set color white
setxy (random-xcor) (random-ycor)
]
; Random Network
ask agents [create-link-with one-of other agents with [not link-neighbor? myself]
ask links [set color white]
]
end
; Defining bagents
to setup-bagent
set-default-shape turtles "circle" ; bagents shape
set beta=0.5
let n-of-bagents beta*n-of-agents
create-bagents beta*n-of-agents ; # of bagents
[set size 2 ; bagent size
set color red
setxy (random-xcor) (random-ycor)
; Network
ask bagents [create-link-with one-of other bagents with [not link-neighbor? myself]
ask links [set color yellow]
]
end
to go
end
I hope you can help me to understand how to build such a network in NetLogo.
Many thanks
This does what you said. I don't think it's actually what you want as your algorithm is much better but still somewhat confused. But hopefully this gets you on the correct path.
UPDATED to make one node add each tick
globals
[ beta
prob
k
]
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
set beta 0.5
set prob 0.2
set k 3
setup-A-seed
setup-B-seed
reset-ticks
end
to go
add-A-node
if random-float 1 < beta [add-B-node]
tick
end
; Defining A seed network
to setup-A-seed
create-A-agents 5
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
]
ask A-agents
[ create-links-to other A-agents
[ set color white ]
]
end
; Defining B seed network
to setup-B-seed
create-B-agents 5
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
]
ask B-agents
[ create-links-to other B-agents
[ set color yellow ]
]
end
to add-A-node
create-A-agents 1
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
let target one-of other A-agents ; target is j in description
create-link-to target
repeat k - 1
[ let candidates (other [link-neighbors] of target) with [not link-neighbor? myself]
ifelse random-float 1 < prob or not any? candidates
[ create-link-to one-of other A-agents with [not link-neighbor? myself]
[ set color white ]
]
[ create-link-to one-of candidates
[ set color white ]
]
]
]
end
to add-B-node
create-B-agents 1
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
let thisB self
ask A-agents
[ if random-float 1 < prob
[ create-link-from thisB
[ set color yellow
]
]
]
]
end
Some of the NetLogo issues I noticed in your code:
NetLogo does not use = for set
you must have space around mathematical operators (or NetLogo will think it's part of the name)
Some of the things you need to think about in your algorithm:
why do you have an initial B network if all Bs connect to each A with fixed probability?
what do you do if the selected A doesn't have any edges to follow?
As general advice, don't try writing something this complicated in one piece. Create your seed networks with 5 fully connected nodes. Make that work. Then do network A and make that work. Then bring in B. This iterative building is important for all programming languages. It is particularly important when using a new language so that you only have to debug one or two errors at a time and you know where the bugs are.
I want to extract a node with highest degree centrality from the network. I don't want to extract a node with max links only. I want to extract the node along with the nodes adjacent to it.
Below is the code. In this code, I have loaded a network using nw extensions.
extensions [nw]
turtles-own [ explored? ]
to setup
ca
crt 25
ask turtles [fd random 15]
load-graph
extract_deg
end
to load-graph
let filename user-file
if (filename != false) [
nw:load-graphml filename [
set shape "circle"
set size 1
]
nw:set-context turtles links
]
end
to extract_deg
let n turtles with [my-links = max [count link-neighbors] of turtles]
ask n [show other turtles network:in-link-radius 1 turtles]
end
to layout
ask turtles [ set size sqrt count my-links ]
layout-spring turtles links 0.5 2 1
ask turtles [
facexy 0 0
fd (distancexy 0 0) / 100 ]
end
The code below will choose one of the nodes with largest degree (just remove one-of if you want all of them), turn it red and make its network neighbours green.
You don't need the expression [my-links = max [count link-neighbors] of turtles], standard NetLogo includes the very useful with-max primitive. However, I think your construction would have worked if you had counted my-links (like let n turtles with [count my-links = max [count link-neighbors] of turtles]). Then you have some syntax errors in the next line (the extension is nw and you don't need the turtles.
to extract_deg
let maxk-set one-of turtles with-max [count my-links]
ask maxk-set
[ set color red
ask other nw:turtles-in-radius 1 [set color green]
]
end
I am teaching myself how to create ABMs in Netlogo using the book of Railsback & Grimm 2012. I am having trouble with one book exercise which is on butterflies following "virtual" corridors. Basic idea is that butterflies go uphill for mating using the differences in height as guide. I need to calculate the width of the corridors dividing the number of patches used by the butterflies over the average distance the butterflies fly from the start patch to the end patch. I am
struggling with plotting this corridor width, which I am coding like this:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
I then created a plot in the interface with the command:
plot corridor-width
The error message I get reads:
Division by zero. error while observer running / called by procedure
CORRIDOR-WIDTH called by plot 'Corridor width' pen 'default' update
code called by procedure SETUP called by Button 'setup'
I believe there is something wrong with the way I am coding distance start-patch but I have surfed the web and looked at several codes and I cannot spot my mistake. My whole code looks like this:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
What happens when the mean-distance is 0?
let mean-distance mean [distance start-patch] of turtles
Essentially, in your setup, you set all the turtle's start-patch to their current patch. So, if you ask all the turtles how far away they are from their start patch, they will all tell you 0 units away.
So, [distance start-patch] of turtles is filled with a list of all 0's.
Thus, a mean of a list of all 0s is 0 causing your divide by 0 error.
Maybe in this situation, you want to report 0 instead...so
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]