NetLogo how to randomly generate turtles in the corners and middle area of the world

StackOverflow https://stackoverflow.com//questions/25017529

  •  21-12-2019
  •  | 
  •  

Question

So I'm creating a brown border of the world so that the robot looking for the wilting flowers doesn't go outside of the area. However, when I randomly generate the turtles (flowers), they sometimes are generated in the border, where the robot can't navigate to water them.....any idea how to maybe modify the rand-xy-co to make the plants so that they don't get generated in the boundary.

I have the leaves being created like so:

; creating plants (leaves)
to setup-leaves
create-leaves num-plants [  ; number of plants can vary
rand-xy-co                 ; set random positions for the plants
set shape "flower"         ; initialize the plant to color red and size 2
set color red
set size 2
]
end  

boundary is being set like so:

;set boundary obstacles. These patches tell the robot to stay within identified bounds.
 ask patches                                
  [
    set pcolor background-colour             ; set colour of background
    if (pxcor >= max-pxcor - boundary-width) ; boundary width can vary
      [ set pcolor brown ]
    if (pxcor <= min-pxcor + boundary-width)
      [ set pcolor brown ]
    if (pycor >= max-pycor - boundary-width)
      [ set pcolor brown ]
    if (pycor <= min-pycor + boundary-width)
      [ set pcolor brown ]
  ]

The rand-xy-co:

; Utilities. Positions a new turtle randomly on the grid, taking care not to 
;place it on top of another turtle
to rand-xy-co
  let x 0
  let y 0

  loop [ 
    set x random-pxcor 
    set y random-pycor
    if not any? turtles-on patch x y and not (abs x < 4 and abs y < 4) [setxy x y stop]
  ]
end

The entire code of this project:

; Plant Water and Nutrient Distribution Agent
; Simulates a plant water distribution robot that avoids obstacles and waters plants in the environment
; using simple reactive behaviour.

;TODO: send agent at base when battery life <10% or after all plants are done watering
;initialize robot's position at base
;make sure plants aren't created on the boundary of the house


breed [robots robot]       ; Name of the breed of plant robots
breed [leaves leaf]        ; Name of the breed of plants in need of water
breed [ watered water-1]   ; Name of the breed of watered plants

globals
[
  background-colour ; colour of the background except for obstacles
  obstacles-colour  ; colour of the obstacles
  robot-colour      ; colour of the robot
  clock             ; tracking time
  watered-plants    ; plants watered
  distance-traveled ; distance traveled by the robot
  battery-life      ; battery life left
  water-poured      ; how much water has been used

]

to setup
  ;clear all
  clear-all
  set-default-shape robots "ufo side" ; sets shapes for agent
  set background-colour green + 3 ; set colour of background light green
  set obstacles-colour brown ; set colour of obstacles brown
  setup-leaves ;add plants to the world
  set robot-colour gray ; set colour of robot to gray

  set clock 0 ;initialize time
  set distance-traveled 0 ;initialize distance
  set battery-life 100 ;initialize battery life to 100%
  set water-poured 0 ;initialize water used to 0

  ;set boundary obstacles. These patches tell the robot to stay within identified bounds.
  ask patches                                
  [
    set pcolor background-colour             ; set colour of background
    if (pxcor >= max-pxcor - boundary-width) ; boundary width can vary
      [ set pcolor brown ]
    if (pxcor <= min-pxcor + boundary-width)
      [ set pcolor brown ]
    if (pycor >= max-pycor - boundary-width)
      [ set pcolor brown ]
    if (pycor <= min-pycor + boundary-width)
      [ set pcolor brown ]
  ]

  ; creates colour, size and random location of single robot 
  create-robots 1
  [
    set size robot-size
    set color robot-colour
    let this-patch one-of patches with [pcolor != obstacles-colour]  ; sets an initial random position within the outside boundary
    set xcor [pxcor] of this-patch
    set ycor [pycor] of this-patch
  ]
end   


; creating plants (leaves)
to setup-leaves
  create-leaves num-plants [  ; number of plants can vary
  rand-xy-co                 ; set random positions for the plants
  set shape "flower"         ; initialize the plant to color red and size 2
  set color red
  set size 2
  ]
end 


;detecting a plant (leaf)
to-report detect-leaf
  ifelse any? leaves-here 
    [report true]           ; set as true if plant breed is detected
    [report false]
end


