stopping turtles in the imported drawing to fill the drawing - netlogo

Actually, I am using patch-ahead 1 condition for stopping my turtles in drawing imported by using "import-pcolors" command.
drawing or shape (such as star-fish) is aligned on the center of the world's patches and 4 turtles(seeds) are placed near the center of the origin within shape drawing remained stationary until the end, all other turtles are placed randomly and also moves randomly in the world with stationary? = false.
The goal is to completely fill the shape(drawing) without leaving any patch empty in the drawing, by moving the turtles randomly and stop when approaches to next stationary turtle and become stationary? = true, and reference for all other remaining non-stationary turtles.
here is what I have tried so far,
to setup
import-pcolors "starnew.png" ; image imported in the world on patches for turtles to interact
create-robots num-of-robots
[
set seed? false
set stationary? false
set shape "circle 2"
setxy random-pxcor random-pycor
]
ask turtles
[
if who = 0 ; similarly for who = 1 (setxy = 0 1 ), who = 2 (setxy = 1 0 ), who = 3 (setxy = 0 -1 ), having loaction near centre origin
[
setxy 0 0
set seed? true
set stationary? true
set localized? true
]
]
end
to go
ask robots with [stationary? != true]
[
ifelse pcolor = white ;; out-shape
[
wall
fd 0.1
rt random 30
lt random 30
]
[ ;; In-shape
set pos-inside? true
ifelse ( not any? (robots-on patch-ahead 1 ) with [stationary? = true or seed? = true] )
[
fd 0.1
rt random 30
lt random 30
]
[
set pos-inside? true
set stationary? true
set localized? true
stop
]
]
]
end
the desired behavior I want to achieve is shown in the picture below.

Related

NETLOGO: Iterate over agents with a variable set on a slider

I'm trying to iterate over agents to change them randomly.
Basically there ar "n" number of coins (n is defined by a slider that creates those n number of agents)
The function to change them that I'm trying is the following
to tossing
if any? coins with [color = white] [
ask coins [
let coin-toss random-float 1.0
if coin-toss > 0.5 [
print "hi"
;set shape "coin tails"
set color lime
set flip 1
]
if coin-toss < 0.5 [
set color pink
set flip 0
]
]
]
if not any? coins with [color = white] [
ask coins [
set color white
set flip 3
]
]
end
This doesn't even change the colors, and I'm sure it won't change each coin differently.
I've been searching how to approach this but can't find a thing.
Edited solution and adding previous procedure:
to go
ifelse (any? turtles with [color = red]) [
print "red"
restart
;ask coins [ tossing ]
]
[
ask one-of cryptographers [ update ]
wait 0.6
ask coins [ tossing ]
;recolor
]
tick
end
to tossing
if any? coins with [shape = "circle"] [
ask coins [
;let a random-float 1
ifelse (flip = 3) and (random-float 1 > 0.5) [
;print "hi"
set shape "coin tails"
set color lime
set flip 1
]
[
set shape "coin heads"
set flip 0
set color pink
]
wait 0.2
]
]
end

How do you go about making a convert probability chance for an agent in netlogo?

