NETLOGO: How to use variables from links in "nodes" agent? - netlogo

I am trying to make simple investment model. I am using NETLOGO and have network, where nodes are investors and they are connected with links. Each link have variable "trust" which stand for trust between those two nodes (investors). Each investor also have a kind of subjective view on stock price and in each round, the value of this change with a formula. My problem is, that part of this formula I want to use is sum of multiplication of trust with every neighbor (variable trust on links "leaving" node) and neighbors subjective view from last round. And I am a bit lost how to use link variable for agent from which those links are going to another agent. Is there some way? I hope it is clear how i mean it. I am really sorry for my english.
Here is the code:
globals [realprice
alpha
noise]
directed-link-breed [curved-links curved-link]
breed [investors investor]
curved-links-own [trust]
investors-own [price
stock]
to setup
clear-all
reset-ticks
setup-patches
setup-investors
setup-stocks
setup-links
setup-switch-trust
setup-layout
setup-alpha
setup-realprice
end
to setup-patches
ask patches [set pcolor white]
end
to setup-investors
set-default-shape investors "circle"
create-investors number
[set color red
set size 1
set price random (max-extreme - min-extreme) + min-extreme
set label-color green ]
end
to setup-stocks
ask n-of numberstock investors [set stock 1]
end
to setup-links
set-default-shape curved-links "curved link"
ask investors [create-curved-links-to n-of number2 other investors
[set color blue
set trust random 100
set label trust
set label-color black ]]
end
to setup-switch-trust
ask curved-links
[ifelse show-trust?
[set label trust]
[set label ""]
]
end
to setup-switch-price
ask investors
[ifelse show-price?
[set label price]
[set label ""]
]
end
to setup-layout
layout-circle investors (world-width / 2 - 2)
end
to setup-alpha
ask turtles [set alpha (numberalpha)]
end
to setup-realprice
set realprice random (max-extreme2 - min-extreme2) + min-extreme2
end
to go
set realprice random (max-extreme2 - min-extreme2) + min-extreme2
set noise (1 / (random (100 - 1) + 1))
ask investors [set price ((alpha * price) + (1 - alpha)*(realprice + noise))]
ask investors
[ifelse show-price?
[set label price]
[set label ""]
]

For an investor turtle, the "sum of ... trust with every neighbor" is:
sum [trust] of my-out-curved-links
The primitive I'm using here is: http://ccl.northwestern.edu/netlogo/docs/dictionary.html#my-out-breeds
I think that answers the "how to use link variable for agent from which those links are going to another agent" part of your question.
I'm not sure what the "multiplication... with ... neighbors subjective view from last round" part of your question means. (Perhaps that part would be better as a new, separate question?)

Related

Network modelling using Netlogo

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.

Set Radius to find a partner turtle

I'm very new at Netlogo and programming in general.
I want to create a netlogo model, with female and male turtles. Both populations move through the world via random walk. The female population should find a partner and has the property 'radius'. Her own radius should expand if she has not found a partner until the moment she finds one. How can I program a radius around the female turtles, that expand after each time step if she has not found a partner?
Thanks for your help!
First, you need a turtle attribute that stores the value for each turtle. The way to do that is with a turtles-own statement. Then you simply change the value as required. The primitive in-radius looks at everything within the specified distance, and then you can set a condition by whether there are any? suitable mates. Your code would look something like (this is a complete model):
turtles-own
[ search-radius
mate
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set color blue
set search-radius 1
]
reset-ticks
end
to go
check-for-mate
tick
end
to check-for-mate
ask turtles with [color = blue]
[ let candidates other turtles in-radius search-radius
ifelse any? candidates
[ set mate one-of candidates
set color red
]
[ set search-radius search-radius + 0.5 + random-float 1
]
]
end

How do I assign a gender randomly to turtle breeds in NetLogo?

For an assignment in one of my classes we need to model disease spread and also have the turtles, whether sick or healthy, reproduce when a male and a female end up on the same patch, and it's also based on a slider with probability of reproduction. We're supposed to have the gender assigned randomly at birth so that the reproduction works properly. Any idea how to do this?
This is my code so far:
to setup
clear-all
ifelse netlogo-web? [set max-turtles 300] [set max-turtles 300]
create-healthy-cows Population-Size [ set shape "cow"
set color lime
set infected? false
set disease? false
ask n-of (random Population-Size) turtles [set gender-male? true]
set size 3
setxy random-xcor random-ycor
set age random 200
set label-color black ]
end
and also:
to check-reproduction
ask turtles [if gender-male? = false [
ask turtles [if any? turtles-here with [gender-male? = true]
[reproduce-cows] ]]]
end
to reproduce-cows
ask turtles [ if max-turtles < 300 [
ask turtles [if age >= 50 [if (random-float 100 < Reproduction-Rate)
[hatch 1
[set color blue] ]]]]]
end
Also I have gender-male? set as a turtles-own.
Have a look at one-of and see if you can come up with a solution. It's important to learn what you're doing and why, and not just ask for the answer, especially when its for a class assignment.
I've never seen or heard of NetLogo and it took me less than 5 minutes to come up with a solution and then find a second, better one.. Especially if this is an assignment you should be googling and researching instead of asking to be spoonfed!

Netlogo: How to make turtles stop moving when a condition (suitable patch is found) is met?

