How do I pass lists (and other information) from the parent models global variable to it's child's model global variable in Netlogo's LevelSpace - netlogo

I'm currently building a multi level ABM using Levelspace. I am also using the extrawidget extension as a frontend GUI for entering and capturing user information. The extrawidget GUI is a stand alone Netlogo model and serves as the parent model in Levelspace. Some of the captured information is stored as lists in the parent models global variables.
I am trying pass the content of the extrawidget parent model global variables (particularly the lists) to the child model's global variables. I cannot seem to find a way to accomplish this. I've tried "ls:let" in conjunction with "ls:ask" to set the child models global variable but that did not work. I used "ls:assign" but that was unsuccessful also.
I believe some kind of reporter in both the parent and child model may be the answer but I am unable to determine how to code something like that.
Could someone please assist me in solving this problem.
Thanks
Rudy
--------------------------------------------------------Follow-Up---------------------------------------------------
I discovered that ls:assign did work, "clear-all" in the child model's setup command wiped out the information in global variables. I removed the "clear-all" and can see the lists in monitors I added for the global variables but even though the lists are there I'm still getting this error message when the child model attempts to run "setup"
" Extension exception: Model 0 (Scaled Agile Simulator1.nlogo) encountered an error: ITEM expected input to be a string or list but got the number 0 instead.
error while observer running LS:ASK
called by procedure SETUP
called by procedure __EVALUATOR"
I have included the child model setup code below:
to setup
ls:reset
;clear-all
let n length TotEpics
foreach n-values n [ [i] -> i ] [ [i] ->
create-Epics 1 [
setxy -20 20
set label item i TotEpics
set Complex item i EpicCmplx
set Stories item i EpicStories
set ETime [ ]
set Tasks1 EpicTasks1
set Tasks2 EpicTasks2
set Tasks3 EpicTasks3
set Tasks4 EpicTasks4
set shape "box"
set size 8
]
]
let o length ScrumTN1
foreach n-values o [ [i] -> i ] [ [i] ->
create-Scrum-Teams 1 [
set label item i ScrumTN1
set shape "Scrum"
set color color
set Members item i ScrumTM1
set Strat item i ScrumStrt1
set AsgnEpic item i ScrmEpic1
set S-Stories 0
set Cost item i ScrumCst1
set Value [ ]
set Time [ ]
set S-Story-Size 0
set xcor -16
set ycor 12 + i * -7
set size 4.5
ls:create-interactive-models 1 "Problem_Solving_in_Networks WIP.nlogo"
set model-id last ls:models
ls:hide model-id
]
]
let b length ScrumTN2
foreach n-values b [ [i] -> i ] [ [i] ->
create-Scrum-Teams 1 [
set label item i ScrumTN2
set shape "Scrum"
set color item i Color1
set Members item i ScrumTM2
set Strat item i ScrumStrt2
set AsgnEpic item i ScrmEpic2
set S-Stories 0
set Cost item i ScrumCst2
set Value [ ]
set Time [ ]
set S-Story-Size 0
set xcor -4.5
set ycor 12 + i * -7
set size 4.5
ls:create-interactive-models 1 "Problem_Solving_in_Networks WIP.nlogo"
set model-id last ls:models
ls:hide model-id
]
]
let p length KanbanTN3
foreach n-values p [ [i] -> i ] [ [i] ->
create-Kanban-Teams 1 [
set label item i KanbanTN3
set shape "Kanban"
set color color
set xcor -18 + i * 9
set ycor -20
set size 4.5
ls:create-interactive-models 1 "Kanban3_v5_2.nlogo"
set model-id last ls:models
ls:hide model-id
]
]
All the "variables" in "set variable-name item i variables" are lists so I'm not expecting to see the error message stating "ITEM expected input to be a string or list but got the number 0 instead"
Anyone have any insights?
Thanks
Rudy
---------------------------------------------Please Disregard until further notice-----------------------------------
I've discovered some missing code in my setup command causing this "ERROR". Please disregard my question. If I run into any serious roadblocks I'll reach out.
Thanks

Related

Netlogo: values of same breed variables won't sum. The values are created and updated from models connected to the turtles via Levelspace