I'm currently working on a zombie apocalypse simulation in NetLogo 2D. In my current simulation, when the zombies collide with humans the humans have a 100% chance to turn into a zombie. I would like to change this so that I can use a slider to set the conversion rate - for example if I have a rate of 50% on my slider, then when the zombies collide with humans there's a 50% chance they would turn into a zombie, otherwise kill the attacking zombie.
This is how I have currently setup my project, the way I've done this so far is to detect when they collide, and set the humans health to -1, and then made an if statement that says if the health is below 1, to make them a zombie.
I would appreciate any help in the right direction as I've spent time pondering about this and haven't come up with any solution.
breed[humans person]
breed[zombies zombie]
globals[rad]
humans-own[zombies_seen zombies_hit humans_speed per_vis_rad per_vis_ang health]
zombies-own[zombies_speed humans_around_me closest_humans]
patches-own[solid]
to setup_world
clear-all
reset-ticks
set rad 1
ask patches[
set pcolor green
]
create-humans number_of_humans [
setxy random-xcor random-ycor
set color blue
set size 5
set shape "person"
set humans_speed 1 + random-float 0.1
set health 100
adjust_vision_cone
]
ask humans[
ask patches in-radius 5[
set pcolor yellow
]
]
create-zombies number_of_zombies [
setxy random-xcor random-ycor
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
ask humans[
ask patches in-radius 5[
set pcolor green
]
]
draw_solid_patches
end
to draw_solid_patches
ask patches [
set solid false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set solid true
]
end
to detect_wall
if[solid] of patch-ahead 1 = true[
right 90
left 90
]
end
to run_model
Every 0.01 [
make_humans_move
make_zombies_move
tick
reset_patch_colour
if not any? humans [stop]
if ticks = 500000 [stop]
]
end
to make_humans_move
ask humans[
if health > 1 [
show_visualisations
right 45
left 45
let have_seen_zombies human_function
detect_wall
forward humans_speed
]
if health < 1 [
; ; ; this is where it looks at the health of the humans,
; ; ; and checks to see if they have been hit by a zombie,
; ; ; because their health would be below 1.
set breed zombies
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
]
end
to make_zombies_move
ask zombies[
detect_wall
right 45
left 45
smell_humans
detect_wall
forward zombies_speed
]
end
to smell_humans
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
detect_wall
end
to show_visualisations
if show_vis_cone = true [
ask patches in-cone per_vis_rad per_vis_ang with [pcolor = green] [
set pcolor orange
]
]
end
to reset_patch_colour
ask patches with [pcolor = orange] [
set pcolor green
]
end
to adjust_vision_cone
set per_vis_rad 10 + random 10
set per_vis_ang 90
end
to-report human_function
let seen [false]
let hit [false]
ask zombies in-cone per_vis_rad per_vis_ang [
set seen true
]
ask zombies in-radius rad [
set hit true
]
ifelse seen = true [
set zombies_seen zombies_seen + 1
right 180
][
right (random zwr - (zwr / 2))
]
if hit = true [
set zombies_hit zombies_hit + 1
set color black
set health -1
; ; ; this is where I make the human into a zombie,
; ; ; I need to have a method here that will refer to
; ; ; the convert_probability variable.
]
report seen
end
Making minimal adjustments to your code you could substitute the set health -1 to
if random 100 < conversion_probability [ set health 0 ]
where conversion_probability is a slider going from 0 to 100, as you said in the question.
I also want to point out that you could detect walls like this
to detect_wall
if [solid] of patch-ahead 2
[
face one-of patches in-radius 2 with [not solid]
]
end
doing left x and then right x only makes the turtle turn to the left and then face back where it was originally facing, unless it sees a zombie it won't change the behavior of the humans all that much.

How can I make the model run?

I am essentially trying to combine elements of the 'Segregation' model and 'Rebellion' model to form a model that is representative of alliance forming.
Here is what I have so far- when I attempt to run it I receive the error: THREATS breed does not own variable ACTIVE?
error while threat 0 running ACTIVE?
called by procedure GO
called by Button 'go'
breed [ agents an-agent]
breed [ threats threat ]
globals [
k ; factor for determining attack probability
threshold ; by how much must D - BS > A to make a state burden share
percent-similar ; on the average, what percent of a turtle's neighbors
; are the same color as that turtle? Likely to ally
percent-unhappy ; what percent of the turtles are unhappy? Or percieve threats?
visualization
]
agents-own [
allied-states ; R, fixed for the agent's lifetime, ranging from 0-1 (inclusive)- for each turtle, indicates whether at least %-similar-wanted percent of
; that turtle's neighbors are the same color as the turtle
perceived-threat ; T, also ranging from 0-1 (inclusive)- how many have a turtle of another color?
active? ; if true, then the agent is actively allied
; if false, then the agent is free-riding
conflict ; how many turns in conflict remain? (if 0, the agent is not in conflict)- sum of previous two variables
total-nearby ; sum of previous two variables
]
patches-own [
neighborhood ; surrounding patches within the vision radius
]
to setup
clear-all
; set globals
set k 2.3
set threshold 0.1
ask patches [
; make background a slightly dark gray
set pcolor gray - 1
; cache patch neighborhoods
set neighborhood patches in-radius vision
]
if initial-threats-density + initial-agent-density > 206 [
user-message (word
"The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY "
"should not be greater than 206.")
stop
]
; create threats
create-threats round (initial-threats-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
display-threats
]
; create agents
create-agents round (initial-agent-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
set heading 0
set allied-states random-float 1.0
set perceived-threat random-float 1.0
set active? false
set conflict 0
display-agent
]
; start clock and plot initial state of system
reset-ticks
end
to go
if all? turtles [ active? ] [ stop ]
move-unhappy-turtles
update-turtles
update-globals
tick
end
; unhappy turtles try a new spot
to move-unhappy-turtles
ask turtles with [ not active? ]
[ find-new-spot ]
end
; move until we find an unoccupied spot
to find-new-spot
rt random-float 360
fd random-float 10
if any? other turtles-here [ find-new-spot ] ; keep going until we find an unoccupied patch
move-to patch-here ; move to center of patch
end
to update-turtles
ask turtles [
; in next two lines, we use "neighbors" to test the eight patches
; surrounding the current patch
set allied-states count (turtles-on neighbors) with [ color = [ color ] of myself ]
set perceived-threat count (turtles-on neighbors) with [ color != [ color ] of myself ]
set total-nearby allied-states + perceived-threat
set active? allied-states >= (percent-similar * total-nearby / 100)
; add visualization here
if visualization = "old" [ set shape "default" set size 1.3 ]
if visualization = "square-x" [
ifelse active? [ set shape "square" ] [ set shape "X" ]
]
]
end
to update-globals
let similar-neighbors sum [ allied-states ] of turtles
let total-neighbors sum [ total-nearby ] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [ not active? ]) / (count turtles) * 100
; Agents engaged in conflict have the duration reduced at the end of each clock tick
ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ]
; update agent display
ask agents [ display-agent ]
ask threats [ display-threats ]
; advance clock and update plots
tick
end
; AGENT AND THREAT BEHAVIOR
; move to an empty patch
to move ; turtle procedure
if movement? or breed = threats [
; move to a patch in vision; candidate patches are
; empty or contain only jailed agents
let targets neighborhood with [
not any? threats-here and all? agents-here [ conflict > 0 ]
]
if any? targets [ move-to one-of targets ]
]
end
; AGENT BEHAVIOR
to determine-behavior
set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold)
end
to-report burden-sharing
report perceived-threat * (1 - alliance-protection)
end
to-report estimated-conflict-probability
let t count (threats-on neighborhood)
let a 1 + count (agents-on neighborhood) with [ active? ]
; See Info tab for a discussion of the following formula
report 1 - exp (- k * floor (t / a))
end
to alliance
if any? threats [attack]
set active? true
end
; THREAT BEHAVIOR
to attack
if any? (agents-on neighborhood) with [ active? ] [
; arrest suspect
let suspect one-of (agents-on neighborhood) with [ active? ]
move-to suspect ; move to patch of the jailed agent
ask suspect [
set active? false
set conflict random conflict-term
set color pink
]
]
end
; VISUALIZATION OF AGENTS AND COPS
to display-agent ; agent procedure
set color cyan
set shape "triangle"
end
to display-active?
set color pink
set shape "triangle"
end
to display-threats
set color red
set shape "circle 2"
end
The problem is that you have two breeds of turtles, agents and threats, and only agents "own" the variable active?. turtles is a superset of all breeds, so when the all? primitive tries to query the active? variable of literally all turtles, it tries to query active? for threats too, and can't find it. The line should be
if all? agents [ active? ] [ stop ]

