Imported Patch colors not working correctly in NetLogo - 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

Related

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.

Netlogo turtle skips specific patch

I am trying to instruct turtles to visit all green patches.
There is a behavior which I can't get my head around: patch 0,0 is always avoided if the location of origin = center (see screenshot with 0,0 colored blue), and the lower left corner if location of origin = corner.
Why is this? What error am I making here?
;;==========================================================
globals [
memory
target
]
patches-own [visit-counter]
;;==========================================================
to setup
ca
resize-world -6 6 -6 6
set-patch-size 40
create-turtles 1 [
set memory (list patch-here)
setxy random-pxcor random-pycor
set size 1
set color blue
]
ask patches [if random 100 < 40 [set pcolor green]]
ask patch 0 0 [set pcolor green]
ask patches [set visit-counter 0]
reset-ticks
end
;;==============================================
to go
ask turtles [choose-target]
tick
if ticks > 500 [stop]
end
;;==============================================
to choose-target
pd
;; set of unvisited patches
let unvisited patches with [not member? self [memory] of myself]
;; set of green patches that are not visited
let targets patches with [(member? self unvisited) and (pcolor = green)]
;; select target and move there
set target one-of targets with-min [distance myself]
ifelse target != nobody [
face target
fd 1
set visit-counter (visit-counter + 1)
set memory lput patch-here memory
]
[die
]
end
When you initialise the variable 'memory', you have set memory (list patch-here). At that point, the turtle is sitting on patch 0 0, so the origin patch is in the memory. Your code for finding targets excludes those already in the memory let unvisited patches with [not member? self [memory] of myself]. It is therefore ineligible to be chosen as a target.

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 ask turtles to avoid a patch with specific color at patch-ahead 1 but turtles move forward by bigger number (not fd 1) in one step in NetLogo

I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.
to setup
clear-all
ask patches [set pcolor green ]
ask patches with [pycor = 3] [set pcolor red]
create-turtles 40
[
set color blue
set xcor random-pxcor
set ycor random-pycor
set heading random 360
set size 1
set speed 1 + random-float 2.0
]
end
to go
ask turtles [
fd speed
avoid-walls
]
end
to avoid-walls
if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end
Try using in-cone instead of patch-ahead
to avoid-walls
let front-patches patches in-cone 2 75
if pcolor of one-of front-patches = red [set heading heading - 180]
end