Adversarial Strategies
This commit is contained in:
parent
fc9e8603b5
commit
47eed67c89
|
@ -15,10 +15,10 @@ func (e *Environment) Height() int {
|
||||||
return e.height
|
return e.height
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e * Environment) GetPheromones() []string {
|
func (e *Environment) GetPheromones() []string {
|
||||||
keys := make([]string,len(e.state))
|
keys := make([]string, len(e.state))
|
||||||
i:=0
|
i := 0
|
||||||
for k,_ := range e.state {
|
for k := range e.state {
|
||||||
keys[i] = k
|
keys[i] = k
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,6 @@ func (e *Environment) normXY(x int, y int) (int, int) {
|
||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (e *Environment) Evaporate(rate float32, pheromone string) {
|
func (e *Environment) Evaporate(rate float32, pheromone string) {
|
||||||
for x := 0; x < e.width; x++ {
|
for x := 0; x < e.width; x++ {
|
||||||
for y := 0; y < e.height; y++ {
|
for y := 0; y < e.height; y++ {
|
||||||
|
|
|
@ -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 {
|
func (t *Turtle) AmountAll(env *Environment, distance int, pheromone string) float32 {
|
||||||
total := float32(0)
|
total := float32(0)
|
||||||
for i:=0;i<8;i++ {
|
for i := 0; i < 8; i++ {
|
||||||
dx0 := headings[i][0] * distance
|
dx0 := headings[i][0] * distance
|
||||||
dy0 := headings[i][1] * distance
|
dy0 := headings[i][1] * distance
|
||||||
|
|
||||||
x0 := (t.xpos + dx0)
|
x0 := (t.xpos + dx0)
|
||||||
y0 := (t.ypos + dy0)
|
y0 := (t.ypos + dy0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
total += env.SniffNormalized(x0, y0, pheromone)
|
total += env.SniffNormalized(x0, y0, pheromone)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ type Experiment struct {
|
||||||
turtles []*core.Turtle
|
turtles []*core.Turtle
|
||||||
graphics *graphics.Graphics
|
graphics *graphics.Graphics
|
||||||
initializedTurtles int
|
initializedTurtles int
|
||||||
pheromones []string
|
pheromones []string
|
||||||
OnStep func(*core.Environment, []*core.Turtle, int)
|
OnStep func(*core.Environment, []*core.Turtle, int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ type SlimeMold struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
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})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"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"
|
||||||
|
@ -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 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 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 {
|
type Snowball struct {
|
||||||
color int
|
color int
|
||||||
|
@ -26,17 +29,34 @@ func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) {
|
||||||
} else {
|
} else {
|
||||||
sm.color = 1
|
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) {
|
func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
|
||||||
if sm.Byztantine {
|
if sm.Byztantine {
|
||||||
t.Wiggle()
|
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")
|
t.Drop(env, 1, "2")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
if sm.color == 1 {
|
||||||
|
t.SetColor(color.RGBA{0, 255, 0, 0})
|
||||||
|
} else {
|
||||||
|
t.SetColor(color.RGBA{255, 0, 255, 0})
|
||||||
|
}
|
||||||
t.Wiggle()
|
t.Wiggle()
|
||||||
am1 := t.Amount(env, 1, "1")
|
am1 := t.AmountAll(env, 1, "1")
|
||||||
am2 := t.Amount(env, 1, "2")
|
am2 := t.AmountAll(env, 1, "2")
|
||||||
|
|
||||||
if am1 > sm.sureness || am2 > sm.sureness {
|
if am1 > sm.sureness || am2 > sm.sureness {
|
||||||
if am1 > am2 {
|
if am1 > am2 {
|
||||||
|
@ -85,5 +105,28 @@ func main() {
|
||||||
}, (*byzantineTurtles))
|
}, (*byzantineTurtles))
|
||||||
experiment.InitPheromone("1", color.RGBA{0x00, 0xFF, 0x00, 0x00})
|
experiment.InitPheromone("1", color.RGBA{0x00, 0xFF, 0x00, 0x00})
|
||||||
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 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()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ func (sm *Frog) Setup(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *Frog) Run(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()
|
t.Wiggle()
|
||||||
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
|
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
|
||||||
amountFrog := t.AmountAll(env, 1, "frog-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) {
|
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()
|
t.Wiggle()
|
||||||
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
|
amountTurtle := t.AmountAll(env, 1, "turtle-scent")
|
||||||
amountFrog := t.AmountAll(env, 1, "frog-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.InitPheromone("frog-scent", color.RGBA{0x00, 0xFF, 0xFF, 0x00})
|
||||||
|
|
||||||
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, i int) {
|
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, i int) {
|
||||||
env.Evaporate(.99, "turtle-scent")
|
env.Evaporate(.99, "turtle-scent")
|
||||||
env.Evaporate(.99, "frog-scent")
|
env.Evaporate(.99, "frog-scent")
|
||||||
}
|
}
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
|
|
|
@ -99,7 +99,7 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
|
|
||||||
g.renderer.Present()
|
g.renderer.Present()
|
||||||
g.window.UpdateSurface()
|
g.window.UpdateSurface()
|
||||||
// surface, _ := g.window.GetSurface()
|
// surface, _ := g.window.GetSurface()
|
||||||
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
||||||
g.t++
|
g.t++
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue