forked from sarah/microworlds
Predator Prey
This commit is contained in:
parent
e248e43de6
commit
b9383f8f94
|
@ -4,6 +4,7 @@
|
|||
*.o
|
||||
*.a
|
||||
*.so
|
||||
*csv
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
,sarah,cassandra,28.09.2019 15:14,file:///home/sarah/.config/libreoffice/4;
|
|
@ -0,0 +1 @@
|
|||
,sarah,cassandra,28.09.2019 15:34,file:///home/sarah/.config/libreoffice/4;
|
|
@ -0,0 +1 @@
|
|||
,sarah,cassandra,28.09.2019 15:59,file:///home/sarah/.config/libreoffice/4;
|
|
@ -0,0 +1,5 @@
|
|||
# Predator Prey Model
|
||||
|
||||
Models interactions between prey who eat grass, and predators who eat prey.
|
||||
|
||||
![](graph.png)
|
Binary file not shown.
Before Width: | Height: | Size: 119 KiB |
Binary file not shown.
After Width: | Height: | Size: 99 KiB |
|
@ -10,8 +10,23 @@ import (
|
|||
"math/rand"
|
||||
)
|
||||
|
||||
var numPrey = flag.Int("numPrey", 600, "the number of prey")
|
||||
var numPred = flag.Int("numPred", 60, "the number of predators")
|
||||
var numPrey = flag.Int("numPrey", 1200, "the number of prey")
|
||||
|
||||
var initialPreyEnergy = flag.Int("initialPreyEnergy", 20, "initial prey energy")
|
||||
var preyMaxLife = flag.Int("preyMaxLife", 25, "the max lifespan of prey (in simulation steps)")
|
||||
var preyReproductiveAge = flag.Int("preyReproductiveAge", 4, "the age a prey might reproduce")
|
||||
var preyReproductionEnergy = flag.Int("preyReproductionEnergy", 4, "energy required for prey reproduction")
|
||||
var preyReproductionProbability = flag.Float64("preyReproductionProbability", 0.5, "preys probability of reproducing")
|
||||
|
||||
|
||||
|
||||
var numPred = flag.Int("numPred", 30, "the number of predators")
|
||||
var initialPredatorEnergy = flag.Int("initialPredatorEnergy", 30, "initial predator energy")
|
||||
var predatorMaxLife = flag.Int("predatorMaxLife", 40, "the max lifespan of predators (in simulation steps)")
|
||||
var predatorMaxEnergy = flag.Int("predatorMaxEnergy", 30, "max amount of energy a predator can have")
|
||||
var predatorReproductionEnergy = flag.Int("predatorReproductionEnergy", 50, "predator reproductive energy")
|
||||
var predatorReproductionProbability = flag.Float64("predatorReproductionProbability", 0.1, "predators probability of reproducing")
|
||||
|
||||
|
||||
type Predator struct {
|
||||
Steps int
|
||||
|
@ -22,13 +37,13 @@ func (sm *Predator) Setup(env *core.Environment, t *core.Turtle) {
|
|||
// Do nothing
|
||||
t.SetAttribute("type", "predator")
|
||||
t.SetColor(color.RGBA{255, 200, 0, 0})
|
||||
sm.Energy = 50
|
||||
sm.Energy = *initialPredatorEnergy
|
||||
}
|
||||
|
||||
func (sm *Predator) Run(env *core.Environment, t *core.Turtle) {
|
||||
sm.Steps++
|
||||
|
||||
if sm.Steps == 30 {
|
||||
if sm.Steps == *predatorMaxLife {
|
||||
t.SetAttribute("status", "dead")
|
||||
return
|
||||
}
|
||||
|
@ -42,7 +57,7 @@ func (sm *Predator) Run(env *core.Environment, t *core.Turtle) {
|
|||
if prey != nil {
|
||||
if prey.GetAttribute("type") == "prey" && prey.GetAttribute("status") != "dead" {
|
||||
prey.SetAttribute("status", "dead")
|
||||
sm.Energy = int(math.Max(float64(sm.Energy)+10, 120))
|
||||
sm.Energy = int(math.Max(float64(sm.Energy)+10, float64(*predatorMaxEnergy)))
|
||||
}
|
||||
}
|
||||
t.FollowGradient(env, 1, 3, "scent")
|
||||
|
@ -59,14 +74,14 @@ func (sm *Prey) Setup(env *core.Environment, t *core.Turtle) {
|
|||
t.SetAttribute("type", "prey")
|
||||
t.SetColor(color.RGBA{100, 0, 100, 0})
|
||||
sm.Steps = 0
|
||||
sm.Energy = 25
|
||||
sm.Energy = *initialPreyEnergy
|
||||
}
|
||||
|
||||
func (sm *Prey) Run(env *core.Environment, t *core.Turtle) {
|
||||
sm.Steps++
|
||||
sm.Energy--
|
||||
|
||||
if sm.Steps >= 20 || sm.Energy == 0 {
|
||||
if sm.Steps >= *preyMaxLife || sm.Energy == 0 {
|
||||
t.SetAttribute("status", "dead")
|
||||
return
|
||||
}
|
||||
|
@ -83,7 +98,7 @@ func main() {
|
|||
experiment := new(experiments.Experiment)
|
||||
experiment.InitializeExperiment()
|
||||
experiment.InitEnvironment(func(env *core.Environment) {
|
||||
for i := 0; i < 600; i++ {
|
||||
for i := 0; i < 1200; i++ {
|
||||
x := rand.Intn(env.Width())
|
||||
y := rand.Intn(env.Height())
|
||||
env.PutValue(x, y)
|
||||
|
@ -98,6 +113,7 @@ func main() {
|
|||
return sm
|
||||
}, *numPrey)
|
||||
experiment.InitPheromone("scent", color.RGBA{0x80, 0xFF, 0x00, 0x00})
|
||||
fmt.Printf("%v, %v,%v\n", "time step", "number of prey", "number of predators")
|
||||
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, step int) {
|
||||
alive := 0
|
||||
predalive := 0
|
||||
|
@ -111,7 +127,7 @@ func main() {
|
|||
if turtle.GetAttribute("type") == "prey" && turtle.GetAttribute("status") != "dead" {
|
||||
alive++
|
||||
prey := turtle.GetActor().(*Prey)
|
||||
if prey.Steps > 10 && prey.Energy > 5 && rand.Intn(5) == 1 {
|
||||
if prey.Steps > (*preyReproductiveAge) && prey.Energy > (*preyReproductionEnergy) && float64(rand.Intn(100)) < (100*(*preyReproductionProbability)) {
|
||||
experiment.InitNTurtles(func() core.Actor {
|
||||
sm := new(Prey)
|
||||
return sm
|
||||
|
@ -124,7 +140,7 @@ func main() {
|
|||
|
||||
if turtle.GetAttribute("type") == "predator" {
|
||||
pred := turtle.GetActor().(*Predator)
|
||||
if pred.Energy >= 80 && rand.Intn(16) == 1 {
|
||||
if pred.Energy >= (*predatorReproductionEnergy) && float64(rand.Intn(100)) < (100*(*predatorReproductionProbability)) {
|
||||
experiment.InitNTurtles(func() core.Actor {
|
||||
sm := new(Predator)
|
||||
return sm
|
||||
|
@ -134,9 +150,9 @@ func main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
if step%10 == 0 {
|
||||
fmt.Printf("Prey Alive: %v | Pred Alive: %v \n", alive, predalive)
|
||||
}
|
||||
//if step == 0 {
|
||||
fmt.Printf("%v,%v,%v\n", step, alive, predalive)
|
||||
//}
|
||||
}
|
||||
experiment.Run()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue