Red Reactive : cannot get true value for face data - red

From Red-lang book example about reactive programming, I added test button, weirdly I can get true data value for slider but not for progress bar: for this latter it's always the initial value (20%). How to get the actual value for this latter ?
view [
origin 0x0 space 0x0
tab-panel 500x100 [
"Tab 1 " [
p: progress 100x20 data 20% react [face/data: s/data]
s: slider 100x20 data 20%
button "test" [
vs: s/data
vp: p/data
?? vs
?? vp
]
]
"Tab 2 " [
p: progress 100x20 20%
slider 100x20 data 20% [p/data: face/data]
]
]
]

You set p twice and the last one is valid.
Therefore your p points to your second progress bar in Tab 2 and vp is always 20% as long as you move only the slider in Tab 1. So either move the slider in Tab 2 before hitting the button or better name the progress bar in Tab 2 with a different word.
e.g
"Tab 2 " [
p2: progress 100x20 20%
slider 100x20 data 20% [p2/data: face/data]
]

Related

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

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

NetLogo chain of turtles breaks up when linking

I'm doing a model of some chains of cells (as part of a larger DNA string model). They wiggle around on screen, and when they wiggle so much, that they come close to a cell of the same type in the same string, they must create a link with that cell.
Code below.
It kinda works.... But after I introduced the linking behavior described above, the string breaks into pieces after a short while. I can't figure out why :-) Any ideas?
Thanks,
Palle
breed [cells cell]
cells-own [paired?]
globals [chains]
to setup
clear-all
set chains []
addchain
reset-ticks
end
to go
foreach chains [c -> movechain c]
ask cells [if any? other cells-here with [label = [label] of myself and paired? = false] [
let makker one-of other cells-here with [label = [label] of myself and paired? = false]
create-link-with makker [tie]
set color red
set paired? true
ask makker [set color red set paired? true]
print word label " was found!"
]]
tick
end
to addchain
set chains lput makechain chains
end
to movechain [mylist]
let antalfollowers length mylist - 1
let i antalfollowers
repeat antalfollowers [
ask item i mylist [
move-to (item (i - 1) mylist)
]
set i i - 1
]
ask first mylist [
right random-float wigglefactor
left random-float wigglefactor
fd 1
]
end
to-report makechain
let mylist []
let text "MGIVEQCCTSICSRYQ"
let startx random-xcor / -3
let starty random-ycor / -3
let i 0
repeat length text [
create-cells 1 [
set color green
set shape "circle"
set label item i text
set paired? false
set mylist lput self mylist
setxy startx + i * .75 starty + i * .75
]
set i i + 1
]
report mylist
end
OK - I finally figured out what's going on. This was hard to see. The tricks I used
in order to see it are listed at the bottom of this post.
In the case of a single strand of synthetic DNA floating around,
when two cells of the same type hit and stick, at that point there will
be a loop of all the cells that were between the moving cell and the
cell that got run into in the list of the sequence of cells.
What you need to do is to:
(a) take the cell that just got run into and link it and tie it to the moving
cell, making the moving cell the MASTER of the link [tie]. You do that
already.
(b) remove the cell you just made a SLAVE from the list of cells your code
has to move, because it will now be moved automatically when the master cell
moves.
AND ... importantly ...
(c) repeat steps (a) and (b) for EVERY cell on the list in positions between
the two cells that just collided and stuck.**
That will effectively freeze the configuration of the entire loop and make it
into a single large unit that will move as one unit when the master cell is
moved by your code.
The User Manual states in the subsection on TIE in the section on LINKS:
When the root turtle turns right or left, the leaf turtle rotates
around the root turtle the same amount as if a stiff were attaching
the turtles. When tie-mode is set to “fixed” the heading of the leaf
turtle changes by the same amount.
I think that you do want to set tie-mode to FIXED so the whole cluster will rotate when the master cell rotates.
I think, but have not looked at it, that this algorithm will work even if the linked cell is the very first or very last one on the chain. Better check it though. Maybe the cell that should be the MASTER is the one that got hit, not the one doing the moving.
In the case of two different strands of DNA intersecting,
basically forming an X, or more complicated cases ... I leave it up to you to figure out what you want the larger connected structure to do, and how hit should move. Do BOTH of the leading cells of each strand tug on it at once? Do you need to shut off one of them so there is only one cell moving the crowd? Will you need to change your code that moves the chain to insert one of the chains entirely into the middle of the other chain on the list that moves them in sequence? I don't know!
By the way, to figure this out, I had to:
slow down the motion and
set the color of the cells to [0 255 0 125] which is a half-transparent green
so I could see through them and see through them overlap, and
use the "watch" feature of the Inspector to follow one of the cells I knew
was going to get linked up, and
use the slider in the inspector to zoom up as far as it would go, and
put a 500 millisecond "wait" in side the innermost loop of the motion.
I'm working on your question and have made some progress -- at least I have a revision of your code with some troubleshooting additions to it, and a revision of the Interface. I can't figure out how to attach a model here, or an image of the view you will see when you run it, so I posted the full model and a PNG file in the netlogo-users Google Group.
The revisions do not fully answer your question about how to solve the breaking up of the chain, but they provide a reproducible version of the model that you can be sure will break at step 746 and that you can turn on dense printing and slow action and watch what is going on with attempted moves after that.
Maybe if we both look at that it will be obvious why this is breaking.
One thing I think may be happening is that, once two cells link and tie, your move algorithm no longer is the correct logic for how the knotted chain should move.
Here's the changes that I made:
I added a random-seed command in setup so that the model can be repeatedly run with exactly the same choices in random variables, so we can both look at the results.
I added a "verbose?" switch on the Interface to turn on or off detailed printing of what's going on.
I added a "clear-stop" button that will allow the "stop" commands I put in the model to stop the running using a stop-requested? flag, which the code sets and this button resets so that "go" and "one step" work again after a stop and the ticks counter keeps on incrementing.
I added a msec-per-tick slider to put a fixed number of milliseconds at the end of the go step so the model will run at a speed that is usable for debugging. I don't know how other people can do it -- when I try to use the built-in slider at the top of the interface to control speed, either it's way too slow, or it's way too fast, and I can't seem to be able to tweak it to just right. So I added this kludge.
I guessed what limits you had on your wigglefactor slider on your interface and made one that goes from 0 to 90 (degrees). It works ok and reproduces your bug.
I added some additional tests to prevent the two C-cells that are adjacent in your choice of cells from linking to each other when they discover, as they do sometimes, that they are on the same patch. With a random seed of 23456 your original code ties its own CC adjacent pair together in tick 2 for instance.
(
Here's the revised code, and comments as to how to use it are in the posted model (in netlogo-users) and below after the code as well.
breed [cells cell]
cells-own [paired?]
globals [
chains
stop-requested? ;;//////////////////////////////////////////////////
]
to setup
clear-all
;; random-seed 12345 ;; /////////////// breaks at either step 35 or 45 ////
;; random-seed 23456 ;; ////////////////// ties its own CC in tick 2 ///
random-seed 34567 ;; /////////////////// ties own cc in tick 84 , ties C at 746 and breaks in 6 steps
;;// wow -- it SNIPS TSIC out of the middle of the chain!! snips out CTIS, turtles 7 8 9 and 10
;; cell 6 C is directly followed by cell 11 -- also C
set chains []
addchain
set stop-requested? FALSE ;; ////////////////////////////////////////
reset-ticks
end
to go
if stop-requested? [ stop ] ;;////////////////////////////////////////////
foreach chains [c -> movechain c]
ask cells [if any? other cells-here with [label = [label] of myself and paired? = false
and who != [who] of myself + 1
and who != [who] of myself - 1
] [
let makker one-of other cells-here with [label = [label] of myself and paired? = false]
create-link-with makker [tie]
set color red
set paired? true
ask makker [set color red set paired? true]
print ( word label " was found at tick " ticks)
set stop-requested? TRUE ;;/////////////////////////////////////////////
]]
wait ( msec-per-tick / 1000 ) ;;////////////////////////////////////////
tick
end
to addchain
set chains lput makechain chains
end
to movechain [mylist]
if verbose? [print ( word "\n\nin code at tick " ticks " starting movechain") ];;//////////////
;;if verbose? [ show mylist ]
let antalfollowers length mylist - 1
let i antalfollowers
repeat antalfollowers [
ask item i mylist [
if verbose? [
;;print (word " asking item " i " on mylist to move to item " (i - 1) )
let mover [label] of item i mylist
let moveto [label] of item (i - 1) mylist
let mover-color [color] of item i mylist
let moveto-color [color] of item (i - 1) mylist
;; BLUE one moves TO the YELLOW ONE at the end of this second
print (word " move " mover " to " moveto )
ask item i mylist [set color blue set shape "square" set size 2]
ask item (i - 1) mylist [set color yellow set shape "square" set size 2]
display
wait 4
ask item i mylist [set color mover-color set shape "circle" set size 1]
ask item (i - 1) mylist [set color moveto-color set shape "circle" set size 1]
display
]
move-to (item (i - 1) mylist)
;;print word "just moved follower " i ;;//////////////////////////
;; wait 0.1 ;;////////////////////////////////////////////////////
]
set i i - 1
]
ask first mylist [
right random-float wigglefactor
left random-float wigglefactor
fd 1
]
end
to-report makechain
let mylist []
let text "MGIVEQCCTSICSRYQ" ;; NOTE - this has a repeat of CC in it which SOMETIMES gets tied (23456 tick 2)
let startx random-xcor / -3
let starty random-ycor / -3
let i 0
repeat length text [
create-cells 1 [
set color green
set shape "circle"
set label item i text
set paired? false
set mylist lput self mylist
setxy startx + i * .75 starty + i * .75
]
set i i + 1
]
report mylist
end
to clear-stop ;;////////////////////////////////
set stop-requested? FALSE ;;////////////////////////////////
end ;;////////////////////////////////
Anyway, here's how to run it to reproduce the problem and examine it
in fine detail very closely.
WHAT IS IT?
model of DNA in motion modified for troubleshooting. When a "C" cell
touches another "C" cell in step 746, and sets a link between the two
C cells and ties them to move as one, the algorithm for motion
breaks somehow. This may help figure out why,
HOW IT WORKS
This has added to it a msec-per-tick slider, default 50, to set the
display speed about right if you leave the usual speed slider at the
very top in the middle (normal).
I also added a button ("clear stop") and a switch ( verbose?)
normally off. With verbose? ON, the model runs very very slowly and
color codes which cell is about to move to which cell ( blue is moving
to yellow ) and they both temporarily change to squares.
HOW TO USE IT
1) set msecs-per-tick to 50 2) shut off verbose?, click SETUP, click
GO. The model will run 746 ticks. ( thats with the random-seed set to
a value of 34567 in globals. )
3) After it stops at tick 746. click clear-stop.
NOTICE the CTI group at the upper right. This is logically BETWEEN the
two C-cells which have linked up. This group will be snipped-out of
the moving DNA It has who numbers 7, 8, 9 and 10 as you can see by
inspecting it.
4) click one-step 6 times to advance to where you can see more clearly
that the two groups have become separate. Then turn on verbose? and
click one-step once to watch the gory details with a 4 second pause
between cell-moves and the running commentary in the Command Center
telling which cells are about to move.