I am currently creating a Multi-level ABM using Levelspace. In the parent model I'm trying to sum the values of one variable of multiple turtles of the same breed (the number of turtles are determined by a different breeds-own variable). The variable for each turtle receives it's value from their respective child models connected via Levelspace.
Currently the parent model can receive the values from the child models. I have written a code that has a parent model turtle select particular turtles representing child models, have those selected turtles constantly update their variables totals (received from their respective child models) and display each updated total at each tick.
My problem is summing each updated child model totals into one total in the parent model that updates after each tick. Here's the code that so far:
ask turtle 0 [
if breed = Epics
[
let closteams Teams with [AsgnEpic = "Epic 1"]
foreach sort-on [who] closteams
[
the-closeteams -> ask the-closeteams [show reduce + [Value] of the-closeteams]
]
]
]
I've added a variable in the foreach block and tried to sum the totals but only received 0 as an output (see code below):
foreach sort-on [who] closteams
[
the-closeteams -> ask the-closeteams [show reduce + [Value] of the-closeteams]
let vartot sum [value] of closteams
]
Can anyone guide me on how to solve this problem?
Thanks
Rudy
----------------------------------UPDATED INFO-----------------------------------
The model has three levels consisting of several models connected by the Levelspace extension.
The top level is the parent model. The parent model is a Graphical User Interface model created with the extrawidget extension. Users enter information in this parent model to customize the model. This Graphical User Interface model sends all entered information to a second model (the first level child model) via Levelspace. The second model is where the user runs the model.
Here is the part of the setup procedure from the second model (the first child model). It creates Epics and teams (teams call their own child models (the second level child models) via Levelspace):
to setup
ls:reset
let n length TotEpics
let horizontal-interval (world-width / n)
foreach n-values n [ [i] -> i ] [ [i] ->
create-Epics 1 [
set label item i TotEpics
set color item i ColorEpics
set shape "box"
setxy (min-pxcor - 0.5 + horizontal-interval * (0.5 + who)) 15
set size 8
]
]
let o length TN1
foreach n-values o [ [i] -> i ] [ [i] ->
create-Teams 1 [
set label item i TN1
set color color
set Members item i TM1
set AsgnEpic item i Epic1
set Value []
set xcor -16
set ycor 12 + i * -7
set size 4.5
ls:create-interactive-models 1 "Problem_Solving.nlogo"
set model-id last ls:models
ls:hide model-id
]
ask Teams [
if AsgnEpic = "Epic 1" [
create-link-with epic 0
set xcor [xcor] of epic 0
set ycor [ycor - 8] of epic 0
set color blue ]
while [any? other turtles-here] [if can-move? 2 [set heading 180 fd 5 set
color blue + 2]]
if model-id = 0
[
ls:let name label
ls:let mem Members
ls:let AE AsgnEpic
ls:ask 0 [
set Number-of-Agents mem
set Epic AE
setup ]
]
if model-id = 1
[
ls:let name label
ls:let mem Members
ls:let AE AsgnEpic
ls:ask 1 [
set Number-of-Agents mem
set Epic AE
setup ]
]
;this is repeated several times with the model-id number and ask number
;increasing incrementally to accommodate multiple teams
]
reset-ticks
end
The setup procedure does not have a "clear-all" in it because "clear-all" wipes out all the information received from the Graphical User Interface parent model.
Below is the portion of the go procedure that runs the model and collects the input for the Teams "Value" variable from their respective child models:
to go
ask Teams
[
if model-id = 0 [ls:ask 0 [go] set Value lput [average-score] ls:of 0 Value
]
if model-id = 1 [ls:ask 1 [go] set Value lput [average-score] ls:of 1 Value
]
;this is repeated several times with the model-id number increasing
;incrementally to accommodate multiple teams
]
Value-of-Stories1
tick
end
So the "Value" variable of each team receives it's value from a separate Levelspace connected child models "average-score" global variable. I then sum the totals of each teams Value input every tick with the code below.
to Value-of-Stories1
ask turtle 0 [
if breed = Epics
[
let closteams Teams with [AsgnEpic = "Epic 1"]
foreach sort-on [who] closteams
[
the-closeteams -> ask the-closeteams [show reduce + [Value] of the-
closeteams]
]
]
]
end
Totaling the separate Teams "Value" totals into one number is the challenge.
There's more code then this but I believe this is the code relevant to what I'm trying to resolve.
Thanks
Rudy

How to fix "Nothing named ? has been defined" error in NetLogo 6.0.4

I downloaded the modified random clusters code for generating neutral landscape models using the Millington's version of the modified random clusters approach in the NetLogo modeling commons. When I click the "generate-landscape" button, the "fill-landscape" procedure in the code causes a "Nothing named ? has been defined" error.
When I created the attached interface image and attempted to run the adjoining code below. The problem seems to be related to the question mark in the "occurrences" report function. The reduce function is not working as intended. Is there a work around for this? See interface, then code below:
ifelse ( any? neighbours with [ cluster != nobody ] ) ;; check if there are any assigned patches in neighbourhood
[
let covers []
ask neighbours with [ cluster != nobody ]
[
set covers fput cover covers ;;ask neighbours to add their covers to the list
]
let unique-covers remove-duplicates covers ;;create a list of unique covers
let max-cover-count -1 ;the number of neighbours with the maximum cover
let max-cover -1 ;the maximum cover
ifelse(length unique-covers > 1)
[
;if there is more than one unique-cover
foreach unique-covers ;for each of the unique covers
[
let occ occurrences ? covers ;count how many neighbours had this cover
ifelse(occ > max-cover-count) ;if the count is greater than the current maximum count
[
set max-cover ? ;set this as the dominant cover
set max-cover-count occ ;update the current maximum count
;---------------
to-report occurrences [x the-list]
report reduce
[ifelse-value (?2 = x) [?1 + 1] [?1]] (fput 0 the-list)
end
;---------------
The code is suppose to generate a neutral landscape model using the modified random clusters approach developed by Saura and Martinez-Millan (2000). However, the error "Nothing named ? has been defined" error the code from running smoothly. Looking forward to thoughts ...
The old ? syntax from NetLogo 5.x was replaced with the new -> syntax in NetLogo 6. See https://ccl.northwestern.edu/netlogo/docs/programming.html#anonymous-procedures
So, for example, in NetLogo 5, you would write:
foreach [0 1 2 3] [
print ?
]
in NetLogo 6, you write:
foreach [0 1 2 3] [ x ->
print x
]
A combination of Bryan's answer (first procedure) and the NetLogo Dictionary (second procedure) gives you the following. The comments indicate the new bits. Not tested.
ifelse ( any? neighbours with [ cluster != nobody ] )
[ let covers []
ask neighbours with [ cluster != nobody ]
[ set covers fput cover covers
]
let unique-covers remove-duplicates covers
let max-cover-count - 1 ; added a space around subtraction
let max-cover - 1 ; more spacing
ifelse(length unique-covers > 1)
[ foreach unique-covers
[ this-cover -> ; here's the new bit, calling ? 'this-cover'
let occ occurrences this-cover covers ; passes to the occurrences procedure
ifelse(occ > max-cover-count)
[ set max-cover this-cover ; using the name this-cover again
set max-cover-count occ
And for occurrences, you can take the procedure directly from the NetLogo Dictionary reduce example
to-report occurrences [#x #the-list]
report reduce
[ [occurrence-count next-item] -> ifelse-value (next-item = #x)
[occurrence-count + 1] [occurrence-count] ] (fput 0 #the-list)
end

Netlogo error when using procedure from code example Many Regions Example procedure

I'm new in Netlogo . I'm trying to use the code included in the Many Regions Example of the library. But I get one error in this procedure
to keep-in-region [ which-region ]
if region != which-region [
let region-min-pxcor first item (which-region - 1) region-boundaries
let region-max-pxcor last item (which-region - 1) region-boundaries
let region-width (region-max-pxcor - region-min-pxcor) + 1
ifelse xcor < region-min-pxcor [
set xcor xcor + region-width ]
[if xcor > region-max-pxcor [
set xcor xcor - region-width
]
]
]
The error I get is .... any advices? Thanks in advance
-1 no esta mas que o iqual a cero.
error while a-seller 31 running ITEM
called by procedure KEEP-IN-REGION
called by procedure ADJUST
called by procedure GO
called by Botón 'go'
The error you are getting ("-1 isn't greater than or equal to zero.", once translated to English) is caused by passing -1 as an index for the item primitive.
There are two lines in the code that make use of item:
let region-min-pxcor first item (which-region - 1) region-boundaries
let region-max-pxcor last item (which-region - 1) region-boundaries
As you can see (which-region - 1) is the expression passed to item as index. If you are getting -1, it must be because which-region = 0.
In the "Many Regions Example" model, region 0 is reserved for the patches that are not part of any regions, namely, the patches that act as region boundaries. The regions themselves are numbered from 1.
Look in your code for the place where keep-in-region is called and make sure that you are using a region number that is between 1 and the number of regions you have (inclusively).
Disclaimer: I originally wrote the "Many Regions" code example. Maybe I could have used a value like nobody for the patches outside of any region and number the actual regions starting from 0 instead of one, but I don't remember if I actively decided against it or just didn't think of it.

How to get the id number of a new turtle just created?

I have the following code where I hatch a new agent,
to t-of-slowdown [ es-poi ]
if we-look > 0 [
set we-look (we-look - 1)
if (we-look <= 0) [
if es-poi and (not any? events-here) [
hatch-events 1 [
set color green
set size 5
set is-poi? true
set new-poi true
let m [[ end2 ] of cur-link] of myself
move-to m ]
set events-x ([who] of events-here)
show events-x
set we-poi-var va-geometric (1 / 1500) + we-ticks poi
set sera-poi false
]
set impregna true
set color red
set seguir true
set we-look random-normal 120 20 ;time to watch an event
]
]
end
which is run in a turtle context (walkers breed)
A walker is moving by a 'link' (another procedure which calls this one), and when a counter is <0,
this code generates a new event (events breed) and places it in the same place where the walker is (cur-link is the current walker link).
After that, the walker must get the id number of the new event
set events-x ([who] of events-here)
The problem here is that variable events-x get an empty list []. The next time the walker passes by the same event it does get the number-id of the event.
Something must be wrong but I can not guess what it is.
I would appreciate very much if someone could take a look and point me some help.
Regards
You could:
let child-who -1
hatch-events 1 [
...
set child-who who
...
]
set events-x child-who
Or:
hatch-events 1 [
...
let child-who who
ask myself [ set events-x my-who ]
...
]
Both of these are a bit clunky, sadly. The second one avoids needing to initialize child-who to a meaningless value, but it requires using myself, a primitive that is likely to mystify the reader.
You could avoid both problems with:
let parent self
hatch-events 1 [
...
let child-who who
ask parent [ set events-x child-who ]
...
]
(But note that using who numbers at all, for anything, is rarely the best and most idiomatic solution to any problem. It's almost always better to store a reference to the turtle itself.)

How to store a network context that you want to restore in netlogo?

I am trying to restore an initial(back to its first state) context of turtles and link after killing one turtle.
I have been trying the solution from http://ccl.northwestern.edu/netlogo/docs/nw.html, but somehow it doesn't work.
Below is my code
to cycle
if Measure = "Efficiency of network"
[ ;store node and link
nw:set-context turtles with [ shape = "circle" ] links with [ color = blue ]
show map sort nw:get-context
set old-turtles item 0 nw:get-context
show old-turtles
set old-links item 1 nw:get-context
show old-links
;start process
process-performance
]
end
to process-performance
if NumberOfNodes = 1
[file-open "1node.txt"
while [not file-at-end?]
[
;calculate initial performance value
set initial nw:mean-path-length
show initial
let nodeseq read-from-string (word "[" file-read-line "]")
show item 0 nodeseq
ask turtle (item 0 nodeseq) [ die ]
update-plots
;calculate new performance value
set final nw:mean-path-length
show final
set result (1 - (final / initial)) * 100
show result
nw:set-context old-turtles old-links
show map sort nw:get-context
]
file-close
]
end
I have been using "nw:set-context old-turtles old-links" in the documentation from netlogo but it seems that the original turtle and link context I store in "old-turtles old-links" will be purposefully altered no matter how I store them. I am thinking if [die] function alter the agent-set stored? The old-turtles and old-links are progressively smaller in size as I kill the node. I did not store the renewed context of the nw back to old-turtles and old-links.
Or does anyone have other ways in storing the old agent-set and link and restoring back to its original network structure?
Thanks for reading through.
Killing a turtle does indeed remove it from all agentsets, so restoring the context won't bring it back. You might try removing the turtle from the context rather than killing it. You could hide the turtle and its links to reproduce the visuals of killing it as well. This would be something like:
...
let target-turtle turtle (item 0 nodeseq)
ask target-turtle [
hide-turtle
ask my-links [ hide-link ]
]
nw:with-context (remove turtle (item 0 nodeseq) old-turtles) old-links [
update-plots
;calculate new performance value
set final nw:mean-path-length
show final
set result (1 - (final / initial)) * 100
show result
]
...
This way, the turtle is removed from the context for the purpose of your calculations, but not killed, so its structural information is remembered. nw:with-context handles storing and restoring the context for you, but this code works just as well without it (you just have to restore the context yourself).