I'm trying to construct a model that simulates patterns of housing selection in a given area.
I am asking turtles to seek a settlement patch in radius-3 that offers the most resource, if one is found, they should settle there and stop moving; otherwise, they should move to somewhere else and stop moving. However, no matter what I do, no turtle seems to stop; every turtle is constantly moving around regardless of whether a settlement is found.
Below is my seek-settlement module under "go". Please advise on how to make a turtle stop moving if a condition is met (i.e. a suitable settlement is found)? I tried sticking “stop” in various places but with no difference.
to seek-settlement-1
ask turtles with [wealth >= com]
[
ifelse any? patches in-radius 3 with [pcolor = green and count turtles-here = 0]
[ ;;; if there are any neighboring empty green patches with resource larger than wealth of the turtle
move-to max-one-of patches in-radius 3 with [pcolor = green and count turtles-here = 0] [resource] ;+ (0.5 * count turtles in-radius 3 with [color = blue])]
set settlement patch-here ;;; move to the one with the most utility (defined as an even combination of resource and number of blue turtles in radius 1) and settle there
stop ;;; this stop appears to make no difference
]
[right random 360
forward 3
set wealth (wealth - com)
stop] ;;; this stop appears to make no difference
if settlement != nobody
[
set wealth (wealth - com + (0.5 * [resource] of settlement)) ;;; update the turtle's wealth by subtracting the cost of movement and adding 20% of the resource of the patch here
ask settlement
[set resource (0.5 * resource)] ;;; update the settlement patch's resource by subtracting 20%
stop ;;; this stop appears to make no difference
]
]
end
Please review the documentation of stop:
http://ccl.northwestern.edu/netlogo/docs/dict/stop.html
The stop will merely exit your ask.
Your code does not match your description. Trying to follow your description, I get something like the following:
globals [com]
patches-own [resource]
turtles-own [wealth settlement]
to setup
ca
set com 10
crt 50 [
set wealth random 500
set settlement nobody
setxy random-xcor random-ycor
]
ask patches [
if random-float 1 < 0.2 [set resource 100 set pcolor green]
]
end
to go
;you can change the filter any way you wish
ask turtles with [wealth >= com and settlement = nobody] [seek-settlement]
end
to seek-settlement
let _candidates (patches in-radius 3 with [pcolor = green and count turtles-here = 0])
if any? _candidates [
set settlement max-one-of _candidates [resource]
move-to settlement
]
ifelse (settlement = nobody) [ ;this conditional shd probably be in a separate proc
right random 360
forward 3
set wealth (wealth - com)
][
set wealth (wealth - com + (0.5 * [resource] of settlement))
ask settlement [set resource (0.5 * resource)]
]
end

NetLogo: Changing one breed's variable depending on other breed's variable in an ego-centric network environment

Dear Stackoverflow users,
I am a newbie to NetLogo and the community here, so I hope I can express myself adequately. If you need more information in order to understand my question, please, let me know. As I am not completely sure, where my problem lies, my title might even be misleading.
Here is what I am trying to do: I want an ego-centric network model, in which 1 ego (a Latino immigrant in the US) starts with a given value (between 1 and 6) for
identification with Latino culture and
identification with US/White culture.
The ego (breed #1) has 8 alters (breed #2). The alters consist of Latinos and Whites (ratio to be determined by slider in the interface: number-Latinos). The alters are randomly connected between themselves (amount of undirected links to be determined by another slider in the interface: number-of-alter-links). Each alter has a value for degree d (which is the number of links within the same ethnicity).
At each tick, ego is supposed to interact randomly with one of the alters. If the alter is Latino, then ego's initial value for Latino identification should increase by 0.1 + d * 0.1. If the alter is White, ego's initial value for US identification should increase by 0.1 + d * 0.1. The maximum value that can be reached for the identification variables is 6.
Here comes the code:
breed [egos ego]
breed [alters alter]
egos-own[identification-US identification-Latino]
alters-own[degree]
to setup
clear-all
setup-alters
setup-egos
reset-ticks
end
to setup-alters
create-alters 8
[layout-circle alters 8
if who < number-Latinos [set color orange] ; Latinos are orange
if who >= number-Latinos [set color yellow] ; Whites are yellow
]
while [count links < number-of-alter-links][
let node1 random 8
let node2 random 8
if (node1 != node2)[
ask alter node1 [create-link-with alter node2]
]
]
ask alters [ ; set degree within same ethnicity
ifelse color = yellow
[set degree (count link-neighbors with [color = yellow])]
[set degree (count link-neighbors with [color = orange])]
]
end
to setup-egos
create-egos 1 [
set identification-US initial-US-identification-ego
set identification-Latino initial-Latino-identification-ego]
end
to go
if ticks >= 50 [stop]
interact
change-identification
tick
end
to interact
ask egos [create-link-with one-of alters [set color green]]
end
to change-identification
ask links with [color = green] [let d [degree] of end1
ask egos [
ifelse link-neighbors = yellow
[ifelse (identification-US < 6)
[set identification-US identification-US + 0.1 + d * 0.1]
[set identification-US 6]
]
[ifelse (identification-Latino < 6)
[set identification-Latino identification-Latino + 0.1 + d * 0.1]
[set identification-Latino 6]
]
]
]
ask egos [ask my-links [die]]
end
This is my problem: When I am running the simulation, only the value for Latino identification changes, but not the one for US identification. This is even true, when there are no Latinos in the network. I am not sure where the problem lies. Is it in the nested ifelse command? I have tried to work my way around the nested ifelse and made several if commands, but the problem remains. Does it have to do with how I defined the two ethnicities with colors? Also, when I ask in the command center something about a particular turtle (e.g., turtle 3), I get the answer 9 times (total number of turtles). Maybe the problem is how I ask the link-neighbor(s) for its color?
Thanks for your attention! Any idea, suggestion or possible solution is highly appreciated.
This will always be false: link-neighbors = yellow.
Btw, if you post an entire model like this, you need to replace the interface globals with code-based declaration and initialization of the variables.