Frogs & Turtles (And BUGFIX)
This commit is contained in:
parent
1f7a87bba7
commit
6a7d283bb7
|
@ -1 +0,0 @@
|
|||
,sarah,cassandra,28.09.2019 15:14,file:///home/sarah/.config/libreoffice/4;
|
|
@ -1 +0,0 @@
|
|||
,sarah,cassandra,28.09.2019 15:34,file:///home/sarah/.config/libreoffice/4;
|
|
@ -1 +0,0 @@
|
|||
,sarah,cassandra,28.09.2019 15:59,file:///home/sarah/.config/libreoffice/4;
|
|
@ -46,6 +46,10 @@ func (e *Experiment) InitializeExperiment() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
e.OnStep = func(environment *core.Environment, turtles []*core.Turtle, i int) {
|
||||
|
||||
}
|
||||
|
||||
e.env = core.NewEnvironment(*width, *height)
|
||||
e.turtles = make([]*core.Turtle, 0)
|
||||
e.graphics = graphics.NewGraphics(int32(*width), int32(*height), int32(*pxsize))
|
||||
|
@ -90,6 +94,7 @@ func (e *Experiment) Run() {
|
|||
step := 0
|
||||
for running {
|
||||
e.graphics.Render(e.env, e.turtles)
|
||||
|
||||
e.OnStep(e.env, e.turtles, step)
|
||||
|
||||
newTurtles := make([]*core.Turtle, 0)
|
||||
|
|
|
@ -18,8 +18,6 @@ var preyReproductiveAge = flag.Int("preyReproductiveAge", 4, "the age a prey mig
|
|||
var preyReproductionEnergy = flag.Int("preyReproductionEnergy", 4, "energy required for prey reproduction")
|
||||
var preyReproductionProbability = flag.Float64("preyReproductionProbability", 0.4, "preys probability of reproducing")
|
||||
|
||||
|
||||
|
||||
var numPred = flag.Int("numPred", 30, "the number of predators")
|
||||
var initialPredatorEnergy = flag.Int("initialPredatorEnergy", 40, "initial predator energy")
|
||||
var predatorMaxLife = flag.Int("predatorMaxLife", 60, "the max lifespan of predators (in simulation steps)")
|
||||
|
@ -27,7 +25,6 @@ var predatorMaxEnergy = flag.Int("predatorMaxEnergy", 30, "max amount of energy
|
|||
var predatorReproductionEnergy = flag.Int("predatorReproductionEnergy", 50, "predator reproductive energy")
|
||||
var predatorReproductionProbability = flag.Float64("predatorReproductionProbability", 0.05, "predators probability of reproducing")
|
||||
|
||||
|
||||
type Predator struct {
|
||||
Steps int
|
||||
Energy int
|
||||
|
@ -127,7 +124,7 @@ func main() {
|
|||
if turtle.GetAttribute("type") == "prey" && turtle.GetAttribute("status") != "dead" {
|
||||
alive++
|
||||
prey := turtle.GetActor().(*Prey)
|
||||
if prey.Steps > (*preyReproductiveAge) && prey.Energy > (*preyReproductionEnergy) && float64(rand.Intn(100)) < (100*(*preyReproductionProbability)) {
|
||||
if prey.Steps > (*preyReproductiveAge) && prey.Energy > (*preyReproductionEnergy) && float64(rand.Intn(100)) < (100*(*preyReproductionProbability)) {
|
||||
experiment.InitNTurtles(func() core.Actor {
|
||||
sm := new(Prey)
|
||||
return sm
|
||||
|
@ -151,7 +148,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
//if step == 0 {
|
||||
fmt.Printf("%v,%v,%v\n", step, alive, predalive)
|
||||
fmt.Printf("%v,%v,%v\n", step, alive, predalive)
|
||||
//}
|
||||
}
|
||||
experiment.Run()
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"git.openprivacy.ca/sarah/microworlds/core"
|
||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
var numFrogs = flag.Int("numFrogs", 5000, "the number of frogs")
|
||||
var move = flag.Bool("move", true, "")
|
||||
var turtlePreference = flag.Float64("turtlePreference", 0.3, "")
|
||||
var frogPreference = flag.Float64("frogPreference", 0.3, "")
|
||||
|
||||
type Frog struct {
|
||||
}
|
||||
|
||||
func (sm *Frog) Setup(env *core.Environment, t *core.Turtle) {
|
||||
// Do nothing
|
||||
t.SetAttribute("type", "frog")
|
||||
t.SetColor(color.RGBA{0, 255, 255, 0})
|
||||
}
|
||||
|
||||
func (sm *Frog) Run(env *core.Environment, t *core.Turtle) {
|
||||
t.Wiggle()
|
||||
amountTurtle := t.Amount(env, 1, "turtle-scent")
|
||||
amountFrog := t.Amount(env, 1, "frog-scent")
|
||||
if amountFrog > 1 && amountFrog > (amountTurtle+amountFrog)*float32(*frogPreference) {
|
||||
// Do Nothing
|
||||
} else {
|
||||
if *move {
|
||||
t.Step(env)
|
||||
}
|
||||
}
|
||||
t.Drop(env, 1, "frog-scent")
|
||||
}
|
||||
|
||||
type Turtle struct {
|
||||
}
|
||||
|
||||
func (sm *Turtle) Setup(env *core.Environment, t *core.Turtle) {
|
||||
// Do nothing
|
||||
t.SetAttribute("type", "turtle")
|
||||
t.SetColor(color.RGBA{255, 000, 0, 0})
|
||||
}
|
||||
|
||||
func (sm *Turtle) Run(env *core.Environment, t *core.Turtle) {
|
||||
t.Wiggle()
|
||||
amountTurtle := t.Amount(env, 1, "turtle-scent")
|
||||
amountFrog := t.Amount(env, 1, "frog-scent")
|
||||
if amountTurtle > 1 && amountTurtle > (amountTurtle+amountFrog)*float32(*turtlePreference) {
|
||||
// Do Nothing
|
||||
} else {
|
||||
if *move {
|
||||
t.Step(env)
|
||||
}
|
||||
}
|
||||
t.Drop(env, 1, "turtle-scent")
|
||||
}
|
||||
|
||||
func main() {
|
||||
experiment := new(experiments.Experiment)
|
||||
experiment.InitializeExperiment()
|
||||
num := -1
|
||||
experiment.InitNTurtles(func() core.Actor {
|
||||
num++
|
||||
if num%2 == 0 {
|
||||
sm := new(Frog)
|
||||
return sm
|
||||
}
|
||||
return new(Turtle)
|
||||
}, *numFrogs+experiment.GetNumTurtles())
|
||||
|
||||
experiment.InitPheromone("turtle-scent", color.RGBA{0xFF, 0x00, 0x00, 0x00})
|
||||
experiment.InitPheromone("frog-scent", color.RGBA{0x00, 0xFF, 0xFF, 0x00})
|
||||
|
||||
experiment.Run()
|
||||
}
|
Loading…
Reference in New Issue