Adding snowball inspired algorithm
This commit is contained in:
parent
11c0543c1d
commit
9fe42b1ff2
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
8
main.go
8
main.go
|
@ -101,6 +101,14 @@ func main() {
|
||||||
env.InitPheromone("2")
|
env.InitPheromone("2")
|
||||||
g.ColorPheromone("1", [4]uint8{0x80, 0xFF, 0x00, 0x00})
|
g.ColorPheromone("1", [4]uint8{0x80, 0xFF, 0x00, 0x00})
|
||||||
g.ColorPheromone("2", [4]uint8{0xFF, 0, 0xFF, 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":
|
case "flocking":
|
||||||
for i := 0; i < *numTurtles; i++ {
|
for i := 0; i < *numTurtles; i++ {
|
||||||
turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})
|
turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})
|
||||||
|
|
Loading…
Reference in New Issue