From 9fe42b1ff2939145f102fa8f4abf52aea08bb47c Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sat, 10 Aug 2019 17:17:41 -0700 Subject: [PATCH] Adding snowball inspired algorithm --- actors/snowball.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 8 +++++++ 2 files changed, 65 insertions(+) create mode 100644 actors/snowball.go diff --git a/actors/snowball.go b/actors/snowball.go new file mode 100644 index 0000000..8bbd9c5 --- /dev/null +++ b/actors/snowball.go @@ -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) +} + + diff --git a/main.go b/main.go index 3546295..4cca805 100644 --- a/main.go +++ b/main.go @@ -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})