How to update 'myself' variables from within a block - netlogo

In netlogo 5.3.1, assume:
marketmakers-tier1-own [ trading-capital ]
I want to do this:
ask other marketmakers-tier1 [
let T-capital [ trading-capital ] of myself
let T-cost some-amount
;; do some stuff
; now update myself's trading-capital
set [ trading-capital ] of myself ( T-capital - T-cost )
]
Produces this error on the set command:
This isn't something you can use "set" on.
Any suggestions I will appreciate.

Related

How to access the first turtle of a code sequence in NetLogo 6.2?

I have the following doubts:
I have several individuals of 9 turtle codes.
The turtle codes are: R1M1 R2M1 R3M1 R1M2 R2M2 R3M2 R1M3 R2M3 R3M3. And I have 10 individuals of each code.
As the code is structured in a loop sequence and the order that the code exits in the result exported in .csv, I would like to ask the first individual of the first code (R1M1) to print the output header. But, I'm only getting it using one-of, and then the header often doesn't come out in the first line of the output. And I don't know how I can access the first turtle from the R1M1 code. Does anyone have any ideas?
What I thought of is calling turtle 0 which has the code R1M1. But, I still don't know how to do this in NetLogo.
OBS.: I tried to use only turtle 0, but in the other codes, too, there is turtle 0 and then the header appears.
Thanks in advance
let n count turtles with [ profiles-code = "R1M1" ]
if n > 0
[
ask one-of turtles with [ profiles-code = "R1M1" ]
[
prepare-header-output;; CALL A PROCEDURE
]
]
An example of how the header is coming out (has 3 individuals from each of the 9 codes)
I would use a flag / semaphore variable to indicate if a header is needed. You'll need to adapt the below if you have your turtles outputting the data, but hopefully this will get you pointed in the right direction- more details in comments:
globals [ need-headers? ]
turtles-own [ class ]
to setup
ca
random-seed 1
set need-headers? true
if file-exists? "test_output.csv" [ file-delete "test_output.csv" ]
crt 10 [
set class item random 5 "ABCDE"
]
reset-ticks
end
to go
if count turtles < 1 [ stop ]
ask turtles [
if random-float 1 < 0.25 [ die ]
]
; Toy output example
file-open "test_output.csv"
foreach range length "ABCDE" [ i ->
; Output headers if needed
if need-headers? [
file-print "turtle-type, count, ticks"
; Change the need-headers? to false so this step will only happen once
set need-headers? false
]
let ltr item i "ABCDE"
let n-type count turtles with [ class = ltr ]
file-print ( word ltr "," n-type "," ticks )
]
file-close
tick
end
Output here looks something like:
but if I don't change need-headers? to false, it looks like:

Is it possible to transform a 4-column list into a 4-column table in NetLogo 6.2?

I have a code that generates an output in list form as below:
I would like the output to look like this:
is it possible to do this in netlogo 6.2?
Yes, but- I think your approach may be overcomplicating what you are after. As an alternative, consider something like this toy model:
extensions [ csv ]
turtles-own [ my_xcor my_ycor ]
globals [ output_list ]
to setup
ca
set output_list [["who" "my_xcor" "my_ycor" "tick"]]
crt 2
reset-ticks
end
to go
ask turtles [
rt random 90 - 45
fd 1
set my_xcor pxcor
set my_ycor pycor
set output_list lput ( list who my_xcor my_ycor ticks ) output_list
]
tick
end
to example-experiment
setup
repeat 5 [ go ]
csv:to-file "example_output.csv" output_list
end
If you run the example-experiment procedure, it will export a file that looks something like:
If you must go this other route, and you can't parse your original csv output in something like R instead, which would potentially be simpler, consider this different setup:
extensions [csv]
globals [ output-list ]
turtles-own [ xcor-list ycor-list tick-list]
to setup
ca
reset-ticks
set output-list [["who" "my_xcor" "my_ycor" "tick"]]
crt 2 [
set xcor-list []
set ycor-list []
set tick-list []
]
repeat 5 [
ask turtles [
rt random 90 - 45
fd 1
set xcor-list lput pxcor xcor-list
set ycor-list lput pycor ycor-list
set tick-list lput ticks tick-list
]
]
end
Now, the idea is to loop over the turtles and collapse each of their tracking lists into a list of lists before exporting:
to export-long
; Iterate over each turtle to extract their listed values
foreach sort turtles [
t ->
; Pull values / lists from each turtle in order
let cur-x-list [xcor-list] of t
let cur-y-list [ycor-list] of t
let cur-tick-list [tick-list] of t
let cur-who-list n-values ( length cur-x-list ) [[who] of t]
( foreach cur-who-list cur-x-list cur-y-list cur-tick-list [
[ a b c d ] ->
let to-append ( list a b c d )
set output-list lput to-append output-list
])
]
; Export the list to csv
csv:to-file "list_example_output.csv" output-list
end
Result:

Netlogo Error: "This <turtle> is already dead

I am studying flocking behavior in netlogo, and in order to keep track of various flocks I am using a hidden "flock-holder" turtle that I can hatch or let die if a new flock is created or an existing flock runs out of members. I am coming across an issue however, where on occasion when I am trying to interface with some data within a flock, as referred to through an individual member of a flock, I get a message saying "That is dead", causing the code to fail.
From what I understand of the "die" command, if a turtle of any kind dies, it should remove itself from any agentset or variable that references it, and therefore this sort of error shouldn't be a problem? How can I fix or at least debug this strange issue?
Code of my flock-evaluation function that is having the issues below:
to evaluate-flock
if is-in-flock = True ; checking to see if a flock has died out
[
if get-flock-size flock-reference < 2 ; is the turtle the only one in the flock?
[
if verbose = True [ print "Flock has dwindled to nothing" ]
ask flock-reference ; has no more members, so is removed.
[
ask flock-members
[
set flock-reference nobody ; clear any remaining flock members of association with this flock
]
die
]
set is-in-flock False ; no longer in a flock
]
]
ifelse is-in-flock = True ; is turtle in a flock?
[
if verbose = True [ type "This turtle is in flock " print [ who ] of [ flock-reference ] of self ]
if any? other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ]; check for nearby turtles that are in different flocks
[
if verbose = True [ print "There are other nearby flocks" ]
let current-school-size ( get-flock-size [ flock-reference ] of self )
if verbose = True [ type "I am part of a school of " print current-school-size ]
let temp-list turtle-set other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ] with [ ( get-flock-size flock-reference ) > current-school-size ] with [ subtract-headings ( average-schoolmate-heading [ flock-members ] of flock-reference ) heading < 60]; are any nearby turtles in different, larger flocks that I am alligned with? if so, add them to a list
if count temp-list > 0 ; does the list have any members?
[
if verbose = True [ print "Found a bigger flock" ]
ask flock-reference
[
remove-from-flock myself ; remove myself from my old flock
]
set flock-reference [ flock-reference ] of ( max-one-of temp-list [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; sets it to true in case it wasn't for some reason.
]
]
]
[
if verbose = True [ type "Turtle " type [ who ] of self print " is not in a flock" ]
ifelse any? other preys in-radius vision with [ is-in-flock = True ] ; are there any pre-existing flocks the turtle can join?
[
if verbose = True [ print "There are nearby flocks" ]
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = True ] ; grab any nearby turtles that are already in a flock
***set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading ( [ flock-members ] of flock-reference ) ) heading < 60]; remove any that are not aligned with this turtle***
if count potential-flock > 0
[
if verbose = True [ print "There are nearby flocks that I am aligned with" ]
set flock-reference [ flock-reference ] of ( max-one-of potential-flock [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; turtle is now in a flock
]
]
[ ; if there are no pre-existing flocks, turtle starts its own
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = False ] ; Grab any nearby turtles not already in a flock
set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading potential-flock ) heading < 60]; remove any that that are not aligned with this turtle
if count potential-flock > 0
[
if visualize-flock-creation = True
[
set color green
ask potential-flock [ set color green ]
wait 0.25
set color blue
ask potential-flock [ set color blue ]
]
if verbose = True [ type "Number of nearby potential flockmates " print count potential-flock ]
hatch-flock-holders 1 ; create a new flock-holder
[
set size 0
set color black ; sets the new flock's placeholder color to the background
set flock-members potential-flock ; adds the list of members to the new flock
ask flock-members
[
set flock-reference myself ; asks the new flock members to add the new flock as their flock-reference
set is-in-flock True ; all these turtles are now in a flock
]
]
]
]
]
end
Potentially unclear variable name reference for code above:
flock-reference: - A variable held by each flocking "prey" turtle, that just points to the hidden "flock-holder" turtle.
flock-members: - An agentset of "prey" turtles that are attached to the hidden "flock-holder" turtle.
I have added an image of the full error message below.
Please let me know if there is any confusion about what's going on here, or if there is anything I can clarify. Thank you!
I can't test this, but I expect that the error stems from agent prey 2 having a reference to a flock-holder that has died. When a flock-holder dies, it is (as you know) deleted from any angentsets that it was a member of and variables that hold a pointer to it are reset to point to nobody. However, NetLogo is smart enough to know that this nobody is a dead flock-holder and gives you the error message you've encountered. If after the error you inspected prey 2, or entered show [flock-reference] of prey 2 at the command line, I expect that you would find that flock-reference was indeed set to nobody.
My guess is that somewhere in your code, not all the prey that were in the (now dead) flock were reassigned to another flock, but rather kept their old value of flock-reference, now nobody. When you ask a flock to die, you might add the line show preys with [flock-reference = nobody]. If there are any, you could track down why.
Hope this helps,
Charles

