NetLogo Turtle position - netlogo

I'm really new at programming in NetLogo and i need a little help. I have an assignment and i did most of it. The thing left to do is to make robot walk in labyrinth. Robot can walk only on a black patches (violet patches represent the obstacles).
So, the thing i need help with is to position robot in the center of the labyrinth - i must do it with "patch-here" (...i did it little bit differently in procedure "stvori-agenta") and mark that patch on which robot stands as black. So, afterwards i could write procedures for robots movements only on a black patches.
Here is the code:
breed [robots robot]
to crtaj-zidove
ask patches with
[
( pxcor = max-pxcor)
or (pxcor = min-pxcor)
or ( pycor = max-pycor)
or (pycor = min-pycor) ]
[ set pcolor violet]
end
to labirint
ask n-of 15 patches with [ pcolor != violet ] [
set pcolor violet]
end
to stvori-agenta
set-default-shape robots "robot"
ask patch 5 5 [ sprout-robots 1 ]
ask turtles [
set heading 0
set color grey
]
end
to setup
clear-all
crtaj-zidove
labirint
stvori-agenta
end

This will make the robot turn the patch it is standing on black:
ask robots [ set pcolor black ]
You say you must use patch-here. That isn't actually necessary, since turtles have direct access to the patches they are standing on. But you could also write it as:
ask robots [ ask patch-here [ set pcolor black ] ]
It does the same thing.

Related

Pedestrian environment: turtles do not recognise patch-type and surroundings

Recently I've started working on a pedestrian model simulation. I'm currently having a difficult time with controlling the movement patterns of my turtles. My code and blueprint.png is uploaded to Github.
So first, I upload a floor plan and tried to setup-variables and ask patches with pcolor = 0 to set as walls, pcolor = white to set as the ground, pcolor = red for doors, etc.
I'm able to create turtles, and let's say they start at the doors. I've tried to instruct them to avoid walls, yet the code breaks with runtime error: MOVE-TO expected input to be an agent but got NOBODY instead. Why can turtles start at patches with a colour but not a patch-type?
Even just the way the turtles are walking is unlike previous models I've tested in the model library. Any feedback would be welcome and appreciated. Thanks
Netlogo Code
extensions [ time ]
globals [
time-passed
walls
doors
exits
ground
art
corners-top-left
corners-top-right
corners-bottom-left
corners-bottom-right
]
patches-own [
patch-type
]
turtles-own
[
speed
wait-time
]
to setup
clear-all
import-dwg
setup-turtles
setup-variables
reset-ticks
end
to go
move
tick
update-time
end
to import-dwg
import-pcolors "blueprint.png"
end
to update-time
let minutes floor (ticks / 60)
let seconds ticks mod 60
if(minutes < 10)[set minutes (word "0" minutes)]
if(seconds < 10)[set seconds (word "0" seconds)]
set time-passed (word minutes ":" seconds)
end
to setup-turtles
create-turtles 2 [
move-to one-of patches with [ patch-type = "ground" ]
set heading towards one-of patches with [ pcolor = 65 ]
]
ask turtles [
set speed 1
set wait-time 0
set size 2
set color blue
pen-down
]
end
to move
ask turtles [
If any? Patches with [ pcolor = white ]
[set heading towards one-of patches with [ pcolor = white ]
fd 1]
]
tick
end
to setup-variables
ask patches with [ pcolor = 0 ] [
set patch-type "walls"
]
ask patches with [ pcolor = 15 ] [
set patch-type "doors"
]
ask patches with [ pcolor = white ] [
set patch-type "ground"
]
ask patches with [ pcolor = 65 ] [
set patch-type "art"
]
set time-passed "00:00"
end
Cross-posted to Reddit and found my answer (link):
You're going to hate this - in your setup function, setup-variables needs to be before setup-turtles, otherwise it doesn't know what "patch-type" is.
Edit: Also using "neighbors" for your move so they're looking at
adjacent patches, may help them not walk through walls and art.

set a demand and supply curve for the model tragedy of the commons in the case of an overfished pond

I am new at net logo and I want to write a model based on tragedy of the commons in the case of an overfished pond. The purpose is to find an equilibrium between fishers and fishes based on an economic model with demand and supply. If there are less fishers, more fishes will be in the pond, then after a certain time (ticks) the number of fishers increases and less fishes will be in the pond. Maybe set like a number of fishes per day that can be fished. Thus, the solution is to find a convenient number of fishers as the fishes can still reproduce. I want to have a box in the interface where I can type in a number and see what happens with the number of fishes.
I have no idea how to set this up. I hope to hear from you :)
I started with this code:
breed [fishers fisher]
breed [fishes fish]
to setup
clear-all
reset-ticks
ask patches [set pcolor blue ] ;; lake/pond in form of a rectangle in color
ask patches [ if pxcor > 8 [ set pcolor green ]]
ask patches [ if pycor > 8 [ set pcolor green ]]
ask patches [ if pycor < -8 [ set pcolor green ]]
ask patches [ if pxcor < -8 [ set pcolor green ]]
ask one-of patches with [ pcolor = blue ] [ sprout 20 [set shape "fish" set color pink set size 1.5 ]] ;; creates fishes
ask one-of patches with [ pcolor = green ] [ sprout 2 [set shape "person" set color black set size 3 ] ] ;; creates fishers
end
to go
tick
;;fishes
ask turtles with [ shape = "fish" and color = pink ]
[ right random 360 forward 1
if [pcolor] of patch-ahead 1 = green [ right 180 fd 1 ]]
;; fishers
ask turtles with [ shape = "person" and color = black]
[;right random 360 forward 1
if any? patches with [pcolor = blue]
[set heading towards one-of patches with [pcolor = blue] forward 1]
if [pcolor] of patch-ahead 1 = blue [ right 180 fd 2 ]]
ask turtles with [shape = "person" and color = black]
[if any? turtles with [shape = "fish" and color = pink] in-radius 2
[ask turtles with [shape = "fish" and color = pink] in-radius 2 [die]]]
end
Firstly, I suggest you look through existing models in the Netlogo library (Wolf-sheep-predation model may help). You roughly have the right idea in your current code, but you should look at other models to improve. You've already set your different breeds of turtles, but you should also set up their respective shapes under 'setup'. This would help you a great deal later - instead of calling for
ask turtles with [ shape = "fish"...]
you can simply
ask fishes [do sth...]
For that 'box at the interface', you can have a slider at the interface determining the number of fishers you want your run to start with. With another slider, you can set the fishing pressure in your simulated run (i.e. how many fish each fisher will catch) and I suppose you can also consider how this changes when population of fish decreases.
Finally, for a model like yours, you can observe the supply and demand trend by plotting the curves of no. of fishers over time and no. of fishes over time. Again, look at the wolf-sheep-predation model to have an idea of how to do this.
I can't give you more than this I'm afraid since I'm no pro myself but hope this helps a little. Hope someone else would be able to give you a clearer idea.

