Netlogo- average link length code snippet - netlogo

I'm trying to monitor the length of my links in Netlogo- but I'm having a hard time getting used to the syntax. Is anyone familiar enough with it to just take the mean of all link-lengths?
Thanks.

Using this simple setup:
to setup
ca
crt 10 [
setxy random-pxcor random-pycor
]
ask turtles [
if not any? my-links [
create-link-with one-of other turtles
]
]
reset-ticks
end
You can use of with [link-length] to return a list of link lengths (try just typing print [link-length] of links in the console), then use mean to take the mean of that list. If you put that in a to-report procedure, you will be able to display the mean-link-length in a "monitor" widget in the "Interface":
to-report mean-link-length
report mean [link-length] of links
end

Related

Using the command move-to with max-one-of and the error appears: MOVE-TO expected input to be an agent but got NOBODY instead

I'm new to NetLogo and I have a question that I'm sure is pretty basic. But, I'm not getting over the difficulty.
If anyone can help me overcome this difficulty, I would be very grateful.
I would like from the patch where the turtle is found to consider the 8 neighboring cells in search of the highest pveg value. If it has equally high values, choose 1 of these randomly. Upon finding the highest pveg value of the neighbors, the turtle went there.
I am using the command: max-one-of. I think it serves my purpose. But, I'm making some syntax error that shows the following error: MOVE-TO expected input to be an agent but got NOBODY instead.
Thanks in advance
extensions [ gis ]
globals [ veg ]
patches-own [pveg]
to setup
clear-all
reset-ticks
setup-patches
crt 1 [
ask neighbors [ set pcolor blue ]
set color black
]
end
to setup-patches
end
to go
ask turtles [neighboring]
end
to neighboring
let my-neighWith-pveg [ neighbors with [pveg > 0.2] ]of patch-here
ifelse neighWith-pveg = 0
[ ]
[ move-to max-one-of patches [my-neighWith-pveg] set pcolor red ;;ERROR HERE
]
end
The NetLogo dictionary says, max-one-of needs an agentset and a reporter as input:
max-one-of agentset [reporter]
In your code, you use two agentsets: turtles and my-neighWith-pveg
Since you want to chose from the neighbors (and not all turtles) with the hightes pveg, you can write:
max-one-of my-neighWith-pveg [pveg]

Deleting several links in a complete directed weighted graph of turtles

