Setting an exact percentage of patches red and green color - netlogo

In setup, I am trying to set an exact percentage of the overall patches red pcolor or green pcolor.
I am using the standard patch environment, with 1089 total patches.
I have two sliders:
slider1 (for setting up to 250 green turtles)
slider 2 (for setting up to 250 red turtles)
Is there a way to set an exact percentage of the 1089 total patches red and the remaining percentage green?
Adapting code from a helpful Q&A previously posted on this site, I have this draft coding:
to color-patches
let total slider1 + slider2
let p-red slider1 / total
let p-green slider2 / total
ask patches [
let x random-float 1.0
if x <= p-red [set pcolor red]
if x <= p-green [set pcolor green]
]
end
Any helpful advice would be much appreciated.

You want n-of!
ask n-of (p-red * count patches) patches [ set pcolor red ]
ask patches with [ pcolor != red ] [ set pcolor green ]

Related

Netlogo adding patches by grid percentage

I have added 4 different user input sliders into net-logo. Yellow, red and green should represent percentages of the total patches grid, whilst the blue one is on a set amount of 0-10 (non-percentage). The % sliders vary in value for both green red and yellow. When I'm setting up the grid with the following code, only the blue patches are being inserted in the grid. I'm also looking to continuously have the same % of each colored patch throughout the simulation. Any help is appreciated as I've been trying this one for a good couple hours without any luck googling.
let total 961
let p-yellow yellow-patches-amount-% * 100 / total
let p-green (green-patches-amount-% * 100) / total
let p-red (red-patches-amount-% * 100) / total
let p-blue blue-patches-amount
let patch-yellow p-yellow
ask patches with [pcolor = black] [
if count patches with [pcolor = blue] < p-blue [
ask n-of p-blue patches [set pcolor blue]
]
]
ask patches with [pcolor = black] [
if count patches with [pcolor = yellow] < p-yellow [
ask n-of p-yellow patches [set pcolor yellow]
]
]
end
Your problem is how you define p-yellow, and how you use your percentage.
Let's say yellow-patches-amount-% is 50. Multiply that by 100 and you get 5000. divide that by 941 and you get 5.3 yellow patches. That number is then rounded down by n-of.
The correct way of using percentages here is:
let p-yellow yellow-patches-amount-% / 100 * total
Then there are a few problems with how you recolor your patches. I will address them one by one.
In your code, you have a ask patches [ ask patches [...]] construction. This is almost always a horrible idea, since it means that every patch can be asked to do something patch number of times, instead of just once. Letting one agent ask all agents to do something can be okay. Letting all agents ask one agent to do something can be okay. But letting all agents ask all agents to do something will almost always lead to code not doing what you want. I simplified the code you were using to illustrate this.
to setup
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
ask patches with [pcolor = black] [
if count patches with [pcolor = yellow] < p-yellow [
ask n-of p-yellow patches [set pcolor yellow]
show count patches with [pcolor = yellow] ;first 54, next 108 or a bit lower
]
]
end
With this setup, you would want 54 patches to become yellow. The first black patch that you call upon will now count all yellow patches, which is 0. Because of that, it now asks p-yellow, rounded down, random patches to turn yellow. Now the second black patch comes up and counts all yellow patches. It comes up with 54, which is lower than 54.45, so again it asks 54 random patches to turn yellow. By now you have more than 54.45 yellow patches so all subsequent patches count them but no longer ask any patches to change color. But that still means you turned a patch yellow 108 times instead of 54 times.
Even though you turned a patch yellow 108 times, you didn't necessarily turn 108 different patches yellow. If you ask n-of p-yellow patches [set pcolor yellow] once, then each patch can only be turned yellow once. This breaks down if you do it multiple times, since now ask n-of p-yellow patches [set pcolor yellow] can also ask a patch that you previously turned yellow to turn yellow again. This can be avoided by using ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow]. It is especially important since you are working with multiple colors.
Finally, the if count patches with [pcolor = yellow] < p-yellow construction makes sense if you are turning patches yellow one by one until you have >= p-yellow patches. The moment you turn more than 1 patch yellow at the same time, this sort of construction can easily let you overshoot your target.
With that in mind, I have two different suggestions for you
to setup-1
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow] ;54 in this case
end
to setup-2
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
while [count patches with [pcolor = yellow] < p-yellow] [
ask one-of patches with [pcolor = black] [set pcolor yellow]
] ;55 in this case
end
setup-1 simply turns the required number of patches yellow all at once and is done with it.
setup-2 turns black patches yellow one by one, until the count of yellow patches exceeds p-yellow. Note that this will get you 55 yellow patches instead of 54, since p-yellow > 54. If you would want it to be rounded down instead, you could use floor, which rounds a value down to the nearest integer:
[count patches with [pcolor = yellow] < floor p-yellow]

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.

