when i applied an equation to all turtles (the nodes is taken from a database), the first value is calculated only and it is repeated over the rest?
There are different values for each node(a,b,c,d)
is there another way instead of ask turtles ?? i do not know what the wrong ?
ask turtles [ set total (a+b)*w +(d+c)* w1 / 4]
This is code execution
(turtle 14): 0.0018243243243243246
(turtle 21): 0.0018243243243243246
(turtle 35): 0.0018243243243243246
(turtle 19): 0.0018243243243243246
(turtle 24): 0.0018243243243243246
(turtle 39): 0.0018243243243243246
(turtle 15): 0.0018243243243243246
(turtle 54): 0.0018243243243243246
(turtle 40): 0.0018243243243243246
(turtle 47): 0.0018243243243243246
(turtle 36): 0.0018243243243243246
(turtle 60): 0.0018243243243243246
(turtle 41): 0.0018243243243243246
(turtle 20): 0.0018243243243243246
(turtle 31): 0.0018243243243243246
There is nowhere near enough information to answer your question since you haven't provided the values of any of the variables. As I suggested on your previous (now deleted) question, you need to check that the variables going into the equation are what you think they are. For example:
ask turtles
[ type "a is " print a
type "b is " print b
type "c is " print c
type "d is " print d
type "w is " print w
type "w1 is " print w1
set total (a+b)*w +(d+c)* w1 / 4
print total
]
As someone has already stated there doesn't seem to be enough information provided. From what I can see you may want to create a turtles own variable. This gives each turtle its own variable to hold. For example if you give turtles a health variable of 100 each turtle can individually change their health value independently. I'm not sure if this what you are looking for though
Related
When using NetLogo's R extension, how do I create or load up an R function for use by the agents later? The following doesn't work:
extensions [r]
to setup
r:eval "function line 1"
r:eval "function line 2"
r:eval "function line 3"
etc.
end
and my function seems too long to do it all on one line (I'm not sure that would work when it got sent into R anyway). Does anyone have any advice?
I'd recommend building your functions in an R file then sourcing that file within NetLogo. For example, I have a file called 'example_r.R' that just serves to store Foo:
Foo <- function(value) {
return(abs(value))
}
I can then source() the file with r:eval and all contained functions will be available:
extensions [r]
to setup
ca
ask n-of 5 patches [ sprout 1 ]
r:eval "source('C:/example_r.R')"
reset-ticks
end
to go
ask turtles [
rt random 60 - 30
fd 1
r:put "xcor" xcor
r:eval "out <- Foo(xcor)"
show ( word "My xcor is " round xcor " but my absolute xcor is: " round r:get "out" )
]
tick
end
After setup and go:
(turtle 4): "My xcor is 7 but my absolute xcor is: 7"
(turtle 3): "My xcor is 2 but my absolute xcor is: 2"
(turtle 1): "My xcor is -3 but my absolute xcor is: 3"
(turtle 2): "My xcor is -3 but my absolute xcor is: 3"
(turtle 0): "My xcor is -15 but my absolute xcor is: 15"
I have a lists of values and I want the agents in my model to sample from these lists without replacement. The n-of primitive allows me to sample at random but, this means the values get duplicated sometimes, something that I want to avoid.
For example, if agent-1 gets a 1 from list 1, agent-2 should not be able to get this as well.
Hope you can help.
turtles-own [list1Vals list2Vals]
to test
clear-all
crt 2
let list1 [1 2]
let list2 [3 4]
ask turtles [set xcor random-xcor
set ycor random-ycor
set color red
set list1Vals n-of 1 list1
set list2Vals n-of 1 list2
]
end
Probably easiest to randomly choose an index value rather than randomly choosing from the list, because you can then use item to choose the list value and remove-item to remove it from the original list. More details in comments:
turtles-own [list1Vals list2Vals]
to test
ca
let list1 [1 2 3 4 5]
crt 5 [
; Randomly choose an index based on the
; length of list1
let ind1 one-of range length list1
; Have the turtle choose from list1
; using that index
set list1Vals item ind1 list1
; Remove the indexed value from list1
set list1 remove-item ind1 list1
show ( word "I chose " list1Vals ". list1 is now: " list1 )
]
reset-ticks
end
test outputs something like:
(turtle 1): "I chose 5. list1 is now: [1 2 3 4]"
(turtle 4): "I chose 4. list1 is now: [1 2 3]"
(turtle 0): "I chose 3. list1 is now: [1 2]"
(turtle 2): "I chose 2. list1 is now: [1]"
(turtle 3): "I chose 1. list1 is now: []"
Or
(turtle 1): "I chose 1. list1 is now: [2 3 4 5]"
(turtle 0): "I chose 4. list1 is now: [2 3 5]"
(turtle 4): "I chose 2. list1 is now: [3 5]"
(turtle 3): "I chose 5. list1 is now: [3]"
(turtle 2): "I chose 3. list1 is now: []"
I am trying to port an older code to the latest (6.0.1), the following foreach loop works fine on Netlogo 4.1.3 but when copy and paste the code into version 6.0.1 the "item 0 ?" does not work. It says the "?" is undefined. That line of code is suppose to retrieve the item of the list inside the segment
to setup-row [row colour segments]
foreach segments
[
if pycor = row * row-patches-width and
(pxcor >= col-patches-width * (item 0 ?)) and (pxcor <= col-patches-
width * (item 1 ?))
[set pcolor colour
output-print item 0 ?]
]
end
The passed in "segments" variable contains the following lists:
setup-row 4 blue [[-8 -5] [-3 -1] [0 3] [5 9]]
If the code is working correctly, it should retrieve the -8 with (item 0 ?) and -5 with (item 1 ?) and so on. What I assumed in the old code is that the "?" are the first list retrieved from segments which is [-8 -5] and (item 0 ?) retrieved the -8 and (item 1 ?) retrieved the -5.
I have tried to read through the new user manual to find something that works similarly but to no avail or maybe I didn't look in the right place. Hope some of you can point me in the right direction.
Yep, that's been changed in Netlogo 6.0- see the transition guide page. For more detail on the new foreach syntax, see the dictionary entry. Basically, instead of using ? now, you explicitly define the variable names to use in the foreach procedure. Following your list example above:
to foreach-example
let ex [ [-8 -5] [-3 -1] [0 3] [5 9] ]
foreach ex [
[ xy_coords ] ->
ask patches with [ pxcor = (item 0 xy_coords) and pycor = ( item 1 xy_coords) ] [
set pcolor white
]
]
end
Here, I explicitly state that the list items will be called "xy_coords" throughout the procedure. It's a nice change that allows for more readable code since your variables can have more meaningful names.
I'm trying to make it so turtles that have "immune = 0" and "sick = 0" will have a chance of getting sick when colliding with a turtle with "sick = 1".
ask turtles with [immune = 0 and sick = 0]
[if any? turtles-on patch-ahead 1 with [sick = 1]
[if (random 100) < infection-rate
[set sick 1]
]
]
However, the error given is "WITH expected this input to be an agentset, but got a number instead". Is there any way to do this?
What you're doing is fine, NetLogo just got confused. This code passes the syntax checker - I added some brackets to the offending line.
globals [infection-rate]
turtles-own [immune sick]
to testme
ask turtles with [immune = 0 and sick = 0]
[if any? (turtles-on patch-ahead 1) with [sick = 1]
[if (random 100) < infection-rate
[set sick 1]
]
]
end
From BehaviorSpace, I would like to run a model 100 times by varying two variables as follows:
["x-area" 1 ] and ["y-area" [1 1 100]].
A model is built as follows:
create landscape
add-turtles
go-simulation
By using the two variables, I would like that my 100 models run as follows:
;;; Model 1 ;;
"x-area" = 1
"y-area" = 1
clear-all
create landscape
add-turtles
go-simulation
;;; Model 2 ;;
"x-area" = 1
"y-area" = 2
add-turtles
go-simulation
;;; Model 3 ;;
"x-area" = 1
"y-area" = 3
add-turtles
go-simulation
....
;;; Model 100 ;;
"x-area" = 1
"y-area" = 100
add-turtles
go-simulation
To do this, I built 100 experiments and this method worked. Is there a faster way to run automatically 100 models without doing 100 experiments ? I tried to build 1 experiment like this
But I have this error message:
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
org.nlogo.nvm.ArgumentTypeException: OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
at org.nlogo.prim._asm_proceduremovewithinpolygon_ifelse_86.perform(:4)
at org.nlogo.nvm.Context.runExclusive(Context.java:119)
at org.nlogo.nvm.ExclusiveJob.run(ExclusiveJob.java:57)
at org.nlogo.nvm.Context.runExclusiveJob(Context.java:162)
at org.nlogo.prim._asm_procedurestartsimulation_ask_69.perform(:1)
at org.nlogo.nvm.Context.stepConcurrent(Context.java:91)
at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82)
at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143)
at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at scala.util.control.Exception$Catch.apply(Exception.scala:88)
at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41)
at org.nlogo.job.JobThread.run(JobThread.scala:75)
The problem is that my models continue to run with this error. So it is difficult to see where is the problem. Given the following message:
"at org.nlogo.prim._asm_proceduremovewithinpolygon_ifelse_86.perform(:4)" in the error message,
maybe that the problem is in the procedure "move-within-polygon".
Here is my procedure "move-within-polygon" for a given color of polygons:
if [pcolor] of patch-here = green [
set list-angles-in-green item 0 table-angles
loop [
let angle-in-green one-of list-angles-in-green
ifelse [pxcor] of (patch-right-and-ahead angle-in-green 1) = max-pxcor or [pycor] of (patch-right-and-ahead angle-in-green 1) = max-pycor [
print "die"
die
stop ]
[ ifelse (patch-right-and-ahead angle-in-green 1) != nobody and [pcolor] of (patch-right-and-ahead angle-in-green 1) = green [
print "move"
move-to patch-right-and-ahead angle-in-green 1
[ if not any? neighbors with [pcolor = green] [
print "no neighbors"
move-to patch-here
stop ] ] ] ] ]
Thanks for your help.
If a turtle ends up on the edge of the world, then patch-right-and-ahead angle-in-green 1 might point outside the world (depending on what angle-in-green is), so [pxcor] of in [pxcor] of (patch-right-and-ahead angle-in-green 1) = max-pxcor would ask for the coordinate of nobody. The same thing could happen for pycor later in the same line.
Question: Can a turtle ever get to the edge of the world in your model? It looks to me like the code that you displayed could lead to that result.
If so, then one way to prevent the error would be to replace
[pxcor] of (patch-right-and-ahead angle-in-green 1) = max-pxcor
with
xcor = max-pxcor
That's true when a turtle reaches the last column of patches. If you also want turtles to die when they're close to the edge, you could use both of these expressions in the if-else:
(xcor = max-pxcor) or ([pxcor] of (patch-right-and-ahead angle-in-green 1) = max-pxcor)
(I added parentheses simply for clarity.)
However, I wonder whether this would serve the same purpose:
xcor = max-pxcor or xcor = max-pxcor - 1
If any of these methods are right for your application, then you can obviously do the same thing for the y coordinates.
Your experiment setup appears correct to me, except that you should remove the "Stop condition" of TRUE, because if the stop condition is always true, your runs will never run the go commands even once.
The error you're getting is coming from code that you haven't shown us, so I can't help you there. You'll need to show us the code in which the error is occurring.
Also, at the time the error occurs, what are the values of x-area and y-area? And does the same error occur if you set x-area and y-area to the same values outside BehaviorSpace? If so, then the error doesn't really have anything to do with BehaviorSpace.
Finally, a note on terminology: there is only one model here, not 100, and only one experiment here, not 100. You're attempting to run one experiment on your model, and that experiment consists of 100 model runs. Using the standard terminology will help you communicate your issue clearly.