Adversarial Strategies

This commit is contained in:
Sarah Jamie Lewis 2019-09-29 21:52:01 -07:00
parent fc9e8603b5
commit 47eed67c89
7 changed files with 59 additions and 19 deletions

View File

@ -15,10 +15,10 @@ func (e *Environment) Height() int {
return e.height
}
func (e * Environment) GetPheromones() []string {
keys := make([]string,len(e.state))
i:=0
for k,_ := range e.state {
func (e *Environment) GetPheromones() []string {
keys := make([]string, len(e.state))
i := 0
for k := range e.state {
keys[i] = k
i++
}
@ -112,7 +112,6 @@ func (e *Environment) normXY(x int, y int) (int, int) {
return x, y
}
func (e *Environment) Evaporate(rate float32, pheromone string) {
for x := 0; x < e.width; x++ {
for y := 0; y < e.height; y++ {

View File

@ -110,15 +110,13 @@ func (t *Turtle) Drop(env *Environment, amount float32, pheromone string) {
func (t *Turtle) AmountAll(env *Environment, distance int, pheromone string) float32 {
total := float32(0)
for i:=0;i<8;i++ {
for i := 0; i < 8; i++ {
dx0 := headings[i][0] * distance
dy0 := headings[i][1] * distance
x0 := (t.xpos + dx0)
y0 := (t.ypos + dy0)
total += env.SniffNormalized(x0, y0, pheromone)
}

View File

@ -25,7 +25,7 @@ type Experiment struct {
turtles []*core.Turtle
graphics *graphics.Graphics
initializedTurtles int
pheromones []string
pheromones []string
OnStep func(*core.Environment, []*core.Turtle, int)
}

View File

@ -14,7 +14,7 @@ type SlimeMold struct {
}
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})
}
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {

View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"git.openprivacy.ca/sarah/microworlds/core"
"git.openprivacy.ca/sarah/microworlds/experiments"
"image/color"
@ -10,7 +11,9 @@ import (
)
var prob = flag.Float64("distribution", .51, "drives the probability of color assignment of turtles in consensus models, closer to .5 results in more even assignment, closer to 0 or 1 biases in favor of a color.")
var initialSureness = flag.Int("initialSureness", 2, "how sure an honest node is of their first received colour")
var byzantineTurtles = flag.Int("byzantineTurtles", 50, "in consensus simlulations, the number of turtles who will always vote 2")
var cleverByzantineTurtles = flag.Bool("cleverByzantineTurtles", true, "byzantine turtles try to find each other")
type Snowball struct {
color int
@ -26,17 +29,34 @@ func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) {
} else {
sm.color = 1
}
sm.sureness = 2
sm.sureness = float32(*initialSureness)
}
func (sm *Snowball) GetColor() int {
return sm.color
}
func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
if sm.Byztantine {
t.Wiggle()
if *cleverByzantineTurtles {
t.FollowGradient(env, 5, 2, "3")
t.Drop(env, 1, "3")
}
sm.color = 2
t.SetColor(color.RGBA{255, 0, 0, 0})
t.Drop(env, 1, "2")
} else {
if sm.color == 1 {
t.SetColor(color.RGBA{0, 255, 0, 0})
} else {
t.SetColor(color.RGBA{255, 0, 255, 0})
}
t.Wiggle()
am1 := t.Amount(env, 1, "1")
am2 := t.Amount(env, 1, "2")
am1 := t.AmountAll(env, 1, "1")
am2 := t.AmountAll(env, 1, "2")
if am1 > sm.sureness || am2 > sm.sureness {
if am1 > am2 {
@ -85,5 +105,28 @@ func main() {
}, (*byzantineTurtles))
experiment.InitPheromone("1", color.RGBA{0x00, 0xFF, 0x00, 0x00})
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
experiment.InitPheromone("3", color.RGBA{0xFF, 0x00, 0x00, 0x00})
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, step int) {
num1 := 0
num2 := 0
env.EvaporateAndDiffuse(0.99, "1")
env.EvaporateAndDiffuse(0.99, "2")
env.EvaporateAndDiffuse(0.99, "3")
for _, turtle := range turtles {
agent := turtle.GetActor().(*Snowball)
if agent.GetColor() == 1 {
num1++
} else {
num2++
}
}
//if step == 0 {
fmt.Printf("%v,%v,%v\n", step, num1, num2)
//}
}
experiment.Run()
}

View File

@ -22,7 +22,7 @@ func (sm *Frog) Setup(env *core.Environment, t *core.Turtle) {
}
func (sm *Frog) Run(env *core.Environment, t *core.Turtle) {
t.FollowGradient(env,1,1,"frog-scent")
t.FollowGradient(env, 1, 1, "frog-scent")
t.Wiggle()
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
amountFrog := t.AmountAll(env, 1, "frog-scent")
@ -46,7 +46,7 @@ func (sm *Turtle) Setup(env *core.Environment, t *core.Turtle) {
}
func (sm *Turtle) Run(env *core.Environment, t *core.Turtle) {
t.FollowGradient(env,1,1,"turtle-scent")
t.FollowGradient(env, 1, 1, "turtle-scent")
t.Wiggle()
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
amountFrog := t.AmountAll(env, 1, "frog-scent")
@ -77,8 +77,8 @@ func main() {
experiment.InitPheromone("frog-scent", color.RGBA{0x00, 0xFF, 0xFF, 0x00})
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, i int) {
env.Evaporate(.99, "turtle-scent")
env.Evaporate(.99, "frog-scent")
env.Evaporate(.99, "turtle-scent")
env.Evaporate(.99, "frog-scent")
}
experiment.Run()

View File

@ -99,7 +99,7 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
g.renderer.Present()
g.window.UpdateSurface()
// surface, _ := g.window.GetSurface()
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
// surface, _ := g.window.GetSurface()
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
g.t++
}