More succint plasmodium algo
This commit is contained in:
parent
3e5eac7730
commit
a7be149155
|
@ -1,33 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
"image/color"
|
"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 {
|
type SlimeMold struct {
|
||||||
SniffDistance int
|
Num int
|
||||||
Num int
|
|
||||||
NotMoved int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *SlimeMold) Setup(env *core.Environment, t *core.Turtle) {
|
func (sm *SlimeMold) Setup(env *core.Environment, t *core.Turtle) {
|
||||||
t.SetColor(color.RGBA{100, 255, 10, 0})
|
t.SetColor(color.RGBA{100, 255, 10, 0})
|
||||||
|
t.SetXY(150+(sm.Num%100), 150+(sm.Num/100))
|
||||||
//if sm.Num 1000 {
|
|
||||||
//t.SetXY(150,150)
|
|
||||||
//} else {
|
|
||||||
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
|
neighbours := 0
|
||||||
if env.Check(ox-1, oy-1) {
|
if env.Check(ox-1, oy-1) {
|
||||||
neighbours++
|
neighbours++
|
||||||
|
@ -57,48 +45,36 @@ func (sm * SlimeMold) CheckNeighbours(env *core.Environment, ox,oy int) int {
|
||||||
return neighbours
|
return neighbours
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
||||||
|
|
||||||
//t.FollowGradient(env, 1, 100, "food")
|
// Move around the world, if there are too many slimes around us turn around, otherwise follow slimes and food.
|
||||||
//x,y := t.Pos()
|
|
||||||
//
|
|
||||||
t.Wiggle()
|
t.Wiggle()
|
||||||
if t.Amount(env, 1, "slime") > 10 {
|
if t.Amount(env, 1, "slime") > 10 {
|
||||||
t.TurnAround()
|
t.TurnAround()
|
||||||
} else {
|
} else {
|
||||||
t.FollowGradient(env,1,1,"slime")
|
t.FollowGradient(env, 1, 1, "slime")
|
||||||
}
|
}
|
||||||
t.FollowGradient(env, 1, .1, "food")
|
t.FollowGradient(env, 1, .1, "food")
|
||||||
|
|
||||||
if sm.NotMoved > 10 {
|
// If we have no neighbours we pretend we found some food so the others can find us.
|
||||||
t.Wiggle()
|
|
||||||
t.Wiggle()
|
|
||||||
t.Wiggle()
|
|
||||||
//t.Step(env)
|
|
||||||
}
|
|
||||||
//
|
|
||||||
//px,py := t.Pos()
|
|
||||||
if t.Step(env) {
|
if t.Step(env) {
|
||||||
ox, oy := t.Pos()
|
ox, oy := t.Pos()
|
||||||
if sm.CheckNeighbours(env, ox,oy) >0 || env.HasValue(t.Pos()) {
|
if sm.CheckNeighbours(env, ox, oy) > 0 || env.HasValue(t.Pos()) {
|
||||||
sm.NotMoved = 0
|
|
||||||
t.Drop(env, .1, "slime")
|
t.Drop(env, .1, "slime")
|
||||||
} else {
|
} else {
|
||||||
sm.NotMoved++
|
|
||||||
t.Drop(env, 10, "food")
|
t.Drop(env, 10, "food")
|
||||||
}
|
}
|
||||||
} else {
|
} 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()) {
|
if env.HasValue(t.Pos()) {
|
||||||
env.TakeValue(t.Pos())
|
env.TakeValue(t.Pos())
|
||||||
t.Drop(env, 256, "food")
|
t.Drop(env, 256, "food")
|
||||||
}else {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,42 +85,37 @@ func main() {
|
||||||
num := 0
|
num := 0
|
||||||
|
|
||||||
experiment.InitEnvironment(func(environment *core.Environment) {
|
experiment.InitEnvironment(func(environment *core.Environment) {
|
||||||
x,y := 120,120
|
x, y := 120, 120
|
||||||
for i:=0;i<20;i++ {
|
for i := 0; i < 20; i++ {
|
||||||
for j :=0;j<20;j++ {
|
for j := 0; j < 20; j++ {
|
||||||
environment.PutValue(x+i,y+j)
|
environment.PutValue(x+i, y+j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
x,y = 250,120
|
x, y = 250, 120
|
||||||
for i:=0;i<20;i++ {
|
for i := 0; i < 20; i++ {
|
||||||
for j :=0;j<20;j++ {
|
for j := 0; j < 20; j++ {
|
||||||
environment.PutValue(x+i,y+j)
|
environment.PutValue(x+i, y+j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
x,y = 160,130
|
x, y = 160, 130
|
||||||
for i:=0;i<10;i++ {
|
for i := 0; i < 10; i++ {
|
||||||
for j :=0;j<10;j++ {
|
for j := 0; j < 10; j++ {
|
||||||
environment.PutValue(x+i,y+j)
|
environment.PutValue(x+i, y+j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
experiment.InitNTurtles(func() core.Actor {
|
experiment.InitNTurtles(func() core.Actor {
|
||||||
sm := new(SlimeMold)
|
sm := new(SlimeMold)
|
||||||
sm.SniffDistance = *sniffDistance
|
|
||||||
sm.Num = num
|
sm.Num = num
|
||||||
num++
|
num++
|
||||||
return sm
|
return sm
|
||||||
}, 3000)
|
}, 3000)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
experiment.InitPheromone("slime", color.RGBA{0x80, 0xFF, 0x00, 0x00})
|
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) {
|
experiment.OnStep = func(environment *core.Environment, turtles []*core.Turtle, i int) {
|
||||||
environment.EvaporateAndDiffuse(0.99, "slime")
|
environment.EvaporateAndDiffuse(0.99, "slime")
|
||||||
environment.EvaporateAndDiffuse(0.99, "food")
|
environment.EvaporateAndDiffuse(0.99, "food")
|
||||||
|
|
Loading…
Reference in New Issue