How to assign the shortest-distance to all agents in NetLogo?

How to assign the shortest-distance to all agents
Current status: My codes only finds the shortest-path for 1 person but cannot when there are more than 1 person.
Question: How can I assign the shortest-path for each person?
Background
I manipulated the traffic grid model inside the NetLogo model library to create a shortest-path algorithm for pedestrians (somewhat close to A*). The interface below shows that an agent is standing on its origin which is coloured in pale yellow, and has the same colour as a destination. It also works fine for 3 people.
1 person
3 people
Setup
Here is a code chunck that I wrote as a setup. It doesn't seem to have any problem in this section.
globals
[
grid-x-inc ;; the amount of patches in between two roads in the x direction
grid-y-inc ;; the amount of patches in between two roads in the y direction
intersections ;; agentset containing the patches that are intersections
roads ;; agentset containing the patches that are roads
population
]
breed[people person]
patches-own
[
father
navigated?
active?
Heuristic
step
total-cost
is-intersection?
start
finish
]
people-own
[
origin
destination
h-distance ;; short for heuristic distance. Euclidean distance to goal
path
]
to setup
clear-all
setup-globals
setup-patches
setup-people
setup-destination
reset-ticks
end
to setup-globals
set grid-x-inc world-width / grid-size-x
set grid-y-inc world-height / grid-size-y
ask patches
[
set father nobody
set navigated? false
set active? false
set total-cost 0
]
end
to setup-patches
;; initialize the patch-owned variables and color the patches to a base-color
ask patches [ set pcolor brown + 3 ]
;; initialize the global variables that hold patch agentsets
set roads patches with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
set intersections roads with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
ask roads [ set pcolor white ]
ask intersections [ set is-intersection? true
ask neighbors [ set is-intersection? true ]]
end
to setup-people
create-people no-of-people
[
set shape "person"
set size 1
set color black
move-to one-of patches with [pcolor != brown + 3]
set origin patch-here
set destination one-of patches with [pcolor != brown + 3 and is-intersection? != true]
set h-distance distance destination
]
set population [] ;; create list of individuals
let sort-turtles sort people set population sort-turtles
end
to setup-destination
foreach population [ individual ->
ask individual [
let roads_ roads with [pcolor = white]
ask roads_ [
set navigated? false
set active? false
set Heuristic 0
set step 0
set total-cost 0
set start false
set finish false
]]
let current [origin] of individual
let target [destination] of individual
ask individual [
if target != nobody [
ask current [
set pcolor (10 + random 130)
set step 0
set start true
set finish false
set navigated? false
]
ask target [
set pcolor [pcolor] of current
set navigated? true
set start false
set finish true
]]
]]
end
Problem 1: Set all paths
set-path traces all the steps from the origin patch to everywhere inside the virtual world. The code will colour the road to yellow and add a label on the path. This procedure will repeat 50 times to find all the possible steps.
The problem occurs from this section where this code only works for one agent not for the entire agents.
to set-path
repeat 50 [
ask patches with [pcolor = white and pcolor != yellow] [
if any? neighbors4 with [ start = true ] or
any? neighbors4 with [ pcolor = yellow ]
[ set pcolor yellow
let laststep [step] of one-of neighbors4 with [pcolor = yellow or
start = true]
ifelse [plabel] of patches != nobody [
set step laststep + 1
set plabel step
set father step
set total-cost father + heuristic
set plabel-color red][set plabel ""]
]]]
let allroads [self] of patches with [pcolor != brown]
let final_location one-of patches with [finish = true]
foreach allroads [ road_patch ->
ask road_patch [set Heuristic distance final_location]
]
end
Problem 2: Set shortest path
This procedure finds the shortest path by finding the patches and adding them to a list named path. Because the previous procedure only worked for one agent this procedure will also assign the patches to one agent, which is not what I want.
to shortest-path
ask patches with [start = true] [
let maxsteps 0
ask patches with [navigated? = true]
[ let check-steps min-one-of neighbors4 with
[pcolor != brown + 3 and pcolor != white][step]
set maxsteps [step] of check-steps
]
let num (range maxsteps 0 -1)
let paths []
foreach num [ navigation ->
ask patches with [navigated? = true] [
let nav-patch min-one-of neighbors4 with
[step = navigation and pcolor != brown + 3][step]
if nav-patch != nobody [ask nav-patch [set navigated? true set paths fput self paths]]
]]
ask people [set path paths]
]
ask patches with [pcolor = yellow][set pcolor white set plabel ""]
end
Summary: What I want the model to look like
How can I assign the shortest-path for each person if there are more than 2 people inside NetLogo?
What I want the model to look like
File: https://drive.google.com/file/d/1gxhkcnlBniB5M4aSYmt65PE6LBekgVp6/view?usp=sharing

How to set a default color(turtle's color when its enter in the model) of turtle after it changes its color?

I write a line of code which is this
ask turtles [if count other turtles in-radius 1 > 5 [set color white]]
now when turtle has changed color to white, but after the certain amount of time this property is not true for that same turtle, it should change its color to default color?
how can I fix it?
I think you're after a turtles-own counter that decreases whenever the condition is not satisfied. With this setup:
turtles-own [ default-color countdown ]
to setup
ca
crt 150 [
setxy random-xcor random-ycor
set default-color blue
set color default-color
]
reset-ticks
end
Now you can get your turtles to wander around and change their countdown variable whenever they change color. When that condition is not met, they can decrease the counter until it hits zero, at which point they can revert to their default color. More detail in comments:
to go
ask turtles [
rt random 60 - 30
fd 0.25
; if there are more than 5 turtles in radius 3,
; turn white and set countdown to 5
ifelse count other turtles in-radius 3 > 5 [
set color white
set countdown 5
] [
; If not, and counter is greater than 0,
; decrease the counter.
if countdown > 0 [
set countdown countdown - 1
; If counter gets down to 0,
; set color back to the default.
if countdown = 0 [
set color default-color
]
]
]
]
tick
end