Ask all turtles but apply to every turtle differently - NetLogo - colors

I'm using net logo and I want to ask all turtles something but apply it to every turtle individually:
to setup-t
ask turtles [
if color = white [ set t 99 ]
if color = red [ set t 92.4 ]
if color = orange [ set t 85.8 ]
if color = brown [ set t 79.2 ]
if color = yellow [ set t 72.6 ]
if color = green [ set t 66 ]
if color = lime [ set t 59.4 ]
if color = turquoise [ set t 52.8 ]
if color = cyan [ set t 46.2 ]
if color = sky [ set t 39.6 ]
if color = blue [ set t 33 ]
if color = violet [ set t 26.4 ]
if color = magenta [ set t 19.8 ]
if color = pink [ set t 13.2 ]
if color = black [ set t 6.6 ]
]
end
In this way it applies to all turtles, but every turtle has a different color and I want t to apply to every turtle differently and individually. How can I do that?
Thanks

To make a variable that can be different for every turtle, declare it with turtles-own. So in your case, you would put
turtles-own [ t ]
Then, the code you have should work.

Related

Netlogo: Set a variable to the number of neighbors of a certain color

im just trying to count the patches that have a certain color and add that count number to a variable. Then, if the number of neighbors crosses a threshold the patch changes color but im getting a Set expected 2 inputs. I think this is a basic question but after reading i dont get the result i need.
patches-own
[reforestar
;deforestar
;temperatura
;humedad
;dosel
]
to setup
clear-all
set-default-shape turtles "frog top"
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 35 ;
]
;potrero
ask patches with [
pxcor <= 60 and
pxcor >= 30 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 44 ;
]
;borde
ask patches with [
pxcor <= 90 and
pxcor >= 60 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 66 ;
]
end
to go
ask patches [ deforestacion ]
end
to potrerizar
if pcolor = 44 [
ask patches [ set potrerizado count neighbors with [pcolor = 35] ]]
end
to deforestacion
if potrerizado >= 3 [if random 100 <= tasa-deforestacion
[set pcolor = 35]]
Thanks in advance
Regarding set, it's indeed because set expects two inputs: the variable to set and its new value, without any other sign between these two inputs - see here. You had also used it correctly in to setup.
In NetLogo, = is instead used to evaluate conditions (i.e. it expects two inputs, one on the right and one on the left, and it returns either true or false). Therefore, writing
set pcolor = 35
is the same as writing
set true ; if the patch has pcolor = 35, or
set false ; if the patch has pcolor != 35.
none of which makes sense in NetLogo.
As for the rest: at the moment, what your code does entirely depends on the value of potrerizado that is set in the interface:
If it's less than 3, nothing happens;
If it's at least 3, patches will have a certain chance of becoming brown (set pcolor 35). Which means that, sooner or later, they will all be brown.
In the code, at the moment, to potrerizar is never used.
Anyway, I can see that to potrerizar asks patches to change potrerizado, which is a global variable.
Probably what you want is to have potrerizado as patches-own and not as global, but this is just my guess based on what I can see here.
Less important, you have some superflous conditions: every patch will always have pxcor >= min-pxcor (and similar conditions that you have in to setup).

NetLogo: network- spring layout with 2 breeds