Netlogo different global values from turtles

I don't understand why something appened in my netlogo code, but finally I found that a global variables change values from turtles to turtles, is this possible?
I noticed this because I have 2 print value in my code like this:
breed [players player]
breed [balls ball]
globals [
TURNS;
TEAM-OWNER
]
to setup
clear-all
reset-ticks
set TURNS 0
setup-players
setup-ball
end
to setup-ball
create-balls 1 [
setxy 0 0
set color orange
set shape "bug"
set size 2.5
]
end
to setup-players
create-players 5 [
setxy 0 0
set color white
set shape "bug"
set size 2.5
]
end
to go
set TURNS TURNS + 1
print word "this is the turns " TURNS
print ( word " values " TEAM-OWNER)
if ticks >= 1000 [ stop ]
ask players [
print ( word " values " TEAM-OWNER)
fd 2
set TEAM-OWNER "red"
]
ask balls [
fd 2
set TEAM-OWNER "black"
]
end
Is this possible? Global values doesn't change it values in all turtles?
This is the print that I got:
this is the turns 1
values 0
values 0
values red
values red
values red
values red
this is the turns 2
values black
values black
values red
values red
values red
values red
It has not be like black 1 times ? and red the other times? And Why I get 2 blacks?
A global variable is global - there is only ONE copy of it and all turtles retrieve and modify the same copy.
Look at this piece of your code:
ask players [
print ( word " values " TEAM-OWNER)
fd 2
set TEAM-OWNER "red"
]
ask balls [
fd 2
set TEAM-OWNER "black"
]
First, every one of your players moves forward two distance units, then sets the value of TEAM-OWNER to "red". If you have 10 players, then the global variable TEAM-OWNER is being set to red 10 times. Then every one of your balls moves forward two distance units and sets the variable TEAM-OWNER to "black". As long as you have one (or more) balls, then the global variable will be overwritten.
A global variable is used for something that is the same for every turtle in your model.

