Netlogo: Moving turtles and calculate distance - netlogo

I'm currently do my project about path planning.
So the step of my plans are like this:
Click "setup" to arrange all the coordinate
Click "start" to create the moving turtles
Click "go" to make the moving turtle going to nearest coordinate (label with "x")
Which mean, at step 4, the moving turtle already calculate it's distance with every coordinate.
Here I attach the interface and the coding
to setup
clear-all
set-default-shape turtles "x"
create-turtles 9
ask turtles[set color red]
ask turtle 0[setxy 0 15]
ask turtle 1[setxy 4 15]
ask turtle 2[setxy -4 15]
ask turtle 3[setxy 0 12]
ask turtle 4[setxy 4 12]
ask turtle 5[setxy -4 12]
ask turtle 6[setxy 0 9]
ask turtle 7[setxy 4 9]
ask turtle 8[setxy -4 9]
end
to start
set-default-shape turtles "airplane"
create-turtles 1
ask turtle 9[setxy 0 -15]
end

Don't forget that distance depends on your topology.
to setup
clear-all
let targets [
[0 15] [4 15] [-4 15]
[0 12] [4 12] [-4 12]
[0 9] [4 9] [-4 9]
]
foreach targets [xy -> ask patch item 0 xy item 1 xy [
sprout 1 [set shape "x" set color red]
]
]
create-turtles 1 [ set shape "airplane" setxy 0 -15]
end
EDIT: If you treat all other turtles as eligible targets, then once you move turtle 9 to a target it will stay there. If you do not want that, you say that a turtle in the same location is not eligible:
to move
ask turtle 9 [move-to min-one-of eligibles [distance myself]]
end
to-report eligibles
report turtles with [0 < distance myself]
end

Related

How to extend neighbor function to 24 using code from Moore and Von Nuemman example

I am learning how to use net logo and one of the things I am trying to do is to create a larger neighborhood then the built in 8 that comes with the agent set "neighbor".
I want to use this extended neighborhood to run Conway's Game of Life with more neighbors.
I have used the built in function from the Game of Life available in the netlogo's model library.
to go
let neighbors24 [list pxcor pycor] of patches with [abs pxcor <= 2 and abs pycor <= 2]
ask patches
[ set live-neighbors count neighbors24 with [living?] ]
;; Starting a new "ask patches" here ensures that all the patches
;; finish executing the first ask before any of them start executing
;; the second ask. This keeps all the patches in synch with each other,
;; so the births and deaths at each generation all happen in lockstep.
ask patches
[ ifelse live-neighbors = 3
[ cell-birth ]
[ if live-neighbors != 2
[ cell-death ] ] ]
tick
end
I expect neighbors24 to increase the number of neighboring cells from 8 to 24, instead I am met with the following error.
"WITH expected input to be an agentset but got the list [[-2 -1] [0 0] [2 2] [-2 2] [-1 1] [2 -2] [0 2] [-1 -1] [-2 1] [-1 -2] [2 1] [1 0] [-1 0] [-1 2] [1 -1] [0 -1] [-2 0] [0 -2] [1 2] [-2 -2] [1 -2] [0 1] [2 0] [2 -1] [1 1]] instead."
NetLogo should tell you which line is giving you the error. Please include that in your future questions.
In this case, the error is (presumably) the line set live-neighbors count neighbors24 with [living?]. Your problem is that with selects those agents in the specified agentset that meet a condition. So patches with [pcolor = yellow] would get the yellow patches. However, neighbors24 is not an agentset, it's a list of patch coordinates.
It is a common NetLogo novice mistake to create lists, particularly if you have experience with other programming languages. If you are creating lists of agent identifiers (eg coordinates for patches, or who numbers for turtles) you almost certainly want an agentset instead.
The modified line let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2] will create neighbors24 as an agentset.

agents sharing with each other their lists

I am making a NetLogo model. Each agent has a list of 5 integers (agent-list). On each tick, turtles create link with one other turtle, and share with each other their list.
turtles-own [ agent-list ]
.
.
.
ask turtles [
create-link-with one-of other turtles
set agent-list lput agent-list of link-neighbors agent-list
]
I know the code above doesn't work, how should I fix it?
The simplest way to combine the lists as you've described would probably be sentence:
turtles-own [ agent-list ]
to setup
ca
crt 3 [
set agent-list map [random 10] range 5
]
reset-ticks
end
to link-and-share
ask turtles [
let target one-of other turtles
create-link-with target
set agent-list sentence agent-list [agent-list] of target
show agent-list
]
end
However, you'll have do do some tweaking depending on what you're actually looking to do since that means that turtles linking later in the procedure are likely to pull the agent-list of turtles that have already modified their own agent-list. So, if turtle 0 grabs the agent-list of turtle 1, then later turtle 4 grabs the agent-list of turtle 0, turtle 4 would have an agent-list of 15 integers, not 10, similar to the output below:
(turtle 1): [6 1 5 4 7 3 9 8 1 1]
(turtle 0): [9 0 3 3 5 3 9 8 1 1]
(turtle 2): [3 9 8 1 1 9 0 3 3 5 3 9 8 1 1]

Netlogo ticks as a counter for a race

