Hello i was trying to do some turtles bounce in my code, but apparently my procedure is not working:
to bounce
if [pcolor] of patch-at dx 400 = white [
set heading (- heading)
]
if [pcolor] of patch-at 400 dy = white [
set heading (180 - heading)
]
if abs pxcor = max-pxcor
[ set heading (- heading) ]
if abs pycor = (pycor = 25)
[ set heading (180 - heading) ]
end
The problem is that when the turtles move to the max ycor the turtles just cross the other side and appear in the bottom of the world, so i was thinking that the problem may be that i have a wrong configuration of the settings of the world but i donĀ“t know how to fix it. I forgot to mention that i created a horizontal line with the patches so the turtles bounce with it. The turtles can bounce with the white horizontal column, the problem is just above the window world.
The code is OK, just replace (pycor = 25) with max-pycor:
if [pcolor] of patch-at dx 400 = white [
set heading (- heading)
]
if [pcolor] of patch-at 400 dy = white [
set heading (180 - heading)
]
if abs pxcor = max-pxcor
[ set heading (- heading) ]
; if abs pycor = (pycor = 25)
if abs pycor = max-pycor
[ set heading (180 - heading) ]
Related
Good afternoon everyone:
Currently I am working in a program in Netlogo and I want to divide the world in upper-quadrant and lower-quadrant and ask turtles to move to the upper-quadrant. I have figured out how to divide the world in four quadrant from a previous question answered here, but I don't know how to divide it in two.
Thank you very much for your help
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor > 0]
[
set pcolor red
set quadrant 1
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor > 0]
[
set pcolor blue
set quadrant 2
]
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor < 0]
[
set pcolor green
set quadrant 3
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor < 0]
[
set pcolor yellow
set quadrant 4
]
Given that you are interested in a lower and an upper quadrant, you only need to look at y coordinates. The specific condition depends on where your world's origin (i.e. coordinates [0;0]) is.
If your world's origin is in the default position, which is the center, then do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > 0)
[set quadrant 1]
[set quadrant 2]
]
end
If your world's origin is in a corner (e.g. I assume bottom left corner in this case), just do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > max-pycor / 2)
[set quadrant 1]
[set quadrant 2]
]
end
If you don't know in advance where your world's origin will be, or if your world's origin is in a less common place than the two examples above, you can take a more generalised approach that fits any situation:
patches-own [
quadrant
]
to setup
clear-all
let y-extent (max-pycor - min-pycor + 1)
ask patches [
ifelse (pycor > y-extent / 2)
[set quadrant 1]
[set quadrant 2]
]
end
I want the turtles to back again to their areas if batches which I had defined before. The problem is I got an error
you can not use my_ area in a patch context, it is a turtle-only
the button is
to back_from_event
ask turtles with [ area = 1 ] [ move-to one-of patches with [ (not any? other turtles-here) and ( area = my_area ) ] ]
end
the patches defined as below and the turtles were in areas (2,3,4,5) and moved to area 1 and I need them to back again to there areas:
to define_areas
ask patches with [ (pxcor > -3) and (pxcor < 3) and (pycor > -3) and (pycor < 3) ] [ set pcolor white set area 1 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 2 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 3 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 4 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 5 ]
end
Okay, this is the same problem as your previous question. Please try to understand the answer rather than simply copy the code. Otherwise, you will keep asking the same question.
You have 'area' as a patch variable and 'my_area' as a turtle variable.
What you need to realise is that turtle to patch is unique because a turtle can only be in one place at a time. Therefore, a turtle can access the variables owned by the patch that it is sitting on without having to specify the patch. So code like this is okay:
ask turtles with [area = 1] [ ]
This is because it is equivalent to:
ask turtles with [ [area] of patch-here = 1] [ ]
However, a patch cannot access a variable owned by a turtle because there may be multiple turtles on the same patch. For example, if you asked a patch to set its pcolor to color and you had red turtle and a blue turtle on the patch, how would it know which color to choose?
Your error says "you can not use my_area in a patch context, it is a turtle-only". That is telling you that you tried to have a patch use the my_area variable but that variable is owned by turtles. So you didn't tell it which turtle to get my_area from.
This is what you have:
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = my_area)]
]
end
I assume you want the area of the patch to be the same as the my_area of the turtle doing the asking. That is what myself is for.
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = [my_area] of myself)]
]
end
I am working with the daisyworld model and am planning to add a predator to the model which will eat the daisies (this is for a class project). I have successfully added the predators (brown rabbits eat the white daisies, grey rabbits eat the black daisies). But in order for them to eat the daisies, their coordinates have to line up perfectly causing very little "eating" to occur. Is there a way for me to generate rabbits with random xy cor but be generated in the exact center of the pixel?
globals [
max-age ;; maximum age that all daisies live to
global-temperature ;; the average temperature of the patches in the world
num-blacks ;; the number of black daisies
num-whites ;; the number of white daisies
scenario-phase ;; interval counter used to keep track of what portion of scenario is currently occurring
]
breed [daisies daisy]
breed [brown-rabbits brown-rabbit]
breed [grey-rabbits grey-rabbit]
patches-own [temperature] ;; local temperature at this location
daisies-own [
age ;; age of the daisy
albedo ;; fraction (0-1) of energy absorbed as heat from sunlight
daisy-black ;; boolean if daisy is black
]
brown-rabbits-own
[energy]
grey-rabbits-own
[energy]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Setup Procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set-default-shape daisies "flower"
ask patches [ set pcolor gray ]
set max-age 25
set global-temperature 0
if (scenario = "ramp-up-ramp-down" ) [ set solar-luminosity 0.8 ]
if (scenario = "low solar luminosity" ) [ set solar-luminosity 0.6 ]
if (scenario = "our solar luminosity" ) [ set solar-luminosity 1.0 ]
if (scenario = "high solar luminosity") [ set solar-luminosity 1.4 ]
seed-blacks-randomly
seed-whites-randomly
ask daisies [set age random max-age]
ask patches [calc-temperature]
set global-temperature (mean [temperature] of patches)
update-display
;reset-ticks
create-grey-rabbits grey-rabbit-number
[
setxy random-xcor random-ycor
set shape "rabbit"
set size 1.5
set color grey
set energy random (2 * grey-rabbit-gain-from-food)
]
create-brown-rabbits brown-rabbit-number
[
setxy random-xcor random-ycor
set shape "rabbit"
set size 1.5
set color brown
set energy random (2 * brown-rabbit-gain-from-food)
]
reset-ticks
end
to seed-blacks-randomly
ask n-of round ((start-%-blacks * count patches) / 100) patches with [not any? daisies-here]
[ sprout-daisies 1 [set-as-black] ]
ask daisies [set daisy-black true]
end
to seed-whites-randomly
ask n-of floor ((start-%-whites * count patches) / 100) patches with [not any? daisies-here]
[ sprout-daisies 1 [set-as-white] ]
ask daisies [set daisy-black false]
end
;to daisy-black
; ifelse color = 0
; [daisy-black true]
; [daisy-black false]
;end
;ask daisies if color = 0 [set daisy-black true]
;ask daisies if color = 9.9 [set daisy-black false]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Runtime Procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
ask grey-rabbits [
move
set energy energy - 1
eat-black-daisies
death
reproduce-grey-rabbits
]
ask brown-rabbits [
move
set energy energy - 1
eat-white-daisies
death
reproduce-brown-rabbits
]
ask patches [calc-temperature]
diffuse temperature .5
ask daisies [check-survivability]
set global-temperature (mean [temperature] of patches)
update-display
tick
if scenario = "ramp-up-ramp-down" [
if ticks > 200 and ticks <= 400 [
set solar-luminosity precision (solar-luminosity + 0.005) 4
]
if ticks > 600 and ticks <= 850 [
set solar-luminosity precision (solar-luminosity - 0.0025) 4
]
]
if scenario = "low solar luminosity" [ set solar-luminosity 0.6 ]
if scenario = "our solar luminosity" [ set solar-luminosity 1.0 ]
if scenario = "high solar luminosity" [ set solar-luminosity 1.4 ]
end
to move
rt random 20
lt random 20
fd 1
end
to eat-black-daisies ;grey rabbits eat black daisies
ask grey-rabbits
[if any? daisies-here with [daisy-black = true]
[let prey one-of daisies-here; with [daisy-black = true]
if prey != nobody
[ask prey [die]
set energy energy + grey-rabbit-gain-from-food]
]
]
end
to eat-white-daisies ;brown rabbits eat white daisies
; ask brown-rabbits
; ;[if any? daisies-here with [daisy-black = false]
; patch-here
; [let prey one-of daisies-here with [daisy-black = false]
; if prey != nobody
; [ask prey [die]
; set energy energy + brown-rabbit-gain-from-food]
; ]
; ;]
end
to reproduce-grey-rabbits
if random-float 100 < grey-rabbits-reproduce [
set energy (energy / 2)
hatch 1 [rt random-float 360 fd 1]
]
end
to reproduce-brown-rabbits
if random-float 100 < brown-rabbits-reproduce [
set energy (energy / 2)
hatch 1 [rt random-float 360 fd 1]
]
end
to death
if energy < 0 [die]
end
to set-as-black ;; turtle procedure
set color black
set albedo albedo-of-blacks
set age 0
set size 0.6
end
to set-as-white ;; turtle procedure
set color white
set albedo albedo-of-whites
set age 0
set size 0.6
end
to check-survivability ;; turtle procedure
let seed-threshold 0
let not-empty-spaces nobody
let seeding-place nobody
set age (age + 1)
ifelse age < max-age
[
set seed-threshold ((0.1457 * temperature) - (0.0032 * (temperature ^ 2)) - 0.6443)
;; This equation may look complex, but it is just a parabola.
;; This parabola has a peak value of 1 -- the maximum growth factor possible at an optimum
;; temperature of 22.5 degrees C
;; -- and drops to zero at local temperatures of 5 degrees C and 40 degrees C. [the x-intercepts]
;; Thus, growth of new daisies can only occur within this temperature range,
;; with decreasing probability of growth new daisies closer to the x-intercepts of the parabolas
;; remember, however, that this probability calculation is based on the local temperature.
if (random-float 1.0 < seed-threshold) [
set seeding-place one-of neighbors with [not any? daisies-here]
if (seeding-place != nobody)
[
if (color = white)
[
ask seeding-place [sprout-daisies 1 [set-as-white] ]
]
if (color = black)
[
ask seeding-place [sprout-daisies 1 [set-as-black] ]
]
]
]
]
[die]
end
to calc-temperature ;; patch procedure
let absorbed-luminosity 0
let local-heating 0
ifelse not any? daisies-here
[ ;; the percentage of absorbed energy is calculated (1 - albedo-of-surface) and then multiplied by the solar-luminosity
;; to give a scaled absorbed-luminosity.
set absorbed-luminosity ((1 - albedo-of-surface) * solar-luminosity)
]
[
;; the percentage of absorbed energy is calculated (1 - albedo) and then multiplied by the solar-luminosity
;; to give a scaled absorbed-luminosity.
ask one-of daisies-here
[set absorbed-luminosity ((1 - albedo) * solar-luminosity)]
]
;; local-heating is calculated as logarithmic function of solar-luminosity
;; where a absorbed-luminosity of 1 yields a local-heating of 80 degrees C
;; and an absorbed-luminosity of .5 yields a local-heating of approximately 30 C
;; and a absorbed-luminosity of 0.01 yields a local-heating of approximately -273 C
ifelse absorbed-luminosity > 0
[set local-heating 72 * ln absorbed-luminosity + 80]
[set local-heating 80]
set temperature ((temperature + local-heating) / 2)
;; set the temperature at this patch to be the average of the current temperature and the local-heating effect
end
to paint-daisies ;; daisy painting procedure which uses the mouse location draw daisies when the mouse button is down
if mouse-down?
[
ask patch mouse-xcor mouse-ycor [
ifelse not any? daisies-here
[
if paint-daisies-as = "add black"
[sprout-daisies 1 [set-as-black]]
if paint-daisies-as = "add white"
[sprout-daisies 1 [set-as-white]]
]
[
if paint-daisies-as = "remove"
[ask daisies-here [die]]
]
display ;; update view
]
]
end
to update-display
ifelse (show-temp-map? = true)
[ ask patches [set pcolor scale-color red temperature -50 110] ] ;; scale color of patches to the local temperature
[ ask patches [set pcolor grey] ]
ifelse (show-daisies? = true)
[ ask daisies [set hidden? false] ]
[ ask daisies [set hidden? true] ]
end
; Copyright 2006 Uri Wilensky.
; See Info tab for full copyright and license.
Instead of checking whether 2 turtles are exactly on the same spot, I would use patch-here to see if they are on the same patch.
https://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-here
I'm creating a board game simulation similar to monopoly, where pieces are moving around the board. My turtle acts as the user's game piece and moves around the border of the world. The border is set up in alternating colors. I have a button that generates the number of steps the turtle moves. Those steps are stored as dice-num, a global variable. However, when the turtle lands on the patches before the patch (16 -16) (the bottom right-hand corner of the board) and the number of steps it needs to move exceeds 1, I can't turn the turtle's heading halfway to move up the board. As a result it only moves back to the beginning of the board.
I've tried to treat each case separately:
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
;dice-num refers to number of steps the turtle moves
Heres my code so far:
to setup
board
end
to go
dice-roll
end
to board
ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min-
pxcor or pycor = min-pycor
[set pcolor blue]]
ask patches [if pycor = max-pycor or pycor = min-pycor
[if pxcor mod 2 = 0
[set pcolor orange]]]
ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
[if pycor mod 2 = 0
[set pcolor orange]]]
ask patch min-pxcor min-pycor [set pcolor green]
end
to dice-roll
set dice [1 2 3 4 5 6]
set dice-num one-of dice
user-message (word "You rolled: " dice-num)
ask turtle 0 [
fd dice-num
]
;allows the turtle to turn if it lands on a corner
ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
if xcor = max-pxcor and ycor = max-pycor [set heading 270]
if xcor = min-pxcor and ycor = max-pycor [set heading 180]
;add a statement to end game once player rereaches the green patch
]
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
end
I expect the output to be the turtle to start moving up the right hand side of the board once it lands on the patch (15 -16) and recieves a dice-num greater than 1. However, when the turtle does land on the patch (15 -16) and the dice-num is greater than 1 it simply moves back to the beginning of the board.
Okay, this was simply too long for comments so I had to make some guesses. I expect the problem was that you were forwarding the full amount of the die roll after checking location. This would mean you have to test all the potential patches that could reach the corner. Instead, it is easier to move one patch and test location before moving the next patch. This is a cleaned up version of your code that implements this move and check process.
to setup
clear-all
board
ask one-of patches with [pcolor = green]
[ sprout 1
[ set color white
set heading 90
forward 1
]
]
end
to go
let die-num dice-roll
ask one-of turtles [ move die-num ]
end
to board
ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
[ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
ask patches with [ pycor = max-pycor or pycor = min-pycor ]
[ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
ask patch min-pxcor min-pycor [set pcolor green]
end
to-report dice-roll
let dice-num one-of [1 2 3 4 5 6]
user-message (word "You rolled: " dice-num)
report dice-num
end
to move [#roll]
while [ #roll > 0 ]
[ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
if pxcor = min-pxcor and pycor = min-pycor [ stop ]
fd 1
set #roll #roll - 1
]
end
The alternative is simply to add the die roll to the current location and see if it goes past. If it does, calculate where you want to get to.
How can I accomplish the following in the code below:
patches change color reflective of their distance from the row "min-pycor"
For example, colors alternate from yellow to red and then to black (signifying death).
But this should take into account that the production of yellow patches > red > black.
turtles-own
[
stem? ;; true for stem cells, false for transitory cells
age ;; age of cell. changes color with age
metastatic? ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]
globals
[
cell-count
]
to setup
clear-all
set-default-shape turtles "square"
ask patches[
if pycor = min-pycor [
ifelse random 10 <= 2
[set pcolor white]
[sprout 1 [set shape "square" set color blue] ]
]
]
evaluate-params
reset-ticks
end
to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
ask patches with [pcolor = white]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
tick
end
;;transitional cells move and hatch more. Turtle proc.
to move-transitional-cells
if (not stem?)
[
set color ( red + 0.25 * age )
fd 1
if (age < 6)
[
hatch 1
[ ;amplification
rt random-float 360
fd 1
]
]
]
end
to mitosis ;; turtle proc. - stem cells only
if stem?
[
hatch 1
[
fd 1
set color red
set stem? false
ifelse (who = 1)
[ set age 16 ]
[ set age 0 ]
]
]
end
to death ;; turtle proc.
if (not stem?) and (not metastatic?) and (age > 20)
[ die ]
if (not stem?) and metastatic? and (age > 4)
[ die ]
end
to evaluate-params
set cell-count count turtles ;cell count
if (cell-count <= 0)
[ stop ]
end
to kill-original-stem-cell
ask turtle 0
[ die ]
end
to kill-moving-stem-cell
ask turtle 1
[ die ]
end
to kill-transitory-cells
ask turtles with [ age < 10 and not stem? ]
[ die ]
end
You seem to have two conflicting requirements, the color change based on proximity in your code, and the color change based on PYCOR that you ask about.
Ignoring the code for a moment, we can set color based on PYCOR in many ways. For example, we can create bands of color, we can create dithered interminglings of color, or we can create a "smooth" transition between colors.
The first is easy. We can use an IFELSE structure. This example creates even bands, but you can change the "divide" variables to create bands of any height.
let color1 red
let color2 yellow
let color3 black
let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)
ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
ifelse pycor < divide2 [ set pcolor color2 ][
set pcolor color3
]]
]
We can also do it in a mathy way. This example creates even bands.
let colors [ red yellow black ]
let bands length colors
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]
We can create dithered bands by introducing a random element to the pycor. we also have to add some tests to keep the random numbers in range.
let colors [ red yellow black ]
let bands length colors
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 )
;; you might want to really study the above line to fully grok what's happening there
if py < 0 [ set py 0 ]
if py > world-height [ set py world-height ]
set pcolor item ( floor ( py / band-height ) ) colors
]
Graduated (gradiant) bands are tougher, because the NetLogo color space doesn't do gradual shifts in hue, only tint and shade, so there's really no way to get from red to yellow that way. So we would have to use RGB (three value list)colors, instead of NetLogo (single value) colors. And that's beyond what I'm willing to type out at the moment, so there you go--left as an exersize.