Byzantine Snowfall

This commit is contained in:
Sarah Jamie Lewis 2019-08-18 18:54:27 -07:00
parent 7ab3273f89
commit acc50d7001
4 changed files with 62 additions and 28 deletions

View File

@ -10,6 +10,7 @@ type Snowball struct {
color int
Probability float32
sureness float32
Byztantine bool
}
func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) {
@ -23,37 +24,42 @@ func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) {
}
func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
t.Wiggle()
am1 := t.Amount(env,1,"1")
am2 := t.Amount(env,1,"2")
if sm.Byztantine {
t.Wiggle()
t.Drop(env, 1, "2")
} else {
t.Wiggle()
am1 := t.Amount(env, 1, "1")
am2 := t.Amount(env, 1, "2")
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
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
}
}
}
}
if sm.sureness > 1 {
t.Drop(env, 1, strconv.Itoa(sm.color))
}
if sm.sureness > 1 {
t.Drop(env, 1, strconv.Itoa(sm.color))
}
t.Step(env)
t.Step(env)
}
}

View File

@ -8,6 +8,7 @@ type Turtle struct {
xpos, ypos int
heading int
actor Actor
width,height int
}
type NilActor struct {
@ -22,6 +23,8 @@ func (NilActor) Run(*Environment, *Turtle) {
func NewTurtle(env *Environment, actor Actor) *Turtle {
for {
turtle := new(Turtle)
turtle.width = env.width
turtle.height = env.height
turtle.xpos = rand.Intn(env.width)
turtle.ypos = rand.Intn(env.height)
turtle.actor = actor
@ -46,6 +49,19 @@ func (t *Turtle) setRandomHeading() {
}
func (t *Turtle) SetXY(x, y int) {
if x < 0 {
x = (t.width - 1)
} else if x >= t.width {
x = x % (t.width)
}
if y < 0 {
y = (t.height - 1)
} else if y >=t.height {
y = y % (t.height)
}
t.xpos = x
t.ypos = y
}

View File

@ -97,7 +97,7 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
// TODO: Move this into an environment specification
for name := range g.colorMap {
env.Evaporate(0.95, name)
env.Evaporate(0.99, name)
}
g.renderer.Present()

14
main.go
View File

@ -22,7 +22,7 @@ var width = flag.Int("width", 300, "width of environment")
var height = flag.Int("height", 300, "height of environment")
var pxsize = flag.Int("pxsize", 1, "pixels per tile edge")
var numTurtles = flag.Int("numTurtles", 5000, "number of turtles")
var byzantineTurtles = flag.Int("byzantineTurtles", 50,"in consensus simlulations, the number of turtles who will always vote 2")
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.")
func main() {
@ -109,6 +109,18 @@ func main() {
env.InitPheromone("2")
g.ColorPheromone("1", [4]uint8{0x00, 0xFF, 0x80, 0x00})
g.ColorPheromone("2", [4]uint8{0xFF, 0x80, 0x00, 0x00})
case "snowball-byzantine":
honestTurtles := (*numTurtles)-(*byzantineTurtles)
for i := 0; i < honestTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Snowball{Probability:float32(*prob)})
}
for i := honestTurtles; i < honestTurtles+(*byzantineTurtles); i++ {
turtles[i] = core.NewTurtle(env, &actors.Snowball{Probability:float32(*prob), Byztantine:true})
}
env.InitPheromone("1")
env.InitPheromone("2")
g.ColorPheromone("1", [4]uint8{0x00, 0xFF, 0x80, 0x00})
g.ColorPheromone("2", [4]uint8{0xFF, 0x00, 0xa5, 0x00})
case "flocking":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})