;watering a leaf (plant)
to water-leaf
  set watered-plants watered-plants + 1  ;when a plant is watered, increase counter by 1
  set water-poured  watered-plants * ((4 * (100 - soil-moisture)) / 50) ; when plant is watered, increase water-poured counter
                                                                        ; this formula is based on the average amount of water a houseplant needs >> 4 oz at 50% soil moisture
  set clock clock + 10                                                  ; add 10 ticks to the clock when watering plants since the robot has to spend time to water
  set battery-life battery-life - 5                                     ; 5% of battery life is spent for each plant watered
  ask one-of leaves-here [                                              ; mark breed as watered when plant is watered; change flower color to yellow
    set breed watered
    set shape "flower"
    set color yellow
    set size 3                                                          ;increase the size of the flower (grown)
    ]
end

; This defines how the robot should move.
to make-move                                                         
  if battery-life <= 0 [stop]                                          ; robot cannot run if it doesn't have any battery life
   ;This behaviour is modified from the Look Ahead Example model in the Models Library
   let this-patch patch-ahead 1
      if detect-leaf [water-leaf stop]
         ifelse (this-patch != nobody) and ([pcolor] of this-patch = obstacles-colour)
            [ lt random-float 360 ]                                   ; We see an obstacle patch in front of us. Turn a random amount.
            [ fd 1 ]                                                  ; Otherwise, it is safe to move forward.
        set distance-traveled distance-traveled + 1                   ; every step increases distance-traveled counter by 1
  end


;Procedure when the button Go is pressed
to go
;The robot moves around.
  if count leaves = 0 [stop]                                         ;stop when all leaves are watered
  set clock clock + 1                                                ;update time
  ;wait .05                                                          ; wait for better viewing  
  set battery-life 100 - (distance-traveled / 900)                   ; battery life varies based on distance traveled 
  ask robots [without-interruption [make-move]]                      ;robots move
  update-and-plot                                                    ;update the graphs         
  tick  
end

;plot velocity graph
to update-and-plot-velocity
  set-current-plot "Velocity"
  plotxy distance-traveled clock
end

;plot water usage graph
to update-and-plot-water-usage
  set-current-plot "Water Usage"
  plotxy water-poured watered-plants
end

;plot energy usage graph
to update-and-plot-energy-usage
  set-current-plot "Energy Usage"
  plotxy  clock (100 - battery-life)
end

;plot watered plants graph
to update-and-plot-watered-plants
  set-current-plot "Watered Plants"
  plotxy clock watered-plants
end

;plot wilting plants graph
to update-and-plot-wilting-plants
  set-current-plot "Wilting Plants"
  plotxy clock count leaves
end

;call all the graphs
to update-and-plot
  update-and-plot-velocity
  update-and-plot-water-usage
  update-and-plot-energy-usage
  update-and-plot-watered-plants
  update-and-plot-wilting-plants
end

; Creates obstacles in the environment by drawing them on the world
to make-obstacles
 if mouse-down?
 [ ask patches
   [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
     [set pcolor obstacles-colour]]
 ]
end


; Removes obtacles in the environment.
to erase-obstacles
  if mouse-down?
 [ ask patches
   [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
     [set pcolor background-colour]]
 ]
end

; Detecting obstacles 
; Obstacles are obstacles and other agents.
to-report detect-obstacle
ifelse any? patches in-cone 2 90 with [pcolor = brown] or any? other robots in-cone 2 90 
 [report true]
 [report false]
end   

; The robot avoids any obstacles in the environment.   
;to avoid-obstacles
  ;if (count patches in-cone radius-length radius-angle with [pcolor = obstacles-colour] > 0)  ; there is an obstacle nearby
  ;[ 
    ;set heading heading + random 45 - random 45
   ;]
;end

; This instructs the agent to move the pen up if it is down, or vice versa.
to plot-paths
  ifelse (pen-mode = "up")
    [ pen-down ]
    [ pen-up ]
end

; Utilities. Positions a new turtle randomly on the grid, taking care not to 
;place it on top of another turtle
to rand-xy-co
  let x 0
  let y 0

  loop [ 
    set x random-pxcor 
    set y random-pycor
    if not any? turtles-on patch x y and not (abs x < 4 and abs y < 4) [setxy x y stop]
  ]
end

;Team 6 has modified the Vacuum Cleaner Robot NetLogo model to fit the needs of this project
; Copyright 2009 by Thomas Christy and William John Teahan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Vacuum Cleaner Robot NetLogo model.
; Artificial Intelligence. Teahan, W. J. (2010). Ventus Publishing Aps.
;
Was it helpful?

Solution

The easiest way to do this is to simply create the flowers only on non-white patches:

to rand-xy-co
  move-to one-of patches with [ pcolor != brown and not any? turtles-here ]
end

But make sure you set the color of the patches before calling setup-leaves.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top