I'm writing code to make a race between 5 turtles. I have to show who wins the race through the use of ticks. I think I can use the ticks to count how much time it takes each turtle to move then compare them. However, I can't figure out where to put the "tick" in my code. Here's my code:
to finish
ask patches
;sets finish line pattern
[ifelse (pxcor + pycor) mod 2 = 0
;if true do this
[set pcolor pink]
;if false do this
[set pcolor yellow]
]
ask patches
;sets background black other than the finish line
[if pxcor < 12 [set pcolor black]
]
end
to lanes
ask patches
;sets the lanes
[ if pycor = 3 and pxcor < 12 [set pcolor white]
if pycor = 9 and pxcor < 12 [set pcolor white]
if pycor = -3 and pxcor < 12 [set pcolor white]
if pycor = -9 and pxcor < 12 [set pcolor white] ]
;setup for the turtle positions
cro 5
ask turtle 0 [setxy -15 0]
ask turtle 1 [setxy -15 6]
ask turtle 2 [setxy -15 12]
ask turtle 3 [setxy -15 -6]
ask turtle 4 [setxy -15 -12]
ask turtles [set heading 90] ;set heading 90 means moving the head of the turtle right 90 degrees
reset-ticks
end
to setup
finish
lanes
end
to movecars
every .1
[fd random 10 / 10]
end
to endrace
movecars
if xcor >= 12 [die]
end
to go
endrace
end
Almost always, tick goes as the last command in the go procedure. Certainly that's what you should be doing while you are new to NetLogo. Having said that, it won't make your code work.
Think of ticks as a time step counter. Each loop through the go procedure should do all the actions that need one time step to do and also advances the time step counter. So you don't need the every command, you just have the movecars procedure called by the go procedure and have ask turtles [forward random 10 / 10] in the movecars procedure.
This is a fairly fundamental conceptual gap and I suggest you look through some of the models in the NetLogo models library, focusing on the link between the go procedure and the moving procedure and the passage of time. Or perhaps do the tutorials again. Also, start your model more simply. Just create one car and get it to move, then worry about multiple cars, colours and seeing who wins. Add a little piece of your model and make it work before adding the next piece.
Try this:
to go
movecars
endrace
tick
end
to movecars
ask turtles [ fd random 10 / 10]
end
to endrace
ask turtles [ if xcor >= 12 [die] ]
end

Is there a way to set patch color for multiple patches with a single line of code in NetLogo?

Right now i am using
ask patch 1 1 [set pcolor grey]
ask patch 1 -1 [set pcolor grey]
ask patch -1 1 [set pcolor grey]
ask patch -1 -1 [set pcolor grey]
Is there a way to set patch color for multiple patches with a single line of code in NetLogo?
Simplest way:
ask (patch-set patch 1 1 patch 1 -1 patch -1 1 patch -1 -1) [ set pcolor grey ]
(The following has been updated in response to Seth's comment:)
If this is being run from the observer (that is, it's not being run by any agent) you can use at-points like so:
ask patches at-points [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
However, if this code is run by a turtle or a patch, the list of coordinates will be treated as relative to that agent. So patches at-points [[1 0] [0 1]] will give the patch to the right and the patch above the current agent. You can easily make a version that uses absolute coordinates:
to-report patches-at-coords [ coordinates ]
report (patch-set map [patch first ? last ?] coordinates)
end
and then use it like so:
ask patches-at-coords [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
Regardless, unless you're doing this a lot, I'd go with the first method.

How to move turtles between neighbors on a hex grid?

I want to ask how to move turtles to their neighbors. ( the houses are hex cells)
I tried to make it but give me an error.
;;;;;;;;;;;;;to make houses or the hex cells
to setup-cells
set-default-shape cells "hex"
ask patches
[ sprout-cells 1
[ set color grey
set size 1.2
;; shift even columns down
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ] ] ]
ask cells
[ ifelse pxcor mod 2 = 0
[ create-links-with cells-on patches at-points [[0 1] [1 0] [1 -1]] ]
[ create-links-with cells-on patches at-points [[0 1] [1 1] [1 0]] ] ]
ask links [ hide-link ]
end
;;;;; I add turtles to their houses
to setup-population
ask patches [
sprout 2
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ]]
end
my question is how to move this turtle to the link neighbors
I tried to do this
to move
ask turtles[
if ( random-float 1) < 0.03 [
move-to one-of link-neighbors
fd 1 ]]
end
but this always give me an error
move-to expected input to be agent but got nobody instead
Great question. It's too bad this isn't covered in Hex Cells Example (the code example you seem to be working from). Or maybe we should have a separate Hex Cells With Turtles Example.
First, you should make a separate breed for the turtles that live in the houses, so you can refer to them separately from the cell turtles. Suppose you call them people:
breed [people person]
then you should change sprout 2 to sprout-people 2.
As for implementing movement, the code you tried doesn't work because people don't have link-neighbors. It's the cells that have link-neighbors.
So instead of:
move-to one-of link-neighbors
you must write:
move-to [one-of link-neighbors] of one-of cells-here
If you want the people to face in the direction of movement, then it's:
let target [one-of link-neighbors] of one-of cells-here
face target
move-to target