Untitled
unknown
plain_text
10 months ago
4.5 kB
11
Indexable
globals [ age ]
;; Set two breeds of agents, one for the bugs and one for food
breed [ food a-food ]
breed [ bugs bug ]
bugs-own [
energy ;; The resource bugs will be depleting and refilling by moving and eating
speed ;; The "gene" of movement speed for each bug
vision ;; The "gene" that decides how far a bug can see
]
to setup
clear-all
reset-ticks
setup-world
setup-bugs
end
to setup-world
ask patches [
set pcolor green + 2
]
end
to setup-bugs
;; Creating the bugs and setting their initial states to numbers that, after trials, suits the model
create-bugs 100 [
set color 10
set speed .5
set energy 50
set vision 3
]
ask bugs [
set shape "bug"
setxy random-xcor random-ycor]
end
to go
;; Two criteria for stop: One if no bugs are alive and one if any setting results in uncontrolled reproduction
if not any? bugs [ stop ]
if count bugs >= 2000 [ stop ]
move-bug ;; Bugs move forward, change direction or towards food within their vision.
eat-food ;; If standing on a patch where there is food, eats it
eat-prey ;; If standing on a patch where there is prey, eats it
check-death ;; Bugs check if their energy is zero, if yes they die
reproduce ;; Bugs check if their energy is at or above the reproduction threshold. If yes they reproduce
;; with a 20% chance to mutate each gene and direction.
regrow-food ;; Food grows at random places at a rate set in the interface
tick
end
to move-bug
ask bugs [
;; Make bugs consider the nearest bug with half the size of self "prey". If there is no such bug within vision, set prey to the closest piece of food within vision.
let prey min-one-of other ((bugs in-radius vision) with [ (size * 2) < [size] of myself ]) [distance myself]
if prey = nobody [
set prey min-one-of (food in-radius vision) [distance myself]
]
;; If there is any prey, bug or food, within vision: Move towards it.
ifelse prey != nobody [
face prey
ifelse distance prey < speed [
move-to prey
][
fd speed
]
]
[
;; If no prey within vision, move forward at respective speed with a 20% chance of changing direction
fd speed
if random 100 < 20 [right random 360]
]
;; Set energy to decrease relative to current size and speed as well as the decided cost of each
set energy energy - ((size * cost-of-size) * (speed * cost-of-speed))
]
end
to eat-food
;; If there is any food at current position, eat it and set energy to increase by current food-value
ask bugs [
let prey one-of food-here
if prey != nobody [
ask prey [ die ]
set energy energy + food-value ]
]
end
to eat-prey
ask bugs [
;; If there is any bugs with half the size of self at current position, eat it and set energy to increase by current prey-value
let prey one-of other bugs-here with [ (size * 2) < [size] of myself ]
if prey != nobody [
ask prey [ die ]
set energy energy + prey-value
]
]
end
to check-death ;; If energy is at zero, the bug dies
ask bugs [
if energy <= 0 [ die ]
]
end
to reproduce
ask bugs [
;; If energy is above 100, decrease it by 50 and hatch a bug identical to self
if energy > 100 [
set energy energy - 50
hatch 1 [
;; Each "gene" has a set chance of increasing or decreasing a set amount when being passed on to the offspring
if random 100 < mutation-rate [
ifelse random 100 < 50 [
set speed speed * (1 + mutation-amount / 100) ][
set speed speed * (1 - mutation-amount / 100)
]
]
if random 100 < mutation-rate [
ifelse random 100 < 50 [
set size size * (1 + mutation-amount / 100) ][
set size size * (1 - mutation-amount / 100)
]
]
if random 100 < mutation-rate [
ifelse random 100 < 50 [
set vision vision * (1 + mutation-amount / 100) ][
set vision vision * (1 - mutation-amount / 100)
]
]
]
]
]
end
to regrow-food
;; Create food at the rate of food-growth at random places.
if random 3 > 1 [
create-food food-growth [
setxy random-xcor random-ycor
set shape "circle"
set color red
set size 0.5
]
]
end Editor is loading...
Leave a Comment