I'm new to NetLogo and having trouble adding 'layout spring'.
Two of my main issues are:
Nodes can be too close to the edges of the world (I want them to be randomly allocated, but not so close to the edges)
Nodes create links that go over the world boundaries.
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
reset-ticks
spawn-A
spawn-B
connect-spawns
end
to spawn-A ;; create one intial A-agents and add to setup
create-A-agents 1
[ set shape "triangle"
set size 0.75
set color 44
setxy random-xcor random-ycor
]
end
to spawn-B ;; create one initial B-agents and add to setup
create-B-agents 1
[ set shape "circle"
set size 0.5
set color red
setxy random-xcor random-ycor
]
end
to connect-spawns ;; create a link between B and A agent
ask B-agents [create-links-with A-agents [set color green]]
end
to go ;; create a new node based on the emprical user distribution of A-agents/B-agents
let p random-float 100 ;; create a random number between 1-100
if (p > 96) [create-A-agents 1
[ set shape "triangle"
set size 0.75
set color 44
setxy random-xcor random-ycor
let thisA self
let test-num random-float 1
ifelse test-num > 0.58
[ create-link-with one-of other B-agents [set color green]]
[ create-link-with one-of other A-agents [set color 44]]]
]
if (p <= 96) [create-B-agents 1
[ set shape "circle"
set size 0.5
set color red
setxy random-xcor random-ycor
let thisB self
let test-num random-float 1
ifelse test-num >= 0.56
[ create-link-with one-of other B-agents [set color red]]
[ create-link-with one-of other A-agents [set color cyan]]]
]
tick
end
To leave an empty space around the edge of the world where no agents are created, one way would be to change this:
setxy random-xcor random-ycor
to this:
setxy (0.8 * random-xcor) (0.8 * random-ycor)
To make it so links don't cross the world boundaries, open the view settings and turn world wrapping in both directions off (see http://ccl.northwestern.edu/netlogo/docs/interfacetab.html#the-2d-and-3d-views).

How to vary colors of NetLogo patches randomly

