Netlogo ticks as a counter for a race - netlogo

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

Related

How can I properly implement a function at the end of a counter in Netlogo?

I am writing an assembly line model, and I would like to implement a counter to hold a turtle at a specific patch (in this case, patch 3 0) for 10 ticks. Once 10 ticks have passed, I would like the turtle to keep on moving at the rate of one patch per tick and for the next turtle in line to begin its own 10 tick timer once it arrives at the specified patch.
So far, I can stop the turtles at the specified patch and run a 10 tick counter; however, I cannot seem to get the turtles to keep moving continuously after the timer is completed.
Here are the relevant parts of my code so far.
to go
move-tubs
move-drums
machine-marriage
move-machines
stay
keep-going
tick
end
to move-machines
ask wmachines [
if not any? turtles-on patch-ahead 1 and xcor < 3
[ forward 1]
]
end
to stay
ask wmachines-on patch 3 0[
ifelse counter = 0 [
set counter 10
]
[set counter counter - 1
set label counter
if counter = 0
[forward 1]
]
]
end
to keep-going
ask wmachines-on patch 4 0[
if not any? turtles-on patch-ahead 1 and xcor < 12
[ forward 1]
]
end
If your problem is that turtles leave patch 3 0 but then they do not move forward continuously beyond patch 4 0, it is simply because your keep-going procedure is only addressing turtles that are exactly on patch 4 0 (and for this reason the xcor < 12 part is completely unused).
In general, it looks very complicated and unnecessary that you are using three different procedures (i.e. one before patch 3 0, one for patch 3 0, and one for patch 4 0 but which should really be beyond patch 3 0) each of which is hard-coding some location in your model.
The whole point of having a counter is that you can generalise a waiting condition across the whole model, so your go procedure can be simplified a lot by simply asking agents that have concluded their countdown to do one thing, and those who have not concluded their countdown to do another thing.
Look at this minimal reproducible example where I have an unpredictable arrangement of stopping-patches but implement the waiting condition in a very general and simple way:
turtles-own [
counter
]
to setup
clear-all
reset-ticks
resize-world 0 30 0 4
set-patch-size 25
ask patches with [pxcor = min-pxcor] [
sprout 1 [
set heading 90
set color lime
]
]
ask n-of 15 patches with [pxcor > min-pxcor] [
set pcolor brown
]
end
to go
ask turtles [
ifelse (counter = 0)
;; If the counter equals 0:
[forward 1
if (pcolor != black) [
set counter 10
]
]
;; If the counter does not equal 0:
[set counter counter - 1]
ifelse (pcolor = black)
;; If the turtle is on a black patch:
[set label ""]
;; If the turtle is not on a black patch:
[set label counter]
]
tick
end

Color a radius of patches Netlogo