Red updating text in VID using reactive method

Red [needs: 'view]
num: ["1^/"]
k: num/1
view [
size 600x600
txt: text 30x50 k
ar: area 300x400 "" focus on-change[
txt/size: ar/size
len: length? split face/text newline
either (len - face/data) > 0 [
append num append form (len + 1) newline
face/data: len
][
remove back tail num
face/data: face/data - 1
]
txt/text: form num
]
do [ar/data: 0]
]
This Red program contains a "text face" and an "area face". The text face contains a vertical list of serial numbers. When a newline is added in the area face, the serial number will increase as per number of lines. And when a line is removed in the area face, the serial number will decrease as well.
This is using a non-reactive method. Is there a reactive approach to do it?
I believe that you we're looking for the react function. The reactive framework was introduced in this blog post and there is a very similar example of converting an example using on-change to its reactive version.
Anyhow, I was reading a lot lately about Red, and I was looking for an first exercise; My to-list implementation could be improved probably, but the view declaration is more compact now:
Red [needs: 'view]
to-list: function [text][
; converts text area string to list of numbers separated by newlines
txt: copy text
append txt "dummy" ; handle empty lines
len: length? split txt newline
x: copy ""
repeat i len [ append x mold i append x newline]
]
view [
size 600x600
text 30x600 react [
face/text: to-list text-area/text
]
text-area: area 300x400 ""
]

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