I'm trying to setup turtles in a Netlogo model where the turtles are arranged randomly along the x-axis (from -16 to 16 on the interface) and along the y-axis at -16. Essentially across the bottom of the interface.
I've tried setxy xcor -16 16 ycor -16 but get "expected command" when checking the code.
Any thoughts would be appreciated.
If you look at the NetLogo Dictionary for setxy, one of the examples is:
setxy random-xcor random-ycor
That places the turtle in a random location. You want the ycor to be -16, so this would work:
setxy random-xcor -16.5
Even better, if you want to be able to adjust your world size and always be along the bottom, then:
setxy random-xcor min-pycor - 0.5
You may want to adjust the 0.5 a little to get them slightly off the bottom.
Related
I am a high school student and I have to make a model about plants and plagues. I want to setup turtles with a preset distance from each other, but I can't figure out how to do that without creating an infinite loop. This is what my code looks like. Now it creates 20 turtles with random distance between them, but I want them to have a preset distance and as much turtles there can be on the screen with that distance between them.
to setup
clear-all
ask patches [ set pcolor 33 ]
make
repeat 20 [maken]
reset-ticks
end
to make
create-aardappelplanten 1[
setxy random-xcor random-ycor
set color green
set size 1.5
set shape "plant"
set age 1
]
end
Thank you so much!!
in a NetLogo model, I have some "plant" turtles in a space. The number of which is 5, and will always be 5.
set-default-shape plants "plant"
create-plants 10
[
set color green
set size 2
setxy random-xcor random-ycor
setxy random-xcor random-ycor
]
At the moment each of these plants randomly spawns in the world. I want to be able to set the point at which each of these plants is placed.
Something akin to:
setxy plant-1 25 25
setxy plant-2 25 25
Is there any way to achieve this?
Currently, the line setxy random-xcor random-ycor is setting the x-coordinate and y-coordinate of the plants to random values. Note that you seem to have that line in your code twice. The first instance of it gets overwritten by the second. Anyway, you can also use setxy to move plants to particularly coordinates, e.g. setxy 25 25. However, replacing setxy random-xcor random-ycor with setxy 25 25 would put all the plants at that location. I assume you want multiple plants in multiple specific locations. To do that, you can just ask the individual plants to move:
ask plant 0 [ setxy 25 25 ]
ask plant 1 [ setxy 10 40 ]
and so forth.
i'm new using NetLogo and I'm trying to learn it using the 'Hello World' model found on the net. I'm running NetLogo 5.2 on Mac OS X (Yosemite). When I try to set the turtles randomly, in this way
setxy random screen-size-x random screen-size-y
I get this error: Nothing named SCREEN-SIZE-X has been defined
screen-size-x appears in capitalization and so i get an error on this built-in function. Can anyone help me? Thank you
This is the NetLogo code I'm using:
globals [buttons] ; Global variables
to setup ; Initializes model for new run.
set-default-shape turtles "circle" ; Turtles are circles
clear-all ; Reset turtles and patches
set buttons 500 ; Set number of buttons to 500
create-turtles (buttons) ; Create "buttons" number of turtles
ask turtles [setup-turtles] ; Initialize each turtle
end
to setup-turtles ; Called for each turtle during setup
setxy random screen-size-x random screen-size-y ; Set our x,y randomly
end
I think screen-size-x and screen-size-y are NetLogo history. You can use max-pxcor, max-pycor and min-pxcor, min-pycor to get the world borders or world-width and world-height to get just the size.
To get a random position there are random-xcor and random-ycor.
to setup-turtles
setxy random-xcor random-ycor
end
I am trying to move turtle around patch 0 0 starting from random position in world. But circle keeps on growing. What am I doing wrong here?.
Code:
to setup
clear-all
create-turtles 5
ask turtles [
setxy random-xcor random-ycor
]
ask patch 0 0 [ set pcolor green ]
reset-ticks
end
to go
move-turtles
tick
end
to move-turtles
ask turtles
[
face patch 0 0
right 90
fd 0.01
set pen-size 3
pen-down
]
end
Secondly I want a turtle to move around any patch I define when it reaches with in a certain range
Your approach is to take a small step along a tangent to the circle you want, but this takes you a little bit outside the circle. You do this repeatedly, so it accumulates over time.
For a better way, see the Turtles Circling example in the NetLogo Models Library.
I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
Cheers
Paul
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
in-cone is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
This code may look puzzling at first, but what it does it actually quite simple:
It uses n-values to build a list of incrementing values: n-values (dist / step) [ step + ? * step ]. For example, if dist was 1 and step was 0.2, you'd get [0.2 0.4 0.6 0.8 1]. These values represent the distances at which we are going to be looking for a patch.
It uses map to call patch-ahead for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step is small, since patch-ahead 0.1 and patch-ahead 0.2, for example, may very well be the same patch.
It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(You could achieve the same thing with a while loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
To use it, just replace instances of patches in-cone 10 1 in your code by something like patches-ahead 10 0.1.
You will notice that there is a trade-off between precision and speed: the smaller step is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.