NetLogo.Trying to make turtle on a specific patch color die

So I've made the breeds player and zombie:
breed [zombies zombie]
breed [players player]
I would like to kill a zombie if it walks unto a black patch (pit).
to go
ask zombies
[
;set heading (heading + 45 - (random 90))
let closest-player min-one-of players[distance myself]
set heading towards closest-player
forward 1
;if xcor = pcolor black [Death] I have a lot to learn for netlogo syntax
;if ycor = pcolor black [Death] these lines are to give an Idea of what I'm trying to do.
]
end
to Death ;; turtle procedure
set shape "skull"
set color white
set heading 0
end
As described in the Variables section of the programming guide:
a turtle can read and set patch variables of the patch it is standing on.
In your case, this means that your zombies can directly check the pcolor of the patch that they're on:
if pcolor = black [ Death ]
This is the equivalent of the more verbose form using patch-here:
if [ pcolor ] of patch-here = black [ Death ]
You often don't need to use coordinates to identify a patch. NetLogo has plenty of reporters that can help you getting the patch you want. For example: patch-ahead, patch-at, patch-at-heading-and-distance, patch-here, patch-left-and-ahead and patch-right-and-ahead.
But in those cases where you do need to find a patch using coordinates, there is patch:
if [ pcolor ] of patch xcor ycor = black [ Death ]
But all of this is unnecessary in your case. Stick to the simple if pcolor = black [ Death ].

How to replace color with another color in a polygon?

I have patches as follows:
I would like to color white patches as in this figure:
Here my code to color white patches in blue:
ask patches with [pcolor = white] [
if any? neighbors with [pcolor = blue] [set pcolor blue] ]
But the problem is that I obtain this figure: .
Thanks in advance for your help.
Alan is right about the cause of your problem, but you don't need to create a next-pcolor patch variable. If you put both conditions inside the with block, NetLogo will first construct the agentset of patches, and then ask these patches to do stuff, thereby avoiding the timing problem that you had. Let's try it. And since you're obviously going to have to do it with both blue and cyan patches, let's build a more general version:
to color-white-patches-v1 [ c ]
ask patches with [ pcolor = white and any? neighbors with [ pcolor = c ]] [
set pcolor c
]
end
That you can then call with:
color-white-patches-v1 cyan
color-white-patches-v1 blue
Here is the result:
But it's not quite what you wanted. That's because neighbors gives you all 8 neighbors of a patch. Let's try with neighbors4 instead:
to color-white-patches-v2 [ c ]
ask patches with [ pcolor = white and any? neighbors4 with [ pcolor = c ]] [
set pcolor c
]
end
Not quite there yet. It think you are going to have to resort to something like patch-at. In this example, I look at only the patch above:
to color-white-patches-v3 [ c ]
ask patches with [ pcolor = white and [ pcolor ] of patch-at 0 1 = c ] [
set pcolor c
]
end
I'm not sure if that was exactly what you wanted and how well that applies to your general problem, but with some combination of patch-at, you should be able to get what you need.
The problem arises because patches are being changed sequentially, so some white patches find they have new blue neighbors by the time they respond to ask.
ask patches with [pcolor = white and any? neighbors with [pcolor = blue]] [set pcolor blue]
Note: this answer is edited in response to Nicholas's observation that with will create the entire agent set before ask is called. However I stuck with neighbors since that's how I read the question.

Netlogo Sprouting turtles spaced at less than one patch

I want to place turtles on each of the black patches(below Figure) such that there is no gap between turtles at all:
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
But I want to place turtles in between each of the two patches as well. So that I can cover the showing black part.
Note: Changing the shape of turtles is no use to my purpose though it would cover the black area. My aim to create a replusion force field from these agents and gaps in between are loop holes from where agents may escape.[Somewhat similar to agents bouncing back on a wall].
Here is a fun solution:
breed [ dead-turtles dead-turtle ]
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape dead-turtles "circle"
; sprout a first set of turtles:
ask patches with [ pcolor = black ] [
sprout-dead-turtles 1 [ set color red ]
]
; create temporary links between these, and use the
; links to place a new set of turtles in between:
ask dead-turtles [
create-links-with turtles-on neighbors4
]
ask links [
let target end2
ask end1 [
hatch 1 [
face target
fd distance target / 2
]
]
die ; remove the link
]
end
I'm not saying that it is the only possible solution, but it's simple enough, and it works. (World wrapping has to be turned off, though, which I assume is the case.)