NetLogo: network- spring layout with 2 breeds - layout

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).

Related

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

probability for variable value in agentsets, netlogo

I am trying to use probability to assign [0] or [1] individual values for a turtles-own variable in NetLogo, but have only found ways of printing or reporting probability outputs rather than using them to determine a variable value.
Example:
I am asking two turtles to check whether they each want to exchange information with each other, and have assigned a variable exchangeinfo. If exchangeinfo = 0, then no information exchange happens. If exchangeinfo = 1, then information exchange occurs.
Currently I've hard-coded [set exchangeinfo 1] as a placeholder.
But I'd like each turtle to have a 25% chance of exchangeinfo = 1, but I do not want to set variables one at a time.
Any suggestions?
#Alan's comment will work. Here is a super simple model that will do what I think you're asking.
turtles-own[exchangeinfo]
to setup
clear-all
reset-ticks
make_turtles
end
to go
move
tick
if (ticks = 1) [inspect turtle 1]
end
to make_turtles
create-turtles 10
ask turtles
[
set color pink
set size 2
set xcor random max-pxcor
set ycor random max-pycor
set exchangeinfo 0
]
end
to move
ask turtles
[right random-float 270
forward random-float 3
if ((count (turtles in-radius 2)) > 0)
[move-to one-of turtles in-radius 2]
]
encounter ;<- this is the function that will decide whether or not to exchange info.
end
to encounter
ask turtles[
if (count turtles-here > 0)
[ifelse (random-float 1 < 0.25) ;note this is essentially #Alan's answer
[set exchangeinfo 1]
[set exchangeinfo 0]
]
]
end
I'm assuming you'll then want some sort of
ask turtles-here [if (exchangeinfo = 1) [do stuff]]
as well

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

Ask all turtles but apply to every turtle differently - NetLogo

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.

rotation graphics in post script

I was wondering how can I rotate a graphic, say a rectangular by certain angle in post script.
Or at least is there any way to draw a very bold ! like, with an angle !?
I have list of sentence around a circle, so each or in 1 direction, and now, I would like to put each in a rectangular and make hyperlink for them.
The power of Postscript is its ruthless pursuit of the ideal of "delayed binding". The implementation of rotations is no exception. It works by making use of a more general tool, the Affine Transformation Matrix.
You can rotate both text and graphics (because text IS graphics) because all user specified coordinates are first multiplied through this matrix to produce device coordinates.
To perform all the necessary tricks (scaling, rotation, shear, translation), we first have to extend the 2d points to 3d points on the plane z=1 (don't ask me why; read Bill Casselman's Mathematical Illustrations or the Adobe Blue Book for more).
[ x [ a b 0
y * c d 0 = [ x' y' 1 ] = [ ax+cy+e bx+dy+f 1 ]
1 ] e f 1 ]
Since the 3rd column of the matrix is always [ 0 0 1 ] it is omitted from the external representation, and the matrix is described in postscript as:
[ a b c d e f ]
So when you use a coordinate pair for, say, a moveto operator, moveto first transforms it to device coordinates, x' = ax+by+e, y' = cx+dy+f, before adding a <</move [x' y']>> element to the current path.
Change the matrix: change the "meaning" of user coordinates.
The identity matrix is this:
[ 1 0 0 1 0 0 ] % x' = x, y' = y
To scale, replace the 1s with x and y scaling factors:
[ Sx 0 0 Sy 0 0 ] % x' = Sx*x, y' = Sy*y
To translate, replace e and f with the x and y translation offsets:
[ 1 0 0 1 Tx Ty ] % x' = x+Tx, y' = y+Ty
To rotate, replace a,b,c,d with sin and cos scaling and shearing factors:
[ cosW sinW -sinW cosW 0 0 ] % x' = x*cosW-y*sinW, y' = x*sinW+y*cosW, where W is angle(degrees) from x-axis
You "install" this matrix with concat which takes the Current Tranformation Matrix (CTM), multiplies it by your new matrix, and uses the product as the new CTM. So translate, rotate, and scale are just "convenience functions" which could be implemented like this:
/translate { [ 1 0 0 1 7 -2 roll ] concat } def
/scale { [ 3 1 roll 0 0 3 -1 roll 0 0 ] concat } def
/rotate { [ exch dup cos exch sin dup neg 2 index 0 0 ] concat } def
Since the CTM is part of the graphics state, you can use the graphics state stack to manipulate your transformations in a hierarchical manner:
/box { % x y w h %create a path in the shape of a box w*h with lower left corner at x,y
4 2 roll moveto
exch dup 3 1 roll
0 rlineto
0 exch rlineto
neg 0 rlineto
closepath
} def
/Courier 10 selectfont
100 100 100 100 box stroke % draw an oriented box
120 120 moveto (inside) show
gsave
150 150 translate % make the center of the box the new 0,0 point
45 rotate % rotate CCW 45 degrees
0 0 100 100 box stroke % a rotated, shifted box
20 20 moveto (inside) show
grestore
100 200 100 100 box stroke % another box, just north of the first, in the original coordinte system
120 220 moveto (inside) show
This produces the following image:
(source: googlecode.com)
I haven't used PostScript for a long time, but as I remember you could just use "rotate".
% do some steps
% ...
% ...
20 20 moveto % go to new position
30 /Times-Roman SetFont % select active font
45 rotate % set direction to diagonal
(Something)show % print text "Something"
showpage % show it all
cheers Kris
Postscript renders graphics in a given context - and it is this context that can be rotated (or scaled/translated) before drawing. Therefore any element on the image can be transformed as one wishes, all you have to do is to perform the necessary context transforms beforehand.
However, unfortunately, while I can give you an idea of it in this writing, i is a fundamental concept of Postscript, and you won't be able to do any real work in it without understanding that first. I suggest reading a brief tutorial such as the one in http://paulbourke.net/dataformats/postscript/ .
So, the "rotate" name is a function that does rotate the graphcis context - you use rotate, before drawing anything you want (rendering text also being "drawing" in this case).
%!PS
(Helvetica) findfont 12 scalefont setfont %select a font to use
300 300 translate % sets the orign at 300,300 points from the bottom left of page
/start 5 def % creates variable for keeping track of horizontal position of text
36 % pushes number of repeats on the stack
{
start 5 moveto % places cursor on the starting position
(postscript) show % renders the string in the starting position, within the current context
/start start 3 add def % increases the value on the variable
10 rotate % rotates the context 10 degrees clockwise (around the 300,300 new origin)
} repeat
showpage % renders whole page

Resources