I have n turtles which all have links with one another. That means I posses a complete directed weighted graph. I've managed to locate the links with maximum value per each turtle, but now I want to delete all other links without maximum value, again per each turtle.
I'm using the [die] function but in order to distinguish the set of links per each turtle and then clear links from each set
This is the line of code I'm using:
> ask turtles
[
> ask my-in-links with [trust < max [trust] of links with [other-end] = ] [die]
]
However, I thought instead of using other-end function I could also use this line of code
> ask turtles [
ask my-in-links with [trust < max [trust] of links with [out-link-to] = ] [die]
]
My initial thought was by using one of the two functions (other-end/ out-link-to), I could create a common attribute between the set of links.
My main problem is
I'm not sure whether the functions (other-end/ out-link-to) are correct
I don't know what to write after the "=" expression
I don't know which turtle the ask turtle function begins to work with
Here's a complete model that does what you are trying to achieve.
links-own [trust]
to testme
clear-all
; create complete directed network
create-turtles 10
ask turtles
[ create-links-to other turtles
[ set trust random 50
]
]
; display complete network briefly
layout-circle turtles 10
type "average trust value is: " print mean [trust] of links
wait 5
; delete all except highest trust for each turtle
ask turtles
[ let keeper my-in-links with-max [trust]
ask my-in-links with [not member? self keeper][die]
]
type "average trust value is: " print mean [trust] of links
end
I have it showing the initial network and printing average trust values so you can see what's going on.
The important concept here is that my-links is an agentset (in particular, it's a linkset because the agents in the set are links). Since what you're trying to do is just look through the links that are connected to one turtle, then it doesn't matter which turtles are at the other end and you don't need to refer to the other end at all.
You can simply look at the set of links that attach to the particular turtle (my-in-links or my-links or my-out-links) and then look at the values of trust for those links. I have used with-max to find the one with the highest value and then used set membership. But you could also use
ask turtles
[ let upper max [trust] of my-in-links
ask my-in-links with [trust < upper] [die]
]
which is closest to the code you wrote in your question.

netlogo tie-mode "fixed" fails to maintain link-length in graphs with medium-level degree

I am trying to create a network that moves through the environment as a "static" unit, i.e. nothing in the simulation changes except the position and orientation of the whole, the position and orientation of the individual turtles relative to one another are fixed by their links. Turtles are connected via undirected links, which are tied and set to tie-mode "fixed".
The problem is that in certain situations the links fail to remain fixed and link-lengths begin to change. Initially I noticed that, where the average network degree is relatively low or the network is a complete graph, the tie primitive works. However, when links were created to produce a graph that is moderately connected the link-lengths between the turtles begins to change. Upon further experimentation I can create a network with the same number of links and turtles but with different configurations i.e. the network structure is different, that sometimes maintain the positions and link-lengths but in other situations fail to do so.
How do I get the network to move as a unit no matter how connected the network is or what the configuration of the network is? See example code below, I have added code at the end where you can run multiple configurations of a network with 6 turtles and 6 links to see the issue for yourself, try running at least a half dozen iterations. Thanks!
this produces a network that moves as a unit
to setup
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
This produces a network whose links are still tied and set to tie-mode "fixed", but change their link-lengths. The more links that are asked to die, the more the link-lengths change.
to setup
clear-all
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
ask one-of links [die]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
Here is additional code showing a specific instance of link-length change. Please input the seed 659269695 when prompted by the button "use-seed-from-user". Apologies if the code is clunky, first time using random-seed. "Print-lengths" button is to confirm that lengths change.
;USE seed: 659269695
to use-new-seed
let my-seed new-seed ;; generate a new seed
output-print word "Generated seed: " my-seed ;; print it out
random-seed my-seed ;; use the new seed
reset-ticks
end
;; Use a seed entered by the user
to use-seed-from-user
loop [
let my-seed user-input "Enter a random seed (an integer):"
carefully [ set my-seed read-from-string my-seed ] [ ]
ifelse is-number? my-seed and round my-seed = my-seed [
random-seed my-seed ;; use the new seed
output-print word "User-entered seed: " my-seed ;; print it out
reset-ticks
stop
] [
user-message "Please enter an integer."
]
]
end
to setup
clear-all
create-turtles 6
ask turtles [
fd 5
set shape "circle"
set size 1
set color yellow
if count links < 7 [ask one-of turtles [create-link-with one-of other turtles
[tie]]]]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
to print-lengths
print sort-by < [precision link-length 2] of links
end
I slightly revised your code so that the go procedure includes breaking a link. I also got rid of the explicit setting of tie-mode since that is done by setting the link to tie and added a tick so I could plot. So the code looks like this:
to setup
clear-all
create-turtles 10 [fd 2]
ask turtles [create-links-with other turtles [tie] ]
reset-ticks
end
to go
ask one-of links [die]
ask turtles [lt 1 fd 1]
tick
end
As far as I can see, the turtles move as a unit until it fragments with the loss of links.
I added a monitor for mean [link-length] of links, which is what I think you are asking about and also a plot of the same calculation. Yes, it is true that the average link length changes, but remember that the links are not all the same length. If a longer one dies, then the average length will reduce, and if a shorter one dies then the average will increase. The plot wanders a little, but it is basically flat until fragmentation.

NetLogo agentset form links with other agentset

I'm looking to have a turtle-set form links with other turtle-set.
My current attempt that also feels contrived, as not every hive here will be selected.Is there another way of going about this?
to link-bees-to-hives [bees-agentset hives-agentset]
ask bees-agentset [
create-link-with one-of hives-agentset
]
end
How can I create links between two agentsets netlogo, in order of the turtles in the set?
Do you want to have bees make a link with just one other hive? If you have relatively enough bees your attempt is probably fine, but if you want to weight the bee selection so that they preferentially link with hives with fewer associate bees, you could use some kind of min-one-of solution or a perhaps something from the rnd extension. For example, a bee and hive setup:
extensions [ rnd ]
breed [ bees bee ]
breed [ hives hive ]
to setup
ca
create-hives 3 [
set color white
set shape "box"
set size 2
setxy random-xcor random-ycor
]
create-bees 15 [
set color yellow
set shape "bug"
setxy random-xcor random-ycor
]
reset-ticks
end
And the weighted selection:
to link-bee-to-hive
ask bees [
create-link-with rnd:weighted-one-of hives [ 1 - count my-links / count bees ]
]
print [ count my-links ] of hives
end
Of course, if you have few enough bees and hives, you may still end up with a hive or two not getting linked-to.

NetLogo BehaviorSpace does not recognize initial settings on patches by mouse-down

I have a patch lattice with an arbitrary configuration of states whose initial setting is done manually using mouse-down primitive.
While running BehaviorSpace, it moves the arbitrary setting starting with a random configuration of states.
How can I fix it?
I'm really not sure what you're asking, so here are two possibilities.
If you want BehaviorSpace to use a random state for patches
to setup
clear-all
if behaviorspace-run-number != 0 [ ; if BehaviorSpace is running
ask patches [
; use whatever random state you want...
set pcolor one-of [ black white ]
]
]
reset-ticks
end
Another option is the use the "Setup commands" of the BehaviorSpace dialog box:
If you want to run BehaviorSpace experiments with state previouly entered with the mouse by a user...
...then things are a little trickier. The basic idea is to save that state to a file and then load that file and initialize the state when the model runs from BehaviorSpace.
In the example below, I do this using the csv extension.
Keep in mind that taking pcolor to represent the patches' state is just for the sake of example; it could be any other kind of state.
extensions [ csv ]
to setup
clear-all
if behaviorspace-run-number != 0 [
(foreach (sort patches) (first csv:from-file "patch-states.csv") [
ask ?1 [ set pcolor ?2 ]
])
]
reset-ticks
end
to draw ; call this from a "forever" button
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor white ]
]
end
to save ; call this from a regular button
let patch-states map [ [ pcolor ] of ? ] sort patches
(csv:to-file "patch-states.csv" (list patch-states))
end
I realize that some parts of the code above can be tricky to understand (i.e., the use of foreach and map). Feel free to ask follow up questions if there is a specific part that you don't understand.