How to replace color with another color in a polygon? - netlogo

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.

Related

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.

Why do i get this error every time i try to run the code within my U.I.?

I have the code:
if pcolor = blue
[ask turtles-here [set heading towards one-of neighbors4 with [pcolor] = grey ] fd speed ].
This has the aim of making a turtle that is spawned on a blue patch set its heading towards a neighbour with pcolor = grey and then follow that path. However when I run the code I get the error :
WITH expected a true/false value from (patch -12 14), but got 55 instead.
I don't know what caused the error and what I should do to correct it. Any help would be greatly appreciated.
I think it come from a mise understanding of with condition ...
something like
ask patches [
if pcolor = blue [
ask turtles-here [
set heading towards one-of patches with [pcolor = grey]
]
]
]
work for me !

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.)

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.

NetLogo Turtle position

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.