im trying to show a cultivation process. At setup im creating the farmers and giving them a random farm size (with the splotch) then, at go, im telling them that if they have enought money all patches near them that are the splotch turn into green, representing cultivation. But its just changing one pixel and not all round it. It most be someting small but i cant see it. Thanks in advance for the help
.
breed [cercas cerca]
breed [medios medio]
breed [lejos lejo]
patches-own[calidad
cercanialago
cultivado
]
turtles-own [ingresos
gastos]
create-cercas 10 + random 10
[ set size 1 ;; easier to see
set color 135
setxy random xcor random ycor
move-to one-of patches with [not any? other turtles in-radius 3 and pcolor = 57]
set heading random 45 + 45
set ingresos 1000000 + random 6000000
]
ask turtles
[ ask patches in-radius (1 + random 3)
[ set pcolor 35 ] ]
to go
ask cercas [
ifelse ingresos > 2000000 [if any? patches in-radius 4 with [pcolor = 35] [if ticks mod 3 = 0 [set pcolor 62] ]]
[]
] ```
Your cercas are a breed of turtles.
A useful feature of NetLogo is that any turtle can directly read and modify the variables of the patch it is on (i.e. without the need to invoke such patch).
So when you ask a turtle to set pcolor 62, it will automatically refer to pcolor of the patch it is on.
If we eliminate all of the conditions from your last block of commands, we have:
ask cercas [set pcolor 62]. This is what you are asking cercas to do: simply changing the pcolor of the patch they are on.
The fact that you use patches in-radius 4 in the condition for the first if statement does not influence the ask cercas [set pcolor 62] part. The condition is one thing, the command to be executed if the condition holds true is a separate thing.
Therefore you should make cercas ask patches in-radius 4 to change their pcolors.

How to force turtles to move only in a half of the world?

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

You can't use Migrate in a turtle context, because migrate is observer-only

I managed to build my small model, but i do get an error, but don't know why?
Ps: i'm a bloody beginner
The Error code is:
You can't use migrate in a turtle context, because migarte ia observer-only.
So what can I do?
Thank you for your answers.
breed [fish a-fish]
breed [boats boat]
boats-own [profit]
to setup
clear-all
ask patches [set pcolor blue]
set-default-shape fish "fish"
create-fish initial-number-fish
[
set color grey
set size 1.0
setxy random-xcor random-ycor
]
set-default-shape boats "boat"
create-boats initial-number-boats
[
set color black
set size 1.5
set profit random (1 * profit-per-fish)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask fish
[
move
fish-reproduce
]
ask boats
[
move-boats
catch-fish
death
reproduce-boats
migrate
]
tick
end
to move
rt random 50
lt random 50
fd 1
end
to fish-reproduce
if random-float 100 < fish-growth
[hatch 1 [rt random-float 360 fd 1]]
end
to move-boats
rt random 50
lt random 50
fd 1
set profit profit - 0.1
end
to catch-fish
let prey one-of fish-here
if prey != nobody
[ask prey [die]
set profit profit + profit-per-fish]
end
to death
if profit < 0 [die]
end
to reproduce-boats
if profit > 1
[
set profit (profit / 2)
hatch 1 [rt random-float 360 fd 1]]
end
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
There are 3 different primitives for creating new turtles in NetLogo:
create (observer), hatch (turtle) and sprout (patch).
Each one works only in a specific context.
This means you have to be aware of the context in which you are calling your procedure.
In your example, you are calling the create-turtles primitive inside of an ask turtles (specifically ask boats) context. Which means you are in a turtle context and not in an observer context and therefore create-turtles is not allowed to use.
To solve your problem, you just have to replace create-turtles by hatch (or hatch-<breed>, like hatch-fish if you want to spawn new agents in a specific breed).

What would i need for this code to make turtles spawn slower, but not have to hold down the mouse to kill a turtle

Im playing whack a mole, and here is my code thus far
globals [
score
three-sec-spawn
]
turtles-own [
ttl]
extensions [bitmap]
To setup
ca
reset-ticks
import-drawing "whackamole.jpg"
import-pcolors-rgb "whackamole.jpg"
set-default-shape turtles "turtle"
ask turtles [
set size 7
set color red
set ttl 100]
reset-ticks
ask patch 18 2 [set plabel
"DIRECTIONS : To kill a bug, click on it. If you misclick, the bug stays there."]
ask patch 18 -1 [set plabel
"If there are 5 bugs on the screen, or if you dont kill it fast enough, you lose"]
set score 0
end
To play
if ticks > 65000 - (difficulty * 10000) [user-message "GAMEOVER" stop]
if (count turtles with [color = red]) = 10 [user-message "GAMEOVER" stop]
ask turtles [
set size 7
set color red]
ask patch 18 2 [set plabel ""]
ask patch 18 -1 [set plabel ""]
ask one-of patches with [pcolor = [42 13 9]] [sprout 1]
ask turtles [
set size 7
set color red
set ttl ttl - .5]
wait .5
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 1.5]
[set score score + 1
die]
reset-ticks
]
tick
end
because of the wait .5, turtles spawn too fast. But if i make the wait time any longer, id have to hold down the mouse button in order to "whack" the turtle
In your case I would not use the "wait." primitive at all, because it creates a gap of time where no actions (for example mouse clicks) can be tracked.
Instead I would use a time counter to delay the spawning and to create turtles on every x time units. Generally I would use the tick counter itself. For example, if you would like to spawn 1 turtle on every 100th tick:
if (ticks mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
In your example, the tick counter gets reset on mouse clicks. In that case you should define an additional time-counter for the spawning which does not get reset at anytime and gets increased by 1 on every timestep (analougous to the ticks but without resetting it back to 0 on mouse clicks):
if (your-counter mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
I hope this helps..?!?