Adding snowball inspired algorithm

This commit is contained in:
Sarah Jamie Lewis 2019-08-10 17:17:41 -07:00
parent 11c0543c1d
commit 9fe42b1ff2
2 changed files with 65 additions and 0 deletions

57
actors/snowball.go Normal file
View File

@ -0,0 +1,57 @@
package actors
import (
"git.openprivacy.ca/sarah/microworlds/core"
"math/rand"
"strconv"
)
type Snowball struct {
color int
Probability float32
sureness float32
}
func (sm *Snowball) Snowball(env *core.Environment, t *core.Turtle) {
num := rand.Intn(100)
if num >= int(sm.Probability*100.0) {
sm.color = 2
} else {
sm.color = 1
}
sm.sureness = 1
}
func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
t.Wiggle()
am1 := t.Amount(env,1,"1")
am2 := t.Amount(env,1,"2")
t.Drop(env, sm.sureness, strconv.Itoa(sm.color))
if am1 > sm.sureness || am2 > sm.sureness {
if am1 > am2 {
if sm.color == 2 {
sm.sureness--
} else {
sm.sureness++
}
if sm.sureness == 0 {
sm.color = 1
}
} else if am2 > am1 {
if sm.color == 1 {
sm.sureness--
} else {
sm.sureness++
}
if sm.sureness == 0 {
sm.color = 2
}
}
}
t.Step(env)
}

View File

@ -101,6 +101,14 @@ func main() {
env.InitPheromone("2")
g.ColorPheromone("1", [4]uint8{0x80, 0xFF, 0x00, 0x00})
g.ColorPheromone("2", [4]uint8{0xFF, 0, 0xFF, 0x00})
case "snowball":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Slush{Probability:float32(*prob)})
}
env.InitPheromone("1")
env.InitPheromone("2")
g.ColorPheromone("1", [4]uint8{0x00, 0xFF, 0x80, 0x00})
g.ColorPheromone("2", [4]uint8{0xFF, 0x80, 0x00, 0x00})
case "flocking":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})