More succint plasmodium algo

This commit is contained in:
Sarah Jamie Lewis 2019-11-03 10:12:43 -08:00
parent 3e5eac7730
commit a7be149155
1 changed files with 26 additions and 55 deletions

View File

@ -1,33 +1,21 @@
package main
import (
"flag"
"git.openprivacy.ca/sarah/microworlds/core"
"git.openprivacy.ca/sarah/microworlds/experiments"
"image/color"
)
var sniffDistance = flag.Int("sniffDistance", 3, "the distance a turtle can detect pheromone levels from")
var defensiveDecentralization = flag.Bool("defensiveDecentralization", false, "if true, slime molds will break up if the concentration is too great")
type SlimeMold struct {
SniffDistance int
Num int
NotMoved int
Num int
}
func (sm *SlimeMold) Setup(env *core.Environment, t *core.Turtle) {
t.SetColor(color.RGBA{100, 255, 10, 0})
//if sm.Num 1000 {
//t.SetXY(150,150)
//} else {
t.SetXY(150+(sm.Num % 100),150+(sm.Num/100))
//}
t.SetXY(150+(sm.Num%100), 150+(sm.Num/100))
}
func (sm * SlimeMold) CheckNeighbours(env *core.Environment, ox,oy int) int {
func (sm *SlimeMold) CheckNeighbours(env *core.Environment, ox, oy int) int {
neighbours := 0
if env.Check(ox-1, oy-1) {
neighbours++
@ -57,48 +45,36 @@ func (sm * SlimeMold) CheckNeighbours(env *core.Environment, ox,oy int) int {
return neighbours
}
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
//t.FollowGradient(env, 1, 100, "food")
//x,y := t.Pos()
//
// Move around the world, if there are too many slimes around us turn around, otherwise follow slimes and food.
t.Wiggle()
if t.Amount(env, 1, "slime") > 10 {
if t.Amount(env, 1, "slime") > 10 {
t.TurnAround()
} else {
t.FollowGradient(env,1,1,"slime")
t.FollowGradient(env, 1, 1, "slime")
}
t.FollowGradient(env, 1, .1, "food")
if sm.NotMoved > 10 {
t.Wiggle()
t.Wiggle()
t.Wiggle()
//t.Step(env)
}
//
//px,py := t.Pos()
// If we have no neighbours we pretend we found some food so the others can find us.
if t.Step(env) {
ox, oy := t.Pos()
if sm.CheckNeighbours(env, ox,oy) >0 || env.HasValue(t.Pos()) {
sm.NotMoved = 0
if sm.CheckNeighbours(env, ox, oy) > 0 || env.HasValue(t.Pos()) {
t.Drop(env, .1, "slime")
} else {
sm.NotMoved++
t.Drop(env, 10, "food")
}
} else {
sm.NotMoved++
// We are on top of other slime, add some more randomness
t.Wiggle()
t.Wiggle()
t.Wiggle()
}
// We've found food, let's drop some chemical to tell others
if env.HasValue(t.Pos()) {
env.TakeValue(t.Pos())
t.Drop(env, 256, "food")
}else {
}
}
@ -109,42 +85,37 @@ func main() {
num := 0
experiment.InitEnvironment(func(environment *core.Environment) {
x,y := 120,120
for i:=0;i<20;i++ {
for j :=0;j<20;j++ {
environment.PutValue(x+i,y+j)
x, y := 120, 120
for i := 0; i < 20; i++ {
for j := 0; j < 20; j++ {
environment.PutValue(x+i, y+j)
}
}
x,y = 250,120
for i:=0;i<20;i++ {
for j :=0;j<20;j++ {
environment.PutValue(x+i,y+j)
x, y = 250, 120
for i := 0; i < 20; i++ {
for j := 0; j < 20; j++ {
environment.PutValue(x+i, y+j)
}
}
x,y = 160,130
for i:=0;i<10;i++ {
for j :=0;j<10;j++ {
environment.PutValue(x+i,y+j)
x, y = 160, 130
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
environment.PutValue(x+i, y+j)
}
}
})
experiment.InitNTurtles(func() core.Actor {
sm := new(SlimeMold)
sm.SniffDistance = *sniffDistance
sm.Num = num
num++
return sm
}, 3000)
experiment.InitPheromone("slime", color.RGBA{0x80, 0xFF, 0x00, 0x00})
experiment.InitPheromone("food", color.RGBA{0x00, 0x00, 0x00, 0x00})
experiment.InitPheromone("food", color.RGBA{0x80, 0xFF, 0x00, 0x00})
experiment.OnStep = func(environment *core.Environment, turtles []*core.Turtle, i int) {
environment.EvaporateAndDiffuse(0.99, "slime")
environment.EvaporateAndDiffuse(0.99, "food")