Preferential attachment: Selecting a node to attach

I've been struggling with this one for the last 24 hours or so I feel like I'm missing something relatively simple here.
to setup-scale-free-network
clear-all
;; Make a circle of turtles
set num-nodes (num-children + num-adults + num-toddlers)
create-children num-children
create-adults num-adults
create-toddlers num-toddlers
layout-circle turtles (max-pxcor - 8)
ask turtles[
create-links-with turtles with [self > myself and random-float 5 < probability]
]
setup
end
to-report find-partner
report [one-of both-ends] of one-of links
end
The above code creates a set number of turtles of various breeds and creates a number of links between these breeds.
to go
reset-ticks
make-link find-partner
tick
end
The two procedures would be called until the needed level of degree distribution as been met.
What I want to do is use the find-partner procedure to move towards preferential attachment to do this I need to modify this code to create a link from the node find partner has selected to one of each of the other three types of breeds in my network.
to make-node [old-node]
crt 1
[
set color red
if old-node != nobody
[ create-link-with old-node [ set color green ]
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
My own attempts have lead no where to be honest. I know I'm asking for a lot of help but I'm at my wits end here, thank you for your help and patience.
I was unable to completely understand your question. I am guessing that you wish to create another type of link which you call preferential attachment (with green color) between turtles who are already a part of the network.
One thing you might want in this situation is that you do not pick turtles which are already in a preferential attachment network. [this is just my assumption]
I have modified your code (as follows) to get the desired network with preferential attachment shown with green colored links, have added a turtle-variable already-attached which is used to exclude turtles which are already preferentially attached to others.
Hope this helps!
breed [ children child ]
breed [ adults adult ]
breed [ toddlers toddler ]
children-own [ already-attached ]
adults-own [ already-attached ]
toddlers-own [ already-attached ]
to setup-scale-free-network
clear-all
let num-children 5
let num-adults 5
let num-toddlers 5
let probability 1
;; Make a circle of turtles
create-children num-children [ set color orange ]
create-adults num-adults [ set color green ]
create-toddlers num-toddlers [ set color blue ]
layout-circle turtles (max-pxcor - 8)
ask turtles
[
create-links-with turtles with [self > myself and random-float 5 < probability]
]
end
to go
setup-scale-free-network
ask turtles with [already-attached = 0]
[
attach-to-one-of-each-breed self
]
end
to attach-to-one-of-each-breed [ source-node ]
ask source-node
[
if any? other children with [ already-attached = 0 ]
[
ask one-of other children
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
if any? other adults with [ already-attached = 0 ]
[
ask one-of other adults
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
if any? other toddlers with [ already-attached = 0 ]
[
ask one-of other toddlers
[
set already-attached 1
create-link-with source-node [ set color green ]
]
]
]
end

How can I set a value to myself's variable in an ask other turtles[ ] block

So I have this current setup
ask turtles [ ;i want this turtle (myself)
ask other turtles [ ;to ask other turtle, one by one (self)
if Smin < Sim myself self [ ;to run a function wherein
ifelse Sim myself self < Smax ;if Smin < Sim myself self < Smax
[ ;if block ] ;self will be assigned to the variable
[ ;else block ] ;of myself called 'Ac'
]
]
]
How can I do this?
Well, you could ask myself [ set Ac myself ], but this is a bit confusing. (The referent of myself changes every time you enter an ask block, so myself is used twice to refer to two different agents.)
What I would suggest is to assign more explicit variable names to your agents. self/myself are convenient for simple code, but you don't have to use them all the way:
ask turtles [
let t1 self
ask other turtles [
let t2 self
if Smin < Sim t1 t2 [
ifelse Sim t1 t2 < Smax
[ ask t1 [ set Ac t2 ] ]
[ ] ; do something else?
]
]
]