My patches initiate as green (65), black (0), or random base-colors. Every 5 ticks, I would like the green ones to change to a random base color, the black ones to stay black, and the random-colored ones to EITHER become green (65) or change to another random color (one-of remove 55 base-colors).
Implementing this final function (using a subset of the existing random-colored patches) is what I am struggling with.
I have tried ifelse and just different if statements, specifying ask n-of somenumber patches to turn green with the rest turning a random color. This did not work.
to setup-patches ;;PATCHES
ask n-of 400 patches [ set pcolor 65]
ask n-of 600 patches [ set pcolor one-of remove 55 base-colors ]
end
......
to vary-patches
if ticks > 0 and ticks mod 5 = 0 [
ask patches [
if pcolor = 65 [
set pcolor one-of remove 55 base-colors ]
if pcolor = black [
set pcolor 0 ]
if pcolor = one-of remove 65 base-colors [
set pcolor ............................................. ]
end
I think you're more or less there, it's just the if pcolor = one-of remove 65 base-colors that is problematic. Every time that statement is evaluated, pcolor is compared to a random one-of the list generated by remove 65 base-colors- it is not checking to see if the pcolor is found within that group. For that you need the member? primitive. For example, the slight modification below should be doing what you need:
to setup-patches ;;PATCHES
ca
ask n-of 400 patches [ set pcolor 65]
ask n-of 600 patches [ set pcolor one-of remove 55 base-colors ]
reset-ticks
end
to vary-patches
if ticks > 0 and ticks mod 5 = 0 [
ask patches [
if pcolor = 65 [
set pcolor one-of remove 55 base-colors ]
if member? pcolor remove 65 base-colors [
set pcolor one-of base-colors
]
]
]
tick
end
Edit:
to vary-patches
if ticks > 0 and ticks mod 5 = 0 [
ask patches [
ifelse pcolor = 65 [
set pcolor one-of remove 55 base-colors
] [
if member? pcolor remove 65 base-colors [
ifelse random-float 1 < ( 1 / 3 ) [
set pcolor one-of base-colors
] [
set pcolor 65
]
]
]
]
]
tick
end

Change turtle context into patch context to color patches

A table is written in turtle context with values between 0 and 1 and patchID as keys. Each patch should be colored according to the value in the table with scale-color. The result should look sth. like the heat diffusion model in the library. So far, it only colors the patches my turtles are located on. I guess I need to write the table from turtle to patch context, but have no idea how this should be done. Does Netlogo provide any option for that? Thanks!
clear-links
let alone turtles with [not any? link-neighbors]
ask turtles[
let res2 one-of other alone
;;go over each patch
foreach table:keys dict_Opinion[
;;get current opinion for both agents and store it op_a1 and op_a2
let op_a1 table:get dict_Opinion ?
let op_a2 [table:get dict_Opinion ?] of res2
let soc-dist 0
let new_op_a1 0
let new_op_a2 0
;;calculate social distance
set soc-dist abs(op_a1 - op_a2)
;;check if social distance is less than threshold D
ifelse soc-dist < updated_D [
;;if lower than D calculate new opinions for both agents
set new_op_a1 (op_a1 + (mu * ( op_a1 - op_a2)))
if new_op_a1 > 1 [set new_op_a1 1]
if new_op_a1 < 0 [set new_op_a1 0]
set new_op_a2 (op_a2 + (mu * ( op_a2 - op_a1)))
if new_op_a2 > 1 [set new_op_a2 1]
if new_op_a2 < 0 [set new_op_a2 0] ]
;;else: if the soc distance is too large opinions remain unchanged
[set new_op_a1 op_a1
set new_op_a2 op_a2]
;;newly calculated opinions are put in the opinion lists of the agents
table:put dict_Opinion ? precision new_op_a1 4
ask res2 [table:put dict_Opinion ? precision new_op_a2 4]]]
Until here, everything works fine... I tried to write new_op_a1 and new_op_a2 to a patch-own new_op, but this changes only the color of the patch, turtles are located on.
set new_op ((new_op_a1 + new_op_a2) / 2)
set pcolor scale-color white new_op 0 1
this isn't right because i don't really understand how you access a particular turtle's opinion about a particular patch, but the structure should be like this
patches-own
[ ...
what-turtles-think
...
]
ask patches
[ set what-turtles-think mean [opinion about myself] of turtles
set pcolor scale-color red what-turtles-think 0 1
]
The solution to the problem:
;; add two new variable to patches-own
patches-own [... a b ...]
to patch_avg_op
;;calculate the sum of the opinion of all turtles
ask turtles [
foreach table:keys dict_Opinion [
let c table:get dict_Opinion ?
let d patches with [plabel = ?]
ask c [
set a a + c
]
]
]
;; calculate the average and put it in patch variable b
let nr_of_turtles count turtles
ask patches [
set b (a / nr_of_turtles)
set a 0
]
end
to color-patches
ask patches [ set pcolor scale-color black b 0 1]
end

How can I change the cluster color using gvmap?

I have a .dot file below. I can draw a map visualization by using this command "sfdp -Goverlap=prism INPUT.gv | gvmap -e | neato -n2 -Ecolor=#55555522 -Tpng > OUTPUT.png".
graph {
"0" [cluster="3", label="Drawing", pos="26.163,130.97"];
"1" [cluster="3", label="Visualization", pos="270.82,243.14"];
"2" [cluster="2", label="Graph", pos="271.43,16.263"];
"3" [cluster="2", label="Arizona", pos="670.15,16.263"];
"4" [cluster="2", label="University", pos="415.11,16.263"];
"5" [cluster="1", label="Map", pos="513.44,131.26"];
"0" -- "1";
"1" -- "2";
"0" -- "2";
"3" -- "4";
"5" -- "1";
"5" -- "2";
}
the algorithm behind GraphViz assigns color to my clusters. So the output is an image like bellow.
How can I assign my own colors to the clusters instead of colors used by the algorithm?
Thansk
Yes and no. Based on this: https://docs.oracle.com/cd/E36784_01/html/E36870/gvmap-1.html,
you can add this modifier to the command you use in the gvmap section to create the graph:
-c k The integer k specifies color scheme used to color the
countries. By default k = 1.
Acceptable values are:
0 : no polygons
1 : pastel
2 : blue to yellow
3 : white to red
4 : light grey to red
5 : primary colors
6 : sequential single hue red
7 : sequential single hue lighter red
8 : light grey

Resources