Imported Patch colors not working correctly in NetLogo

I've been trying to get my turtles to 'bounce' off a wall using Netlogo.
I've imported a png file that has the colors of the different surfaces (walls, interface, liquid, heater eventually there will be a few more). I used MSPaint and the colormap from netlogo color chart to create my shapes (a purple square, with brown on the side borders, red border on the bottom and blue border on the top)
What I'm trying to do is have the turtles start on the liquid patches and move in straight lines until they bump into a surface (different colored wall). If they bump into a wall, they should 'bounce' off in a random direction, if they hit the heater, their temperature should go increase and they should also bounce off of the heater surface. If they bump into the interface and their temperature is above 100, they should move through the interface and then move around above the interface (basically teleport up a few pixels).
The issue I'm having is that the turtles move around and they appear to be bouncing off the walls just fine, but they seem to be oblivious to the colors of the heater and the interface. I'm sure I'm missing something basic or obvious, but I've been struggling for three days on this same quirk. Any help would be greatly appreciated.
Here is the code I have right now:
(in this code I have it set to just 'teleport' through the interface regardless of the temperature of the water)
globals[ liquid-color heater-color wall-color reflection-color air-color interface-color
liquid heater wall reflection air interface]
breed [h2o water]
to setup
clear-turtles
reset-ticks
clear-all-plots
import-pcolors "boilermap.png"
set liquid-color 115
set heater-color 19
set wall-color 35
set interface-color 105
setup-patches
create-molecules
end
to setup-patches
set heater patches with [pcolor = heater-color]
set heater-color 19
ask heater [set pcolor 19]
set interface patches with [pcolor = interface-color]
set interface-color 105
ask interface [set pcolor 105]
set liquid patches with [pcolor = liquid-color]
set liquid-color 115
ask liquid [set pcolor 115]
set wall patches with [pcolor = wall-color]
set wall-color 35
ask wall [set pcolor 35]
end
to create-molecules
create-h2o (totalmoles * h20number / 100)[
set shape "circle"
set color black
set size 2
set temperature 20
setxy random xcor random ycor
move-to one-of patches with [pcolor = liquid-color]
]
to go
ask h2o [
(ifelse
pcolor = liquid-color[fd 1 ];ifblock
pcolor = heater-color [set temperature temperature + 5]
pcolor = interface-color [set ycor ycor + 100] ;just trying to get them to jump here, regardless of their temperature
;elseblock
[ bk 1
rt random 180]
)]
end
Diesel, you have a great start on this! I put some code below that answers your questions.
By the way, the title on your question doesn't really match what your actual problem was: imported patch colors not working correctly
I suspect the reason your code ignores the heater and interface is that they imported as pcolors that don't match the ones you expected. I added a verify-colors command that checks to see if the imported colors are correct. I also added an option to generate a new boiler image you can work with in case importing still causes more problems.
Also, I notice you are adding 100 to ycor when your molecule hits the interface. It's possible this moves the molecule entirely up out of view, which could be confusing, as the default view is only 32 units high. I changed the 100 to 5 to keep the molecule in view.
I cleaned up a few more things and confirmed the model ran successfully. The handling of correct bouncing off walls and off the heater was not addressed -- that's your job!
Here's the things I tweaked in your code:
;;
;; added h2o-own section so molecules each have a temperature
;; added a user-choice of whether to import the boilermap.png or create a new one here
;; if importing, verify the colors exactly match the valid colors
;; added "make-drawing" and "verify-colors" sections at the end
;; moved reset-ticks to the end of setup, its usual location
;; added ticks to the end of the go section !!
;; added set-aircolor grey ( it wasn't defined )
;; cleaned out unneeded commands from the setup-patches section. You already have the
pcolors imported, so you don't need to set them again.
;; added "set label temperature" to the create-molecules section as well as to the place
;; where temperature is increased at the heater, so each molecule shows its current temperature
I tested it with interface variables h2o-number =100 and total-moles either 1 or 5 and speed set to very slow. It works!
globals[ liquid-color heater-color wall-color reflection-color air-color interface-color
liquid heater wall reflection air interface]
breed [h2o water]
h2o-own [
temperature
]
to setup
clear-turtles
clear-all-plots
set liquid-color 115
set heater-color 19
set wall-color 35
set interface-color 105
set air-color grey
if-else user-yes-or-no? "import boilermap.png?"
[
import-pcolors "boilermap.png"
verify-colors ;; check the command section for counts of pcolors
]
[
make-drawing ;; just make a new image of a boiler
]
setup-patches
create-molecules
reset-ticks ;; usually, do this as the last step in setup
end
to setup-patches
set heater patches with [pcolor = heater-color]
set interface patches with [pcolor = interface-color]
set liquid patches with [pcolor = liquid-color]
set wall patches with [pcolor = wall-color]
end
to create-molecules
no-display
create-h2o (totalmoles * h20number / 100)[
set shape "circle"
set color black
set size 2
set temperature 20
setxy random xcor random ycor
move-to one-of patches with [pcolor = liquid-color]
set label temperature
]
display
end
to go
ask h2o [
(ifelse
pcolor = liquid-color[fd 1 ]
pcolor = heater-color [ set temperature temperature + 5 set label temperature set heading 0 forward 1]
pcolor = interface-color [set ycor ycor + 5 forward 1 ]
pcolor = wall-color and pxcor < 0 [set heading 135 forward 1]
pcolor = wall-color and pxcor > 0 [set heading 220 forward 1]
[ bk 1
rt random 180]
)]
tick ; DONT FORGET THIS !!
end
;; ========================== new utility functions ===
to make-drawing
no-display
ask patches with [ pycor > 1] [ set pcolor air-color]
ask patches with [ pycor < 1] [set pcolor liquid-color]
ask patches with [ abs pycor <= 1] [ set pcolor interface-color]
ask patches with [ pycor < (min-pycor + 4)] [ set pcolor heater-color]
ask patches with [ pxcor > (max-pxcor - 2)] [ set pcolor wall-color]
ask patches with [ pxcor < (min-pxcor + 2)] [ set pcolor wall-color]
ask patches with [ pycor > (max-pycor - 2)] [ set pcolor wall-color]
ask patches with [ pycor < (min-pycor + 2)] [ set pcolor wall-color]
display
end
to verify-colors ;; see what we imported
let good-colors (list
liquid-color
heater-color
wall-color
interface-color
air-color )
let good-count count patches with [member? pcolor good-colors]
let bad-count count patches - good-count
type "good patch count " type good-count type ", bad patch count: " print bad-count
if bad-count > 0 [ if "no" = user-yes-or-no? " bad patch colors, should I continue?" [stop] ]
end

Netlogo - Ordered Movement

I have the following error in Netlogo and I'm unsure why. I am trying to make the turtles move around in a ordered fashion but I keep getting an error when I change the d angle: "FACE expected input to be an agent but got NOBODY instead".
Any help would be appreciated.
globals [Angles]
to setup
clear-all
create-turtles amount [ setxy random-xcor random-ycor ]
ask turtles [
set Angles (random 360)
]
reset-ticks
end
to monitor
show count patches
show ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
face min-one-of patches with [ pcolor = black ] [ distance myself ]
;; This line of code tells the turtle to head towards the nearest patch containing the colour of black.
set Angle d Angle * 1 - Angle
rightt Angle
forwardd 1
ifelse show-travel-line? [pen-down][pen-up]
set color red
if pcolor = black [
set pcolor yellow
]
]
tick
end
You can unveil the problem but running this test:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
You will see that the heading is NaN because you gave it a turn of -Infinity. Now if you move the turtle, the xcor and ycor will become Nan.
To avoid this problem, you need to limit the values taken by angle. For example,
globals [turn angle]
to setup
clear-all
set turn random-float 1
create-turtles 10 [
setxy random-xcor random-ycor
set color red
pen-down
]
reset-ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
part1
part2
if pcolor = black [
set pcolor yellow
]
]
tick
end
to part1
let _patches (patches with [ pcolor = black ])
face min-one-of _patches [ distance myself ]
end
to part2
set turn (4 * turn * (1 - turn))
set angle turn * 360
rt angle
fd 1
end

How to stop turtles when all patches have been colored

When the turtles have covered the world in patches, I would like the turtles to stop on the last one so that I can record the amount of ticks it took.
Here is my code so far:
globals [marked-patches angle nextangle]
to setup ca ask patches [ set pcolor black ] crt turtle_amount
[set color red
set size 1
setxy (random 20) (random 20)] reset-ticks
end
to go ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [set pcolor yellow] ]
tick end
In go, specifically in the turtle command, you can add:
to go
ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [
set pcolor yellow
if count patches with [pcolor = black] = 0 [
stop
]
]
]
tick
end